diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index a69152ad2b5..32bf64d5c53 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -780,8 +780,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 })); @@ -794,6 +797,22 @@ namespace ts.codefix { const symbolId = getUniqueSymbolId(exportSymbolWithIdenticalName, checker); symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, { ...context, kind: ImportKind.Named })); } + + function getEscapedNameForExportDefault(symbol: Symbol): __String | undefined { + return firstDefined(symbol.declarations, declaration => { + if (isExportAssignment(declaration)) { + if (isIdentifier(declaration.expression)) { + return declaration.expression.escapedText; + } + } + else if (isExportSpecifier(declaration)) { + Debug.assert(declaration.name.escapedText === InternalSymbolName.Default); + if (declaration.propertyName) { + return declaration.propertyName.escapedText; + } + } + }); + } }); return symbolIdActionMap.getAllActions(); diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index fa8c750e45d..8b7473357fe 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -496,6 +496,9 @@ 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 (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, 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 41e3aba24ed..9a208ddff9f 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1274,19 +1274,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) && diff --git a/tests/cases/fourslash/importNameCodeFixDefaultExport2.ts b/tests/cases/fourslash/importNameCodeFixDefaultExport2.ts new file mode 100644 index 00000000000..f6c12044dd9 --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFixDefaultExport2.ts @@ -0,0 +1,14 @@ +/// + +// @Filename: /lib.ts +////class Base { } +////export default Base; + +// @Filename: /test.ts +////[|class Derived extends Base { }|] + +goTo.file("/test.ts"); +verify.importFixAtPosition([ +`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 { }`,]); 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