Skip to content

Commit

Permalink
fix: Make transformer to remove multiple import names
Browse files Browse the repository at this point in the history
  • Loading branch information
Quramy committed Mar 15, 2024
1 parent e6ced8d commit 5026b77
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/ts-ast-util/utilily-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ describe(removeAliasFromImportDeclaration, () => {
function remove(text: string, name: string) {
const inputSource = ts.createSourceFile('input.ts', text, ts.ScriptTarget.Latest);
const statements = inputSource.statements as ts.NodeArray<ts.ImportDeclaration>;
const out = removeAliasFromImportDeclaration(statements[0], name);
const out = removeAliasFromImportDeclaration(statements[0], [name]);
if (!out) return undefined;
return printNode(removeAliasFromImportDeclaration(statements[0], name)).trim();
return printNode(removeAliasFromImportDeclaration(statements[0], [name])).trim();
}

it('should return base statement when name does not match', () => {
Expand Down
18 changes: 9 additions & 9 deletions src/ts-ast-util/utilily-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ function mergeNamedBinding(base: ts.NamedImportBindings | undefined, head: ts.Na
return astf.updateNamedImports(base, [...base.elements, ...head.elements]);
}

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

function removeFromImportClause(base: ts.ImportClause | undefined, name: string) {
function removeFromImportClause(base: ts.ImportClause | undefined, names: string[]) {
if (!base) return undefined;
const namedBindings = removeFromNamedBinding(base.namedBindings, name);
const nameId = base.name?.text !== name ? base.name : undefined;
if (!nameId && !namedBindings) return undefined;
return astf.updateImportClause(base, base.isTypeOnly, nameId, namedBindings);
const namedBindings = removeFromNamedBinding(base.namedBindings, names);
const nameIdentifier = base.name && names.includes(base.name.text) ? undefined : base.name;
if (!nameIdentifier && !namedBindings) return undefined;
return astf.updateImportClause(base, base.isTypeOnly, nameIdentifier, namedBindings);
}

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

export function removeAliasFromImportDeclaration(base: ts.ImportDeclaration, name: string) {
export function removeAliasFromImportDeclaration(base: ts.ImportDeclaration, names: string[]) {
const modifiers = base.modifiers;
const importClause = removeFromImportClause(base.importClause, name);
const importClause = removeFromImportClause(base.importClause, names);
if (!importClause) return undefined;
return astf.updateImportDeclaration(base, modifiers, importClause, base.moduleSpecifier, undefined);
}

0 comments on commit 5026b77

Please sign in to comment.