Skip to content

Fix issues caused by reachability analysis of this.foo(...) like calls. #33598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17135,7 +17135,7 @@ namespace ts {
const symbol = getResolvedSymbol(<Identifier>node);
return getExplicitTypeOfSymbol(symbol.flags & SymbolFlags.Alias ? resolveAlias(symbol) : symbol);
case SyntaxKind.ThisKeyword:
return checkThisExpression(node);
return getExplicitThisType(node);
case SyntaxKind.PropertyAccessExpression:
const type = getTypeOfDottedName((<PropertyAccessExpression>node).expression);
const prop = type && getPropertyOfType(type, (<PropertyAccessExpression>node).name.escapedText);
Expand Down Expand Up @@ -18720,6 +18720,20 @@ namespace ts {
}
}

function getExplicitThisType(node: Expression) {
const container = getThisContainer(node, /*includeArrowFunctions*/ false);
if (isFunctionLike(container)) {
const signature = getSignatureFromDeclaration(container);
if (signature.thisParameter) {
return getExplicitTypeOfSymbol(signature.thisParameter);
}
}
if (isClassLike(container.parent)) {
const symbol = getSymbolOfNode(container.parent);
return hasModifier(container, ModifierFlags.Static) ? getTypeOfSymbol(symbol) : (getDeclaredTypeOfSymbol(symbol) as InterfaceType).thisType!;
}
}

function getClassNameFromPrototypeMethod(container: Node) {
// Check if it's the RHS of a x.prototype.y = function [name]() { .... }
if (container.kind === SyntaxKind.FunctionExpression &&
Expand Down
69 changes: 69 additions & 0 deletions tests/baselines/reference/neverReturningFunctions1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,73 @@ tests/cases/conformance/controlFlow/neverReturningFunctions1.ts(153,5): error TS
~~
!!! error TS7027: Unreachable code detected.
}

// Repro from #33582

export interface Component<T extends object = any> {
attrName?: string;
data: T;
dependencies?: string[];
el: any;
id: string;
multiple?: boolean;
name: string;
schema: unknown;
system: any;

init(data?: T): void;
pause(): void;
play(): void;
remove(): void;
tick?(time: number, timeDelta: number): void;
update(oldData: T): void;
updateSchema?(): void;

extendSchema(update: unknown): void;
flushToDOM(): void;
}

export interface ComponentConstructor<T extends object> {
new (el: unknown, attrValue: string, id: string): T & Component;
prototype: T & {
name: string;
system: unknown;
play(): void;
pause(): void;
};
}

declare function registerComponent<T extends object>(
name: string,
component: ComponentDefinition<T>
): ComponentConstructor<T>;

export type ComponentDefinition<T extends object = object> = T & Partial<Component> & ThisType<T & Component>;

const Component = registerComponent('test-component', {
schema: {
myProperty: {
default: [],
parse() {
return [true];
}
},
string: { type: 'string' },
num: 0
},
init() {
this.data.num = 0;
this.el.setAttribute('custom-attribute', 'custom-value');
},
update() {},
tick() {},
remove() {},
pause() {},
play() {},

multiply(f: number) {
// Reference to system because both were registered with the same name.
return f * this.data.num * this.system!.data.counter;
}
});

148 changes: 123 additions & 25 deletions tests/baselines/reference/neverReturningFunctions1.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,80 @@ function f42(x: number) {
}
x; // Unreachable
}

// Repro from #33582

export interface Component<T extends object = any> {
attrName?: string;
data: T;
dependencies?: string[];
el: any;
id: string;
multiple?: boolean;
name: string;
schema: unknown;
system: any;

init(data?: T): void;
pause(): void;
play(): void;
remove(): void;
tick?(time: number, timeDelta: number): void;
update(oldData: T): void;
updateSchema?(): void;

extendSchema(update: unknown): void;
flushToDOM(): void;
}

export interface ComponentConstructor<T extends object> {
new (el: unknown, attrValue: string, id: string): T & Component;
prototype: T & {
name: string;
system: unknown;
play(): void;
pause(): void;
};
}

declare function registerComponent<T extends object>(
name: string,
component: ComponentDefinition<T>
): ComponentConstructor<T>;

export type ComponentDefinition<T extends object = object> = T & Partial<Component> & ThisType<T & Component>;

const Component = registerComponent('test-component', {
schema: {
myProperty: {
default: [],
parse() {
return [true];
}
},
string: { type: 'string' },
num: 0
},
init() {
this.data.num = 0;
this.el.setAttribute('custom-attribute', 'custom-value');
},
update() {},
tick() {},
remove() {},
pause() {},
play() {},

multiply(f: number) {
// Reference to system because both were registered with the same name.
return f * this.data.num * this.system!.data.counter;
}
});


//// [neverReturningFunctions1.js]
"use strict";
exports.__esModule = true;
function fail(message) {
throw new Error(message);
}
Expand Down Expand Up @@ -305,33 +375,61 @@ function f42(x) {
}
x; // Unreachable
}
var Component = registerComponent('test-component', {
schema: {
myProperty: {
"default": [],
parse: function () {
return [true];
}
},
string: { type: 'string' },
num: 0
},
init: function () {
this.data.num = 0;
this.el.setAttribute('custom-attribute', 'custom-value');
},
update: function () { },
tick: function () { },
remove: function () { },
pause: function () { },
play: function () { },
multiply: function (f) {
// Reference to system because both were registered with the same name.
return f * this.data.num * this.system.data.counter;
}
});


//// [neverReturningFunctions1.d.ts]
declare function fail(message?: string): never;
declare function f01(x: string | undefined): void;
declare function f02(x: number): number;
declare function f03(x: string): void;
declare function f11(x: string | undefined, fail: (message?: string) => never): void;
declare function f12(x: number, fail: (message?: string) => never): number;
declare function f13(x: string, fail: (message?: string) => never): void;
declare namespace Debug {
function fail(message?: string): never;
export interface Component<T extends object = any> {
attrName?: string;
data: T;
dependencies?: string[];
el: any;
id: string;
multiple?: boolean;
name: string;
schema: unknown;
system: any;
init(data?: T): void;
pause(): void;
play(): void;
remove(): void;
tick?(time: number, timeDelta: number): void;
update(oldData: T): void;
updateSchema?(): void;
extendSchema(update: unknown): void;
flushToDOM(): void;
}
declare function f21(x: string | undefined): void;
declare function f22(x: number): number;
declare function f23(x: string): void;
declare function f24(x: string): void;
declare class Test {
fail(message?: string): never;
f1(x: string | undefined): void;
f2(x: number): number;
f3(x: string): void;
export interface ComponentConstructor<T extends object> {
new (el: unknown, attrValue: string, id: string): T & Component;
prototype: T & {
name: string;
system: unknown;
play(): void;
pause(): void;
};
}
declare function f30(x: string | number | undefined): void;
declare function f31(x: {
a: string | number;
}): void;
declare function f40(x: number): void;
declare function f41(x: number): void;
declare function f42(x: number): void;
export declare type ComponentDefinition<T extends object = object> = T & Partial<Component> & ThisType<T & Component>;
Loading