Skip to content

Commit 8db1d7b

Browse files
authored
Do not adjust location for import/export keywords with more than one possible binding (#36560)
1 parent 9a357c1 commit 8db1d7b

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/services/utilities.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -785,28 +785,36 @@ namespace ts {
785785

786786
function getAdjustedLocationForImportDeclaration(node: ImportDeclaration, forRename: boolean) {
787787
if (node.importClause) {
788+
if (node.importClause.name && node.importClause.namedBindings) {
789+
// do not adjust if we have both a name and named bindings
790+
return;
791+
}
792+
788793
// /**/import [|name|] from ...;
789794
// import /**/type [|name|] from ...;
790-
if (node.importClause.name && !node.importClause.namedBindings) {
795+
if (node.importClause.name) {
791796
return node.importClause.name;
792797
}
798+
793799
// /**/import { [|name|] } from ...;
794800
// /**/import { propertyName as [|name|] } from ...;
795801
// /**/import * as [|name|] from ...;
796802
// import /**/type { [|name|] } from ...;
797803
// import /**/type { propertyName as [|name|] } from ...;
798804
// import /**/type * as [|name|] from ...;
799-
if (!node.importClause.name && node.importClause.namedBindings) {
805+
if (node.importClause.namedBindings) {
800806
if (isNamedImports(node.importClause.namedBindings)) {
801-
if (node.importClause.namedBindings.elements.length === 1) {
802-
return node.importClause.namedBindings.elements[0].name;
807+
// do nothing if there is more than one binding
808+
const onlyBinding = singleOrUndefined(node.importClause.namedBindings.elements);
809+
if (!onlyBinding) {
810+
return;
803811
}
812+
return onlyBinding.name;
804813
}
805814
else if (isNamespaceImport(node.importClause.namedBindings)) {
806815
return node.importClause.namedBindings.name;
807816
}
808817
}
809-
810818
}
811819
if (!forRename) {
812820
// /**/import "[|module|]";
@@ -825,9 +833,12 @@ namespace ts {
825833
// export /**/type { propertyName as [|name|] } from ...
826834
// export /**/type * as [|name|] ...
827835
if (isNamedExports(node.exportClause)) {
828-
if (node.exportClause.elements.length === 1) {
829-
return node.exportClause.elements[0].name;
836+
// do nothing if there is more than one binding
837+
const onlyBinding = singleOrUndefined(node.exportClause.elements);
838+
if (!onlyBinding) {
839+
return;
830840
}
841+
return node.exportClause.elements[0].name;
831842
}
832843
else if (isNamespaceExport(node.exportClause)) {
833844
return node.exportClause.name;

tests/cases/fourslash/referencesForStatementKeywords.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ verify.referenceGroups([importDecl3_importKeyword, importDecl3_typeKeyword], [
224224
verify.referenceGroups(importDecl3_fromKeyword, [{ definition: "module \"/d\"", ranges: [importDecl3_module] }]);
225225

226226
// importDecl4:
227-
verify.referenceGroups([importDecl4_importKeyword, importDecl4_typeKeyword, importDecl4_fromKeyword], [{ definition: "module \"/e\"", ranges: [importDecl4_module] }]);
227+
verify.noReferences(importDecl4_importKeyword);
228+
verify.noReferences(importDecl4_typeKeyword);
229+
verify.referenceGroups(importDecl4_fromKeyword, [{ definition: "module \"/e\"", ranges: [importDecl4_module] }]);
228230
verify.referenceGroups(importDecl4_asKeyword, [{ definition: "(alias) const e3: 2\nimport e3", ranges: [importDecl4_name] }]);
229231

230232
// importDecl5
@@ -245,7 +247,9 @@ verify.referenceGroups([exportDecl3_exportKeyword, exportDecl3_typeKeyword], [
245247
verify.referenceGroups(exportDecl3_fromKeyword, [{ definition: "module \"/i\"", ranges: [exportDecl3_module] }]);
246248

247249
// exportDecl4:
248-
verify.referenceGroups([exportDecl4_exportKeyword, exportDecl4_typeKeyword, exportDecl4_fromKeyword], [{ definition: "module \"/j\"", ranges: [exportDecl4_module] }]);
250+
verify.noReferences(exportDecl4_exportKeyword);
251+
verify.noReferences(exportDecl4_typeKeyword);
252+
verify.referenceGroups(exportDecl4_fromKeyword, [{ definition: "module \"/j\"", ranges: [exportDecl4_module] }]);
249253
verify.referenceGroups(exportDecl4_asKeyword, [{ definition: "(alias) const j3: 2\nexport j3", ranges: [exportDecl4_name] }]);
250254

251255
// exportDecl5:

0 commit comments

Comments
 (0)