@octanejs/codemod 0.2.2 → 0.2.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 -0
- package/dist/forwarded-props.d.ts +1 -0
- package/dist/forwarded-props.js +9 -9
- package/dist/local-react-hook.d.ts +1 -1
- package/dist/local-react-hook.js +7 -6
- package/dist/project.js +234 -47
- package/dist/react-island-sources.d.ts +6 -0
- package/dist/react-island-sources.js +210 -22
- package/dist/transform.js +406 -77
- package/dist/types.d.ts +5 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -100,6 +100,16 @@ whose local component returns an intrinsic element. Migrate the server entry
|
|
|
100
100
|
with that component or keep the component in React. Review custom bundler
|
|
101
101
|
resolution, indirect calls, and dynamic imports.
|
|
102
102
|
|
|
103
|
+
A retained React DOM client root also keeps ownership of its elements.
|
|
104
|
+
The codemod blocks writes when a direct `createRoot(...).render(...)`, a local
|
|
105
|
+
root handle, or `hydrateRoot(...)` renders a direct local TSX import that would
|
|
106
|
+
become an Octane template. It also covers a root stored in a JavaScript private
|
|
107
|
+
field when the constructor starts by assigning `createRoot(...)`, the field is
|
|
108
|
+
used only for direct `render` and `unmount` calls, and React's named
|
|
109
|
+
`createElement` creates the element. Migrate the root and component
|
|
110
|
+
together, keep the component in React, or give React an authored
|
|
111
|
+
`OctaneCompatInline` boundary.
|
|
112
|
+
|
|
103
113
|
### Element-only children
|
|
104
114
|
|
|
105
115
|
A nested component call in a DOM template supplies a render block. When a
|
|
@@ -6,6 +6,7 @@ export declare function hasPrototypeAccess(file: ts.SourceFile): boolean;
|
|
|
6
6
|
export declare function rendersElementChildren(element: ts.JsxOpeningLikeElement): boolean;
|
|
7
7
|
export declare function renderUses(refs: ts.Identifier[]): ts.Identifier[];
|
|
8
8
|
/** A local root handle stays owned while callers only invoke its two root methods. */
|
|
9
|
+
export declare function callsRootMethod(root: ts.Expression): boolean;
|
|
9
10
|
export declare function rootHandleEscapes(refs: ts.Identifier[], inOwner?: (ref: ts.Identifier) => boolean): boolean;
|
|
10
11
|
type RenderTypes = {
|
|
11
12
|
typeOf: (node: ts.Node) => ts.Type;
|
package/dist/forwarded-props.js
CHANGED
|
@@ -94,16 +94,16 @@ export function renderUses(refs) {
|
|
|
94
94
|
});
|
|
95
95
|
}
|
|
96
96
|
/** A local root handle stays owned while callers only invoke its two root methods. */
|
|
97
|
+
export function callsRootMethod(root) {
|
|
98
|
+
const member = root.parent;
|
|
99
|
+
return (ts.isPropertyAccessExpression(member) &&
|
|
100
|
+
member.expression === root &&
|
|
101
|
+
(member.name.text === 'render' || member.name.text === 'unmount') &&
|
|
102
|
+
ts.isCallExpression(member.parent) &&
|
|
103
|
+
member.parent.expression === member);
|
|
104
|
+
}
|
|
97
105
|
export function rootHandleEscapes(refs, inOwner) {
|
|
98
|
-
return renderUses(refs).some((ref) =>
|
|
99
|
-
const member = ref.parent;
|
|
100
|
-
return (!ts.isPropertyAccessExpression(member) ||
|
|
101
|
-
member.expression !== ref ||
|
|
102
|
-
(member.name.text !== 'render' && member.name.text !== 'unmount') ||
|
|
103
|
-
!ts.isCallExpression(member.parent) ||
|
|
104
|
-
member.parent.expression !== member ||
|
|
105
|
-
inOwner?.(ref) === false);
|
|
106
|
-
});
|
|
106
|
+
return renderUses(refs).some((ref) => !callsRootMethod(ref) || inOwner?.(ref) === false);
|
|
107
107
|
}
|
|
108
108
|
/** Follow the value being rendered all the way to its containing component's return. */
|
|
109
109
|
export function nativeOutputOwner(value, slot, { typeOf, text }) {
|
|
@@ -12,7 +12,7 @@ export declare function nativeSetupHook(ref: ts.Expression, uses: ReturnType<typ
|
|
|
12
12
|
originalReact(): boolean;
|
|
13
13
|
contextSetup?: boolean;
|
|
14
14
|
privateContext?: boolean;
|
|
15
|
-
},
|
|
15
|
+
}, storeRoot?: boolean): false | "diagnostic" | "first" | "afterRefs";
|
|
16
16
|
/** Find renderer calls at the start of an unchanged export's setup. */
|
|
17
17
|
export declare function directRendererCalls(sf: ts.SourceFile, checker: ts.TypeChecker, module: 'react' | 'react-dom' | 'react-intl' | ((specifier: string, name: string) => boolean), accepts?: (name: string) => boolean, privateContextExports?: Set<string>): Map<string, string>;
|
|
18
18
|
/** Clone a stateless hook while leaving its DOM reader and subscription shared. */
|
package/dist/local-react-hook.js
CHANGED
|
@@ -119,8 +119,8 @@ function conditionalOutput(value) {
|
|
|
119
119
|
}
|
|
120
120
|
/** A direct initialization in an ordinary component with native output. */
|
|
121
121
|
export function nativeSetupHook(ref, uses, checker, diagnostic,
|
|
122
|
-
//
|
|
123
|
-
|
|
122
|
+
// These shapes are used for store-root diagnostics, without adapting a hook.
|
|
123
|
+
storeRoot = false) {
|
|
124
124
|
const call = ref.parent;
|
|
125
125
|
if (!ts.isCallExpression(call) || call.expression !== ref)
|
|
126
126
|
return false;
|
|
@@ -193,10 +193,12 @@ allowAnonymousDefault = false) {
|
|
|
193
193
|
name = fn.name;
|
|
194
194
|
}
|
|
195
195
|
else {
|
|
196
|
-
if (!diagnostic
|
|
196
|
+
if ((!diagnostic && !storeRoot) ||
|
|
197
|
+
(ts.isFunctionExpression(fn) && fn.name && renderUses(uses(fn.name)).length))
|
|
197
198
|
return false;
|
|
198
199
|
let value = fn;
|
|
199
|
-
while (
|
|
200
|
+
while (diagnostic &&
|
|
201
|
+
ts.isCallExpression(value.parent) &&
|
|
200
202
|
value.parent.arguments[0] === value &&
|
|
201
203
|
/^(?:memo|forwardRef)$/.test(diagnostic.calleeName(value.parent.expression) ?? ''))
|
|
202
204
|
value = value.parent;
|
|
@@ -213,8 +215,7 @@ allowAnonymousDefault = false) {
|
|
|
213
215
|
}
|
|
214
216
|
if ((name
|
|
215
217
|
? !/^[A-Z]/.test(name.text)
|
|
216
|
-
: !
|
|
217
|
-
!owner.modifiers?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword)) ||
|
|
218
|
+
: !storeRoot || !owner.modifiers?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword)) ||
|
|
218
219
|
!ts.isSourceFile(owner.parent) ||
|
|
219
220
|
!owner.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) ||
|
|
220
221
|
(name &&
|
package/dist/project.js
CHANGED
|
@@ -16,16 +16,55 @@ import { strengthen } from './strong.js';
|
|
|
16
16
|
import { bind, BUFFERED_SSR_OPTIONS, BUFFERED_SSR_REACT_COMPONENT, CONTEXT_ALIAS, CONTEXT_PROVIDER_GETTER, ELEMENT_CHILD_ALIAS, INTL_PROVIDER_DEFAULTS, MAP_CALLBACK_SCOPE, MAP_PARAMETER_TDZ, MAP_PARAMETER_WRITE, projectTypeAt, REACT_TYPE_PROJECTION, textKind, transformModule, } from './transform.js';
|
|
17
17
|
import { directRendererCalls, namedRuntimeImport, nativeExternalStore, } from './local-react-hook.js';
|
|
18
18
|
import { contains, forwardedProps } from './forwarded-props.js';
|
|
19
|
-
import { importsReactBridge, islandComponents, executableImports, hostedComponents, directReactExports, migratedJsxFactories, octaneRootComponents, reactClassExports, reactServerComponents, retainedReactComponentReturns, retainedReactContextExports, retainedReactElementReturns, reverseIslandExports, } from './react-island-sources.js';
|
|
19
|
+
import { importsReactBridge, islandComponents, executableImports, hostedComponents, directReactExports, migratedJsxFactories, octaneRootComponents, reactClientComponents, reactClassExports, reactServerComponents, retainedReactComponentReturns, retainedReactContextExports, retainedReactElementReturns, reverseIslandExports, } from './react-island-sources.js';
|
|
20
20
|
const SOURCE = /\.(?:[cm]?[jt]sx?)$/;
|
|
21
21
|
const ADAPTER_OCTANE_VERSION = /^0\.(?:16\.(?:[7-9]|[1-9]\d+)|1[789]\.(?:0|[1-9]\d*))$/;
|
|
22
|
-
|
|
23
|
-
const INTL_HOOK_FILES = {
|
|
22
|
+
const INTL_ENTRIES = {
|
|
24
23
|
'index.js': '8f5854dadcd055f066234e13a344bd0b102aea2d50ddfd2f0cf12e9f48116059',
|
|
25
24
|
'lib/index.js': '208130dc122cbb84cbc0d4cce54fd7f458954f45d2cd5dc505d7263686ca6093',
|
|
25
|
+
};
|
|
26
|
+
// Both React Intl 7.1.14 entries re-export a hook that calls React.useContext.
|
|
27
|
+
const INTL_HOOK_FILES = {
|
|
28
|
+
...INTL_ENTRIES,
|
|
26
29
|
'src/components/useIntl.js': '7f26c3c5aebaf874aa2ee7f56952358530c246382f7ca9e729a3ee33d196d6c8',
|
|
27
30
|
'lib/src/components/useIntl.js': '36bb0433939586c08e79a211372987b4173c73552f75d2ae66b6250bdf1f3b8c',
|
|
28
31
|
};
|
|
32
|
+
// These are the React wrappers and FormatJS paths that dispatch the two onError calls.
|
|
33
|
+
const INTL_FACTORY_FILES = {
|
|
34
|
+
...INTL_ENTRIES,
|
|
35
|
+
'src/components/createIntl.js': 'a94ae5971e93412d91f03d44fdb0412a0593f444d64c373b26bd9b76120e6448',
|
|
36
|
+
'lib/src/components/createIntl.js': '61ade432d00522e58379cc55fb8d2a7b5aa5e8a2a869638a010f0bb53e679d56',
|
|
37
|
+
};
|
|
38
|
+
const FORMATJS_FACTORY_FILES = {
|
|
39
|
+
'index.js': '5d533bc4ecb92df151a5778494dc55c529fc433f48c0e3fe0562af5847f27391',
|
|
40
|
+
'lib/index.js': 'c7a6b9d2c163232345a6b65918385c04ae8dd1a54bc8ab07d77308861c128ec3',
|
|
41
|
+
'src/create-intl.js': 'e50ab7b2f4adbcfc2639d9f8e7f3a80f365b343650c89f0a9a2572449d8850a7',
|
|
42
|
+
'lib/src/create-intl.js': 'b198b844cb3aa00893841f401dd50f9797444db417933b4b77049961a350ed3c',
|
|
43
|
+
'src/message.js': 'bffb1daf92059cfb21f8de7aa19b4e739a94854024c689ae9af00caa4af484f4',
|
|
44
|
+
'lib/src/message.js': '6a052dd6e84bb6d5dfc15537fb2145e18f4831c240204ddfdd018e72d1bc0840',
|
|
45
|
+
};
|
|
46
|
+
function stockIntlLayout(root, name, version) {
|
|
47
|
+
const installed = readInstalled(root, name);
|
|
48
|
+
if (installed?.manifest.name === name &&
|
|
49
|
+
installed.version === version &&
|
|
50
|
+
!installed.manifest.browser &&
|
|
51
|
+
!installed.manifest.exports &&
|
|
52
|
+
installed.manifest.main === 'index.js' &&
|
|
53
|
+
installed.manifest.module === 'lib/index.js')
|
|
54
|
+
return fs.realpathSync(installed.dir);
|
|
55
|
+
}
|
|
56
|
+
function matchIntlFiles(root, files) {
|
|
57
|
+
return Object.entries(files).every(([name, hash]) => {
|
|
58
|
+
const file = path.join(root, name);
|
|
59
|
+
const stat = fs.lstatSync(file, { throwIfNoEntry: false });
|
|
60
|
+
const stem = file.slice(0, -3);
|
|
61
|
+
return (stat?.isFile() &&
|
|
62
|
+
stat.size < 16_384 &&
|
|
63
|
+
fs.realpathSync(file) === file &&
|
|
64
|
+
!['', '.mjs', '.mts', '.ts', '.jsx', '.tsx', '.tsrx', '.json'].some((extension) => fs.lstatSync(stem + extension, { throwIfNoEntry: false })) &&
|
|
65
|
+
createHash('sha256').update(fs.readFileSync(file)).digest('hex') === hash);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
29
68
|
/** React packages and the Octane entry point that replaces each. */
|
|
30
69
|
const CATALOG = { ...DATA.reactPackages, ...PORTED_PACKAGES };
|
|
31
70
|
const MOTION_BINDING = '@octanejs/motion';
|
|
@@ -50,6 +89,8 @@ export async function runCodemod(paths, options = {}) {
|
|
|
50
89
|
findRoot(fs.statSync(first, { throwIfNoEntry: false })?.isFile() ? path.dirname(first) : first));
|
|
51
90
|
const known = new Set(paths.flatMap((entry) => collect(path.resolve(entry))));
|
|
52
91
|
const files = [...known];
|
|
92
|
+
const config = tsconfigOptions(root);
|
|
93
|
+
const rootProject = ts.findConfigFile(root, ts.sys.fileExists);
|
|
53
94
|
let selectedRealpaths;
|
|
54
95
|
const renames = new Map();
|
|
55
96
|
const exports = new Map();
|
|
@@ -103,7 +144,31 @@ export async function runCodemod(paths, options = {}) {
|
|
|
103
144
|
});
|
|
104
145
|
for (const file of files) {
|
|
105
146
|
const source = fs.readFileSync(file, 'utf8');
|
|
106
|
-
const result = transformModule(source, file, {
|
|
147
|
+
const result = transformModule(source, file, {
|
|
148
|
+
target,
|
|
149
|
+
mutableExports() {
|
|
150
|
+
const options = bindingOptions(file);
|
|
151
|
+
if (!options)
|
|
152
|
+
return false;
|
|
153
|
+
const kind = options.module;
|
|
154
|
+
if (kind === undefined) {
|
|
155
|
+
return (!options.noEmit &&
|
|
156
|
+
options.moduleResolution !== ts.ModuleResolutionKind.Bundler &&
|
|
157
|
+
(options.target ?? ts.ScriptTarget.ES5) < ts.ScriptTarget.ES2015);
|
|
158
|
+
}
|
|
159
|
+
if (kind === ts.ModuleKind.CommonJS ||
|
|
160
|
+
kind === ts.ModuleKind.AMD ||
|
|
161
|
+
kind === ts.ModuleKind.UMD)
|
|
162
|
+
return true;
|
|
163
|
+
if (kind !== ts.ModuleKind.Node16 &&
|
|
164
|
+
kind !== ts.ModuleKind.Node18 &&
|
|
165
|
+
kind !== ts.ModuleKind.Node20 &&
|
|
166
|
+
kind !== ts.ModuleKind.NodeNext)
|
|
167
|
+
return false;
|
|
168
|
+
return (ts.getImpliedNodeFormatForFile(file, undefined, ts.sys, options) ===
|
|
169
|
+
ts.ModuleKind.CommonJS);
|
|
170
|
+
},
|
|
171
|
+
});
|
|
107
172
|
exports.set(file, result.exports);
|
|
108
173
|
if (!result.template)
|
|
109
174
|
continue;
|
|
@@ -150,7 +215,6 @@ export async function runCodemod(paths, options = {}) {
|
|
|
150
215
|
// must not participate in the preview as if it had been migrated.
|
|
151
216
|
for (const file of conflicts.keys())
|
|
152
217
|
exports.delete(file);
|
|
153
|
-
const config = tsconfigOptions(root);
|
|
154
218
|
const strictConfig = (config?.strictNullChecks ?? config?.strict) ? config : null;
|
|
155
219
|
let program;
|
|
156
220
|
function typesOf(file) {
|
|
@@ -252,7 +316,6 @@ export async function runCodemod(paths, options = {}) {
|
|
|
252
316
|
// `ReactCompat` hosts a component from a retained package import in a React
|
|
253
317
|
// root, from 19.2 within the React 19 series it supports.
|
|
254
318
|
const reactCompat = ['react', 'react-dom'].every((name) => /^19\.(?:[2-9]|\d{2,})\./.test(readInstalled(root, name)?.version ?? ''));
|
|
255
|
-
const rootProject = ts.findConfigFile(root, ts.sys.fileExists);
|
|
256
319
|
const reactExports = new Map();
|
|
257
320
|
function originalReactExports(file) {
|
|
258
321
|
let exports = reactExports.get(file);
|
|
@@ -312,6 +375,13 @@ export async function runCodemod(paths, options = {}) {
|
|
|
312
375
|
fs.realpathSync(target.resolvedFileName).split(path.sep).includes('node_modules'));
|
|
313
376
|
}
|
|
314
377
|
let physicalRoot;
|
|
378
|
+
function isPlainFile(file) {
|
|
379
|
+
const stat = fs.lstatSync(file, { throwIfNoEntry: false });
|
|
380
|
+
return (!!stat?.isFile() &&
|
|
381
|
+
stat.nlink === 1 &&
|
|
382
|
+
fs.realpathSync(file) ===
|
|
383
|
+
path.join((physicalRoot ??= fs.realpathSync(root)), path.relative(root, file)));
|
|
384
|
+
}
|
|
315
385
|
const retainedStoreArguments = new Map();
|
|
316
386
|
const descriptorModules = new Map();
|
|
317
387
|
function retainedStoreArgument(file, source, name) {
|
|
@@ -626,6 +696,7 @@ export async function runCodemod(paths, options = {}) {
|
|
|
626
696
|
function initializerResourceHint(from, module, name, retainedOnly = false) {
|
|
627
697
|
const file = localFile(from, module);
|
|
628
698
|
if (!file ||
|
|
699
|
+
(retainedOnly && !module.endsWith('.ts') && fs.existsSync(file.slice(0, -3) + '.tsrx')) ||
|
|
629
700
|
ts.findConfigFile(path.dirname(file), ts.sys.fileExists) !== rootProject ||
|
|
630
701
|
!installedReact(from))
|
|
631
702
|
return;
|
|
@@ -646,28 +717,52 @@ export async function runCodemod(paths, options = {}) {
|
|
|
646
717
|
}
|
|
647
718
|
const localHooks = new Map();
|
|
648
719
|
const intlHookPackages = new Map();
|
|
649
|
-
|
|
720
|
+
const intlFactoryPackages = new Map();
|
|
721
|
+
function originalIntlPackage(file) {
|
|
650
722
|
const types = originalModule(file, 'react-intl');
|
|
651
723
|
if (types?.packageId?.name !== 'react-intl')
|
|
724
|
+
return;
|
|
725
|
+
try {
|
|
726
|
+
const dir = stockIntlLayout(path.dirname(file), 'react-intl', '7.1.14');
|
|
727
|
+
if (dir?.split(path.sep).includes('node_modules') &&
|
|
728
|
+
fs.realpathSync(types.resolvedFileName) === path.join(dir, 'index.d.ts') &&
|
|
729
|
+
createRequire(file).resolve('react-intl') === path.join(dir, 'index.js'))
|
|
730
|
+
return dir;
|
|
731
|
+
}
|
|
732
|
+
catch {
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
function originalIntlFactory(file) {
|
|
737
|
+
const dir = originalIntlPackage(file);
|
|
738
|
+
if (!dir)
|
|
652
739
|
return false;
|
|
653
|
-
const
|
|
654
|
-
if (
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
740
|
+
const cached = intlFactoryPackages.get(dir);
|
|
741
|
+
if (cached !== undefined)
|
|
742
|
+
return cached;
|
|
743
|
+
let verified = false;
|
|
744
|
+
try {
|
|
745
|
+
const core = '@formatjs/intl';
|
|
746
|
+
const wrappers = ['src', 'lib/src'].map((prefix) => path.join(dir, prefix, 'components/createIntl.js'));
|
|
747
|
+
const dependency = stockIntlLayout(path.dirname(wrappers[0]), core, '3.1.8');
|
|
748
|
+
verified =
|
|
749
|
+
!!dependency &&
|
|
750
|
+
wrappers.every((wrapper) => createRequire(wrapper).resolve(core) === path.join(dependency, 'index.js')) &&
|
|
751
|
+
matchIntlFiles(dir, INTL_FACTORY_FILES) &&
|
|
752
|
+
matchIntlFiles(dependency, FORMATJS_FACTORY_FILES);
|
|
753
|
+
}
|
|
754
|
+
catch {
|
|
755
|
+
// An unreadable or incomplete package does not establish the callback behavior.
|
|
756
|
+
}
|
|
757
|
+
intlFactoryPackages.set(dir, verified);
|
|
758
|
+
return verified;
|
|
759
|
+
}
|
|
760
|
+
function originalIntlHook(file) {
|
|
761
|
+
const dir = originalIntlPackage(file);
|
|
762
|
+
if (!dir || readInstalled(path.dirname(file), 'react')?.manifest.name !== 'react')
|
|
660
763
|
return false;
|
|
661
764
|
try {
|
|
662
|
-
const
|
|
663
|
-
const entry = path.join(dir, 'index.js');
|
|
664
|
-
const issuer = createRequire(file);
|
|
665
|
-
if (!dir.split(path.sep).includes('node_modules') ||
|
|
666
|
-
fs.realpathSync(types.resolvedFileName) !== path.join(dir, 'index.d.ts') ||
|
|
667
|
-
issuer.resolve('react-intl') !== entry ||
|
|
668
|
-
readInstalled(path.dirname(file), 'react')?.manifest.name !== 'react')
|
|
669
|
-
return false;
|
|
670
|
-
const react = issuer.resolve('react');
|
|
765
|
+
const react = createRequire(file).resolve('react');
|
|
671
766
|
if (!['src', 'lib/src'].every((prefix) => createRequire(path.join(dir, prefix, 'components/useIntl.js')).resolve('react') ===
|
|
672
767
|
react))
|
|
673
768
|
return false;
|
|
@@ -894,6 +989,52 @@ export async function runCodemod(paths, options = {}) {
|
|
|
894
989
|
checkedHelpers.set(output, { code, state, error });
|
|
895
990
|
return error;
|
|
896
991
|
}
|
|
992
|
+
function selectedContextReexport(file, name) {
|
|
993
|
+
const seen = new Set();
|
|
994
|
+
for (;;) {
|
|
995
|
+
const selected = exports.get(file);
|
|
996
|
+
if (!selected ||
|
|
997
|
+
seen.has(file) ||
|
|
998
|
+
!isPlainFile(file) ||
|
|
999
|
+
ts.findConfigFile(path.dirname(file), ts.sys.fileExists) !== rootProject)
|
|
1000
|
+
return false;
|
|
1001
|
+
const hasContext = selected.contexts.includes(name);
|
|
1002
|
+
if (!hasContext && !selected.reexports.length)
|
|
1003
|
+
return false;
|
|
1004
|
+
// TypeScript's source lookup omits these competing bundler targets.
|
|
1005
|
+
const stem = file.slice(0, -path.extname(file).length);
|
|
1006
|
+
if (['.json', '.tsrx'].some((ext) => fs.lstatSync(stem + ext, { throwIfNoEntry: false })))
|
|
1007
|
+
return false;
|
|
1008
|
+
const source = fs.readFileSync(file, 'utf8');
|
|
1009
|
+
if (hasContext)
|
|
1010
|
+
return installedReact(file) && retainedReactContextExports(file, source).has(name);
|
|
1011
|
+
seen.add(file);
|
|
1012
|
+
const { statements } = ts.createSourceFile(file, source, ts.ScriptTarget.Latest);
|
|
1013
|
+
const [statement] = statements;
|
|
1014
|
+
// A pure named forwarding module has no competing value export.
|
|
1015
|
+
if (statements.length !== 1 ||
|
|
1016
|
+
!ts.isExportDeclaration(statement) ||
|
|
1017
|
+
statement.isTypeOnly ||
|
|
1018
|
+
statement.attributes ||
|
|
1019
|
+
!statement.exportClause ||
|
|
1020
|
+
!ts.isNamedExports(statement.exportClause) ||
|
|
1021
|
+
!statement.moduleSpecifier ||
|
|
1022
|
+
!ts.isStringLiteral(statement.moduleSpecifier))
|
|
1023
|
+
return false;
|
|
1024
|
+
const specifier = statement.moduleSpecifier.text;
|
|
1025
|
+
if (!/^\.{1,2}\//.test(specifier) || /[!%?#\\]/.test(specifier))
|
|
1026
|
+
return false;
|
|
1027
|
+
const bindings = statement.exportClause.elements.filter((element) => !element.isTypeOnly && element.name.text === name);
|
|
1028
|
+
if (bindings.length !== 1)
|
|
1029
|
+
return false;
|
|
1030
|
+
const next = localFile(file, specifier, true);
|
|
1031
|
+
const direct = path.resolve(path.dirname(file), specifier);
|
|
1032
|
+
if (!next || (next !== direct && next !== direct + path.extname(next)))
|
|
1033
|
+
return false;
|
|
1034
|
+
file = next;
|
|
1035
|
+
name = (bindings[0].propertyName ?? bindings[0].name).text;
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
897
1038
|
const results = [];
|
|
898
1039
|
const storeHookComponents = new Map();
|
|
899
1040
|
if (options.svgPaths && target === 'tsrx') {
|
|
@@ -928,7 +1069,7 @@ export async function runCodemod(paths, options = {}) {
|
|
|
928
1069
|
const reactStoreComponents = new Map();
|
|
929
1070
|
let installedClsx;
|
|
930
1071
|
let reactJsx;
|
|
931
|
-
const isContext = (specifier, name, local) => {
|
|
1072
|
+
const isContext = (specifier, name, local, attributed) => {
|
|
932
1073
|
if (!specifier.startsWith('.')) {
|
|
933
1074
|
if (!contextPaths.length ||
|
|
934
1075
|
!esmContextProject ||
|
|
@@ -938,11 +1079,13 @@ export async function runCodemod(paths, options = {}) {
|
|
|
938
1079
|
: specifier.startsWith(start) && specifier.endsWith(end)))
|
|
939
1080
|
return null;
|
|
940
1081
|
const source = localFile(file, specifier, true);
|
|
1082
|
+
const selected = source && exports.get(source);
|
|
941
1083
|
return source &&
|
|
942
|
-
|
|
1084
|
+
selected &&
|
|
943
1085
|
ts.findConfigFile(path.dirname(source), ts.sys.fileExists) === rootProject &&
|
|
944
|
-
(
|
|
945
|
-
(name === 'default' ? local : name).endsWith('Context')
|
|
1086
|
+
(selected.contexts.includes(name) ||
|
|
1087
|
+
(name === 'default' ? local : name).endsWith('Context') ||
|
|
1088
|
+
(!attributed && selected.reexports.length > 0 && selectedContextReexport(source, name)))
|
|
946
1089
|
? 'review-alias'
|
|
947
1090
|
: null;
|
|
948
1091
|
}
|
|
@@ -1092,6 +1235,7 @@ export async function runCodemod(paths, options = {}) {
|
|
|
1092
1235
|
initializerResourceHint: renames.has(file)
|
|
1093
1236
|
? (module, name, retainedOnly) => initializerResourceHint(file, module, name, retainedOnly)
|
|
1094
1237
|
: undefined,
|
|
1238
|
+
originalIntlFactory: () => originalIntlFactory(file),
|
|
1095
1239
|
localReactHook: renames.has(file)
|
|
1096
1240
|
? (module, name, nativeOnly) => localReactHook(file, module, name, nativeOnly)
|
|
1097
1241
|
: undefined,
|
|
@@ -1439,13 +1583,18 @@ export async function runCodemod(paths, options = {}) {
|
|
|
1439
1583
|
const jsxFactories = new Map();
|
|
1440
1584
|
const renamedJsx = [...renames.keys()].some((file) => file.endsWith('.tsx'));
|
|
1441
1585
|
const namedReexports = new Map();
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1586
|
+
function canResolveExtensionlessJsx(specifier) {
|
|
1587
|
+
return (/^\.{1,2}\//.test(specifier) &&
|
|
1588
|
+
!/[!%?#\\]/.test(specifier) &&
|
|
1589
|
+
!/\.(?:[cm]?[jt]sx?|tsrx)$/.test(specifier) &&
|
|
1590
|
+
(config?.moduleResolution === undefined ||
|
|
1591
|
+
config.moduleResolution === ts.ModuleResolutionKind.Bundler) &&
|
|
1592
|
+
!config?.rootDirs &&
|
|
1593
|
+
!config?.moduleSuffixes?.some(Boolean));
|
|
1594
|
+
}
|
|
1595
|
+
function isExtensionlessJsxTarget(file, original) {
|
|
1596
|
+
return file.endsWith('.tsx') && unambiguousJsxPath(file, original) && isPlainFile(file);
|
|
1597
|
+
}
|
|
1449
1598
|
function retainedJsxTarget(from, specifier, name) {
|
|
1450
1599
|
if (!/^\.{1,2}\//.test(specifier) ||
|
|
1451
1600
|
/[!%?#\\]/.test(specifier) ||
|
|
@@ -1679,6 +1828,47 @@ export async function runCodemod(paths, options = {}) {
|
|
|
1679
1828
|
leaf.notes.push({ ...note, line: 1 });
|
|
1680
1829
|
}
|
|
1681
1830
|
}
|
|
1831
|
+
if (paths &&
|
|
1832
|
+
!edited.has(file) &&
|
|
1833
|
+
!isTsrx(file) &&
|
|
1834
|
+
(source.includes('react-dom/client') || source.includes('\\')) &&
|
|
1835
|
+
ts.findConfigFile(path.dirname(file), ts.sys.fileExists) === rootProject &&
|
|
1836
|
+
originalReactEntry(file, 'react-dom/client')) {
|
|
1837
|
+
for (const edge of reactClientComponents(file, source, (module) => module === 'react'
|
|
1838
|
+
? originalReactEntry(file, module)
|
|
1839
|
+
: automaticReactJsx(file) && originalReactEntry(file, module))) {
|
|
1840
|
+
const explicit = edge.module.endsWith('.tsx');
|
|
1841
|
+
if (!explicit && !canResolveExtensionlessJsx(edge.module))
|
|
1842
|
+
continue;
|
|
1843
|
+
const original = path.resolve(path.dirname(file), edge.module);
|
|
1844
|
+
const match = explicit
|
|
1845
|
+
? retainedJsxTarget(file, edge.module, edge.name)
|
|
1846
|
+
: renamed(file, edge.module);
|
|
1847
|
+
if (!match ||
|
|
1848
|
+
('indirect' in match && match.indirect) ||
|
|
1849
|
+
(explicit ? match.file !== original : !isExtensionlessJsxTarget(match.file, original)) ||
|
|
1850
|
+
!automaticReactJsx(match.file) ||
|
|
1851
|
+
!originalReactEntry(match.file, 'react/jsx-runtime'))
|
|
1852
|
+
continue;
|
|
1853
|
+
const leaf = byFile.get(match.file);
|
|
1854
|
+
if (!leaf?.output.endsWith('.tsrx'))
|
|
1855
|
+
continue;
|
|
1856
|
+
const { intrinsics, wrappers } = originalReactExports(match.file);
|
|
1857
|
+
if ((!intrinsics.has(edge.name) && !wrappers.has(edge.name)) ||
|
|
1858
|
+
!hasTemplateExport(leaf, edge.name))
|
|
1859
|
+
continue;
|
|
1860
|
+
const note = {
|
|
1861
|
+
rule: 'react-compat',
|
|
1862
|
+
severity: 'error',
|
|
1863
|
+
line: edge.line,
|
|
1864
|
+
message: `The retained React DOM client root in ${name} renders ${leaf.file}. Migrate the root too or keep this component in React.`,
|
|
1865
|
+
};
|
|
1866
|
+
if (write)
|
|
1867
|
+
return blockedWrite(root, unchangedFile(name, source, note), compiler?.from);
|
|
1868
|
+
host.notes.push(note);
|
|
1869
|
+
leaf.notes.push({ ...note, line: 1 });
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1682
1872
|
const retainedServer = paths && !isTsrx(file) && (source.includes('react-dom/server') || source.includes('\\'));
|
|
1683
1873
|
const retainedElement = paths &&
|
|
1684
1874
|
!known.has(file) &&
|
|
@@ -1703,14 +1893,7 @@ export async function runCodemod(paths, options = {}) {
|
|
|
1703
1893
|
for (const edge of edges) {
|
|
1704
1894
|
const factory = 'factory' in edge && edge.factory;
|
|
1705
1895
|
const inferredServerPath = edge.renderer === 'server render' && !/\.[jt]sx$/.test(edge.module);
|
|
1706
|
-
if (inferredServerPath &&
|
|
1707
|
-
(/\.(?:[cm]?[jt]sx?|tsrx)$/.test(edge.module) ||
|
|
1708
|
-
(config?.moduleResolution !== undefined &&
|
|
1709
|
-
config.moduleResolution !== ts.ModuleResolutionKind.Bundler) ||
|
|
1710
|
-
!/^\.{1,2}\//.test(edge.module) ||
|
|
1711
|
-
/[!%?#\\]/.test(edge.module) ||
|
|
1712
|
-
config?.rootDirs ||
|
|
1713
|
-
config?.moduleSuffixes?.some(Boolean)))
|
|
1896
|
+
if (inferredServerPath && !canResolveExtensionlessJsx(edge.module))
|
|
1714
1897
|
continue;
|
|
1715
1898
|
const original = path.resolve(path.dirname(file), edge.module);
|
|
1716
1899
|
const direct = edge.renderer === 'createElement' ? null : renamed(file, edge.module);
|
|
@@ -1725,10 +1908,7 @@ export async function runCodemod(paths, options = {}) {
|
|
|
1725
1908
|
if (!match || (edge.renderer === 'createElement' && proven?.indirect))
|
|
1726
1909
|
continue;
|
|
1727
1910
|
const exportName = proven?.name ?? edge.name;
|
|
1728
|
-
const extensionlessServer = inferredServerPath &&
|
|
1729
|
-
match.file.endsWith('.tsx') &&
|
|
1730
|
-
unambiguousJsxPath(match.file, original) &&
|
|
1731
|
-
isPlainFile(match.file);
|
|
1911
|
+
const extensionlessServer = inferredServerPath && isExtensionlessJsxTarget(match.file, original);
|
|
1732
1912
|
const explicit = match.file === original && (edge.renderer !== 'JSX' || /\.[jt]sx$/.test(edge.module));
|
|
1733
1913
|
if (!explicit &&
|
|
1734
1914
|
!extensionlessServer &&
|
|
@@ -1762,10 +1942,17 @@ export async function runCodemod(paths, options = {}) {
|
|
|
1762
1942
|
if (edge.lazy && originalExports.mayExportThen)
|
|
1763
1943
|
continue;
|
|
1764
1944
|
if (!originalExports.intrinsics.has(exportName)) {
|
|
1945
|
+
const extensionlessJsx = edge.renderer === 'JSX' &&
|
|
1946
|
+
!!direct &&
|
|
1947
|
+
canResolveExtensionlessJsx(edge.module) &&
|
|
1948
|
+
isExtensionlessJsxTarget(match.file, original);
|
|
1765
1949
|
const splitIntrinsicDefault = edge.renderer === 'createElement' &&
|
|
1766
1950
|
exportName === 'default' &&
|
|
1767
1951
|
originalExports.splitIntrinsicDefault;
|
|
1768
|
-
if ((!explicit &&
|
|
1952
|
+
if ((!explicit &&
|
|
1953
|
+
!extensionlessServer &&
|
|
1954
|
+
!extensionlessJsx &&
|
|
1955
|
+
edge.renderer !== 'createElement') ||
|
|
1769
1956
|
edge.lazy ||
|
|
1770
1957
|
(!splitIntrinsicDefault && !originalExports.wrappers.has(edge.name)) ||
|
|
1771
1958
|
!hasTemplateExport(leaf, edge.name))
|
|
@@ -32,6 +32,12 @@ export declare function octaneRootComponents(file: string, source: string): {
|
|
|
32
32
|
name: string;
|
|
33
33
|
line: number;
|
|
34
34
|
}[];
|
|
35
|
+
/** Retained React DOM client roots that render a directly imported React element. */
|
|
36
|
+
export declare function reactClientComponents(file: string, source: string, original: (module: 'react' | 'react/jsx-runtime') => boolean): {
|
|
37
|
+
module: string;
|
|
38
|
+
name: string;
|
|
39
|
+
line: number;
|
|
40
|
+
}[];
|
|
35
41
|
/** React server calls that render directly imported components. */
|
|
36
42
|
export declare function reactServerComponents(file: string, source: string, original: (module: 'react' | 'react/jsx-runtime') => boolean, selected?: boolean): {
|
|
37
43
|
module: string;
|