From d2b933e0d6a707e7513920a3a59a599f3e47c28d Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Thu, 15 Feb 2018 09:36:48 -0800 Subject: [PATCH] Cleanup in binder: rename and move --- src/compiler/binder.ts | 30 +++++++++---------- src/compiler/checker.ts | 15 +++++----- ...typeFromPropertyAssignmentOutOfOrder.types | 4 +-- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index f419c1e0e2c..be07402ff13 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2045,7 +2045,7 @@ namespace ts { bindThisPropertyAssignment(node as BinaryExpression); break; case SpecialPropertyAssignmentKind.Property: - bindStaticBinaryExpressionAssignment(node as BinaryExpression); + bindSpecialPropertyAssignment(node as BinaryExpression); break; case SpecialPropertyAssignmentKind.None: // Nothing to do @@ -2296,7 +2296,6 @@ namespace ts { } function bindExportsPropertyAssignment(node: BinaryExpression) { - // TODO: Maybe this should support nested exports too // When we create a property via 'exports.foo = bar', the 'exports.foo' property access // expression is the declaration setCommonJsModuleIndicator(node); @@ -2375,8 +2374,9 @@ namespace ts { } - function bindStaticBinaryExpressionAssignment(node: BinaryExpression) { + function bindSpecialPropertyAssignment(node: BinaryExpression) { const lhs = node.left as PropertyAccessEntityNameExpression; + // Fix up parent pointers since we're going to use these nodes before we bind into them lhs.parent = node; if (isIdentifier(lhs.expression) && container === file && isNameOfExportsOrModuleExportsAliasDeclaration(file, lhs.expression)) { // This can be an alias for the 'exports' or 'module.exports' names, e.g. @@ -2388,26 +2388,16 @@ namespace ts { bindStaticPropertyAssignment(lhs); } } + /** - * For nodes like `x.y = z`, declare a member 'y' on 'x' if x is a function or class or {}, or not declared. + * For nodes like `x.y = z`, declare a member 'y' on 'x' if x is a function (or IIFE) or class or {}, or not declared. * Also works for expression statements preceded by JSDoc, like / ** @type number * / x.y; */ function bindStaticPropertyAssignment(node: PropertyAccessEntityNameExpression) { - // Fix up parent pointers since we're going to use these nodes before we bind into them node.expression.parent = node; bindPropertyAssignment(node.expression, node, /*isPrototypeProperty*/ false); } - function lookupSymbolForPropertyAccess(node: EntityNameExpression): Symbol | undefined { - if (isIdentifier(node)) { - return lookupSymbolForNameWorker(container, node.escapedText); - } - else { - const symbol = getJSInitializerSymbol(lookupSymbolForPropertyAccess(node.expression)); - return symbol && symbol.exports && symbol.exports.get(node.name.escapedText); - } - } - function bindPropertyAssignment(name: EntityNameExpression, propertyAccess: PropertyAccessEntityNameExpression, isPrototypeProperty: boolean) { // Look up the property in the local scope, since property assignments should follow the declaration let symbol = getJSInitializerSymbol(lookupSymbolForPropertyAccess(name)); @@ -2451,6 +2441,16 @@ namespace ts { declareSymbol(symbolTable, symbol, propertyAccess, SymbolFlags.Property, SymbolFlags.PropertyExcludes); } + function lookupSymbolForPropertyAccess(node: EntityNameExpression): Symbol | undefined { + if (isIdentifier(node)) { + return lookupSymbolForNameWorker(container, node.escapedText); + } + else { + const symbol = getJSInitializerSymbol(lookupSymbolForPropertyAccess(node.expression)); + return symbol && symbol.exports && symbol.exports.get(node.name.escapedText); + } + } + function forEachIdentifierInEntityName(e: EntityNameExpression, action: (e: Identifier, symbol: Symbol) => Symbol): Symbol { if (isIdentifier(e)) { return action(e, lookupSymbolForPropertyAccess(e)); diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2870aba2589..4fc1dad1cc0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14767,13 +14767,14 @@ namespace ts { let patternWithComputedProperties = false; let hasComputedStringProperty = false; let hasComputedNumberProperty = false; - // TODO: This seems like it might be wrong, or at least should come earlier - // (maybe check SymbolFlags.JSContainer? This currently misses normal declarations like `var my = {}`, but shouldn't) - if (isInJSFile && node.symbol && node.symbol.exports && node.properties.length === 0) { - const symbol = getMergedSymbol(node.symbol); - propertiesTable = symbol.exports; - symbol.exports.forEach(symbol => propertiesArray.push(getMergedSymbol(symbol))); - return createObjectLiteralType(); + if (isInJSFile && node.properties.length === 0) { + // an empty JS object literal that nonetheless has members is a JS namespace + const symbol = getSymbolOfNode(node); + if (symbol.exports) { + propertiesTable = symbol.exports; + symbol.exports.forEach(symbol => propertiesArray.push(getMergedSymbol(symbol))); + return createObjectLiteralType(); + } } propertiesTable = createSymbolTable(); diff --git a/tests/baselines/reference/typeFromPropertyAssignmentOutOfOrder.types b/tests/baselines/reference/typeFromPropertyAssignmentOutOfOrder.types index c31516f883d..a2cdc80b508 100644 --- a/tests/baselines/reference/typeFromPropertyAssignmentOutOfOrder.types +++ b/tests/baselines/reference/typeFromPropertyAssignmentOutOfOrder.types @@ -34,9 +34,9 @@ var am; === tests/cases/conformance/salsa/roots.js === var Common = {}; >Common : typeof Common ->{} : { [x: string]: any; } +>{} : { [x: string]: any; Item: typeof I; Object: typeof (Anonymous class); } var Workspace = {}; >Workspace : typeof Workspace ->{} : { [x: string]: any; } +>{} : { [x: string]: any; Object: typeof (Anonymous class); }