Skip to content

Commit af774ff

Browse files
Merge pull request #165 from CodeshiftCommunity/fix-import-spec-inserter
reference handling insertImportSpec
2 parents af0de32 + d239ca6 commit af774ff

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

.changeset/lazy-crabs-vanish.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codeshift/utils': patch
3+
---
4+
5+
InsertImportSpecifier now retains the reference to the original import declaration

packages/utils/src/imports.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,9 @@ export function insertImportSpecifier(
122122
importSpecifier: ImportSpecifier | ImportDefaultSpecifier,
123123
sourcePath: string,
124124
) {
125-
getImportDeclaration(j, source, sourcePath).replaceWith(declaration =>
126-
j.importDeclaration(
127-
[
128-
// we are appending to the existing specifiers
129-
// We are doing a filter hear because sometimes specifiers can be removed
130-
// but they hang around in the declaration
131-
...(declaration.value.specifiers || []).filter(
132-
item => item.type === 'ImportSpecifier' && item.imported != null,
133-
),
134-
importSpecifier,
135-
],
136-
j.literal(sourcePath),
137-
),
138-
);
125+
const importDeclaration = getImportDeclaration(j, source, sourcePath);
126+
127+
if (!importDeclaration) return;
128+
129+
importDeclaration.get().value.specifiers.push(importSpecifier);
139130
}

0 commit comments

Comments
 (0)