Skip to content

Commit 7bb3b74

Browse files
committed
fix(codemod): visit nested import types inside remapped specifiers
replaceSpecifier used source.slice() to copy the tail of a node after rewriting its specifier, which skipped child nodes entirely. A nested import('react') inside a type argument of a remapped specifier (e.g. import('./App').Props<import('react').ReactNode>) was therefore left unrewritten after migration. Replace the raw slice with span(), which iterates child nodes through emit() and rewrites them. This is safe for all callers: import/export declarations and require() calls have no children in the tail range.
1 parent 718665b commit 7bb3b74

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎packages/codemod/src/transform.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ class ModuleTransform {
766766
quote +
767767
next +
768768
quote +
769-
this.source.slice(specifier.end, node.end)
769+
this.span(node, specifier.end, node.end, PLAIN)
770770
);
771771
}
772772

‎packages/codemod/tests/transform.test.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ describe('transformModule', () => {
134134
expect(code).toContain("Component = import('octane').FC<{ id: string }>;");
135135
});
136136

137+
it('rewrites nested `import()` types inside remapped specifiers', () => {
138+
const source = "export type Result = import('./App').Props<import('react').ReactNode>;\n";
139+
const { code } = transformModule(source, 'nested.ts', {
140+
resolveSpecifier: (s) => (s === './App' ? './App.tsrx' : null),
141+
});
142+
expect(code).toContain("import('./App.tsrx').Props");
143+
expect(code).toContain("import('octane').OctaneNode");
144+
expect(code).not.toContain("import('react')");
145+
});
146+
137147
it('rewrites self-closing context providers', () => {
138148
const source =
139149
"import { createContext } from 'react';\nconst Theme = createContext('light');\nexport function A() {\n\treturn <Theme.Provider value=\"dark\" />;\n}\n";

0 commit comments

Comments
 (0)