Do not adjust location for import/export keywords with more than one possible binding (#36560)

This commit is contained in:
Ron Buckton
2020-01-31 14:58:30 -08:00
committed by GitHub
parent 9a357c19f3
commit 8db1d7bc6d
2 changed files with 24 additions and 9 deletions

View File

@@ -785,28 +785,36 @@ namespace ts {
function getAdjustedLocationForImportDeclaration(node: ImportDeclaration, forRename: boolean) {
if (node.importClause) {
if (node.importClause.name && node.importClause.namedBindings) {
// do not adjust if we have both a name and named bindings
return;
}
// /**/import [|name|] from ...;
// import /**/type [|name|] from ...;
if (node.importClause.name && !node.importClause.namedBindings) {
if (node.importClause.name) {
return node.importClause.name;
}
// /**/import { [|name|] } from ...;
// /**/import { propertyName as [|name|] } from ...;
// /**/import * as [|name|] from ...;
// import /**/type { [|name|] } from ...;
// import /**/type { propertyName as [|name|] } from ...;
// import /**/type * as [|name|] from ...;
if (!node.importClause.name && node.importClause.namedBindings) {
if (node.importClause.namedBindings) {
if (isNamedImports(node.importClause.namedBindings)) {
if (node.importClause.namedBindings.elements.length === 1) {
return node.importClause.namedBindings.elements[0].name;
// do nothing if there is more than one binding
const onlyBinding = singleOrUndefined(node.importClause.namedBindings.elements);
if (!onlyBinding) {
return;
}
return onlyBinding.name;
}
else if (isNamespaceImport(node.importClause.namedBindings)) {
return node.importClause.namedBindings.name;
}
}
}
if (!forRename) {
// /**/import "[|module|]";
@@ -825,9 +833,12 @@ namespace ts {
// export /**/type { propertyName as [|name|] } from ...
// export /**/type * as [|name|] ...
if (isNamedExports(node.exportClause)) {
if (node.exportClause.elements.length === 1) {
return node.exportClause.elements[0].name;
// do nothing if there is more than one binding
const onlyBinding = singleOrUndefined(node.exportClause.elements);
if (!onlyBinding) {
return;
}
return node.exportClause.elements[0].name;
}
else if (isNamespaceExport(node.exportClause)) {
return node.exportClause.name;

View File

@@ -224,7 +224,9 @@ verify.referenceGroups([importDecl3_importKeyword, importDecl3_typeKeyword], [
verify.referenceGroups(importDecl3_fromKeyword, [{ definition: "module \"/d\"", ranges: [importDecl3_module] }]);
// importDecl4:
verify.referenceGroups([importDecl4_importKeyword, importDecl4_typeKeyword, importDecl4_fromKeyword], [{ definition: "module \"/e\"", ranges: [importDecl4_module] }]);
verify.noReferences(importDecl4_importKeyword);
verify.noReferences(importDecl4_typeKeyword);
verify.referenceGroups(importDecl4_fromKeyword, [{ definition: "module \"/e\"", ranges: [importDecl4_module] }]);
verify.referenceGroups(importDecl4_asKeyword, [{ definition: "(alias) const e3: 2\nimport e3", ranges: [importDecl4_name] }]);
// importDecl5
@@ -245,7 +247,9 @@ verify.referenceGroups([exportDecl3_exportKeyword, exportDecl3_typeKeyword], [
verify.referenceGroups(exportDecl3_fromKeyword, [{ definition: "module \"/i\"", ranges: [exportDecl3_module] }]);
// exportDecl4:
verify.referenceGroups([exportDecl4_exportKeyword, exportDecl4_typeKeyword, exportDecl4_fromKeyword], [{ definition: "module \"/j\"", ranges: [exportDecl4_module] }]);
verify.noReferences(exportDecl4_exportKeyword);
verify.noReferences(exportDecl4_typeKeyword);
verify.referenceGroups(exportDecl4_fromKeyword, [{ definition: "module \"/j\"", ranges: [exportDecl4_module] }]);
verify.referenceGroups(exportDecl4_asKeyword, [{ definition: "(alias) const j3: 2\nexport j3", ranges: [exportDecl4_name] }]);
// exportDecl5: