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

+4-2
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

+27-19
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

+4-1
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

-3
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

+13-7
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);
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

+1-1
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

+1-1
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

+2-2
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

+2-2
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) ====

tests/baselines/reference/multipleExportAssignments.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/compiler/multipleExportAssignments.ts(13,1): error TS2300: Duplicate identifier 'export='.
2-
tests/cases/compiler/multipleExportAssignments.ts(14,1): error TS2300: Duplicate identifier 'export='.
1+
tests/cases/compiler/multipleExportAssignments.ts(13,10): error TS2300: Duplicate identifier 'export='.
2+
tests/cases/compiler/multipleExportAssignments.ts(14,10): error TS2300: Duplicate identifier 'export='.
33

44

55
==== tests/cases/compiler/multipleExportAssignments.ts (2 errors) ====
@@ -16,10 +16,10 @@ tests/cases/compiler/multipleExportAssignments.ts(14,1): error TS2300: Duplicate
1616
test2(): connectModule;
1717
};
1818
export = server;
19-
~~~~~~~~~~~~~~~~
19+
~~~~~~
2020
!!! error TS2300: Duplicate identifier 'export='.
2121
export = connectExport;
22-
~~~~~~~~~~~~~~~~~~~~~~~
22+
~~~~~~~~~~~~~
2323
!!! error TS2300: Duplicate identifier 'export='.
2424

2525

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts(4,5): error TS2300: Duplicate identifier 'export='.
2-
tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts(5,5): error TS2300: Duplicate identifier 'export='.
1+
tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts(4,14): error TS2300: Duplicate identifier 'export='.
2+
tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts(5,14): error TS2300: Duplicate identifier 'export='.
33

44

55
==== tests/cases/compiler/multipleExportAssignmentsInAmbientDeclaration.ts (2 errors) ====
66
declare module "m1" {
77
var a: number
88
var b: number;
99
export = a;
10-
~~~~~~~~~~~
10+
~
1111
!!! error TS2300: Duplicate identifier 'export='.
1212
export = b;
13-
~~~~~~~~~~~
13+
~
1414
!!! error TS2300: Duplicate identifier 'export='.
1515
}

tests/baselines/reference/typeAliasExport.symbols

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
=== tests/cases/compiler/typeAliasExport.ts ===
22
declare module "a" {
33
export default undefined
4-
>undefined : Symbol(default)
4+
>undefined : Symbol(undefined)
55

66
export var a;
77
>a : Symbol(a, Decl(typeAliasExport.ts, 2, 12), Decl(typeAliasExport.ts, 2, 15))
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
// @Filename: /a.ts
4-
////export default function [|{| "isWriteAccess": true, "isDefinition": true |}f|]() {}
4+
////export [|{| "isWriteAccess": true, "isDefinition": true |}default|] function [|{| "isWriteAccess": true, "isDefinition": true |}f|]() {}
55

66
// @Filename: /b.ts
77
////export import a = require("./a");
@@ -13,16 +13,17 @@
1313
////declare const x: { [|{| "isWriteAccess": true, "isDefinition": true |}default|]: number };
1414
////x.[|default|];
1515

16-
const [r0, r1, r2, r3] = test.ranges();
16+
const [r0, r1, r2, r3, r4] = test.ranges();
1717

18-
verify.singleReferenceGroup("function f(): void", [r0, r1]);
19-
verify.singleReferenceGroup("(property) default: number", [r2, r3]);
18+
verify.referenceGroups([r0], [{ definition: "function f(): void", ranges: [r1, r2] }]);
19+
verify.singleReferenceGroup("function f(): void", [r1, r2]);
20+
verify.singleReferenceGroup("(property) default: number", [r3, r4]);
2021

21-
verify.rangesAreRenameLocations([r0]);
22+
verify.rangesAreRenameLocations([r1]);
2223

2324
// Can't rename a default import.
24-
goTo.rangeStart(r1);
25+
goTo.rangeStart(r2);
2526
verify.renameInfoFailed();
2627

2728
// Can rename a default property.
28-
verify.rangesAreRenameLocations([r2, r3]);
29+
verify.rangesAreRenameLocations([r3, r4]);

tests/cases/fourslash/findAllRefsForDefaultExport04.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ verify.referenceGroups([r0, r2], [
1414
{ definition: "import a", ranges: [r3, r4] }
1515
]);
1616
verify.referenceGroups(r1, [
17-
// TODO:GH#17990
18-
{ definition: "import default", ranges: [r1] },
17+
{ definition: "export default a", ranges: [r1] },
1918
{ definition: "import a", ranges: [r3, r4] },
2019
]);
2120
verify.referenceGroups([r3, r4], [
2221
{ definition: "import a", ranges: [r3, r4] },
23-
// TODO:GH#17990
24-
{ definition: "import default", ranges: [r1] },
22+
{ definition: "export default a", ranges: [r1] },
2523
]);

tests/cases/fourslash/getOccurrencesIsDefinitionOfComputedProperty.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
const ranges = test.ranges();
77
const [r0, r1, r2] = ranges;
88
verify.referenceGroups(r0, [{ definition: '(property) ["foo"]: number', ranges }]);
9-
verify.referenceGroups([r1, r2], undefined); // TODO: fix
9+
verify.referenceGroups([r1, r2], [
10+
// TODO: these are the same thing, should be in the same group.
11+
{ definition: "(property) [\"foo\"]: number", ranges: [r0] },
12+
{ definition: "(property) [\"foo\"]: number", ranges: [r1, r2] },
13+
]);

0 commit comments

Comments
 (0)