diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 60f53c2f527..57fe2a7a0e4 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -555,7 +555,10 @@ namespace ts.FindAllReferences.Core { if (singleReferences.length) { const addRef = state.referenceAdder(exportSymbol); for (const singleRef of singleReferences) { - addRef(singleRef); + // At `default` in `import { default as x }` or `export { default as x }`, do add a reference, but do not rename. + if (!(state.options.isForRename && (isExportSpecifier(singleRef.parent) || isImportSpecifier(singleRef.parent)) && singleRef.escapedText === InternalSymbolName.Default)) { + addRef(singleRef); + } } } @@ -887,7 +890,10 @@ namespace ts.FindAllReferences.Core { } if (!propertyName) { - addRef(); + // Don't rename at `export { default } from "m";`. (but do continue to search for imports of the re-export) + if (!(state.options.isForRename && name.escapedText === InternalSymbolName.Default)) { + addRef(); + } } else if (referenceLocation === propertyName) { // For `export { foo as bar } from "baz"`, "`foo`" will be added from the singleReferences for import searches of the original export. diff --git a/src/services/importTracker.ts b/src/services/importTracker.ts index 80f0721e642..1a4b2a9f751 100644 --- a/src/services/importTracker.ts +++ b/src/services/importTracker.ts @@ -254,7 +254,7 @@ namespace ts.FindAllReferences { } // 'default' might be accessed as a named import `{ default as foo }`. - if (!isForRename && exportKind === ExportKind.Default) { + if (exportKind === ExportKind.Default) { searchForNamedImport(namedBindings as NamedImports | undefined); } } @@ -286,7 +286,9 @@ namespace ts.FindAllReferences { if (propertyName) { // This is `import { foo as bar } from "./a"` or `export { foo as bar } from "./a"`. `foo` isn't a local in the file, so just add it as a single reference. singleReferences.push(propertyName); - if (!isForRename) { // If renaming `foo`, don't touch `bar`, just `foo`. + // If renaming `{ foo as bar }`, don't touch `bar`, just `foo`. + // But do rename `foo` in ` { default as foo }` if that's the original export name. + if (!isForRename || name.escapedText === exportSymbol.escapedName) { // Search locally for `bar`. addSearch(name, checker.getSymbolAtLocation(name)); } diff --git a/tests/cases/fourslash/findAllRefsRenameImportWithSameName.ts b/tests/cases/fourslash/findAllRefsRenameImportWithSameName.ts index 62b1c19c5ed..38c3aaf9398 100644 --- a/tests/cases/fourslash/findAllRefsRenameImportWithSameName.ts +++ b/tests/cases/fourslash/findAllRefsRenameImportWithSameName.ts @@ -16,5 +16,6 @@ const bGroup = { definition: "(alias) const x: 0\nimport x", ranges: bRanges }; verify.referenceGroups(aRanges, [aGroup, bGroup]); verify.referenceGroups(bRanges, [bGroup]); -verify.rangesAreRenameLocations(aRanges); -verify.rangesAreRenameLocations(aRanges); +verify.renameLocations(r0, [r0, r1, r2, r3]); +verify.renameLocations(r1, [r0, r1, r2, r3]); +verify.rangesAreRenameLocations([r2, r3]); diff --git a/tests/cases/fourslash/renameReExportDefault.ts b/tests/cases/fourslash/renameReExportDefault.ts new file mode 100644 index 00000000000..0eb5b81d209 --- /dev/null +++ b/tests/cases/fourslash/renameReExportDefault.ts @@ -0,0 +1,19 @@ +/// + +// @Filename: /a.ts +////export { default } from "./b"; +////export { default as [|b|] } from "./b"; +////export { default as bee } from "./b"; +////import { default as [|b|] } from "./b"; +////import { default as bee } from "./b"; +////import [|b|] from "./b"; + +// @Filename: /b.ts +////const [|b|] = 0; +////export default [|b|]; + +const [r0, r1, r2, r3, r4] = test.ranges(); +verify.renameLocations(r0, [r0]); +verify.renameLocations(r1, [r1]); +verify.renameLocations(r2, [r2]); +verify.renameLocations([r3, r4], [r0, r1, r2, r3, r4]);