@octanejs/codemod 0.2.0 → 0.2.2
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 +140 -12
- package/dist/bin.js +2 -2
- package/dist/effect-receivers.d.ts +14 -2
- package/dist/effect-receivers.js +171 -22
- package/dist/expression-wrappers.d.ts +2 -0
- package/dist/expression-wrappers.js +19 -0
- package/dist/forwarded-props.d.ts +4 -1
- package/dist/forwarded-props.js +18 -3
- package/dist/linked-state.d.ts +1 -1
- package/dist/linked-state.js +7 -3
- package/dist/local-react-hook.d.ts +13 -6
- package/dist/local-react-hook.js +316 -53
- package/dist/local-uses.d.ts +10 -1
- package/dist/local-uses.js +87 -5
- package/dist/mappings.d.ts +7 -1
- package/dist/mappings.js +21 -3
- package/dist/path-svg.d.ts +68 -4
- package/dist/path-svg.js +162 -56
- package/dist/project.js +1589 -147
- package/dist/react-intl.d.ts +1 -1
- package/dist/react-intl.js +43 -6
- package/dist/react-island-sources.d.ts +53 -0
- package/dist/react-island-sources.js +979 -0
- package/dist/react-renderer.d.ts +2 -0
- package/dist/react-renderer.js +94 -0
- package/dist/scoped-store-contract.d.ts +9 -2
- package/dist/scoped-store-contract.js +209 -33
- package/dist/store-hook-uses.d.ts +7 -5
- package/dist/store-hook-uses.js +218 -62
- package/dist/store-inputs.d.ts +9 -2
- package/dist/store-inputs.js +213 -16
- package/dist/strong.d.ts +2 -2
- package/dist/strong.js +72 -18
- package/dist/transform.d.ts +14 -0
- package/dist/transform.js +2856 -292
- package/dist/types.d.ts +40 -10
- package/package.json +4 -3
- package/src/path-svg.ts +183 -62
- package/templates/octane-path-svg.ts +5 -1
- package/templates/react-intl.tsrx +64 -11
package/dist/store-inputs.js
CHANGED
|
@@ -2,13 +2,15 @@ import fs from 'node:fs';
|
|
|
2
2
|
import { createRequire, isBuiltin } from 'node:module';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import ts from 'typescript';
|
|
5
|
-
import { skipWrappers } from './expression-wrappers.js';
|
|
6
|
-
import {
|
|
5
|
+
import { isWrapper, skipWrappers } from './expression-wrappers.js';
|
|
6
|
+
import { renderUses } from './forwarded-props.js';
|
|
7
|
+
import { hasLocalMemberWrite, localUses, transportedValue } from './local-uses.js';
|
|
7
8
|
import { bind } from './transform.js';
|
|
8
9
|
import { hasRuntimeDependency } from './scoped-store-contract.js';
|
|
10
|
+
const emptyValue = { jsx: false, imports: [] };
|
|
11
|
+
const MAX_EXPORT_SELECTIONS = 64;
|
|
9
12
|
const sourceExtensions = ['.ts', '.tsx', '.js', '.jsx', '.mts', '.mjs', '.cts', '.cjs'];
|
|
10
|
-
const resourceHints = new Set([
|
|
11
|
-
'default',
|
|
13
|
+
export const resourceHints = new Set([
|
|
12
14
|
'preconnect',
|
|
13
15
|
'prefetchDNS',
|
|
14
16
|
'preinit',
|
|
@@ -25,12 +27,14 @@ function importsResourceHint(statement) {
|
|
|
25
27
|
return true;
|
|
26
28
|
if (!ts.isNamedImports(bindings) && !ts.isNamedExports(bindings))
|
|
27
29
|
return true;
|
|
28
|
-
return bindings.elements.some((item) => !item.isTypeOnly &&
|
|
30
|
+
return bindings.elements.some((item) => !item.isTypeOnly &&
|
|
31
|
+
((item.propertyName ?? item.name).text === 'default' ||
|
|
32
|
+
resourceHints.has((item.propertyName ?? item.name).text)));
|
|
29
33
|
}
|
|
30
34
|
function dataInput(expr, locals) {
|
|
31
35
|
const value = skipWrappers(expr);
|
|
32
36
|
if (ts.isIdentifier(value)) {
|
|
33
|
-
const input = locals
|
|
37
|
+
const input = locals?.get(value.text);
|
|
34
38
|
return !!input && 'literal' in input;
|
|
35
39
|
}
|
|
36
40
|
if (ts.isArrayLiteralExpression(value))
|
|
@@ -55,12 +59,64 @@ function inertLiteral(value) {
|
|
|
55
59
|
value.kind === ts.SyntaxKind.FalseKeyword ||
|
|
56
60
|
value.kind === ts.SyntaxKind.NullKeyword);
|
|
57
61
|
}
|
|
62
|
+
/** Read const data exported by an original module without executable dependencies. */
|
|
63
|
+
export function retainedLiteralExports(file) {
|
|
64
|
+
const source = ts.createSourceFile(file, fs.readFileSync(file, 'utf8'), ts.ScriptTarget.Latest);
|
|
65
|
+
const exported = new Set();
|
|
66
|
+
for (const statement of source.statements) {
|
|
67
|
+
if (ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement))
|
|
68
|
+
continue;
|
|
69
|
+
if (ts.isImportDeclaration(statement) &&
|
|
70
|
+
!statement.attributes &&
|
|
71
|
+
statement.importClause?.isTypeOnly)
|
|
72
|
+
continue;
|
|
73
|
+
if (ts.isEnumDeclaration(statement) &&
|
|
74
|
+
statement.members.every((member) => !ts.isComputedPropertyName(member.name) &&
|
|
75
|
+
member.initializer &&
|
|
76
|
+
inertLiteral(skipWrappers(member.initializer))))
|
|
77
|
+
continue;
|
|
78
|
+
if (!ts.isVariableStatement(statement) ||
|
|
79
|
+
(statement.declarationList.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const)
|
|
80
|
+
return null;
|
|
81
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
82
|
+
if (!ts.isIdentifier(declaration.name) ||
|
|
83
|
+
!declaration.initializer ||
|
|
84
|
+
!dataInput(declaration.initializer))
|
|
85
|
+
return null;
|
|
86
|
+
if (ts.isObjectLiteralExpression(skipWrappers(declaration.initializer)) &&
|
|
87
|
+
statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword))
|
|
88
|
+
exported.add(declaration.name.text);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return exported;
|
|
92
|
+
}
|
|
58
93
|
/** Resolve application source used by a workspace store consumer. */
|
|
59
|
-
export function storeSources(root, config, unchanged) {
|
|
94
|
+
export function storeSources(root, config, unchanged, changesRenderer = () => false) {
|
|
60
95
|
const options = { moduleResolution: ts.ModuleResolutionKind.Bundler, ...config };
|
|
61
96
|
const cache = ts.createModuleResolutionCache(root, (file) => file, options);
|
|
62
97
|
const modules = new Map();
|
|
63
98
|
const project = fs.realpathSync(root);
|
|
99
|
+
const bindings = new Map();
|
|
100
|
+
function bindingFor(file) {
|
|
101
|
+
let binding = bindings.get(file);
|
|
102
|
+
if (!binding) {
|
|
103
|
+
binding = bind(file, fs.readFileSync(file, 'utf8'));
|
|
104
|
+
bindings.set(file, binding);
|
|
105
|
+
}
|
|
106
|
+
return binding;
|
|
107
|
+
}
|
|
108
|
+
function exportsConstObject(file, name) {
|
|
109
|
+
const { sf, checker } = bindingFor(file);
|
|
110
|
+
const symbol = checker.getSymbolAtLocation(sf)?.exports?.get(ts.escapeLeadingUnderscores(name));
|
|
111
|
+
const declaration = symbol?.valueDeclaration;
|
|
112
|
+
if (!declaration ||
|
|
113
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
114
|
+
!declaration.initializer ||
|
|
115
|
+
(declaration.parent.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
116
|
+
ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Ambient)
|
|
117
|
+
return false;
|
|
118
|
+
return ts.isObjectLiteralExpression(skipWrappers(declaration.initializer));
|
|
119
|
+
}
|
|
64
120
|
function inputs(file) {
|
|
65
121
|
const saved = modules.get(file);
|
|
66
122
|
if (saved)
|
|
@@ -68,7 +124,31 @@ export function storeSources(root, config, unchanged) {
|
|
|
68
124
|
const exported = new Map();
|
|
69
125
|
const locals = new Map();
|
|
70
126
|
const localFunctions = new Set();
|
|
71
|
-
const { sf, checker } =
|
|
127
|
+
const { sf, checker } = bindingFor(file);
|
|
128
|
+
let uses;
|
|
129
|
+
function handleEscapes(name) {
|
|
130
|
+
const refs = (uses ??= localUses(sf, checker));
|
|
131
|
+
const pending = [name];
|
|
132
|
+
while (pending.length) {
|
|
133
|
+
for (const ref of renderUses(refs(pending.pop()))) {
|
|
134
|
+
let value = ref;
|
|
135
|
+
while (isWrapper(value.parent))
|
|
136
|
+
value = value.parent;
|
|
137
|
+
const parent = value.parent;
|
|
138
|
+
if (ts.isExportSpecifier(parent) || ts.isExportAssignment(parent))
|
|
139
|
+
continue;
|
|
140
|
+
if (!ts.isVariableDeclaration(parent) ||
|
|
141
|
+
parent.initializer !== value ||
|
|
142
|
+
!ts.isIdentifier(parent.name) ||
|
|
143
|
+
!ts.isVariableDeclarationList(parent.parent) ||
|
|
144
|
+
(parent.parent.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
145
|
+
!ts.isSourceFile(parent.parent.parent.parent))
|
|
146
|
+
return true;
|
|
147
|
+
pending.push(parent.name);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
72
152
|
for (const s of sf.statements) {
|
|
73
153
|
if (ts.isFunctionDeclaration(s) && s.name && s.body)
|
|
74
154
|
localFunctions.add(s.name.text);
|
|
@@ -123,7 +203,8 @@ export function storeSources(root, config, unchanged) {
|
|
|
123
203
|
input = {
|
|
124
204
|
factory: callee.reexport,
|
|
125
205
|
direct: !callee.opaqueOnly && call === d.initializer && target === call.expression,
|
|
126
|
-
|
|
206
|
+
// A local use can expose a literal-initialized handle to a callback.
|
|
207
|
+
data: call.arguments.every((arg) => dataInput(arg, locals)) && !handleEscapes(d.name),
|
|
127
208
|
};
|
|
128
209
|
}
|
|
129
210
|
else if (localFunctions.has(target.text))
|
|
@@ -166,8 +247,10 @@ export function storeSources(root, config, unchanged) {
|
|
|
166
247
|
return exported;
|
|
167
248
|
}
|
|
168
249
|
function typedFile(from, specifier) {
|
|
169
|
-
|
|
250
|
+
const file = ts.resolveModuleName(specifier, from, options, ts.sys, cache).resolvedModule
|
|
170
251
|
?.resolvedFileName;
|
|
252
|
+
// TypeScript uses slash-delimited paths even on Windows.
|
|
253
|
+
return file && path.normalize(file);
|
|
171
254
|
}
|
|
172
255
|
function inProject(file, directory) {
|
|
173
256
|
return (file.startsWith(directory + path.sep) && !file.includes(path.sep + 'node_modules' + path.sep));
|
|
@@ -181,17 +264,21 @@ export function storeSources(root, config, unchanged) {
|
|
|
181
264
|
return undefined;
|
|
182
265
|
}
|
|
183
266
|
}
|
|
184
|
-
function localFile(from, specifier) {
|
|
267
|
+
function localFile(from, specifier, allowTsx = false) {
|
|
185
268
|
const file = typedFile(from, specifier);
|
|
186
|
-
if (!file ||
|
|
269
|
+
if (!file || file.endsWith('.d.ts') || !inProject(file, root))
|
|
270
|
+
return null;
|
|
271
|
+
const extension = path.extname(file);
|
|
272
|
+
if (extension !== '.ts' && !(allowTsx && extension === '.tsx'))
|
|
187
273
|
return null;
|
|
188
274
|
const physical = fs.realpathSync(file);
|
|
189
275
|
if (!inProject(physical, project))
|
|
190
276
|
return null;
|
|
191
277
|
// A JS sibling or directory can implement a different export even for a tsconfig alias.
|
|
192
|
-
if (!specifier.endsWith(
|
|
193
|
-
const stem = file.slice(0, -
|
|
194
|
-
if (
|
|
278
|
+
if (!specifier.endsWith(extension)) {
|
|
279
|
+
const stem = file.slice(0, -extension.length);
|
|
280
|
+
if (sourceExtensions.some((ext) => ext !== extension && fs.existsSync(stem + ext)) ||
|
|
281
|
+
fs.existsSync(stem))
|
|
195
282
|
return null;
|
|
196
283
|
}
|
|
197
284
|
const runtime = runtimeFile(from, specifier);
|
|
@@ -266,6 +353,116 @@ export function storeSources(root, config, unchanged) {
|
|
|
266
353
|
resourceFiles.set(current, false);
|
|
267
354
|
return false;
|
|
268
355
|
}
|
|
356
|
+
function readExport(file, name, selection) {
|
|
357
|
+
const { sf, checker } = bindingFor(file);
|
|
358
|
+
const exported = checker.getSymbolAtLocation(sf)?.exports;
|
|
359
|
+
const symbol = exported?.get(ts.escapeLeadingUnderscores(name));
|
|
360
|
+
const declaration = symbol?.valueDeclaration ??
|
|
361
|
+
symbol?.declarations?.find((node) => (ts.isExportSpecifier(node) && !node.isTypeOnly && !node.parent.parent.isTypeOnly) ||
|
|
362
|
+
(ts.isNamespaceExport(node) && !node.parent.isTypeOnly) ||
|
|
363
|
+
(ts.isExportAssignment(node) && !node.isExportEquals));
|
|
364
|
+
// A runtime export shadows stars. Type declarations are erased and default never travels through a star.
|
|
365
|
+
if (!declaration || ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Ambient) {
|
|
366
|
+
if (name === 'default')
|
|
367
|
+
return emptyValue;
|
|
368
|
+
const imports = [];
|
|
369
|
+
for (const star of exported?.get(ts.InternalSymbolName.ExportStar)?.declarations ?? []) {
|
|
370
|
+
if (ts.isExportDeclaration(star) &&
|
|
371
|
+
!star.isTypeOnly &&
|
|
372
|
+
!star.attributes &&
|
|
373
|
+
star.moduleSpecifier &&
|
|
374
|
+
ts.isStringLiteral(star.moduleSpecifier))
|
|
375
|
+
imports.push({ from: file, module: star.moduleSpecifier.text, name, selection });
|
|
376
|
+
}
|
|
377
|
+
return { jsx: false, imports };
|
|
378
|
+
}
|
|
379
|
+
if (ts.isNamespaceExport(declaration)) {
|
|
380
|
+
const statement = declaration.parent;
|
|
381
|
+
const [member, ...rest] = selection;
|
|
382
|
+
return member !== undefined &&
|
|
383
|
+
!statement.attributes &&
|
|
384
|
+
statement.moduleSpecifier &&
|
|
385
|
+
ts.isStringLiteral(statement.moduleSpecifier)
|
|
386
|
+
? {
|
|
387
|
+
jsx: false,
|
|
388
|
+
imports: [
|
|
389
|
+
{ from: file, module: statement.moduleSpecifier.text, name: member, selection: rest },
|
|
390
|
+
],
|
|
391
|
+
}
|
|
392
|
+
: emptyValue;
|
|
393
|
+
}
|
|
394
|
+
let value;
|
|
395
|
+
if (ts.isExportSpecifier(declaration)) {
|
|
396
|
+
const statement = declaration.parent.parent;
|
|
397
|
+
if (declaration.isTypeOnly || statement.isTypeOnly || statement.attributes)
|
|
398
|
+
return emptyValue;
|
|
399
|
+
const exported = declaration.propertyName ?? declaration.name;
|
|
400
|
+
if (statement.moduleSpecifier) {
|
|
401
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier))
|
|
402
|
+
return emptyValue;
|
|
403
|
+
return {
|
|
404
|
+
jsx: false,
|
|
405
|
+
imports: [
|
|
406
|
+
{ from: file, module: statement.moduleSpecifier.text, name: exported.text, selection },
|
|
407
|
+
],
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
value = exported;
|
|
411
|
+
}
|
|
412
|
+
else if (ts.isExportAssignment(declaration) && !declaration.isExportEquals) {
|
|
413
|
+
value = declaration.expression;
|
|
414
|
+
}
|
|
415
|
+
else if ((ts.isVariableDeclaration(declaration) || ts.isBindingElement(declaration)) &&
|
|
416
|
+
ts.isIdentifier(declaration.name)) {
|
|
417
|
+
if (ts.isVariableDeclaration(declaration) &&
|
|
418
|
+
declaration.initializer &&
|
|
419
|
+
ts.isVariableDeclarationList(declaration.parent) &&
|
|
420
|
+
declaration.parent.flags & ts.NodeFlags.Const &&
|
|
421
|
+
inertLiteral(skipWrappers(declaration.initializer)))
|
|
422
|
+
return emptyValue;
|
|
423
|
+
value = declaration.name;
|
|
424
|
+
}
|
|
425
|
+
else
|
|
426
|
+
return emptyValue;
|
|
427
|
+
const imports = [];
|
|
428
|
+
const jsx = transportedValue(value, checker, selection, (module, name, selected) => {
|
|
429
|
+
imports.push({ from: file, module, name, selection: selected });
|
|
430
|
+
return false;
|
|
431
|
+
}, () => changesRenderer(file));
|
|
432
|
+
return { jsx, imports };
|
|
433
|
+
}
|
|
434
|
+
const queriedExports = new Map();
|
|
435
|
+
function changedJsxExport(from, module, name, selection) {
|
|
436
|
+
const pending = [{ from, module, name, selection }];
|
|
437
|
+
const seen = new Set();
|
|
438
|
+
while (pending.length) {
|
|
439
|
+
const { from, module, name, selection } = pending.pop();
|
|
440
|
+
for (const file of executableFiles(from, module)) {
|
|
441
|
+
const key = file + '\0' + name;
|
|
442
|
+
let paths = queriedExports.get(key);
|
|
443
|
+
if (!paths)
|
|
444
|
+
queriedExports.set(key, (paths = new Map()));
|
|
445
|
+
const requested = JSON.stringify(selection);
|
|
446
|
+
const join = selection.length > MAX_EXPORT_SELECTIONS ||
|
|
447
|
+
(!paths.has(requested) && paths.size >= MAX_EXPORT_SELECTIONS);
|
|
448
|
+
const path = join ? [] : selection;
|
|
449
|
+
const shape = join ? '[]' : requested;
|
|
450
|
+
const query = key + '\0' + shape;
|
|
451
|
+
if (seen.has(query))
|
|
452
|
+
continue;
|
|
453
|
+
seen.add(query);
|
|
454
|
+
let result = paths.get(shape);
|
|
455
|
+
if (!result) {
|
|
456
|
+
result = readExport(file, name, path);
|
|
457
|
+
paths.set(shape, result);
|
|
458
|
+
}
|
|
459
|
+
if (result.jsx)
|
|
460
|
+
return true;
|
|
461
|
+
pending.push(...result.imports);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return false;
|
|
465
|
+
}
|
|
269
466
|
// Follow named re-exports to a direct factory or an opaque local initializer.
|
|
270
467
|
function inputFactory(from, specifier, name, shared = () => false) {
|
|
271
468
|
let ref = { module: specifier, name, from };
|
|
@@ -387,5 +584,5 @@ export function storeSources(root, config, unchanged) {
|
|
|
387
584
|
}
|
|
388
585
|
return exported.has(name);
|
|
389
586
|
}
|
|
390
|
-
return { inputFactory, helperFunction, localFile };
|
|
587
|
+
return { inputFactory, changedJsxExport, helperFunction, localFile, exportsConstObject };
|
|
391
588
|
}
|
package/dist/strong.d.ts
CHANGED
|
@@ -8,8 +8,8 @@ import type { Compiler, StrongResult } from './types.js';
|
|
|
8
8
|
*
|
|
9
9
|
* - an explicit dependency list the compiler reports as equivalent to the one
|
|
10
10
|
* it infers is dropped;
|
|
11
|
-
* - state initialized from a prop is captured
|
|
12
|
-
*
|
|
11
|
+
* - state initialized from a prop is captured once, with the argument
|
|
12
|
+
* evaluated on every render and a callable initializer run at mount.
|
|
13
13
|
*
|
|
14
14
|
* Strong errors that remain mean the module needs a person. With `force`
|
|
15
15
|
* unset the module falls back to compatibility mode so the migrated app still
|
package/dist/strong.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { applyFixes, pruneImports } from '@octanejs/cli/fix';
|
|
2
|
+
import { parseSync } from '@tsrx/oxc/parser';
|
|
2
3
|
import { finding, thrown } from './compiler.js';
|
|
3
4
|
/**
|
|
4
5
|
* The source without the directive the codemod added: its own line after any
|
|
@@ -19,6 +20,7 @@ function withoutDirective(code) {
|
|
|
19
20
|
};
|
|
20
21
|
}
|
|
21
22
|
const MAX_ROUNDS = 6;
|
|
23
|
+
const UNLINKED_PROP_STATE = 'OCTANE_STRONG_UNLINKED_PROP_STATE';
|
|
22
24
|
/**
|
|
23
25
|
* Compile a module the codemod emitted in Strong mode and repair what the
|
|
24
26
|
* compiler reports, until it compiles clean or nothing more can be fixed.
|
|
@@ -28,8 +30,8 @@ const MAX_ROUNDS = 6;
|
|
|
28
30
|
*
|
|
29
31
|
* - an explicit dependency list the compiler reports as equivalent to the one
|
|
30
32
|
* it infers is dropped;
|
|
31
|
-
* - state initialized from a prop is captured
|
|
32
|
-
*
|
|
33
|
+
* - state initialized from a prop is captured once, with the argument
|
|
34
|
+
* evaluated on every render and a callable initializer run at mount.
|
|
33
35
|
*
|
|
34
36
|
* Strong errors that remain mean the module needs a person. With `force`
|
|
35
37
|
* unset the module falls back to compatibility mode so the migrated app still
|
|
@@ -39,7 +41,7 @@ export function strengthen(compiler, code, filename, { force = false, withEffect
|
|
|
39
41
|
const original = code;
|
|
40
42
|
const fixed = [];
|
|
41
43
|
let diagnostics = [];
|
|
42
|
-
for (let round = 0; round
|
|
44
|
+
for (let round = 0; round <= MAX_ROUNDS; round++) {
|
|
43
45
|
const result = compiler.collectDiagnostics(code, filename, { strong: true });
|
|
44
46
|
if (result.error != null) {
|
|
45
47
|
// A module that compatibility mode cannot compile either has no
|
|
@@ -48,7 +50,11 @@ export function strengthen(compiler, code, filename, { force = false, withEffect
|
|
|
48
50
|
return fallback(code, compat.error == null ? [thrown(result.error)] : [], fixed, force);
|
|
49
51
|
}
|
|
50
52
|
diagnostics = result.diagnostics;
|
|
51
|
-
|
|
53
|
+
// The last pass checks the final edit without attempting another repair.
|
|
54
|
+
if (round === MAX_ROUNDS)
|
|
55
|
+
break;
|
|
56
|
+
const stateArguments = propStateArguments(code, filename, diagnostics);
|
|
57
|
+
const fixes = diagnostics.flatMap((diagnostic) => fixesFor(code, diagnostic, stateArguments));
|
|
52
58
|
if (fixes.length === 0)
|
|
53
59
|
break;
|
|
54
60
|
const { text, applied } = applyFixes(code, fixes);
|
|
@@ -99,7 +105,7 @@ function fallback(code, blockers, fixed, force) {
|
|
|
99
105
|
return { code: compat.code, strong: false, fixed, blockers: kept };
|
|
100
106
|
}
|
|
101
107
|
/** The edits that resolve one diagnostic, as `applyFixes` takes them. */
|
|
102
|
-
function fixesFor(code, diagnostic) {
|
|
108
|
+
function fixesFor(code, diagnostic, stateArguments) {
|
|
103
109
|
const fixes = [];
|
|
104
110
|
for (const suggestion of diagnostic.suggestions ?? []) {
|
|
105
111
|
if ('edits' in suggestion && suggestion.edits?.length) {
|
|
@@ -120,23 +126,71 @@ function fixesFor(code, diagnostic) {
|
|
|
120
126
|
fixes.push({ code: diagnostic.code, edits: [{ start: comma.index, end, text: '' }] });
|
|
121
127
|
}
|
|
122
128
|
}
|
|
123
|
-
if (diagnostic.code ===
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const body = /^[\w$.]+$/.test(value) ? value : `(${value})`;
|
|
130
|
-
fixes.push({ code: diagnostic.code, edits: [{ start, end, text: `() => ${body}` }] });
|
|
131
|
-
}
|
|
132
|
-
else if (before === ',') {
|
|
133
|
-
// useReducer(reducer, prop): an explicit initializer is the
|
|
134
|
-
// deliberate capture.
|
|
129
|
+
if (diagnostic.code === UNLINKED_PROP_STATE) {
|
|
130
|
+
const match = stateArguments?.get(`${start}:${end}`);
|
|
131
|
+
if (!match)
|
|
132
|
+
return fixes;
|
|
133
|
+
const { start: from, end: to } = match.argument;
|
|
134
|
+
if (match.reducer) {
|
|
135
135
|
fixes.push({
|
|
136
136
|
code: diagnostic.code,
|
|
137
|
-
edits: [{ start:
|
|
137
|
+
edits: [{ start: to, end: to, text: ', (initial) => initial' }],
|
|
138
138
|
});
|
|
139
139
|
}
|
|
140
|
+
else {
|
|
141
|
+
// Preserve eager evaluation and keep the original type for hook overload inference.
|
|
142
|
+
const capture = "(<Input,>(initial: Input): Input => typeof initial === 'function' ? initial : (() => initial) as Input)";
|
|
143
|
+
const text = `${capture}(${code.slice(from, to)})`;
|
|
144
|
+
fixes.push({ code: diagnostic.code, edits: [{ start: from, end: to, text }] });
|
|
145
|
+
}
|
|
140
146
|
}
|
|
141
147
|
return fixes;
|
|
142
148
|
}
|
|
149
|
+
function propStateArguments(code, filename, diagnostics) {
|
|
150
|
+
const diagnosticsForState = diagnostics.filter((diagnostic) => diagnostic.code === UNLINKED_PROP_STATE);
|
|
151
|
+
if (diagnosticsForState.length === 0)
|
|
152
|
+
return;
|
|
153
|
+
const ranges = new Set(diagnosticsForState.map(({ start, end }) => `${start?.offset}:${end?.offset}`));
|
|
154
|
+
const { program, errors } = parseSync(filename, code, { preserveParens: true });
|
|
155
|
+
if (errors.length > 0)
|
|
156
|
+
return;
|
|
157
|
+
const matches = new Map();
|
|
158
|
+
function visit(node) {
|
|
159
|
+
if (Array.isArray(node)) {
|
|
160
|
+
for (const child of node)
|
|
161
|
+
visit(child);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (!node || typeof node !== 'object' || !('type' in node))
|
|
165
|
+
return;
|
|
166
|
+
if (node.type === 'CallExpression') {
|
|
167
|
+
const { arguments: args } = node;
|
|
168
|
+
const argument = args.at(-1);
|
|
169
|
+
if (argument && args.length <= 2 && args.every(({ type }) => type !== 'SpreadElement')) {
|
|
170
|
+
// The diagnostic points to the expression inside any wrappers.
|
|
171
|
+
const { start, end } = unwrapArgument(argument);
|
|
172
|
+
const range = `${start}:${end}`;
|
|
173
|
+
if (ranges.has(range))
|
|
174
|
+
matches.set(range, { argument, reducer: args.length === 2 });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
for (const child of Object.values(node))
|
|
178
|
+
visit(child);
|
|
179
|
+
}
|
|
180
|
+
visit(program);
|
|
181
|
+
return matches;
|
|
182
|
+
}
|
|
183
|
+
function unwrapArgument(argument) {
|
|
184
|
+
switch (argument.type) {
|
|
185
|
+
case 'ChainExpression':
|
|
186
|
+
case 'ParenthesizedExpression':
|
|
187
|
+
case 'TSAsExpression':
|
|
188
|
+
case 'TSInstantiationExpression':
|
|
189
|
+
case 'TSNonNullExpression':
|
|
190
|
+
case 'TSSatisfiesExpression':
|
|
191
|
+
case 'TSTypeAssertion':
|
|
192
|
+
return unwrapArgument(argument.expression);
|
|
193
|
+
default:
|
|
194
|
+
return argument;
|
|
195
|
+
}
|
|
196
|
+
}
|
package/dist/transform.d.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
import type { TransformOptions, TransformResult } from './types.js';
|
|
3
|
+
export declare const MAP_PARAMETER_TDZ = "map-parameter-tdz";
|
|
4
|
+
export declare const MAP_PARAMETER_WRITE = "map-parameter-write";
|
|
5
|
+
export declare const MAP_CALLBACK_SCOPE = "map-callback-scope";
|
|
6
|
+
export declare const CONTEXT_ALIAS = "context-path-alias";
|
|
7
|
+
export declare const CONTEXT_PROVIDER_GETTER = "context-provider-getter";
|
|
8
|
+
export declare const ELEMENT_CHILD_ALIAS = "element-child-path-alias";
|
|
9
|
+
export declare const INTL_PROVIDER_DEFAULTS = "intl-provider-defaults";
|
|
10
|
+
export declare const REACT_TYPE_PROJECTION = "react-type-projection";
|
|
11
|
+
export declare const BUFFERED_SSR_OPTIONS = "buffered-ssr-options";
|
|
12
|
+
export declare const BUFFERED_SSR_REACT_COMPONENT = "buffered-ssr-react-component";
|
|
3
13
|
/**
|
|
4
14
|
* Rewrite one React module as Octane.
|
|
5
15
|
*/
|
|
@@ -19,9 +29,13 @@ export declare function bind(filename: string, source: string): {
|
|
|
19
29
|
sf: ts.SourceFile;
|
|
20
30
|
checker: ts.TypeChecker;
|
|
21
31
|
};
|
|
32
|
+
export declare function nativeReactCompat(module: string, name: string): boolean;
|
|
33
|
+
export declare function skipParens(node: ts.Expression): ts.Expression;
|
|
22
34
|
/**
|
|
23
35
|
* What a type renders as: 1 when every member is a string, 2 when every
|
|
24
36
|
* member is a string, number, or bigint and one is not a string, else 0. The
|
|
25
37
|
* compiler's typed-text proof applies the same test.
|
|
26
38
|
*/
|
|
27
39
|
export declare function textKind(type: ts.Type): 0 | 1 | 2;
|
|
40
|
+
/** JSX emitters agree on empty ASCII indentation; spaces on a single line are children. */
|
|
41
|
+
export declare function withoutJsxIndentation(children: readonly ts.JsxChild[]): ts.JsxChild[];
|