Skip to content

Commit dc8c718

Browse files
author
Andy Hanson
committed
Have getNameOfDeclaration return x for export default x.
1 parent b9b1127 commit dc8c718

18 files changed

+116
-118
lines changed

src/compiler/binder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ namespace ts {
230230
// Should not be called on a declaration with a computed property name,
231231
// unless it is a well known Symbol.
232232
function getDeclarationName(node: Declaration): __String {
233+
if (node.kind === SyntaxKind.ExportAssignment) {
234+
return (<ExportAssignment>node).isExportEquals ? InternalSymbolName.ExportEquals : InternalSymbolName.Default;
235+
}
236+
233237
const name = getNameOfDeclaration(node);
234238
if (name) {
235239
if (isAmbientModule(node)) {
@@ -261,8 +265,6 @@ namespace ts {
261265
return InternalSymbolName.Index;
262266
case SyntaxKind.ExportDeclaration:
263267
return InternalSymbolName.ExportStar;
264-
case SyntaxKind.ExportAssignment:
265-
return (<ExportAssignment>node).isExportEquals ? InternalSymbolName.ExportEquals : InternalSymbolName.Default;
266268
case SyntaxKind.BinaryExpression:
267269
if (getSpecialPropertyAssignmentKind(node as BinaryExpression) === SpecialPropertyAssignmentKind.ModuleExports) {
268270
// module.exports = ...

src/compiler/checker.ts

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,15 +2425,6 @@ namespace ts {
24252425
}
24262426
};
24272427

2428-
interface NodeBuilderContext {
2429-
enclosingDeclaration: Node | undefined;
2430-
flags: NodeBuilderFlags | undefined;
2431-
2432-
// State
2433-
encounteredError: boolean;
2434-
symbolStack: Symbol[] | undefined;
2435-
}
2436-
24372428
function createNodeBuilderContext(enclosingDeclaration: Node | undefined, flags: NodeBuilderFlags | undefined): NodeBuilderContext {
24382429
return {
24392430
enclosingDeclaration,
@@ -3027,30 +3018,6 @@ namespace ts {
30273018
}
30283019
}
30293020
}
3030-
3031-
function getNameOfSymbol(symbol: Symbol, context: NodeBuilderContext): string {
3032-
const declaration = firstOrUndefined(symbol.declarations);
3033-
if (declaration) {
3034-
const name = getNameOfDeclaration(declaration);
3035-
if (name) {
3036-
return declarationNameToString(name);
3037-
}
3038-
if (declaration.parent && declaration.parent.kind === SyntaxKind.VariableDeclaration) {
3039-
return declarationNameToString((<VariableDeclaration>declaration.parent).name);
3040-
}
3041-
if (!context.encounteredError && !(context.flags & NodeBuilderFlags.AllowAnonymousIdentifier)) {
3042-
context.encounteredError = true;
3043-
}
3044-
switch (declaration.kind) {
3045-
case SyntaxKind.ClassExpression:
3046-
return "(Anonymous class)";
3047-
case SyntaxKind.FunctionExpression:
3048-
case SyntaxKind.ArrowFunction:
3049-
return "(Anonymous function)";
3050-
}
3051-
}
3052-
return unescapeLeadingUnderscores(symbol.escapedName);
3053-
}
30543021
}
30553022

30563023
function typePredicateToString(typePredicate: TypePredicate, enclosingDeclaration?: Declaration, flags?: TypeFormatFlags): string {
@@ -3115,7 +3082,16 @@ namespace ts {
31153082
return type.flags & TypeFlags.StringLiteral ? '"' + escapeString((<StringLiteralType>type).value) + '"' : "" + (<NumberLiteralType>type).value;
31163083
}
31173084

3118-
function getNameOfSymbol(symbol: Symbol): string {
3085+
interface NodeBuilderContext {
3086+
enclosingDeclaration: Node | undefined;
3087+
flags: NodeBuilderFlags | undefined;
3088+
3089+
// State
3090+
encounteredError: boolean;
3091+
symbolStack: Symbol[] | undefined;
3092+
}
3093+
3094+
function getNameOfSymbol(symbol: Symbol, context?: NodeBuilderContext): string {
31193095
if (symbol.declarations && symbol.declarations.length) {
31203096
const declaration = symbol.declarations[0];
31213097
const name = getNameOfDeclaration(declaration);
@@ -3125,6 +3101,9 @@ namespace ts {
31253101
if (declaration.parent && declaration.parent.kind === SyntaxKind.VariableDeclaration) {
31263102
return declarationNameToString((<VariableDeclaration>declaration.parent).name);
31273103
}
3104+
if (context && !context.encounteredError && !(context.flags & NodeBuilderFlags.AllowAnonymousIdentifier)) {
3105+
context.encounteredError = true;
3106+
}
31283107
switch (declaration.kind) {
31293108
case SyntaxKind.ClassExpression:
31303109
return "(Anonymous class)";

src/compiler/utilities.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4004,27 +4004,35 @@ namespace ts {
40044004
if (!declaration) {
40054005
return undefined;
40064006
}
4007-
if (isJSDocPropertyLikeTag(declaration) && declaration.name.kind === SyntaxKind.QualifiedName) {
4008-
return declaration.name.right;
4009-
}
4010-
if (declaration.kind === SyntaxKind.BinaryExpression) {
4011-
const expr = declaration as BinaryExpression;
4012-
switch (getSpecialPropertyAssignmentKind(expr)) {
4013-
case SpecialPropertyAssignmentKind.ExportsProperty:
4014-
case SpecialPropertyAssignmentKind.ThisProperty:
4015-
case SpecialPropertyAssignmentKind.Property:
4016-
case SpecialPropertyAssignmentKind.PrototypeProperty:
4017-
return (expr.left as PropertyAccessExpression).name;
4018-
default:
4019-
return undefined;
4007+
switch (declaration.kind) {
4008+
case SyntaxKind.JSDocPropertyTag:
4009+
case SyntaxKind.JSDocParameterTag: {
4010+
const { name } = declaration as JSDocPropertyLikeTag;
4011+
if (name.kind === SyntaxKind.QualifiedName) {
4012+
return name.right;
4013+
}
4014+
break;
4015+
}
4016+
case SyntaxKind.BinaryExpression: {
4017+
const expr = declaration as BinaryExpression;
4018+
switch (getSpecialPropertyAssignmentKind(expr)) {
4019+
case SpecialPropertyAssignmentKind.ExportsProperty:
4020+
case SpecialPropertyAssignmentKind.ThisProperty:
4021+
case SpecialPropertyAssignmentKind.Property:
4022+
case SpecialPropertyAssignmentKind.PrototypeProperty:
4023+
return (expr.left as PropertyAccessExpression).name;
4024+
default:
4025+
return undefined;
4026+
}
4027+
}
4028+
case SyntaxKind.JSDocTypedefTag:
4029+
return getNameOfJSDocTypedef(declaration as JSDocTypedefTag);
4030+
case SyntaxKind.ExportAssignment: {
4031+
const { expression } = declaration as ExportAssignment;
4032+
return isIdentifier(expression) ? expression : undefined;
40204033
}
40214034
}
4022-
else if (declaration.kind === SyntaxKind.JSDocTypedefTag) {
4023-
return getNameOfJSDocTypedef(declaration as JSDocTypedefTag);
4024-
}
4025-
else {
4026-
return (declaration as NamedDeclaration).name;
4027-
}
4035+
return (declaration as NamedDeclaration).name;
40284036
}
40294037

40304038
/**

src/services/findAllReferences.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,10 @@ namespace ts.FindAllReferences.Core {
482482
/** @param allSearchSymbols set of additinal symbols for use by `includes`. */
483483
createSearch(location: Node, symbol: Symbol, comingFrom: ImportExport | undefined, searchOptions: { text?: string, allSearchSymbols?: Symbol[] } = {}): Search {
484484
// Note: if this is an external module symbol, the name doesn't include quotes.
485-
const { text = stripQuotes(getDeclaredName(this.checker, symbol, location)), allSearchSymbols = undefined } = searchOptions;
485+
const {
486+
text = stripQuotes(unescapeLeadingUnderscores((getLocalSymbolForExportDefault(symbol) || symbol).escapedName)),
487+
allSearchSymbols = undefined,
488+
} = searchOptions;
486489
const escapedText = escapeLeadingUnderscores(text);
487490
const parents = this.options.implementations && getParentSymbolsOfPropertyAccess(location, symbol, this.checker);
488491
return {

src/services/importTracker.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,6 @@ namespace ts.FindAllReferences {
609609
}
610610

611611
return forEach(symbol.declarations, decl => {
612-
if (isExportAssignment(decl)) {
613-
return isIdentifier(decl.expression) ? decl.expression.escapedText : undefined;
614-
}
615612
const name = getNameOfDeclaration(decl);
616613
return name && name.kind === SyntaxKind.Identifier && name.escapedText;
617614
});

src/services/symbolDisplay.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,19 @@ namespace ts.SymbolDisplay {
345345
}
346346
if (symbolFlags & SymbolFlags.Alias) {
347347
addNewLineIfDisplayPartsExist();
348-
if (symbol.declarations[0].kind === SyntaxKind.NamespaceExportDeclaration) {
349-
displayParts.push(keywordPart(SyntaxKind.ExportKeyword));
350-
displayParts.push(spacePart());
351-
displayParts.push(keywordPart(SyntaxKind.NamespaceKeyword));
352-
}
353-
else {
354-
displayParts.push(keywordPart(SyntaxKind.ImportKeyword));
348+
switch (symbol.declarations[0].kind) {
349+
case SyntaxKind.NamespaceExportDeclaration:
350+
displayParts.push(keywordPart(SyntaxKind.ExportKeyword));
351+
displayParts.push(spacePart());
352+
displayParts.push(keywordPart(SyntaxKind.NamespaceKeyword));
353+
break;
354+
case SyntaxKind.ExportAssignment:
355+
displayParts.push(keywordPart(SyntaxKind.ExportKeyword));
356+
displayParts.push(spacePart());
357+
displayParts.push(keywordPart((symbol.declarations[0] as ExportAssignment).isExportEquals ? SyntaxKind.EqualsToken : SyntaxKind.DefaultKeyword));
358+
break;
359+
default:
360+
displayParts.push(keywordPart(SyntaxKind.ImportKeyword));
355361
}
356362
displayParts.push(spacePart());
357363
addFullSymbolName(symbol);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
=== tests/cases/compiler/declarationQuotedMembers.ts ===
22
export declare const mapped: { [K in 'a-b-c']: number }
3-
>mapped : { a-b-c: number; }
3+
>mapped : { "a-b-c": number; }
44
>K : K
55

66
export const example = mapped;
7-
>example : { a-b-c: number; }
8-
>mapped : { a-b-c: number; }
7+
>example : { "a-b-c": number; }
8+
>mapped : { "a-b-c": number; }
99

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
tests/cases/conformance/externalModules/foo1.ts(3,1): error TS2300: Duplicate identifier 'export='.
2-
tests/cases/conformance/externalModules/foo1.ts(4,1): error TS2300: Duplicate identifier 'export='.
3-
tests/cases/conformance/externalModules/foo2.ts(3,1): error TS2300: Duplicate identifier 'export='.
4-
tests/cases/conformance/externalModules/foo2.ts(4,1): error TS2300: Duplicate identifier 'export='.
5-
tests/cases/conformance/externalModules/foo3.ts(7,1): error TS2300: Duplicate identifier 'export='.
6-
tests/cases/conformance/externalModules/foo3.ts(8,1): error TS2300: Duplicate identifier 'export='.
7-
tests/cases/conformance/externalModules/foo4.ts(1,1): error TS2300: Duplicate identifier 'export='.
8-
tests/cases/conformance/externalModules/foo4.ts(8,1): error TS2300: Duplicate identifier 'export='.
9-
tests/cases/conformance/externalModules/foo5.ts(4,1): error TS2300: Duplicate identifier 'export='.
10-
tests/cases/conformance/externalModules/foo5.ts(5,1): error TS2300: Duplicate identifier 'export='.
11-
tests/cases/conformance/externalModules/foo5.ts(6,1): error TS2300: Duplicate identifier 'export='.
1+
tests/cases/conformance/externalModules/foo1.ts(3,10): error TS2300: Duplicate identifier 'export='.
2+
tests/cases/conformance/externalModules/foo1.ts(4,10): error TS2300: Duplicate identifier 'export='.
3+
tests/cases/conformance/externalModules/foo2.ts(3,10): error TS2300: Duplicate identifier 'export='.
4+
tests/cases/conformance/externalModules/foo2.ts(4,10): error TS2300: Duplicate identifier 'export='.
5+
tests/cases/conformance/externalModules/foo3.ts(7,10): error TS2300: Duplicate identifier 'export='.
6+
tests/cases/conformance/externalModules/foo3.ts(8,10): error TS2300: Duplicate identifier 'export='.
7+
tests/cases/conformance/externalModules/foo4.ts(1,10): error TS2300: Duplicate identifier 'export='.
8+
tests/cases/conformance/externalModules/foo4.ts(8,10): error TS2300: Duplicate identifier 'export='.
9+
tests/cases/conformance/externalModules/foo5.ts(4,10): error TS2300: Duplicate identifier 'export='.
10+
tests/cases/conformance/externalModules/foo5.ts(5,10): error TS2300: Duplicate identifier 'export='.
11+
tests/cases/conformance/externalModules/foo5.ts(6,10): error TS2300: Duplicate identifier 'export='.
1212

1313

1414
==== tests/cases/conformance/externalModules/foo1.ts (2 errors) ====
1515
var x = 10;
1616
var y = 20;
1717
export = x;
18-
~~~~~~~~~~~
18+
~
1919
!!! error TS2300: Duplicate identifier 'export='.
2020
export = y;
21-
~~~~~~~~~~~
21+
~
2222
!!! error TS2300: Duplicate identifier 'export='.
2323

2424
==== tests/cases/conformance/externalModules/foo2.ts (2 errors) ====
2525
var x = 10;
2626
class y {};
2727
export = x;
28-
~~~~~~~~~~~
28+
~
2929
!!! error TS2300: Duplicate identifier 'export='.
3030
export = y;
31-
~~~~~~~~~~~
31+
~
3232
!!! error TS2300: Duplicate identifier 'export='.
3333

3434
==== tests/cases/conformance/externalModules/foo3.ts (2 errors) ====
@@ -39,15 +39,15 @@ tests/cases/conformance/externalModules/foo5.ts(6,1): error TS2300: Duplicate id
3939
y: number;
4040
}
4141
export = x;
42-
~~~~~~~~~~~
42+
~
4343
!!! error TS2300: Duplicate identifier 'export='.
4444
export = y;
45-
~~~~~~~~~~~
45+
~
4646
!!! error TS2300: Duplicate identifier 'export='.
4747

4848
==== tests/cases/conformance/externalModules/foo4.ts (2 errors) ====
4949
export = x;
50-
~~~~~~~~~~~
50+
~
5151
!!! error TS2300: Duplicate identifier 'export='.
5252
function x(){
5353
return 42;
@@ -56,20 +56,20 @@ tests/cases/conformance/externalModules/foo5.ts(6,1): error TS2300: Duplicate id
5656
return 42;
5757
}
5858
export = y;
59-
~~~~~~~~~~~
59+
~
6060
!!! error TS2300: Duplicate identifier 'export='.
6161

6262
==== tests/cases/conformance/externalModules/foo5.ts (3 errors) ====
6363
var x = 5;
6464
var y = "test";
6565
var z = {};
6666
export = x;
67-
~~~~~~~~~~~
67+
~
6868
!!! error TS2300: Duplicate identifier 'export='.
6969
export = y;
70-
~~~~~~~~~~~
70+
~
7171
!!! error TS2300: Duplicate identifier 'export='.
7272
export = z;
73-
~~~~~~~~~~~
73+
~
7474
!!! error TS2300: Duplicate identifier 'export='.
7575

tests/baselines/reference/es5-commonjs7.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=== tests/cases/compiler/test.d.ts ===
22
export default undefined;
3-
>undefined : Symbol(default)
3+
>undefined : Symbol(undefined)
44

55
export var __esModule;
66
>__esModule : Symbol(__esModule, Decl(test.d.ts, 1, 10))

tests/baselines/reference/exportDefaultVariable.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ declare var io: any;
66

77
declare module 'module' {
88
export default io;
9-
>io : Symbol(default, Decl(exportDefaultVariable.ts, 2, 11))
9+
>io : Symbol(io, Decl(exportDefaultVariable.ts, 2, 11))
1010
}
1111

0 commit comments

Comments
 (0)