Skip to content

Commit 9435062

Browse files
committed
fix(codemod): preserve side-effect imports of React modules in .d.ts files
Side-effect imports like `import 'react'` that exist solely to make `declare module 'react' { ... }` an augmentation (not an ambient definition) were being pushed to importSlots and then silently dropped by finishImports, which skips imports with no importClause. Rewrite such imports in place instead of deferring them, so the specifier is mapped (e.g. `import 'octane'`) and the file stays a module. This preserves TypeScript's augmentation semantics.
1 parent 77138ec commit 9435062

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

‎packages/codemod/src/transform.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,10 @@ class ModuleTransform {
11171117
);
11181118
}
11191119
if (module in REACT_MODULES) {
1120+
if (!node.importClause) {
1121+
this.count(RULE.import);
1122+
return this.replaceSpecifier(node, node.moduleSpecifier, REACT_MODULES[module]);
1123+
}
11201124
this.importSlots.push(node);
11211125
return `\u0000IMPORT${this.importSlots.length - 1}\u0000`;
11221126
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ describe('runCodemod', () => {
183183
"import type { HTMLAttributes } from 'octane';\ndeclare module 'octane/jsx-runtime' {\n\tnamespace JSX {\n\t\tinterface IntrinsicElements { 'x-clock': HTMLAttributes<HTMLElement> }\n\t}\n}\n",
184184
);
185185
const css = result.files.find((f) => f.file === 'src/css.d.ts');
186+
expect(code('src/css.d.ts')).toContain("import 'octane'");
186187
expect(css?.notes.map((note) => note.rule)).toEqual(['unsupported-api']);
187188
expect(css?.diagnostics).toEqual([]);
188189
expect(code('src/Chart.tsx')).toContain("from '@octanejs/visx/hierarchy'");

0 commit comments

Comments
 (0)