Skip to content

Commit 5be3332

Browse files
author
Sebastian Silbermann
committed
Preserve type import
1 parent ca29ad8 commit 5be3332

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

transforms/__tests__/deprecated-legacy-ref.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ test("named import", () => {
4848
`);
4949
});
5050

51+
test("named type import", () => {
52+
expect(
53+
applyTransform(`
54+
import { type LegacyRef } from 'react';
55+
interface Props<T> {
56+
legacyRef?: LegacyRef;
57+
legacyRefTyped?: LegacyRef<T>;
58+
}
59+
`),
60+
).toMatchInlineSnapshot(`
61+
"import { type Ref } from 'react';
62+
interface Props<T> {
63+
legacyRef?: Ref;
64+
legacyRefTyped?: Ref<T>;
65+
}"
66+
`);
67+
});
68+
5169
test("named import with existing target import", () => {
5270
expect(
5371
applyTransform(`

transforms/deprecated-legacy-ref.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ const deprecatedLegacyRefTransform = (file, api) => {
3535
sourceIdentifierImports.remove();
3636
} else if (sourceIdentifierImports.length > 0) {
3737
hasChanges = true;
38-
sourceIdentifierImports.replaceWith(() => {
38+
sourceIdentifierImports.replaceWith((path) => {
3939
const importSpecifier = j.importSpecifier(j.identifier("Ref"));
40+
if ("importKind" in path.node) {
41+
// @ts-expect-error -- Missing types in jscodeshift. Babel uses `importKind`: https://astexplorer.net/#/gist/a76bd35f28483a467fef29d3c63aac9b/0e7ba6688fc09bd11b92197349b2384bb4c94574
42+
importSpecifier.importKind = path.node.importKind;
43+
}
4044

4145
return importSpecifier;
4246
});

0 commit comments

Comments
 (0)