Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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|]";
Expand All @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions tests/cases/fourslash/referencesForStatementKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down