From 8db58bbd69a9328ced57186af37689d5c89cd445 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 19 May 2017 13:19:18 -0700 Subject: [PATCH 1/5] Make getNameOfDeclaration public --- src/compiler/utilities.ts | 60 +++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 6f99a71ab31..cccabded02a 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1776,36 +1776,6 @@ namespace ts { } } - export function getNameOfDeclaration(declaration: Declaration): DeclarationName { - if (!declaration) { - return undefined; - } - if (declaration.kind === SyntaxKind.BinaryExpression) { - const kind = getSpecialPropertyAssignmentKind(declaration as BinaryExpression); - const lhs = (declaration as BinaryExpression).left; - switch (kind) { - case SpecialPropertyAssignmentKind.None: - case SpecialPropertyAssignmentKind.ModuleExports: - return undefined; - case SpecialPropertyAssignmentKind.ExportsProperty: - if (lhs.kind === SyntaxKind.Identifier) { - return (lhs as PropertyAccessExpression).name; - } - else { - return ((lhs as PropertyAccessExpression).expression as PropertyAccessExpression).name; - } - case SpecialPropertyAssignmentKind.ThisProperty: - case SpecialPropertyAssignmentKind.Property: - return (lhs as PropertyAccessExpression).name; - case SpecialPropertyAssignmentKind.PrototypeProperty: - return ((lhs as PropertyAccessExpression).expression as PropertyAccessExpression).name; - } - } - else { - return (declaration as NamedDeclaration).name; - } - } - export function isLiteralComputedPropertyDeclarationName(node: Node) { return (node.kind === SyntaxKind.StringLiteral || node.kind === SyntaxKind.NumericLiteral) && node.parent.kind === SyntaxKind.ComputedPropertyName && @@ -4729,4 +4699,34 @@ namespace ts { export function unescapeIdentifier(identifier: string): string { return identifier.length >= 3 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ && identifier.charCodeAt(2) === CharacterCodes._ ? identifier.substr(1) : identifier; } + + export function getNameOfDeclaration(declaration: Declaration): DeclarationName { + if (!declaration) { + return undefined; + } + if (declaration.kind === SyntaxKind.BinaryExpression) { + const kind = getSpecialPropertyAssignmentKind(declaration as BinaryExpression); + const lhs = (declaration as BinaryExpression).left; + switch (kind) { + case SpecialPropertyAssignmentKind.None: + case SpecialPropertyAssignmentKind.ModuleExports: + return undefined; + case SpecialPropertyAssignmentKind.ExportsProperty: + if (lhs.kind === SyntaxKind.Identifier) { + return (lhs as PropertyAccessExpression).name; + } + else { + return ((lhs as PropertyAccessExpression).expression as PropertyAccessExpression).name; + } + case SpecialPropertyAssignmentKind.ThisProperty: + case SpecialPropertyAssignmentKind.Property: + return (lhs as PropertyAccessExpression).name; + case SpecialPropertyAssignmentKind.PrototypeProperty: + return ((lhs as PropertyAccessExpression).expression as PropertyAccessExpression).name; + } + } + else { + return (declaration as NamedDeclaration).name; + } + } } From 6c4e747295c65fed3d00efaf9c17a46deaa7271d Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 19 May 2017 14:13:33 -0700 Subject: [PATCH 2/5] Add `undefined` to return of getNameOfDeclaration --- src/compiler/utilities.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index cccabded02a..64f5445d3df 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4700,7 +4700,7 @@ namespace ts { return identifier.length >= 3 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ && identifier.charCodeAt(2) === CharacterCodes._ ? identifier.substr(1) : identifier; } - export function getNameOfDeclaration(declaration: Declaration): DeclarationName { + export function getNameOfDeclaration(declaration: Declaration): DeclarationName | undefined { if (!declaration) { return undefined; } From c70fa1e573b388019352c915989ff45a19480eac Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 19 May 2017 15:08:24 -0700 Subject: [PATCH 3/5] Remove incorrect code in getNameOfDeclaration --- src/compiler/utilities.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 64f5445d3df..aaf9fdd97f3 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4712,12 +4712,6 @@ namespace ts { case SpecialPropertyAssignmentKind.ModuleExports: return undefined; case SpecialPropertyAssignmentKind.ExportsProperty: - if (lhs.kind === SyntaxKind.Identifier) { - return (lhs as PropertyAccessExpression).name; - } - else { - return ((lhs as PropertyAccessExpression).expression as PropertyAccessExpression).name; - } case SpecialPropertyAssignmentKind.ThisProperty: case SpecialPropertyAssignmentKind.Property: return (lhs as PropertyAccessExpression).name; From 3c6393afe1b60cf10c6fef72cd39089e9f7927dc Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 19 May 2017 15:18:26 -0700 Subject: [PATCH 4/5] More fix+cleanup in getNameOfDeclaration --- src/compiler/utilities.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index aaf9fdd97f3..2a2fcd3820e 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -4705,18 +4705,15 @@ namespace ts { return undefined; } if (declaration.kind === SyntaxKind.BinaryExpression) { - const kind = getSpecialPropertyAssignmentKind(declaration as BinaryExpression); - const lhs = (declaration as BinaryExpression).left; - switch (kind) { - case SpecialPropertyAssignmentKind.None: - case SpecialPropertyAssignmentKind.ModuleExports: - return undefined; + const expr = declaration as BinaryExpression; + switch (getSpecialPropertyAssignmentKind(expr)) { case SpecialPropertyAssignmentKind.ExportsProperty: case SpecialPropertyAssignmentKind.ThisProperty: case SpecialPropertyAssignmentKind.Property: - return (lhs as PropertyAccessExpression).name; case SpecialPropertyAssignmentKind.PrototypeProperty: - return ((lhs as PropertyAccessExpression).expression as PropertyAccessExpression).name; + return (expr.left as PropertyAccessExpression).name; + default: + return undefined; } } else { From fea8561d1b69311e70a396f3b62d41af0583239c Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 22 May 2017 10:06:35 -0700 Subject: [PATCH 5/5] Remove dead code from binder `getNameOfDeclaration` now handles a lot of the special property assignment kinds in `getDeclarationName` --- src/compiler/binder.ts | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index 1940231a6d1..9bdc85ef6ba 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -260,18 +260,9 @@ namespace ts { case SyntaxKind.ExportAssignment: return (node).isExportEquals ? "export=" : "default"; case SyntaxKind.BinaryExpression: - switch (getSpecialPropertyAssignmentKind(node as BinaryExpression)) { - case SpecialPropertyAssignmentKind.ModuleExports: - // module.exports = ... - return "export="; - case SpecialPropertyAssignmentKind.ExportsProperty: - case SpecialPropertyAssignmentKind.ThisProperty: - case SpecialPropertyAssignmentKind.Property: - // exports.x = ... or this.y = ... - return ((node as BinaryExpression).left as PropertyAccessExpression).name.text; - case SpecialPropertyAssignmentKind.PrototypeProperty: - // className.prototype.methodName = ... - return (((node as BinaryExpression).left as PropertyAccessExpression).expression as PropertyAccessExpression).name.text; + if (getSpecialPropertyAssignmentKind(node as BinaryExpression) === SpecialPropertyAssignmentKind.ModuleExports) { + // module.exports = ... + return "export="; } Debug.fail("Unknown binary declaration kind"); break;