Skip to content

Commit 6425837

Browse files
committed
Salsa: get members of variables whose initialisers are functions
1 parent e6eb36e commit 6425837

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ namespace ts {
19451945
classPrototype.parent = leftSideOfAssignment;
19461946

19471947
const funcSymbol = container.locals[constructorFunction.text];
1948-
if (!funcSymbol || !(funcSymbol.flags & SymbolFlags.Function)) {
1948+
if (!funcSymbol || !(funcSymbol.flags & SymbolFlags.Function || isDeclarationOfFunctionExpression(funcSymbol))) {
19491949
return;
19501950
}
19511951

src/compiler/checker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11481,8 +11481,12 @@ namespace ts {
1148111481
// When resolved signature is a call signature (and not a construct signature) the result type is any, unless
1148211482
// the declaring function had members created through 'x.prototype.y = expr' or 'this.y = expr' psuedodeclarations
1148311483
// in a JS file
11484-
const funcSymbol = checkExpression(node.expression).symbol;
11485-
if (funcSymbol && funcSymbol.members && (funcSymbol.flags & SymbolFlags.Function)) {
11484+
// Note:JS inferred classes might come from a variable declaration instead of a function declaration.
11485+
// In this case, using getResolvedSymbol directly is required to avoid losing the members from the declaration.
11486+
const funcSymbol = node.expression.kind === SyntaxKind.Identifier ?
11487+
getResolvedSymbol(node.expression as Identifier) :
11488+
checkExpression(node.expression).symbol;
11489+
if (funcSymbol && funcSymbol.members && (funcSymbol.flags & SymbolFlags.Function || isDeclarationOfFunctionExpression(funcSymbol))) {
1148611490
return getInferredClassType(funcSymbol);
1148711491
}
1148811492
else if (compilerOptions.noImplicitAny) {

src/compiler/utilities.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,18 @@ namespace ts {
12571257
return charCode === CharacterCodes.singleQuote || charCode === CharacterCodes.doubleQuote;
12581258
}
12591259

1260+
/**
1261+
* Returns true if the node is a variable declaration whose initializer is a function expression.
1262+
* This function does not test if the node is in a JavaScript file or not.
1263+
*/
1264+
export function isDeclarationOfFunctionExpression(s: Symbol) {
1265+
if (s.valueDeclaration && s.valueDeclaration.kind === SyntaxKind.VariableDeclaration) {
1266+
const declaration = s.valueDeclaration as VariableDeclaration;
1267+
return declaration.initializer && declaration.initializer.kind === SyntaxKind.FunctionExpression;
1268+
}
1269+
return false;
1270+
}
1271+
12601272
/// Given a BinaryExpression, returns SpecialPropertyAssignmentKind for the various kinds of property
12611273
/// assignments we treat as special in the binder
12621274
export function getSpecialPropertyAssignmentKind(expression: Node): SpecialPropertyAssignmentKind {

0 commit comments

Comments
 (0)