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
5 changes: 5 additions & 0 deletions .changeset/lazy-crabs-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@codeshift/utils': patch
---

InsertImportSpecifier now retains the reference to the original import declaration
19 changes: 5 additions & 14 deletions packages/utils/src/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,9 @@ export function insertImportSpecifier(
importSpecifier: ImportSpecifier | ImportDefaultSpecifier,
sourcePath: string,
) {
getImportDeclaration(j, source, sourcePath).replaceWith(declaration =>
j.importDeclaration(
[
// we are appending to the existing specifiers
// We are doing a filter hear because sometimes specifiers can be removed
// but they hang around in the declaration
...(declaration.value.specifiers || []).filter(
item => item.type === 'ImportSpecifier' && item.imported != null,
),
importSpecifier,
],
j.literal(sourcePath),
),
);
const importDeclaration = getImportDeclaration(j, source, sourcePath);

if (!importDeclaration) return;

importDeclaration.get().value.specifiers.push(importSpecifier);
}