@octanejs/codemod 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -1
- package/package.json +2 -1
- package/src/project.js +68 -0
- package/src/transform.js +40 -10
package/README.md
CHANGED
|
@@ -66,6 +66,10 @@ Every module with JSX becomes `.tsrx`, entries and helpers without a component
|
|
|
66
66
|
included, so Strong mode can reach all of them; one that was JavaScript gains
|
|
67
67
|
`// @ts-nocheck`, so it stays as unchecked as it was. Every import of a renamed
|
|
68
68
|
module, dynamic imports and `index.html` entry scripts included, is updated.
|
|
69
|
+
So is each string elsewhere in the project that is exactly a renamed module's
|
|
70
|
+
path, from its own file or from the project root, such as Vite's
|
|
71
|
+
`server.warmup.clientFiles: ['./src/main.tsx']`: the `module-paths` rule.
|
|
72
|
+
Globs and paths without an extension stay as they are.
|
|
69
73
|
|
|
70
74
|
## Strong mode
|
|
71
75
|
|
|
@@ -90,7 +94,12 @@ has React and React DOM 19.2 or newer in the React 19 series, a component of tha
|
|
|
90
94
|
its own React root through `ReactCompat` if its element gives React nothing
|
|
91
95
|
Octane renders: text children and props without elements. The package's hooks
|
|
92
96
|
and contexts, and its components that take Octane children or render
|
|
93
|
-
callbacks, are reported where they are used.
|
|
97
|
+
callbacks, are reported where they are used. Each such component is wrapped
|
|
98
|
+
in a local `NoOctanePort`, which throws the reported error where the component
|
|
99
|
+
would render, so the page names the package instead of failing deep in
|
|
100
|
+
Octane's renderer. Once you have checked that nothing Octane renders reaches
|
|
101
|
+
it, such as a spread of props whose `children` no caller passes, swap the
|
|
102
|
+
wrapper for `ReactCompat`. **No Octane port** lists each such
|
|
94
103
|
package with the modules that import it, the elements hosted in islands, and
|
|
95
104
|
the uses that need a port.
|
|
96
105
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octanejs/codemod",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.22.2"
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@octanejs/cli": "0.3.3",
|
|
43
|
+
"@tsrx/oxc": "0.23.0",
|
|
43
44
|
"octane": "0.16.7",
|
|
44
45
|
"picocolors": "^1.1.1",
|
|
45
46
|
"typescript": "^5.9.3"
|
package/src/project.js
CHANGED
|
@@ -199,6 +199,37 @@ export async function runCodemod(paths, options = {}) {
|
|
|
199
199
|
});
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
+
// Configs and scripts name modules by path too, such as Vite's
|
|
203
|
+
// `server.warmup.clientFiles`, including those outside the migrated paths.
|
|
204
|
+
const byFile = new Map(results.map((result) => [path.join(root, result.file), result]));
|
|
205
|
+
for (const file of collect(root, (name) => SOURCE.test(name))) {
|
|
206
|
+
if (renames.has(file)) continue;
|
|
207
|
+
const result = byFile.get(file);
|
|
208
|
+
const paths = rewriteModulePaths(
|
|
209
|
+
result?.code ?? fs.readFileSync(file, 'utf8'),
|
|
210
|
+
file,
|
|
211
|
+
root,
|
|
212
|
+
renames,
|
|
213
|
+
);
|
|
214
|
+
if (!paths) continue;
|
|
215
|
+
const rules = { ...result?.rules, 'module-paths': paths.count };
|
|
216
|
+
if (result) Object.assign(result, { code: paths.code, changed: true, rules });
|
|
217
|
+
else {
|
|
218
|
+
const name = path.relative(root, file);
|
|
219
|
+
results.push({
|
|
220
|
+
file: name,
|
|
221
|
+
output: name,
|
|
222
|
+
changed: true,
|
|
223
|
+
rules,
|
|
224
|
+
notes: [],
|
|
225
|
+
strong: null,
|
|
226
|
+
strongFixes: [],
|
|
227
|
+
diagnostics: [],
|
|
228
|
+
code: paths.code,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
202
233
|
const html = rewriteEntryPages(
|
|
203
234
|
root,
|
|
204
235
|
paths.map((entry) => path.resolve(entry)),
|
|
@@ -650,6 +681,43 @@ function rewriteEntryPages(root, targets, renames, write) {
|
|
|
650
681
|
return updated;
|
|
651
682
|
}
|
|
652
683
|
|
|
684
|
+
/**
|
|
685
|
+
* Point the exact path strings in a module that keeps its name at the modules
|
|
686
|
+
* renamed to `.tsrx`. A path resolves from the module or, as Vite resolves its
|
|
687
|
+
* options, from the root; a glob or an extensionless path stays.
|
|
688
|
+
*
|
|
689
|
+
* @param {string} code
|
|
690
|
+
* @param {string} file
|
|
691
|
+
* @param {string} root
|
|
692
|
+
* @param {Map<string, string>} renames
|
|
693
|
+
* @returns {{ code: string, count: number } | null}
|
|
694
|
+
*/
|
|
695
|
+
function rewriteModulePaths(code, file, root, renames) {
|
|
696
|
+
const sf = ts.createSourceFile(file, code, ts.ScriptTarget.Latest);
|
|
697
|
+
/** @type {ts.StringLiteralLike[]} */
|
|
698
|
+
const paths = [];
|
|
699
|
+
const visit = (/** @type {ts.Node} */ node) => {
|
|
700
|
+
if (
|
|
701
|
+
ts.isStringLiteralLike(node) &&
|
|
702
|
+
SOURCE.test(node.text) &&
|
|
703
|
+
[path.dirname(file), root].some((dir) => renames.has(path.join(dir, node.text)))
|
|
704
|
+
) {
|
|
705
|
+
paths.push(node);
|
|
706
|
+
}
|
|
707
|
+
ts.forEachChild(node, visit);
|
|
708
|
+
};
|
|
709
|
+
visit(sf);
|
|
710
|
+
if (paths.length === 0) return null;
|
|
711
|
+
let next = code;
|
|
712
|
+
for (const node of paths.reverse()) {
|
|
713
|
+
// The extension ends the literal, just before its closing quote.
|
|
714
|
+
const end = node.getEnd() - 1;
|
|
715
|
+
const start = node.getStart(sf);
|
|
716
|
+
next = next.slice(0, start) + next.slice(start, end).replace(SOURCE, '.tsrx') + next.slice(end);
|
|
717
|
+
}
|
|
718
|
+
return { code: next, count: paths.length };
|
|
719
|
+
}
|
|
720
|
+
|
|
653
721
|
/**
|
|
654
722
|
* The HTML pages a Vite or CRA app keeps beside its sources: those in the
|
|
655
723
|
* root, `public/`, `src/`, and each migrated directory.
|
package/src/transform.js
CHANGED
|
@@ -79,6 +79,8 @@ const LITERAL_NEWLINE = '\u0000NL\u0000';
|
|
|
79
79
|
|
|
80
80
|
const TEMPLATE = { template: true };
|
|
81
81
|
const PLAIN = { template: false };
|
|
82
|
+
/** The local component that wraps an element no island can host and throws its `error`. */
|
|
83
|
+
const NO_PORT = 'NoOctanePort';
|
|
82
84
|
|
|
83
85
|
/** Rule names, as reported. */
|
|
84
86
|
const RULE = {
|
|
@@ -682,6 +684,8 @@ class ModuleTransform {
|
|
|
682
684
|
this.unported = new Map();
|
|
683
685
|
/** @type {Set<ts.JsxElement | ts.JsxSelfClosingElement>} Elements to host in a `ReactCompat` island. */
|
|
684
686
|
this.islands = new Set();
|
|
687
|
+
/** @type {Map<ts.Node, string>} Elements no island can host, with the error each throws where it renders. */
|
|
688
|
+
this.unrenderable = new Map();
|
|
685
689
|
/** @type {ts.ImportDeclaration[]} Placeholder index -> import declaration. */
|
|
686
690
|
this.importSlots = [];
|
|
687
691
|
this.hasJsx = false;
|
|
@@ -698,6 +702,12 @@ class ModuleTransform {
|
|
|
698
702
|
code = this.finishImports(code);
|
|
699
703
|
code = code.replace(/^[ \t]*\u0000DROP\u0000[ \t]*\r?\n/gm, '').replaceAll(DROP, '');
|
|
700
704
|
code = code.replaceAll(LITERAL_NEWLINE, '\n');
|
|
705
|
+
if (this.unrenderable.size > 0) {
|
|
706
|
+
const [props, result] = this.isTypeScript()
|
|
707
|
+
? [': { error: string; children?: unknown }', ': never']
|
|
708
|
+
: ['', ''];
|
|
709
|
+
code = `${code.trimEnd()}\n\nfunction ${NO_PORT}({ error }${props})${result} {\n${this.indentUnit}throw new Error(error);\n}\n`;
|
|
710
|
+
}
|
|
701
711
|
const tsrx =
|
|
702
712
|
this.template || (this.target === 'tsrx' && (this.hasJsx || /\.[jt]sx$/.test(this.filename)));
|
|
703
713
|
if (tsrx) code = code.replace(/^\/\*\*\s*@jsxImportSource\s+\S+\s*\*\/[ \t]*\r?\n/m, '');
|
|
@@ -899,7 +909,9 @@ class ModuleTransform {
|
|
|
899
909
|
const parent = member.parent;
|
|
900
910
|
const block = (/** @type {string} */ message) => {
|
|
901
911
|
port.blocked++;
|
|
902
|
-
|
|
912
|
+
const error = `${message} \`${port.name}\` has no Octane port.`;
|
|
913
|
+
this.note(RULE.bindings, 'error', member, error);
|
|
914
|
+
return error;
|
|
903
915
|
};
|
|
904
916
|
if (
|
|
905
917
|
(ts.isJsxOpeningElement(parent) || ts.isJsxSelfClosingElement(parent)) &&
|
|
@@ -907,17 +919,18 @@ class ModuleTransform {
|
|
|
907
919
|
) {
|
|
908
920
|
const element = ts.isJsxOpeningElement(parent) ? parent.parent : parent;
|
|
909
921
|
if (this.inIsland(element)) return;
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
`\`<${text}>\` can render in a \`ReactCompat\` island only with React and React DOM 19.2 or newer in the React 19 series.`,
|
|
917
|
-
);
|
|
918
|
-
} else {
|
|
922
|
+
const reason = !this.hostsInReact(element)
|
|
923
|
+
? `\`<${text}>\` takes children or props that Octane renders, which cannot cross into a React island.`
|
|
924
|
+
: this.options.reactCompat
|
|
925
|
+
? null
|
|
926
|
+
: `\`<${text}>\` can render in a \`ReactCompat\` island only with React and React DOM 19.2 or newer in the React 19 series.`;
|
|
927
|
+
if (reason === null) {
|
|
919
928
|
port.islands++;
|
|
920
929
|
this.islands.add(element);
|
|
930
|
+
} else {
|
|
931
|
+
// Octane fails on a React component deep in its renderer, naming
|
|
932
|
+
// neither it nor its package, so it throws this error there instead.
|
|
933
|
+
this.unrenderable.set(element, block(reason));
|
|
921
934
|
}
|
|
922
935
|
} else if (ts.isCallExpression(parent) && parent.expression === member) {
|
|
923
936
|
if (/^use[A-Z0-9]/.test(name))
|
|
@@ -1553,8 +1566,25 @@ class ModuleTransform {
|
|
|
1553
1566
|
const island = this.require('octane/react', 'ReactCompat', false);
|
|
1554
1567
|
return `<${island}>${this.span(node, node.getStart(this.sf), node.end, PLAIN)}</${island}>`;
|
|
1555
1568
|
}
|
|
1569
|
+
// An earlier run's wrapper goes: each run decides again whether its
|
|
1570
|
+
// element throws or renders in an island.
|
|
1571
|
+
if (ts.isJsxElement(node) && node.openingElement.tagName.getText(this.sf) === NO_PORT) {
|
|
1572
|
+
const element = node.children.find(
|
|
1573
|
+
(child) => ts.isJsxElement(child) || ts.isJsxSelfClosingElement(child),
|
|
1574
|
+
);
|
|
1575
|
+
if (element) return this.emit(element, ctx);
|
|
1576
|
+
}
|
|
1577
|
+
const error = this.unrenderable.get(node);
|
|
1578
|
+
if (error !== undefined) {
|
|
1579
|
+
const element =
|
|
1580
|
+
(ts.isJsxElement(node) ? this.rewriteElement(node, ctx) : null) ??
|
|
1581
|
+
this.span(node, node.getStart(this.sf), node.end, ctx);
|
|
1582
|
+
return `<${NO_PORT} error="${error}">${element}</${NO_PORT}>`;
|
|
1583
|
+
}
|
|
1556
1584
|
if (ts.isJsxElement(node)) return this.rewriteElement(node, ctx);
|
|
1557
1585
|
if (ts.isJsxExpression(node) && ctx.template) return this.rewriteHole(node);
|
|
1586
|
+
// So does its `NoOctanePort`, which `run` adds back while one still throws.
|
|
1587
|
+
if (ts.isFunctionDeclaration(node) && node.name?.text === NO_PORT) return DROP;
|
|
1558
1588
|
if (
|
|
1559
1589
|
ts.isFunctionDeclaration(node) ||
|
|
1560
1590
|
ts.isFunctionExpression(node) ||
|