Skip to content

Mark js-assignment functions' symbols as methods #23078

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 1 commit into from
Apr 2, 2018
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
6 changes: 4 additions & 2 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2473,8 +2473,10 @@ namespace ts {
(symbol.exports || (symbol.exports = createSymbolTable()));

// Declare the method/property
const symbolFlags = SymbolFlags.Property | (isToplevelNamespaceableInitializer ? SymbolFlags.JSContainer : 0);
const symbolExcludes = SymbolFlags.PropertyExcludes & ~(isToplevelNamespaceableInitializer ? SymbolFlags.JSContainer : 0);
const jsContainerFlag = isToplevelNamespaceableInitializer ? SymbolFlags.JSContainer : 0;
const isMethod = isFunctionLikeDeclaration(getAssignedJavascriptInitializer(propertyAccess));
const symbolFlags = (isMethod ? SymbolFlags.Method : SymbolFlags.Property) | jsContainerFlag;
const symbolExcludes = (isMethod ? SymbolFlags.MethodExcludes : SymbolFlags.PropertyExcludes) & ~jsContainerFlag;
declareSymbol(symbolTable, symbol, propertyAccess, symbolFlags, symbolExcludes);
}

Expand Down
11 changes: 7 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4679,6 +4679,10 @@ namespace ts {
if (symbol.flags & SymbolFlags.Module && isShorthandAmbientModuleSymbol(symbol)) {
links.type = anyType;
}
else if (symbol.valueDeclaration.kind === SyntaxKind.BinaryExpression ||
symbol.valueDeclaration.kind === SyntaxKind.PropertyAccessExpression && symbol.valueDeclaration.parent.kind === SyntaxKind.BinaryExpression) {
links.type = getWidenedTypeFromJSSpecialPropertyDeclarations(symbol);
}
else {
const type = createObjectType(ObjectFlags.Anonymous, symbol);
if (symbol.flags & SymbolFlags.Class) {
Expand Down Expand Up @@ -6935,7 +6939,8 @@ namespace ts {
if (!symbol) return emptyArray;
const result: Signature[] = [];
for (let i = 0; i < symbol.declarations.length; i++) {
const node = symbol.declarations[i];
const decl = symbol.declarations[i];
const node = isPropertyAccessExpression(decl) ? getAssignedJavascriptInitializer(decl) : decl;
if (!isFunctionLike(node)) continue;
// Don't include signature if node is the implementation of an overloaded function. A node is considered
// an implementation node if it has a body and the previous node is of the same kind and immediately
Expand Down Expand Up @@ -16221,9 +16226,7 @@ namespace ts {
}

function isMethodLike(symbol: Symbol) {
return !!(symbol.flags & SymbolFlags.Method ||
getCheckFlags(symbol) & CheckFlags.SyntheticMethod ||
isInJavaScriptFile(symbol.valueDeclaration) && isFunctionLikeDeclaration(getAssignedJavascriptInitializer(symbol.valueDeclaration)));
return !!(symbol.flags & SymbolFlags.Method || getCheckFlags(symbol) & CheckFlags.SyntheticMethod);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/convertFunctionToEs6Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ namespace ts.codefix {
}

function createClassElement(symbol: Symbol, modifiers: Modifier[]): ClassElement {
// both properties and methods are bound as property symbols
if (!(symbol.flags & SymbolFlags.Property)) {
// Right now the only thing we can convert are function expressions, which are marked as methods
if (!(symbol.flags & SymbolFlags.Method)) {
return;
}

Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/constructorFunctions2.types
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ B.prototype.m = function() { this.x = 2; }
>function() { this.x = 2; } : () => void
>this.x = 2 : 2
>this.x : number
>this : { id: number; m: () => void; x: number; }
>this : { id: number; m(): void; x: number; }
>x : number
>2 : 2

const b = new B();
>b : { id: number; m: () => void; x: number; }
>new B() : { id: number; m: () => void; x: number; }
>b : { id: number; m(): void; x: number; }
>new B() : { id: number; m(): void; x: number; }
>B : () => void

b.id;
>b.id : number
>b : { id: number; m: () => void; x: number; }
>b : { id: number; m(): void; x: number; }
>id : number

b.x;
>b.x : number
>b : { id: number; m: () => void; x: number; }
>b : { id: number; m(): void; x: number; }
>x : number

=== tests/cases/conformance/salsa/other.js ===
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/constructorFunctionsStrict.types
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ C.prototype.m = function() {
this.y = 12
>this.y = 12 : 12
>this.y : number | undefined
>this : { x: number; m: () => void; y: number | undefined; }
>this : { x: number; m(): void; y: number | undefined; }
>y : number | undefined
>12 : 12
}
var c = new C(1)
>c : { x: number; m: () => void; y: number | undefined; }
>new C(1) : { x: number; m: () => void; y: number | undefined; }
>c : { x: number; m(): void; y: number | undefined; }
>new C(1) : { x: number; m(): void; y: number | undefined; }
>C : (x: number) => void
>1 : 1

c.x = undefined // should error
>c.x = undefined : undefined
>c.x : number
>c : { x: number; m: () => void; y: number | undefined; }
>c : { x: number; m(): void; y: number | undefined; }
>x : number
>undefined : undefined

c.y = undefined // ok
>c.y = undefined : undefined
>c.y : number | undefined
>c : { x: number; m: () => void; y: number | undefined; }
>c : { x: number; m(): void; y: number | undefined; }
>y : number | undefined
>undefined : undefined

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ OOOrder.prototype.m = function () {
this.p = 1
>this.p = 1 : 1
>this.p : number
>this : { x: number; m: () => void; p: number; }
>this : { x: number; m(): void; p: number; }
>p : number
>1 : 1
}
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/jsContainerMergeTsDeclaration.types
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
=== tests/cases/conformance/salsa/a.js ===
var /*1*/x = function foo() {
>x : { (): void; a: () => void; }
>function foo() {} : { (): void; a: () => void; }
>foo : { (): void; a: () => void; }
>x : { (): void; a(): void; }
>function foo() {} : { (): void; a(): void; }
>foo : { (): void; a(): void; }
}
x.a = function bar() {
>x.a = function bar() {} : () => void
>x.a : () => void
>x : { (): void; a: () => void; }
>x : { (): void; a(): void; }
>a : () => void
>function bar() {} : () => void
>bar : () => void
}
=== tests/cases/conformance/salsa/b.ts ===
var x = function () {
>x : { (): void; a: () => void; }
>x : { (): void; a(): void; }
>function () { return 1;}() : number
>function () { return 1;} : () => number

Expand Down
Loading