Skip to content

Commit 71f8852

Browse files
author
Andy
authored
Have getNameOfDeclaration return x for export default x. (#18616)
1 parent b7e744a commit 71f8852

16 files changed

+100
-81
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/utilities.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4112,27 +4112,35 @@ namespace ts {
41124112
if (!declaration) {
41134113
return undefined;
41144114
}
4115-
if (isJSDocPropertyLikeTag(declaration) && declaration.name.kind === SyntaxKind.QualifiedName) {
4116-
return declaration.name.right;
4117-
}
4118-
if (declaration.kind === SyntaxKind.BinaryExpression) {
4119-
const expr = declaration as BinaryExpression;
4120-
switch (getSpecialPropertyAssignmentKind(expr)) {
4121-
case SpecialPropertyAssignmentKind.ExportsProperty:
4122-
case SpecialPropertyAssignmentKind.ThisProperty:
4123-
case SpecialPropertyAssignmentKind.Property:
4124-
case SpecialPropertyAssignmentKind.PrototypeProperty:
4125-
return (expr.left as PropertyAccessExpression).name;
4126-
default:
4127-
return undefined;
4115+
switch (declaration.kind) {
4116+
case SyntaxKind.JSDocPropertyTag:
4117+
case SyntaxKind.JSDocParameterTag: {
4118+
const { name } = declaration as JSDocPropertyLikeTag;
4119+
if (name.kind === SyntaxKind.QualifiedName) {
4120+
return name.right;
4121+
}
4122+
break;
4123+
}
4124+
case SyntaxKind.BinaryExpression: {
4125+
const expr = declaration as BinaryExpression;
4126+
switch (getSpecialPropertyAssignmentKind(expr)) {
4127+
case SpecialPropertyAssignmentKind.ExportsProperty:
4128+
case SpecialPropertyAssignmentKind.ThisProperty:
4129+
case SpecialPropertyAssignmentKind.Property:
4130+
case SpecialPropertyAssignmentKind.PrototypeProperty:
4131+
return (expr.left as PropertyAccessExpression).name;
4132+
default:
4133+
return undefined;
4134+
}
4135+
}
4136+
case SyntaxKind.JSDocTypedefTag:
4137+
return getNameOfJSDocTypedef(declaration as JSDocTypedefTag);
4138+
case SyntaxKind.ExportAssignment: {
4139+
const { expression } = declaration as ExportAssignment;
4140+
return isIdentifier(expression) ? expression : undefined;
41284141
}
41294142
}
4130-
else if (declaration.kind === SyntaxKind.JSDocTypedefTag) {
4131-
return getNameOfJSDocTypedef(declaration as JSDocTypedefTag);
4132-
}
4133-
else {
4134-
return (declaration as NamedDeclaration).name;
4135-
}
4143+
return (declaration as NamedDeclaration).name;
41364144
}
41374145

41384146
/**

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
@@ -341,13 +341,19 @@ namespace ts.SymbolDisplay {
341341
}
342342
if (symbolFlags & SymbolFlags.Alias) {
343343
addNewLineIfDisplayPartsExist();
344-
if (symbol.declarations[0].kind === SyntaxKind.NamespaceExportDeclaration) {
345-
displayParts.push(keywordPart(SyntaxKind.ExportKeyword));
346-
displayParts.push(spacePart());
347-
displayParts.push(keywordPart(SyntaxKind.NamespaceKeyword));
348-
}
349-
else {
350-
displayParts.push(keywordPart(SyntaxKind.ImportKeyword));
344+
switch (symbol.declarations[0].kind) {
345+
case SyntaxKind.NamespaceExportDeclaration:
346+
displayParts.push(keywordPart(SyntaxKind.ExportKeyword));
347+
displayParts.push(spacePart());
348+
displayParts.push(keywordPart(SyntaxKind.NamespaceKeyword));
349+
break;
350+
case SyntaxKind.ExportAssignment:
351+
displayParts.push(keywordPart(SyntaxKind.ExportKeyword));
352+
displayParts.push(spacePart());
353+
displayParts.push(keywordPart((symbol.declarations[0] as ExportAssignment).isExportEquals ? SyntaxKind.EqualsToken : SyntaxKind.DefaultKeyword));
354+
break;
355+
default:
356+
displayParts.push(keywordPart(SyntaxKind.ImportKeyword));
351357
}
352358
displayParts.push(spacePart());
353359
addFullSymbolName(symbol);
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

tests/baselines/reference/jsFileCompilationBindMultipleDefaultExports.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/compiler/a.js(1,22): error TS2528: A module cannot have multiple default exports.
22
tests/cases/compiler/a.js(1,22): error TS2652: Merged declaration 'a' cannot include a default export declaration. Consider adding a separate 'export default a' declaration instead.
3-
tests/cases/compiler/a.js(3,1): error TS2528: A module cannot have multiple default exports.
3+
tests/cases/compiler/a.js(3,15): error TS2528: A module cannot have multiple default exports.
44
tests/cases/compiler/a.js(3,16): error TS1109: Expression expected.
55
tests/cases/compiler/a.js(3,20): error TS2652: Merged declaration 'a' cannot include a default export declaration. Consider adding a separate 'export default a' declaration instead.
66

@@ -13,7 +13,7 @@ tests/cases/compiler/a.js(3,20): error TS2652: Merged declaration 'a' cannot inc
1313
!!! error TS2652: Merged declaration 'a' cannot include a default export declaration. Consider adding a separate 'export default a' declaration instead.
1414
}
1515
export default var a = 10;
16-
~~~~~~~~~~~~~~
16+
1717
!!! error TS2528: A module cannot have multiple default exports.
1818
~~~
1919
!!! error TS1109: Expression expected.

tests/baselines/reference/multipleDefaultExports01.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/es6/modules/m1.ts(1,22): error TS2528: A module cannot have multiple default exports.
22
tests/cases/conformance/es6/modules/m1.ts(5,25): error TS2528: A module cannot have multiple default exports.
3-
tests/cases/conformance/es6/modules/m1.ts(10,1): error TS2528: A module cannot have multiple default exports.
3+
tests/cases/conformance/es6/modules/m1.ts(10,16): error TS2528: A module cannot have multiple default exports.
44
tests/cases/conformance/es6/modules/m2.ts(3,1): error TS2348: Value of type 'typeof foo' is not callable. Did you mean to include 'new'?
55

66

@@ -19,7 +19,7 @@ tests/cases/conformance/es6/modules/m2.ts(3,1): error TS2348: Value of type 'typ
1919

2020
var x = 10;
2121
export default x;
22-
~~~~~~~~~~~~~~~~~
22+
~
2323
!!! error TS2528: A module cannot have multiple default exports.
2424

2525
==== tests/cases/conformance/es6/modules/m2.ts (1 errors) ====

0 commit comments

Comments
 (0)