Skip to content

Commit 515a0e8

Browse files
authored
Merge pull request #15958 from Microsoft/make-getNameOfDeclaration-public
Make getNameOfDeclaration public
2 parents bba8e74 + fea8561 commit 515a0e8

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed

src/compiler/binder.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -260,18 +260,9 @@ namespace ts {
260260
case SyntaxKind.ExportAssignment:
261261
return (<ExportAssignment>node).isExportEquals ? "export=" : "default";
262262
case SyntaxKind.BinaryExpression:
263-
switch (getSpecialPropertyAssignmentKind(node as BinaryExpression)) {
264-
case SpecialPropertyAssignmentKind.ModuleExports:
265-
// module.exports = ...
266-
return "export=";
267-
case SpecialPropertyAssignmentKind.ExportsProperty:
268-
case SpecialPropertyAssignmentKind.ThisProperty:
269-
case SpecialPropertyAssignmentKind.Property:
270-
// exports.x = ... or this.y = ...
271-
return ((node as BinaryExpression).left as PropertyAccessExpression).name.text;
272-
case SpecialPropertyAssignmentKind.PrototypeProperty:
273-
// className.prototype.methodName = ...
274-
return (((node as BinaryExpression).left as PropertyAccessExpression).expression as PropertyAccessExpression).name.text;
263+
if (getSpecialPropertyAssignmentKind(node as BinaryExpression) === SpecialPropertyAssignmentKind.ModuleExports) {
264+
// module.exports = ...
265+
return "export=";
275266
}
276267
Debug.fail("Unknown binary declaration kind");
277268
break;

src/compiler/utilities.ts

+21-30
Original file line numberDiff line numberDiff line change
@@ -1785,36 +1785,6 @@ namespace ts {
17851785
}
17861786
}
17871787

1788-
export function getNameOfDeclaration(declaration: Declaration): DeclarationName {
1789-
if (!declaration) {
1790-
return undefined;
1791-
}
1792-
if (declaration.kind === SyntaxKind.BinaryExpression) {
1793-
const kind = getSpecialPropertyAssignmentKind(declaration as BinaryExpression);
1794-
const lhs = (declaration as BinaryExpression).left;
1795-
switch (kind) {
1796-
case SpecialPropertyAssignmentKind.None:
1797-
case SpecialPropertyAssignmentKind.ModuleExports:
1798-
return undefined;
1799-
case SpecialPropertyAssignmentKind.ExportsProperty:
1800-
if (lhs.kind === SyntaxKind.Identifier) {
1801-
return (lhs as PropertyAccessExpression).name;
1802-
}
1803-
else {
1804-
return ((lhs as PropertyAccessExpression).expression as PropertyAccessExpression).name;
1805-
}
1806-
case SpecialPropertyAssignmentKind.ThisProperty:
1807-
case SpecialPropertyAssignmentKind.Property:
1808-
return (lhs as PropertyAccessExpression).name;
1809-
case SpecialPropertyAssignmentKind.PrototypeProperty:
1810-
return ((lhs as PropertyAccessExpression).expression as PropertyAccessExpression).name;
1811-
}
1812-
}
1813-
else {
1814-
return (declaration as NamedDeclaration).name;
1815-
}
1816-
}
1817-
18181788
export function isLiteralComputedPropertyDeclarationName(node: Node) {
18191789
return (node.kind === SyntaxKind.StringLiteral || node.kind === SyntaxKind.NumericLiteral) &&
18201790
node.parent.kind === SyntaxKind.ComputedPropertyName &&
@@ -4738,4 +4708,25 @@ namespace ts {
47384708
export function unescapeIdentifier(identifier: string): string {
47394709
return identifier.length >= 3 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ && identifier.charCodeAt(2) === CharacterCodes._ ? identifier.substr(1) : identifier;
47404710
}
4711+
4712+
export function getNameOfDeclaration(declaration: Declaration): DeclarationName | undefined {
4713+
if (!declaration) {
4714+
return undefined;
4715+
}
4716+
if (declaration.kind === SyntaxKind.BinaryExpression) {
4717+
const expr = declaration as BinaryExpression;
4718+
switch (getSpecialPropertyAssignmentKind(expr)) {
4719+
case SpecialPropertyAssignmentKind.ExportsProperty:
4720+
case SpecialPropertyAssignmentKind.ThisProperty:
4721+
case SpecialPropertyAssignmentKind.Property:
4722+
case SpecialPropertyAssignmentKind.PrototypeProperty:
4723+
return (expr.left as PropertyAccessExpression).name;
4724+
default:
4725+
return undefined;
4726+
}
4727+
}
4728+
else {
4729+
return (declaration as NamedDeclaration).name;
4730+
}
4731+
}
47414732
}

0 commit comments

Comments
 (0)