Fix bug: Don't rename 'default' in export { default as x } from "m"; (#23434)

* Fix bug: Don't rename 'default' in `export { default as x } from "m";`

* Rename `foo` in `{ default as foo }` if that's the original export name
This commit is contained in:
Andy
2018-04-19 15:35:25 -07:00
committed by GitHub
parent bc7979c174
commit c258d6e1b6
4 changed files with 34 additions and 6 deletions
+8 -2
View File
@@ -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.
+4 -2
View File
@@ -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));
}
@@ -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]);
@@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
// @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]);