From ff58f1f2f9b479d73f4026da1b98b3a8c7175c49 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Wed, 3 Jan 2018 16:37:43 -0800 Subject: [PATCH 1/8] Handle different default export forms the same way in import code fixes `export default C` and `export { C as default }` should be handled the same as `export default class C { }`. Fixes #19115 --- src/services/codefixes/importFixes.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 20d6ac4d5ba..936bb15e947 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -770,8 +770,11 @@ namespace ts.codefix { const defaultExport = checker.tryGetMemberInModuleExports(InternalSymbolName.Default, moduleSymbol); if (defaultExport) { const localSymbol = getLocalSymbolForExportDefault(defaultExport); - if ((localSymbol && localSymbol.escapedName === symbolName || moduleSymbolToValidIdentifier(moduleSymbol, context.compilerOptions.target) === symbolName) - && checkSymbolHasMeaning(localSymbol || defaultExport, currentTokenMeaning)) { + if (( + localSymbol && localSymbol.escapedName === symbolName || + getEscapedNameForExportDefault(defaultExport) === symbolName || + moduleSymbolToValidIdentifier(moduleSymbol, context.compilerOptions.target) === symbolName + ) && checkSymbolHasMeaning(localSymbol || defaultExport, currentTokenMeaning)) { // check if this symbol is already used const symbolId = getUniqueSymbolId(localSymbol || defaultExport, checker); symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, { ...context, kind: ImportKind.Default })); @@ -784,6 +787,23 @@ namespace ts.codefix { const symbolId = getUniqueSymbolId(exportSymbolWithIdenticalName, checker); symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, { ...context, kind: ImportKind.Named })); } + + function getEscapedNameForExportDefault(symbol: Symbol): __String | undefined { + const declarations = symbol.declarations; + if (length(declarations) > 0) { + const declaration = declarations[0]; + if (isExportAssignment(declaration)) { + if (isIdentifier(declaration.expression)) { + return declaration.expression.escapedText; + } + } + else if (isExportSpecifier(declaration)) { + if (declaration.propertyName) { + return declaration.propertyName.escapedText; + } + } + } + } }); return symbolIdActionMap.getAllActions(); From 1e93967ec4161fe209768d5a0a581be00ffaa152 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Wed, 3 Jan 2018 17:58:49 -0800 Subject: [PATCH 2/8] Add regression tests --- src/services/codefixes/importFixes.ts | 1 + .../importNameCodeFixDefaultExport2.ts | 21 +++++++++++++++++++ .../importNameCodeFixDefaultExport3.ts | 21 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 tests/cases/fourslash/importNameCodeFixDefaultExport2.ts create mode 100644 tests/cases/fourslash/importNameCodeFixDefaultExport3.ts diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 936bb15e947..1ff23f679d8 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -798,6 +798,7 @@ namespace ts.codefix { } } else if (isExportSpecifier(declaration)) { + Debug.assert(declaration.name.escapedText === InternalSymbolName.Default); if (declaration.propertyName) { return declaration.propertyName.escapedText; } diff --git a/tests/cases/fourslash/importNameCodeFixDefaultExport2.ts b/tests/cases/fourslash/importNameCodeFixDefaultExport2.ts new file mode 100644 index 00000000000..8e09196d06c --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFixDefaultExport2.ts @@ -0,0 +1,21 @@ +/// + +// @allowJs: true +// @checkJs: true + +// @Filename: /lib.js +////class Base { } +////export default Base; + +// @Filename: /test.js +////[|class Derived extends Base { }|] + +goTo.file("/test.js"); +verify.importFixAtPosition([ +`// @ts-ignore +class Derived extends Base { }`, +`// @ts-nocheck +class Derived extends Base { }`, +`import Base from "./lib"; + +class Derived extends Base { }`,]); diff --git a/tests/cases/fourslash/importNameCodeFixDefaultExport3.ts b/tests/cases/fourslash/importNameCodeFixDefaultExport3.ts new file mode 100644 index 00000000000..1d4c6bae8c2 --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFixDefaultExport3.ts @@ -0,0 +1,21 @@ +/// + +// @allowJs: true +// @checkJs: true + +// @Filename: /lib.js +////class Base { } +////export { Base as default }; + +// @Filename: /test.js +////[|class Derived extends Base { }|] + +goTo.file("/test.js"); +verify.importFixAtPosition([ +`// @ts-ignore +class Derived extends Base { }`, +`// @ts-nocheck +class Derived extends Base { }`, +`import Base from "./lib"; + +class Derived extends Base { }`,]); From f0d9cb3fbfe7317ff3f82c36cde5601992ed2875 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Thu, 4 Jan 2018 14:28:24 -0800 Subject: [PATCH 3/8] Add explanatory comment to FAR call-site --- src/services/findAllReferences.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index fa56326b9f0..de81cce8000 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -510,6 +510,8 @@ namespace ts.FindAllReferences.Core { /** @param allSearchSymbols set of additinal symbols for use by `includes`. */ createSearch(location: Node, symbol: Symbol, comingFrom: ImportExport | undefined, searchOptions: { text?: string, allSearchSymbols?: Symbol[] } = {}): Search { // Note: if this is an external module symbol, the name doesn't include quotes. + // Note: getLocalSymbolForExportDefault handles `export default class C {}`, but not `export default C` or `export { C as default }`. + // The other two forms seem to be handled downstream (i.e. special-casing the first form here appears to be intentional). const { text = stripQuotes(unescapeLeadingUnderscores((getLocalSymbolForExportDefault(symbol) || symbol).escapedName)), allSearchSymbols = undefined, From 50e3044ed2bb335830193f837bdf0cdc99f23cd4 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Thu, 4 Jan 2018 11:13:03 -0800 Subject: [PATCH 4/8] Correct fullDisplayName returned from rename The regular displayName had special handling for `as` clauses in imports and exports but the fullDisplayName did not. They should be consistent. (The impact of this change is very minor - it only affects the root node of the tree control in the rename preview, which is off by default.) Bonus: Eliminate `getDeclaredName` which is only called by rename. Note: Special handling of default exports was not preserved since `default` cannot be renamed. --- src/services/rename.ts | 12 ++++++++++-- src/services/utilities.ts | 13 ------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/services/rename.ts b/src/services/rename.ts index cfe79679fba..ca46a93f3c9 100644 --- a/src/services/rename.ts +++ b/src/services/rename.ts @@ -38,9 +38,17 @@ namespace ts.Rename { return undefined; } - const displayName = stripQuotes(getDeclaredName(typeChecker, symbol, node)); const kind = SymbolDisplay.getSymbolKind(typeChecker, symbol, node); - return kind ? getRenameInfoSuccess(displayName, typeChecker.getFullyQualifiedName(symbol), kind, SymbolDisplay.getSymbolModifiers(symbol), node, sourceFile) : undefined; + if (!kind) { + return undefined; + } + + const specifierName = (isImportOrExportSpecifierName(node) || isStringOrNumericLiteral(node) && node.parent.kind === SyntaxKind.ComputedPropertyName) + ? stripQuotes(getTextOfIdentifierOrLiteral(node)) + : undefined; + const displayName = specifierName || typeChecker.symbolToString(symbol); + const fullDisplayName = specifierName || typeChecker.getFullyQualifiedName(symbol); + return getRenameInfoSuccess(displayName, fullDisplayName, kind, SymbolDisplay.getSymbolModifiers(symbol), node, sourceFile); } } else if (node.kind === SyntaxKind.StringLiteral) { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 140873dc06d..b5178005521 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1278,19 +1278,6 @@ namespace ts { }); } - export function getDeclaredName(typeChecker: TypeChecker, symbol: Symbol, location: Node): string { - // If this is an export or import specifier it could have been renamed using the 'as' syntax. - // If so we want to search for whatever is under the cursor. - if (isImportOrExportSpecifierName(location) || isStringOrNumericLiteral(location) && location.parent.kind === SyntaxKind.ComputedPropertyName) { - return getTextOfIdentifierOrLiteral(location); - } - - // Try to get the local symbol if we're dealing with an 'export default' - // since that symbol has the "true" name. - const localExportDefaultSymbol = getLocalSymbolForExportDefault(symbol); - return typeChecker.symbolToString(localExportDefaultSymbol || symbol); - } - export function isImportOrExportSpecifierName(location: Node): location is Identifier { return location.parent && (location.parent.kind === SyntaxKind.ImportSpecifier || location.parent.kind === SyntaxKind.ExportSpecifier) && From a0495a65fe1f84edc560aaf94dbf56051978a3d5 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 5 Jan 2018 10:53:18 -0800 Subject: [PATCH 5/8] Improve comment --- src/services/findAllReferences.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index de81cce8000..3665e097026 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -511,7 +511,8 @@ namespace ts.FindAllReferences.Core { createSearch(location: Node, symbol: Symbol, comingFrom: ImportExport | undefined, searchOptions: { text?: string, allSearchSymbols?: Symbol[] } = {}): Search { // Note: if this is an external module symbol, the name doesn't include quotes. // Note: getLocalSymbolForExportDefault handles `export default class C {}`, but not `export default C` or `export { C as default }`. - // The other two forms seem to be handled downstream (i.e. special-casing the first form here appears to be intentional). + // The other two forms seem to be handled downstream (e.g. in `skipPastExportOrImportSpecifier`), so special-casing the first form + // here appears to be intentional). const { text = stripQuotes(unescapeLeadingUnderscores((getLocalSymbolForExportDefault(symbol) || symbol).escapedName)), allSearchSymbols = undefined, From 13c26fe8cf16dc812526298e5e78d00d51af98b6 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 5 Jan 2018 13:32:01 -0800 Subject: [PATCH 6/8] Add regression tests for rename display name --- tests/cases/fourslash/renameForAliasingExport01.ts | 9 +++++++++ tests/cases/fourslash/renameForAliasingExport02.ts | 9 +++++++++ 2 files changed, 18 insertions(+) create mode 100644 tests/cases/fourslash/renameForAliasingExport01.ts create mode 100644 tests/cases/fourslash/renameForAliasingExport02.ts diff --git a/tests/cases/fourslash/renameForAliasingExport01.ts b/tests/cases/fourslash/renameForAliasingExport01.ts new file mode 100644 index 00000000000..a46d1806933 --- /dev/null +++ b/tests/cases/fourslash/renameForAliasingExport01.ts @@ -0,0 +1,9 @@ +/// + +// @Filename: foo.ts +////let x = 1; +//// +////export { /**/[|x|] as y }; + +goTo.marker(); +verify.renameInfoSucceeded(/*displayName*/"x", /*fullDisplayName*/"x"); \ No newline at end of file diff --git a/tests/cases/fourslash/renameForAliasingExport02.ts b/tests/cases/fourslash/renameForAliasingExport02.ts new file mode 100644 index 00000000000..ece969ca504 --- /dev/null +++ b/tests/cases/fourslash/renameForAliasingExport02.ts @@ -0,0 +1,9 @@ +/// + +// @Filename: foo.ts +////let x = 1; +//// +////export { x as /**/[|y|] }; + +goTo.marker(); +verify.renameInfoSucceeded(/*displayName*/"y", /*fullDisplayName*/'"/tests/cases/fourslash/foo".y'); \ No newline at end of file From b07797cc97b81652027b7e2606886090ab085c3f Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 5 Jan 2018 16:23:08 -0800 Subject: [PATCH 7/8] Convert test from JS to TS --- .../fourslash/importNameCodeFixDefaultExport2.ts | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/tests/cases/fourslash/importNameCodeFixDefaultExport2.ts b/tests/cases/fourslash/importNameCodeFixDefaultExport2.ts index 8e09196d06c..f6c12044dd9 100644 --- a/tests/cases/fourslash/importNameCodeFixDefaultExport2.ts +++ b/tests/cases/fourslash/importNameCodeFixDefaultExport2.ts @@ -1,21 +1,14 @@ /// -// @allowJs: true -// @checkJs: true - -// @Filename: /lib.js +// @Filename: /lib.ts ////class Base { } ////export default Base; -// @Filename: /test.js +// @Filename: /test.ts ////[|class Derived extends Base { }|] -goTo.file("/test.js"); +goTo.file("/test.ts"); verify.importFixAtPosition([ -`// @ts-ignore -class Derived extends Base { }`, -`// @ts-nocheck -class Derived extends Base { }`, `import Base from "./lib"; class Derived extends Base { }`,]); From 18f9d693fe65080d2a0f24f2c03992f8f213dba9 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 5 Jan 2018 16:29:53 -0800 Subject: [PATCH 8/8] Stop limiting getEscapedNameForExportDefault to the first declaration --- src/services/codefixes/importFixes.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 1ff23f679d8..171a0925843 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -789,9 +789,7 @@ namespace ts.codefix { } function getEscapedNameForExportDefault(symbol: Symbol): __String | undefined { - const declarations = symbol.declarations; - if (length(declarations) > 0) { - const declaration = declarations[0]; + return firstDefined(symbol.declarations, declaration => { if (isExportAssignment(declaration)) { if (isIdentifier(declaration.expression)) { return declaration.expression.escapedText; @@ -803,7 +801,7 @@ namespace ts.codefix { return declaration.propertyName.escapedText; } } - } + }); } });