Skip to content

Commit 5026b77

Browse files
committed
fix: Make transformer to remove multiple import names
1 parent e6ced8d commit 5026b77

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/ts-ast-util/utilily-functions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,9 @@ describe(removeAliasFromImportDeclaration, () => {
309309
function remove(text: string, name: string) {
310310
const inputSource = ts.createSourceFile('input.ts', text, ts.ScriptTarget.Latest);
311311
const statements = inputSource.statements as ts.NodeArray<ts.ImportDeclaration>;
312-
const out = removeAliasFromImportDeclaration(statements[0], name);
312+
const out = removeAliasFromImportDeclaration(statements[0], [name]);
313313
if (!out) return undefined;
314-
return printNode(removeAliasFromImportDeclaration(statements[0], name)).trim();
314+
return printNode(removeAliasFromImportDeclaration(statements[0], [name])).trim();
315315
}
316316

317317
it('should return base statement when name does not match', () => {

src/ts-ast-util/utilily-functions.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ function mergeNamedBinding(base: ts.NamedImportBindings | undefined, head: ts.Na
1010
return astf.updateNamedImports(base, [...base.elements, ...head.elements]);
1111
}
1212

13-
function removeFromNamedBinding(base: ts.NamedImportBindings | undefined, name: string) {
13+
function removeFromNamedBinding(base: ts.NamedImportBindings | undefined, names: string[]) {
1414
if (!base) return undefined;
1515
// treat namedImports only
1616
if (ts.isNamespaceImport(base)) return base;
17-
const elements = base.elements.filter(elm => elm.name.text !== name);
17+
const elements = base.elements.filter(elm => !names.includes(elm.name.text));
1818
if (elements.length === 0) return undefined;
1919
return astf.updateNamedImports(base, elements);
2020
}
@@ -29,12 +29,12 @@ function mergeImportClause(base: ts.ImportClause | undefined, head: ts.ImportCla
2929
return astf.updateImportClause(base, isTypeOnly, name, namedBindings);
3030
}
3131

32-
function removeFromImportClause(base: ts.ImportClause | undefined, name: string) {
32+
function removeFromImportClause(base: ts.ImportClause | undefined, names: string[]) {
3333
if (!base) return undefined;
34-
const namedBindings = removeFromNamedBinding(base.namedBindings, name);
35-
const nameId = base.name?.text !== name ? base.name : undefined;
36-
if (!nameId && !namedBindings) return undefined;
37-
return astf.updateImportClause(base, base.isTypeOnly, nameId, namedBindings);
34+
const namedBindings = removeFromNamedBinding(base.namedBindings, names);
35+
const nameIdentifier = base.name && names.includes(base.name.text) ? undefined : base.name;
36+
if (!nameIdentifier && !namedBindings) return undefined;
37+
return astf.updateImportClause(base, base.isTypeOnly, nameIdentifier, namedBindings);
3838
}
3939

4040
export function findNode(sourceFile: ts.SourceFile, position: number): ts.Node | undefined {
@@ -121,9 +121,9 @@ export function mergeImportDeclarationsWithSameModules(base: ts.ImportDeclaratio
121121
return astf.updateImportDeclaration(base, modifiers, importClause, base.moduleSpecifier, undefined);
122122
}
123123

124-
export function removeAliasFromImportDeclaration(base: ts.ImportDeclaration, name: string) {
124+
export function removeAliasFromImportDeclaration(base: ts.ImportDeclaration, names: string[]) {
125125
const modifiers = base.modifiers;
126-
const importClause = removeFromImportClause(base.importClause, name);
126+
const importClause = removeFromImportClause(base.importClause, names);
127127
if (!importClause) return undefined;
128128
return astf.updateImportDeclaration(base, modifiers, importClause, base.moduleSpecifier, undefined);
129129
}

0 commit comments

Comments
 (0)