From 3eeb54861d310a37518e81642bee77120c097e00 Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 10 Oct 2017 10:53:43 -0700 Subject: [PATCH] Fix invalid cast (#18821) --- src/compiler/binder.ts | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 48cace44841..4cacb5765db 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -2271,16 +2271,13 @@ namespace ts { function isExportsOrModuleExportsOrAlias(node: Node): boolean { return isExportsIdentifier(node) || isModuleExportsPropertyAccessExpression(node) || - isNameOfExportsOrModuleExportsAliasDeclaration(node); + isIdentifier(node) && isNameOfExportsOrModuleExportsAliasDeclaration(node); } - function isNameOfExportsOrModuleExportsAliasDeclaration(node: Node) { - if (isIdentifier(node)) { - const symbol = lookupSymbolForName(node.escapedText); - return symbol && symbol.valueDeclaration && isVariableDeclaration(symbol.valueDeclaration) && - symbol.valueDeclaration.initializer && isExportsOrModuleExportsOrAliasOrAssignment(symbol.valueDeclaration.initializer); - } - return false; + function isNameOfExportsOrModuleExportsAliasDeclaration(node: Identifier): boolean { + const symbol = lookupSymbolForName(node.escapedText); + return symbol && symbol.valueDeclaration && isVariableDeclaration(symbol.valueDeclaration) && + symbol.valueDeclaration.initializer && isExportsOrModuleExportsOrAliasOrAssignment(symbol.valueDeclaration.initializer); } function isExportsOrModuleExportsOrAliasOrAssignment(node: Node): boolean { @@ -2354,20 +2351,22 @@ namespace ts { // Look up the function in the local scope, since prototype assignments should // follow the function declaration const leftSideOfAssignment = node.left as PropertyAccessExpression; - const target = leftSideOfAssignment.expression as Identifier; + const target = leftSideOfAssignment.expression; - // Fix up parent pointers since we're going to use these nodes before we bind into them - leftSideOfAssignment.parent = node; - target.parent = leftSideOfAssignment; + if (isIdentifier(target)) { + // Fix up parent pointers since we're going to use these nodes before we bind into them + leftSideOfAssignment.parent = node; + target.parent = leftSideOfAssignment; - if (isNameOfExportsOrModuleExportsAliasDeclaration(target)) { - // This can be an alias for the 'exports' or 'module.exports' names, e.g. - // var util = module.exports; - // util.property = function ... - bindExportsPropertyAssignment(node); - } - else { - bindPropertyAssignment(target.escapedText, leftSideOfAssignment, /*isPrototypeProperty*/ false); + if (isNameOfExportsOrModuleExportsAliasDeclaration(target)) { + // This can be an alias for the 'exports' or 'module.exports' names, e.g. + // var util = module.exports; + // util.property = function ... + bindExportsPropertyAssignment(node); + } + else { + bindPropertyAssignment(target.escapedText, leftSideOfAssignment, /*isPrototypeProperty*/ false); + } } }