Skip to content

Commit 854cec7

Browse files
authored
fix(47670): remove import alias that uses the same name (#47676)
1 parent afee8bf commit 854cec7

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/services/organizeImports.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,9 @@ namespace ts.OrganizeImports {
236236
}
237237
}
238238

239-
newImportSpecifiers.push(...flatMap(namedImports, i => (i.importClause!.namedBindings as NamedImports).elements)); // TODO: GH#18217
239+
newImportSpecifiers.push(...getNewImportSpecifiers(namedImports));
240240

241241
const sortedImportSpecifiers = sortSpecifiers(newImportSpecifiers);
242-
243242
const importDecl = defaultImports.length > 0
244243
? defaultImports[0]
245244
: namedImports[0];
@@ -492,4 +491,20 @@ namespace ts.OrganizeImports {
492491
return 6;
493492
}
494493
}
494+
495+
function getNewImportSpecifiers(namedImports: ImportDeclaration[]) {
496+
return flatMap(namedImports, namedImport =>
497+
map(tryGetNamedBindingElements(namedImport), importSpecifier =>
498+
importSpecifier.name && importSpecifier.propertyName && importSpecifier.name.escapedText === importSpecifier.propertyName.escapedText
499+
? factory.updateImportSpecifier(importSpecifier, importSpecifier.isTypeOnly, /*propertyName*/ undefined, importSpecifier.name)
500+
: importSpecifier
501+
)
502+
);
503+
}
504+
505+
function tryGetNamedBindingElements(namedImport: ImportDeclaration) {
506+
return namedImport.importClause?.namedBindings && isNamedImports(namedImport.importClause.namedBindings)
507+
? namedImport.importClause.namedBindings.elements
508+
: undefined;
509+
}
495510
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////import { foo as foo } from "foo";
4+
////foo;
5+
6+
verify.organizeImports(
7+
`import { foo } from "foo";
8+
foo;`
9+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////import { a as a, b, c, d as d, e as e } from "foo";
4+
////a(b, d);
5+
6+
verify.organizeImports(
7+
`import { a, b, d } from "foo";
8+
a(b, d);`
9+
);

0 commit comments

Comments
 (0)