@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/transform.js
CHANGED
|
@@ -1,20 +1,33 @@
|
|
|
1
1
|
import { svgDataAttribute, svgLiteralRootOverride } from './path-svg.js';
|
|
2
|
-
import { forwardedProps, literalJsxAttribute, nativeOutputOwner, returnedTextReceiver, renderUses, svgLocalReads, } from './forwarded-props.js';
|
|
3
|
-
import { reactRenderer, staticReactProps } from './react-renderer.js';
|
|
2
|
+
import { forwardedProps, hasEval, literalJsxAttribute, nativeOutputOwner, returnedTextReceiver, renderUses, rootHandleEscapes, svgLocalReads, } from './forwarded-props.js';
|
|
3
|
+
import { reactRenderer, staticReactProps, unreadServerStream } from './react-renderer.js';
|
|
4
4
|
import { linkedRenderState } from './linked-state.js';
|
|
5
5
|
import { nativeSetupHook } from './local-react-hook.js';
|
|
6
|
-
import { localUses, scalar, walkLocalValues } from './local-uses.js';
|
|
7
|
-
import { isWrapper, skipWrappers } from './expression-wrappers.js';
|
|
6
|
+
import { componentTagOrExport, inTypeNode, localSymbol, localUses, scalar, unchangedContextProviderUse, walkLocalValues, } from './local-uses.js';
|
|
7
|
+
import { isWrapper, literalCondition, skipWrappers } from './expression-wrappers.js';
|
|
8
8
|
import ts from 'typescript';
|
|
9
|
-
import { effectReceivers, fixedBinding, initializedBefore } from './effect-receivers.js';
|
|
9
|
+
import { atInitializedModuleHandle, effectHelpers, effectReceivers, fixedBinding, initializedBefore, stableExportedProp, } from './effect-receivers.js';
|
|
10
10
|
import { isBareSvgTag, storeHookUses } from './store-hook-uses.js';
|
|
11
11
|
import { intlImports } from './react-intl.js';
|
|
12
|
-
import { DEPENDENCY_HOOKS, RADIX_TYPES, REACT_MODULES, TEXT_FIELD_COMPONENTS, TEXT_INPUT_TYPES, UNSUPPORTED_VALUES, mapPackageExport, mapQualifiedType, mapReactImport, missingExport, radixNamespace, } from './mappings.js';
|
|
12
|
+
import { DEPENDENCY_HOOKS, RADIX_TYPES, REACT_MODULES, TEXT_FIELD_COMPONENTS, TEXT_INPUT_TYPES, UNSUPPORTED_VALUES, hasOctaneValue, isComputedReactHookOrRef, mapPackageExport, mapQualifiedType, mapReactImport, missingExport, radixNamespace, } from './mappings.js';
|
|
13
13
|
const RADIX_SLOT_MEMBERS = new Map([
|
|
14
14
|
['Root', 'Slot'],
|
|
15
15
|
['Slot', 'Slot'],
|
|
16
16
|
['Slottable', 'Slottable'],
|
|
17
17
|
]);
|
|
18
|
+
function quotedPropWithReference(node) {
|
|
19
|
+
return (ts.isJsxAttribute(node) &&
|
|
20
|
+
!!node.initializer &&
|
|
21
|
+
ts.isStringLiteral(node.initializer) &&
|
|
22
|
+
node.initializer.text.includes('&'));
|
|
23
|
+
}
|
|
24
|
+
function singleCallbackExpression(body) {
|
|
25
|
+
if (!ts.isBlock(body))
|
|
26
|
+
return body;
|
|
27
|
+
const statement = body.statements[0];
|
|
28
|
+
if (body.statements.length === 1 && ts.isReturnStatement(statement))
|
|
29
|
+
return statement.expression;
|
|
30
|
+
}
|
|
18
31
|
function directMember(receiver) {
|
|
19
32
|
const parent = receiver.parent;
|
|
20
33
|
if (ts.isPropertyAccessExpression(parent) &&
|
|
@@ -25,6 +38,38 @@ function directMember(receiver) {
|
|
|
25
38
|
return parent.right;
|
|
26
39
|
return null;
|
|
27
40
|
}
|
|
41
|
+
// React Router applications can augment this loader type on the original module.
|
|
42
|
+
function routerLoadContext(module, name) {
|
|
43
|
+
return name === 'AppLoadContext' && (module === 'react-router' || module === 'react-router-dom');
|
|
44
|
+
}
|
|
45
|
+
function firstQualifiedName(name) {
|
|
46
|
+
while (ts.isQualifiedName(name))
|
|
47
|
+
name = name.left;
|
|
48
|
+
return name;
|
|
49
|
+
}
|
|
50
|
+
function isValueTypeQuery(name) {
|
|
51
|
+
while (ts.isQualifiedName(name.parent) && name.parent.left === name)
|
|
52
|
+
name = name.parent;
|
|
53
|
+
return ts.isTypeQueryNode(name.parent) && name.parent.exprName === name;
|
|
54
|
+
}
|
|
55
|
+
/** The lexical root of an interface base or class implements type. */
|
|
56
|
+
function typeHeritageRoot(node) {
|
|
57
|
+
if (!ts.isPartOfTypeNode(node))
|
|
58
|
+
return;
|
|
59
|
+
let root = node.expression;
|
|
60
|
+
while (ts.isPropertyAccessExpression(root) && !root.questionDotToken)
|
|
61
|
+
root = root.expression;
|
|
62
|
+
if (ts.isIdentifier(root) && root !== node.expression)
|
|
63
|
+
return root;
|
|
64
|
+
}
|
|
65
|
+
function needsTouchValueOrNamespace(ref) {
|
|
66
|
+
if (isValueTypeQuery(ref) || (ts.isQualifiedName(ref.parent) && ref.parent.left === ref))
|
|
67
|
+
return true;
|
|
68
|
+
let access = ref;
|
|
69
|
+
while (ts.isPropertyAccessExpression(access.parent) && access.parent.expression === access)
|
|
70
|
+
access = access.parent;
|
|
71
|
+
return ts.isExpressionWithTypeArguments(access.parent) && typeHeritageRoot(access.parent) === ref;
|
|
72
|
+
}
|
|
28
73
|
/** Marks emitted text whose whole line should go. */
|
|
29
74
|
const DROP = '\u0000DROP\u0000';
|
|
30
75
|
/**
|
|
@@ -37,12 +82,23 @@ const PLAIN = { template: false };
|
|
|
37
82
|
const DESCRIPTOR = { template: false, descriptor: true };
|
|
38
83
|
/** The local component that wraps an element no island can host and throws its `error`. */
|
|
39
84
|
const NO_PORT = 'NoOctanePort';
|
|
85
|
+
const INTL_FORMATTER = /^Formatted[A-Z]/;
|
|
40
86
|
const syntheticEventMembers = {
|
|
41
87
|
nativeEvent: 'unknown',
|
|
42
88
|
persist: '() => void',
|
|
43
89
|
isDefaultPrevented: '() => boolean',
|
|
44
90
|
isPropagationStopped: '() => boolean',
|
|
45
91
|
};
|
|
92
|
+
export const MAP_PARAMETER_TDZ = 'map-parameter-tdz';
|
|
93
|
+
export const MAP_PARAMETER_WRITE = 'map-parameter-write';
|
|
94
|
+
export const MAP_CALLBACK_SCOPE = 'map-callback-scope';
|
|
95
|
+
export const CONTEXT_ALIAS = 'context-path-alias';
|
|
96
|
+
export const CONTEXT_PROVIDER_GETTER = 'context-provider-getter';
|
|
97
|
+
export const ELEMENT_CHILD_ALIAS = 'element-child-path-alias';
|
|
98
|
+
export const INTL_PROVIDER_DEFAULTS = 'intl-provider-defaults';
|
|
99
|
+
export const REACT_TYPE_PROJECTION = 'react-type-projection';
|
|
100
|
+
export const BUFFERED_SSR_OPTIONS = 'buffered-ssr-options';
|
|
101
|
+
export const BUFFERED_SSR_REACT_COMPONENT = 'buffered-ssr-react-component';
|
|
46
102
|
/** Rule names, as reported. */
|
|
47
103
|
const RULE = {
|
|
48
104
|
binding: 'bindings',
|
|
@@ -64,6 +120,7 @@ const RULE = {
|
|
|
64
120
|
documentShell: 'document-shell',
|
|
65
121
|
nativeChange: 'native-text-change',
|
|
66
122
|
nativeSignal: 'native-signal-read',
|
|
123
|
+
nativeRefState: 'native-ref-state',
|
|
67
124
|
className: 'class-attribute',
|
|
68
125
|
provider: 'context-provider',
|
|
69
126
|
strictMode: 'strict-mode',
|
|
@@ -86,10 +143,26 @@ export function transformModule(source, filename, options = {}) {
|
|
|
86
143
|
export function projectTypeAt(node, types) {
|
|
87
144
|
if (types?.sourceFile.text !== node.getSourceFile().text)
|
|
88
145
|
return null;
|
|
89
|
-
const
|
|
90
|
-
|
|
146
|
+
const peer = nodeSpanning(types.sourceFile, node.getStart(), node.end);
|
|
147
|
+
// A shorthand and its identifier occupy the same span. Use the declared
|
|
148
|
+
// prop contract when the binding stays stable and local code cannot supply it.
|
|
149
|
+
if (peer &&
|
|
150
|
+
ts.isShorthandPropertyAssignment(peer) &&
|
|
151
|
+
ts.isIdentifier(node) &&
|
|
152
|
+
ts.isShorthandPropertyAssignment(node.parent) &&
|
|
153
|
+
node.parent.name === node) {
|
|
154
|
+
const { sourceFile, checker } = types;
|
|
155
|
+
const symbol = checker.getShorthandAssignmentValueSymbol(peer);
|
|
156
|
+
const type = checker.getTypeAtLocation(peer.name);
|
|
157
|
+
if (!scalar(type) ||
|
|
158
|
+
!stableExportedProp(symbol, checker) ||
|
|
159
|
+
((sourceFile.text.includes('eval') || sourceFile.text.includes('\\u')) && hasEval(sourceFile)))
|
|
160
|
+
return null;
|
|
161
|
+
return { type, checker };
|
|
162
|
+
}
|
|
163
|
+
if (peer?.kind !== node.kind)
|
|
91
164
|
return null;
|
|
92
|
-
return { type: types.checker.getTypeAtLocation(
|
|
165
|
+
return { type: types.checker.getTypeAtLocation(peer), checker: types.checker };
|
|
93
166
|
}
|
|
94
167
|
/**
|
|
95
168
|
* Parse and bind one module on its own, with only the ES5 library and no
|
|
@@ -99,6 +172,8 @@ export function projectTypeAt(node, types) {
|
|
|
99
172
|
* members.
|
|
100
173
|
*/
|
|
101
174
|
export function bind(filename, source) {
|
|
175
|
+
// The program asks its host for slash-delimited paths even on Windows.
|
|
176
|
+
filename = filename.replaceAll('\\', '/');
|
|
102
177
|
const lib = esLib();
|
|
103
178
|
const sf = ts.createSourceFile(filename, source, ts.ScriptTarget.Latest, true, scriptKind(filename));
|
|
104
179
|
const host = {
|
|
@@ -157,6 +232,14 @@ function isDOMTag(tag) {
|
|
|
157
232
|
}
|
|
158
233
|
return domTagNames.has(tag);
|
|
159
234
|
}
|
|
235
|
+
/** Follow direct JSX children only through ordinary HTML hosts with no content override. */
|
|
236
|
+
function rendersOrdinaryHtmlChildren(opening) {
|
|
237
|
+
return (ts.isIdentifier(opening.tagName) &&
|
|
238
|
+
/^(?:article|aside|div|footer|header|main|nav|section)$/.test(opening.tagName.text) &&
|
|
239
|
+
opening.attributes.properties.every((attr) => ts.isJsxAttribute(attr) &&
|
|
240
|
+
ts.isIdentifier(attr.name) &&
|
|
241
|
+
!/^(?:children|dangerouslySetInnerHTML|is)$/i.test(attr.name.text)));
|
|
242
|
+
}
|
|
160
243
|
function scriptKind(filename) {
|
|
161
244
|
if (filename.endsWith('.tsx'))
|
|
162
245
|
return ts.ScriptKind.TSX;
|
|
@@ -167,11 +250,11 @@ function scriptKind(filename) {
|
|
|
167
250
|
// Plain .js files often hold JSX in React projects.
|
|
168
251
|
return ts.ScriptKind.JSX;
|
|
169
252
|
}
|
|
170
|
-
function nativeReactCompat(module, name) {
|
|
253
|
+
export function nativeReactCompat(module, name) {
|
|
171
254
|
return ((name === 'ReactCompat' || name === 'ReactCompatInline') &&
|
|
172
255
|
(module === 'octane/react' || module === 'octane/react/server'));
|
|
173
256
|
}
|
|
174
|
-
function skipParens(node) {
|
|
257
|
+
export function skipParens(node) {
|
|
175
258
|
while (ts.isParenthesizedExpression(node))
|
|
176
259
|
node = node.expression;
|
|
177
260
|
return node;
|
|
@@ -190,6 +273,37 @@ function asRest(declaration) {
|
|
|
190
273
|
function isJsx(node) {
|
|
191
274
|
return ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node) || ts.isJsxFragment(node);
|
|
192
275
|
}
|
|
276
|
+
function numericConstantValue(node) {
|
|
277
|
+
node = skipWrappers(node);
|
|
278
|
+
if (ts.isNumericLiteral(node))
|
|
279
|
+
return Number(node.text);
|
|
280
|
+
if (!ts.isPrefixUnaryExpression(node) ||
|
|
281
|
+
(node.operator !== ts.SyntaxKind.MinusToken && node.operator !== ts.SyntaxKind.PlusToken))
|
|
282
|
+
return undefined;
|
|
283
|
+
const operand = skipWrappers(node.operand);
|
|
284
|
+
if (!ts.isNumericLiteral(operand))
|
|
285
|
+
return undefined;
|
|
286
|
+
const value = Number(operand.text);
|
|
287
|
+
return node.operator === ts.SyntaxKind.MinusToken ? -value : value;
|
|
288
|
+
}
|
|
289
|
+
function isNumericConstant(node) {
|
|
290
|
+
return numericConstantValue(node) !== undefined;
|
|
291
|
+
}
|
|
292
|
+
const TRUTHY = 1;
|
|
293
|
+
const FALSY = 2;
|
|
294
|
+
const NULLISH = 4;
|
|
295
|
+
function logicalRightCases(operator) {
|
|
296
|
+
switch (operator) {
|
|
297
|
+
case ts.SyntaxKind.AmpersandAmpersandToken:
|
|
298
|
+
return TRUTHY;
|
|
299
|
+
case ts.SyntaxKind.BarBarToken:
|
|
300
|
+
return FALSY | NULLISH;
|
|
301
|
+
case ts.SyntaxKind.QuestionQuestionToken:
|
|
302
|
+
return NULLISH;
|
|
303
|
+
default:
|
|
304
|
+
return 0;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
193
307
|
/** A deferred clsx call may test a binding only if its truth value stays fixed. */
|
|
194
308
|
function svgClassToken(expr, fixedToken, truthTest = false) {
|
|
195
309
|
expr = skipWrappers(expr);
|
|
@@ -233,16 +347,15 @@ function readsCssToken(ref) {
|
|
|
233
347
|
use.operatorToken.kind === ts.SyntaxKind.BarBarToken)));
|
|
234
348
|
}
|
|
235
349
|
/**
|
|
236
|
-
*
|
|
350
|
+
* Describe a package import that the codemod keeps on React.
|
|
237
351
|
*/
|
|
238
352
|
function portComment({ name, islands, blocked }) {
|
|
239
|
-
const
|
|
240
|
-
if (blocked > 0)
|
|
241
|
-
return `${
|
|
242
|
-
}
|
|
353
|
+
const retained = `Retained React import of \`${name}\``;
|
|
354
|
+
if (blocked > 0)
|
|
355
|
+
return `${retained}. Review the uses that need a React renderer.`;
|
|
243
356
|
return islands > 0
|
|
244
|
-
? `${
|
|
245
|
-
: `${
|
|
357
|
+
? `${retained}. Its components render in React through ReactCompat.`
|
|
358
|
+
: `${retained}. Keep React as a dependency.`;
|
|
246
359
|
}
|
|
247
360
|
/**
|
|
248
361
|
* Whether a hole is already spelled as text, or as a literal the cast would
|
|
@@ -316,7 +429,7 @@ function nodeSpanning(sf, start, end) {
|
|
|
316
429
|
node = child;
|
|
317
430
|
}
|
|
318
431
|
}
|
|
319
|
-
function
|
|
432
|
+
function isPrimitiveLiteral(node) {
|
|
320
433
|
node = skipWrappers(node);
|
|
321
434
|
if (ts.isPrefixUnaryExpression(node)) {
|
|
322
435
|
const operand = skipWrappers(node.operand);
|
|
@@ -365,12 +478,14 @@ function guardReturn(statement) {
|
|
|
365
478
|
return null;
|
|
366
479
|
return isJsx(skipParens(then.expression)) ? then.expression : null;
|
|
367
480
|
}
|
|
481
|
+
function hasJsx(node) {
|
|
482
|
+
return isJsx(node) || (ts.forEachChild(node, hasJsx) ?? false);
|
|
483
|
+
}
|
|
368
484
|
/**
|
|
369
485
|
* Whether any `return` in `statements`, outside nested functions, returns
|
|
370
486
|
* JSX.
|
|
371
487
|
*/
|
|
372
488
|
function returnsJsx(statements) {
|
|
373
|
-
const hasJsx = (node) => isJsx(node) || (ts.forEachChild(node, hasJsx) ?? false);
|
|
374
489
|
const visit = (node) => {
|
|
375
490
|
if (isFunctionLike(node))
|
|
376
491
|
return false;
|
|
@@ -380,23 +495,26 @@ function returnsJsx(statements) {
|
|
|
380
495
|
};
|
|
381
496
|
return statements.some(visit);
|
|
382
497
|
}
|
|
498
|
+
/** Follow a clause's straight-line statements and plain lexical blocks to an exit. */
|
|
499
|
+
function clauseReturnsJsx(statements) {
|
|
500
|
+
for (const statement of statements) {
|
|
501
|
+
if (ts.isReturnStatement(statement))
|
|
502
|
+
return !!statement.expression && isJsx(skipWrappers(statement.expression));
|
|
503
|
+
if (ts.isBreakStatement(statement) || ts.isThrowStatement(statement))
|
|
504
|
+
return false;
|
|
505
|
+
if (ts.isBlock(statement)) {
|
|
506
|
+
const returns = clauseReturnsJsx(statement.statements);
|
|
507
|
+
if (returns !== undefined)
|
|
508
|
+
return returns;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
383
512
|
/** Document tags a router shell renders through `@octanejs/tanstack-router`. */
|
|
384
513
|
const DOCUMENT_TAGS = {
|
|
385
514
|
html: 'Html',
|
|
386
515
|
head: 'Head',
|
|
387
516
|
body: 'Body',
|
|
388
517
|
};
|
|
389
|
-
/**
|
|
390
|
-
* Whether an identifier is read in a type, where an import needs no value.
|
|
391
|
-
* A `typeof X` query is a type, which `import type` serves; `extends Base`
|
|
392
|
-
* reads a value, so heritage counts as one.
|
|
393
|
-
*/
|
|
394
|
-
function inTypeNode(node) {
|
|
395
|
-
let parent = node.parent;
|
|
396
|
-
while (ts.isQualifiedName(parent))
|
|
397
|
-
parent = parent.parent;
|
|
398
|
-
return ts.isTypeNode(parent) && !ts.isExpressionWithTypeArguments(parent);
|
|
399
|
-
}
|
|
400
518
|
function isNullish(node) {
|
|
401
519
|
return (node.kind === ts.SyntaxKind.NullKeyword ||
|
|
402
520
|
node.kind === ts.SyntaxKind.FalseKeyword ||
|
|
@@ -475,16 +593,24 @@ function reads(node, test) {
|
|
|
475
593
|
return ts.forEachChild(node, (child) => reads(child, test) || undefined) === true;
|
|
476
594
|
}
|
|
477
595
|
/**
|
|
478
|
-
* Whether
|
|
596
|
+
* Whether moved code reads function context, including through a nested arrow.
|
|
479
597
|
*/
|
|
480
|
-
function
|
|
481
|
-
if (node.kind === ts.SyntaxKind.ThisKeyword
|
|
598
|
+
function readsFunctionContext(node, checker) {
|
|
599
|
+
if (node.kind === ts.SyntaxKind.ThisKeyword ||
|
|
600
|
+
(ts.isMetaProperty(node) && node.keywordToken === ts.SyntaxKind.NewKeyword))
|
|
482
601
|
return true;
|
|
483
|
-
if (ts.isIdentifier(node) && node.text === 'arguments')
|
|
484
|
-
|
|
485
|
-
|
|
602
|
+
if (ts.isIdentifier(node) && node.text === 'arguments') {
|
|
603
|
+
const symbol = localSymbol(checker, node);
|
|
604
|
+
return !!symbol && checker.isArgumentsSymbol(symbol);
|
|
605
|
+
}
|
|
606
|
+
if (isFunctionLike(node) && !ts.isArrowFunction(node)) {
|
|
607
|
+
const name = node.name;
|
|
608
|
+
// A computed method name runs before entering the method's own scope.
|
|
609
|
+
return (!!name && ts.isComputedPropertyName(name) && readsFunctionContext(name.expression, checker));
|
|
610
|
+
}
|
|
611
|
+
if (ts.isTypeNode(node) && ts.isPartOfTypeNode(node))
|
|
486
612
|
return false;
|
|
487
|
-
return ts.forEachChild(node, (child) =>
|
|
613
|
+
return (ts.forEachChild(node, (child) => readsFunctionContext(child, checker) || undefined) === true);
|
|
488
614
|
}
|
|
489
615
|
/**
|
|
490
616
|
* The children of an element that are more than layout whitespace.
|
|
@@ -492,6 +618,10 @@ function usesOwnThis(node) {
|
|
|
492
618
|
function contentOf(node) {
|
|
493
619
|
return node.children.filter((child) => !(ts.isJsxText(child) && child.containsOnlyTriviaWhiteSpaces));
|
|
494
620
|
}
|
|
621
|
+
/** JSX emitters agree on empty ASCII indentation; spaces on a single line are children. */
|
|
622
|
+
export function withoutJsxIndentation(children) {
|
|
623
|
+
return children.filter((child) => !(ts.isJsxText(child) && /^[\t ]*[\r\n][\t\r\n ]*$/.test(child.text)));
|
|
624
|
+
}
|
|
495
625
|
/**
|
|
496
626
|
* A comment's text without its delimiters, to wrap as a JSX comment.
|
|
497
627
|
*/
|
|
@@ -516,9 +646,11 @@ class ModuleTransform {
|
|
|
516
646
|
strong;
|
|
517
647
|
effectAliases;
|
|
518
648
|
effectSnapshots;
|
|
519
|
-
|
|
649
|
+
effectEdits;
|
|
520
650
|
snapshotCaptures;
|
|
651
|
+
effectHelperCaptures;
|
|
521
652
|
linkedStateEdits;
|
|
653
|
+
nativeRefStoreFactory;
|
|
522
654
|
linkedStateNames;
|
|
523
655
|
/** React imports read as values, not only as types. */
|
|
524
656
|
valueUses = new Set();
|
|
@@ -551,6 +683,9 @@ class ModuleTransform {
|
|
|
551
683
|
removed = new Set();
|
|
552
684
|
/** `<Fragment key>` rows whose key moved to the `@for` header. */
|
|
553
685
|
bare = new Set();
|
|
686
|
+
/** Map calls in output with a proven native renderer. */
|
|
687
|
+
nativeOutputMaps;
|
|
688
|
+
logicalResults;
|
|
554
689
|
/** Functions whose parameter list a rule replaced. */
|
|
555
690
|
parameters = new Map();
|
|
556
691
|
/** Rest props that give up `onChange` to a text field. */
|
|
@@ -569,6 +704,7 @@ class ModuleTransform {
|
|
|
569
704
|
unported = new Map();
|
|
570
705
|
/** Elements to host in a `ReactCompat` island. */
|
|
571
706
|
islands = new Map();
|
|
707
|
+
inlineIntlMessages = new Set();
|
|
572
708
|
reactRendererBodies = new Map();
|
|
573
709
|
originalStoreTags = new Map();
|
|
574
710
|
storeSourceNames;
|
|
@@ -576,6 +712,7 @@ class ModuleTransform {
|
|
|
576
712
|
unrenderable = new Map();
|
|
577
713
|
/** Placeholder index -> import declaration. */
|
|
578
714
|
importSlots = [];
|
|
715
|
+
retainedTypeNamespaces = new Set();
|
|
579
716
|
intrinsicPropsHelper = null;
|
|
580
717
|
hasJsx = false;
|
|
581
718
|
/** Whether the module imports React, rather than already being Octane. */
|
|
@@ -599,6 +736,9 @@ class ModuleTransform {
|
|
|
599
736
|
this.checker = checker;
|
|
600
737
|
this.indentUnit = /^\t/m.test(source) ? '\t' : ' ';
|
|
601
738
|
}
|
|
739
|
+
get emitsTsrx() {
|
|
740
|
+
return (this.template || (this.target === 'tsrx' && (this.hasJsx || /\.[jt]sx$/.test(this.filename))));
|
|
741
|
+
}
|
|
602
742
|
run() {
|
|
603
743
|
this.analyze();
|
|
604
744
|
let code = this.emit(this.sf, PLAIN, 0);
|
|
@@ -611,7 +751,28 @@ class ModuleTransform {
|
|
|
611
751
|
: ['', ''];
|
|
612
752
|
code = `${code.trimEnd()}\n\nfunction ${NO_PORT}({ error }${props})${result} {\n${this.indentUnit}throw new Error(error);\n}\n`;
|
|
613
753
|
}
|
|
614
|
-
|
|
754
|
+
if (this.nativeRefStoreFactory) {
|
|
755
|
+
code = `${code.trimEnd()}
|
|
756
|
+
|
|
757
|
+
function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
758
|
+
let current = initial;
|
|
759
|
+
let notify: (() => void) | null = null;
|
|
760
|
+
return {
|
|
761
|
+
get: () => current,
|
|
762
|
+
subscribe(listener: () => void) {
|
|
763
|
+
notify = listener;
|
|
764
|
+
return () => { if (notify === listener) notify = null; };
|
|
765
|
+
},
|
|
766
|
+
attach(next: T) {
|
|
767
|
+
if (current === next) return;
|
|
768
|
+
current = next;
|
|
769
|
+
notify?.();
|
|
770
|
+
},
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
`;
|
|
774
|
+
}
|
|
775
|
+
const tsrx = this.emitsTsrx;
|
|
615
776
|
if (tsrx)
|
|
616
777
|
code = code.replace(/^\/\*\*\s*@jsxImportSource\s+\S+\s*\*\/[ \t]*\r?\n/m, '');
|
|
617
778
|
// A JavaScript module was never type-checked; as TSRX it stays unchecked.
|
|
@@ -688,10 +849,26 @@ class ModuleTransform {
|
|
|
688
849
|
exported.add('default');
|
|
689
850
|
}
|
|
690
851
|
}
|
|
852
|
+
const storeHookComponents = new Set();
|
|
853
|
+
for (const item of this.nativeStoreHooks.keys()) {
|
|
854
|
+
for (const ref of this.localUses(item.name)) {
|
|
855
|
+
const call = ref.parent;
|
|
856
|
+
if (!ts.isCallExpression(call) || call.arguments.length || ts.isOptionalChain(call))
|
|
857
|
+
continue;
|
|
858
|
+
if (nativeSetupHook(ref, this.localUses, this.checker, undefined, true) !== 'first')
|
|
859
|
+
continue;
|
|
860
|
+
const component = ts.findAncestor(ref, ts.isFunctionDeclaration);
|
|
861
|
+
if (component?.modifiers?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword))
|
|
862
|
+
storeHookComponents.add('default');
|
|
863
|
+
else if (component?.name)
|
|
864
|
+
storeHookComponents.add(component.name.text);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
691
867
|
return {
|
|
692
868
|
contexts: [...exported],
|
|
693
869
|
reexports,
|
|
694
870
|
...(descriptorChildren.length ? { descriptorChildren } : {}),
|
|
871
|
+
...(storeHookComponents.size ? { storeHookComponents: [...storeHookComponents] } : {}),
|
|
695
872
|
...(this.elementChildren.length ? { elementChildren: this.elementChildren } : {}),
|
|
696
873
|
};
|
|
697
874
|
}
|
|
@@ -700,7 +877,7 @@ class ModuleTransform {
|
|
|
700
877
|
// Added imports quote their module as the module's own imports do.
|
|
701
878
|
const first = this.sf.statements.find(ts.isImportDeclaration);
|
|
702
879
|
this.quote = first ? first.moduleSpecifier.getText(this.sf)[0] : "'";
|
|
703
|
-
/** Locals
|
|
880
|
+
/** Locals whose package imports stay on React. */
|
|
704
881
|
const portLocals = new Set();
|
|
705
882
|
const reactSvgImports = new Set();
|
|
706
883
|
for (const statement of this.sf.statements) {
|
|
@@ -710,7 +887,7 @@ class ModuleTransform {
|
|
|
710
887
|
ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
711
888
|
const port = this.portOf(statement.moduleSpecifier.text);
|
|
712
889
|
if (port) {
|
|
713
|
-
this.note(RULE.bindings, 'warning', statement, `\`${port.name}\`
|
|
890
|
+
this.note(RULE.bindings, 'warning', statement, `\`${port.name}\` stays on React. Import its components directly where they render so they can render in a \`ReactCompat\` island.`);
|
|
714
891
|
}
|
|
715
892
|
}
|
|
716
893
|
if (!ts.isImportDeclaration(statement) || !statement.importClause)
|
|
@@ -766,7 +943,15 @@ class ModuleTransform {
|
|
|
766
943
|
this.nativeIntl = intlImports(this.sf, this.options.forwardedText, this.checker, (expr) => this.typeOf(expr), (fn) => this.isComponent(fn), (element) => !!this.authoredIsland(element), (element) => this.hostsInReact(element), (tag) => !!returnedTextReceiver(tag, {
|
|
767
944
|
typeOf: (n) => this.typeOf(n),
|
|
768
945
|
text: (n) => textKind(this.typeOf(n)) !== 0,
|
|
769
|
-
}, this.localUses), () => this.options.reactStoreComponent?.('react-intl') === true)
|
|
946
|
+
}, this.localUses), () => this.options.reactStoreComponent?.('react-intl') === true, (callee) => {
|
|
947
|
+
const hook = this.directImportedValue(skipWrappers(callee))?.entry;
|
|
948
|
+
return hook?.module === 'react' && this.options.originalReact === true
|
|
949
|
+
? hook.imported
|
|
950
|
+
: null;
|
|
951
|
+
}, (call, method) => {
|
|
952
|
+
if (method === 'formatMessage' || this.options.automaticReactJsx)
|
|
953
|
+
this.note(RULE.bindings, 'error', call, `A React Intl formatter can schedule React DOM resources on the original request. Verify this ${method} call runs without renderer-owned effects, or keep it in the original React render.`);
|
|
954
|
+
});
|
|
770
955
|
}
|
|
771
956
|
let safeStoreUses;
|
|
772
957
|
const descriptors = new Map();
|
|
@@ -777,11 +962,13 @@ class ModuleTransform {
|
|
|
777
962
|
this.options.resolveStoreHook &&
|
|
778
963
|
ts.isImportSpecifier(declaration) &&
|
|
779
964
|
/^use[A-Z0-9]/.test(entry.imported) &&
|
|
780
|
-
|
|
965
|
+
// The compiler only assigns custom hook slots to these local names.
|
|
966
|
+
/^use[A-Z]/.test(entry.local) &&
|
|
967
|
+
entry.local !== 'useContext') {
|
|
781
968
|
const statement = declaration.parent.parent.parent;
|
|
782
969
|
if (!statement.attributes && !/\/\*|\/\//.test(statement.getText(this.sf))) {
|
|
783
970
|
const pending = new Map();
|
|
784
|
-
const from = this.options.resolveStoreHook(entry.module, entry.imported, (sharedInput, unchangedHelper) => {
|
|
971
|
+
const from = this.options.resolveStoreHook(entry.module, entry.imported, (sharedInput, unchangedHelper, retainedArgument) => {
|
|
785
972
|
safeStoreUses ??= storeHookUses(this.sf, this.checker, (fn) => this.isComponent(fn), (expr) => this.typeOf(expr));
|
|
786
973
|
pending.clear();
|
|
787
974
|
return safeStoreUses(declaration, sharedInput, (elements) => {
|
|
@@ -791,7 +978,7 @@ class ModuleTransform {
|
|
|
791
978
|
for (const [index, arg] of args.entries())
|
|
792
979
|
pending.set(elements[index], arg);
|
|
793
980
|
return true;
|
|
794
|
-
}, unchangedHelper);
|
|
981
|
+
}, unchangedHelper, retainedArgument);
|
|
795
982
|
});
|
|
796
983
|
if (from) {
|
|
797
984
|
for (const [element, args] of pending)
|
|
@@ -817,8 +1004,30 @@ class ModuleTransform {
|
|
|
817
1004
|
const tag = this.originalStoreTags.get(item) ?? item.name.text;
|
|
818
1005
|
this.reactRendererBodies.set(element, tag + ', ' + props);
|
|
819
1006
|
}
|
|
1007
|
+
if (this.target === 'tsrx') {
|
|
1008
|
+
for (const item of this.nativeIntl) {
|
|
1009
|
+
const name = (item.propertyName ?? item.name).text;
|
|
1010
|
+
if (name !== 'FormattedMessage' && name !== 'IntlProvider')
|
|
1011
|
+
continue;
|
|
1012
|
+
for (const ref of this.localUses(item.name)) {
|
|
1013
|
+
const element = ref.parent;
|
|
1014
|
+
if ((!ts.isJsxOpeningElement(element) && !ts.isJsxSelfClosingElement(element)) ||
|
|
1015
|
+
element.tagName !== ref ||
|
|
1016
|
+
this.reactRendererBodies.has(element))
|
|
1017
|
+
continue;
|
|
1018
|
+
if (name === 'IntlProvider')
|
|
1019
|
+
this.checkIntlProviderResources(element);
|
|
1020
|
+
else if (ts.isJsxSelfClosingElement(element)) {
|
|
1021
|
+
this.reportIntlFormatterOwner(element, 'generated native adapter');
|
|
1022
|
+
if (this.inParagraph(element) && this.options.reactCompatInline)
|
|
1023
|
+
this.inlineIntlMessages.add(element);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
820
1028
|
let hasEval = false;
|
|
821
1029
|
const snapshots = [];
|
|
1030
|
+
const helperCalls = this.effectAliases && this.options.captureEffectHelpers ? [] : undefined;
|
|
822
1031
|
if (this.source.includes('SubmitEvent') ||
|
|
823
1032
|
this.source.includes('ReactEventHandler') ||
|
|
824
1033
|
this.source.includes('\\u'))
|
|
@@ -836,12 +1045,18 @@ class ModuleTransform {
|
|
|
836
1045
|
ts.isArrayBindingPattern(node.name) &&
|
|
837
1046
|
node.name.elements.length === 1)
|
|
838
1047
|
snapshots.push(node);
|
|
839
|
-
if ((
|
|
1048
|
+
if ((helperCalls ||
|
|
1049
|
+
this.options.pathSvgAdapter ||
|
|
840
1050
|
this.options.captureEffectSnapshots ||
|
|
841
1051
|
this.options.linkRenderState) &&
|
|
842
1052
|
ts.isIdentifier(node) &&
|
|
843
1053
|
node.text === 'eval')
|
|
844
1054
|
hasEval = true;
|
|
1055
|
+
if (helperCalls &&
|
|
1056
|
+
ts.isCallExpression(node) &&
|
|
1057
|
+
node.arguments.length === 2 &&
|
|
1058
|
+
ts.isArrowFunction(node.arguments[0]))
|
|
1059
|
+
helperCalls.push(node);
|
|
845
1060
|
if (ts.isIdentifier(node) && portLocals.has(node.text))
|
|
846
1061
|
this.planUnported(node);
|
|
847
1062
|
if (isJsx(node))
|
|
@@ -861,6 +1076,17 @@ class ModuleTransform {
|
|
|
861
1076
|
ts.forEachChild(node, visit);
|
|
862
1077
|
};
|
|
863
1078
|
visit(this.sf);
|
|
1079
|
+
for (const call of helperCalls ?? []) {
|
|
1080
|
+
const plan = effectHelpers(call, this.checker, (name) => this.reactHook(name), this.localUses, !hasEval);
|
|
1081
|
+
if (!plan)
|
|
1082
|
+
continue;
|
|
1083
|
+
const captures = (this.effectHelperCaptures ??= new Map());
|
|
1084
|
+
captures.set(plan.callback, plan.declarations.map(({ statement }) => statement));
|
|
1085
|
+
for (const { statement, move } of plan.declarations) {
|
|
1086
|
+
if (move)
|
|
1087
|
+
(this.effectEdits ??= new Map()).set(statement, DROP);
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
864
1090
|
if (!hasEval)
|
|
865
1091
|
for (const decl of snapshots)
|
|
866
1092
|
this.planEffectSnapshot(decl);
|
|
@@ -919,7 +1145,9 @@ class ModuleTransform {
|
|
|
919
1145
|
// An explicit loader, query, or fragment doesn't use the bare SVG convention.
|
|
920
1146
|
const bare = /^#?[^!?#]*\.svg$/i;
|
|
921
1147
|
for (const [declaration, entry] of this.imports) {
|
|
922
|
-
if (entry.typeOnly ||
|
|
1148
|
+
if (entry.typeOnly ||
|
|
1149
|
+
(entry.imported !== 'default' && !entry.namespace) ||
|
|
1150
|
+
!bare.test(entry.module))
|
|
923
1151
|
continue;
|
|
924
1152
|
const statement = ts.findAncestor(declaration, ts.isImportDeclaration);
|
|
925
1153
|
if (!statement?.attributes && bare.test(this.resolve(entry.module) ?? entry.module))
|
|
@@ -929,8 +1157,9 @@ class ModuleTransform {
|
|
|
929
1157
|
for (const field of fields) {
|
|
930
1158
|
if (pathSvgTags.has(field.tagName))
|
|
931
1159
|
this.count('svg-paths');
|
|
932
|
-
else if (bareSvgImports.size &&
|
|
933
|
-
|
|
1160
|
+
else if (bareSvgImports.size &&
|
|
1161
|
+
isBareSvgTag(field.tagName, this.checker, bareSvgImports, this.localUses)) {
|
|
1162
|
+
this.note('svg-issuer', 'error', field.tagName, 'This JSX tag can resolve to a bare SVG import. Loading the SVG from .tsrx can produce an asset URL. Import and render the SVG in a React-owned .tsx component hosted by ReactCompat, or configure a native SVG loader for .tsrx.' +
|
|
934
1163
|
(unknownSvgSlots.get(field.tagName) ?? '') +
|
|
935
1164
|
(this.options.pathSvgAdapter
|
|
936
1165
|
? ' For an asset with supported static geometry, run --svg-paths on the SVG file and verify the receiving renderer before using the native component.'
|
|
@@ -1010,8 +1239,7 @@ class ModuleTransform {
|
|
|
1010
1239
|
if (!openingElement.attributes.properties.every((attr) => ts.isJsxAttribute(attr) && attr.name.getText(this.sf) !== 'children') ||
|
|
1011
1240
|
closingElement.getText(this.sf) !== `</${openingElement.tagName.getText(this.sf)}>`)
|
|
1012
1241
|
return null;
|
|
1013
|
-
|
|
1014
|
-
const content = children.filter((child) => !(ts.isJsxText(child) && /^[\t ]*[\r\n][\t\r\n ]*$/.test(child.text)));
|
|
1242
|
+
const content = withoutJsxIndentation(children);
|
|
1015
1243
|
const [child] = content;
|
|
1016
1244
|
return content.length === 1 && (ts.isJsxElement(child) || ts.isJsxSelfClosingElement(child))
|
|
1017
1245
|
? child
|
|
@@ -1083,8 +1311,7 @@ class ModuleTransform {
|
|
|
1083
1311
|
});
|
|
1084
1312
|
}
|
|
1085
1313
|
/**
|
|
1086
|
-
* The package
|
|
1087
|
-
* still runs on React, which only the codemod's report and a comment say.
|
|
1314
|
+
* The package that the codemod keeps on React for this specifier, or null.
|
|
1088
1315
|
*/
|
|
1089
1316
|
portOf(module) {
|
|
1090
1317
|
let port = this.ports.get(module);
|
|
@@ -1126,7 +1353,7 @@ class ModuleTransform {
|
|
|
1126
1353
|
}
|
|
1127
1354
|
}
|
|
1128
1355
|
/**
|
|
1129
|
-
* A component from a
|
|
1356
|
+
* A component from a retained React import renders in React, inside
|
|
1130
1357
|
* a `ReactCompat` island, when its element hands React nothing Octane
|
|
1131
1358
|
* renders. Its hooks and contexts cannot run in an Octane component, so
|
|
1132
1359
|
* they are reported.
|
|
@@ -1153,7 +1380,7 @@ class ModuleTransform {
|
|
|
1153
1380
|
const parent = member.parent;
|
|
1154
1381
|
const block = (message) => {
|
|
1155
1382
|
port.blocked++;
|
|
1156
|
-
const error = `${message} \`${port.name}\`
|
|
1383
|
+
const error = `${message} \`${port.name}\` stays on React.`;
|
|
1157
1384
|
this.note(RULE.bindings, 'error', member, error);
|
|
1158
1385
|
return error;
|
|
1159
1386
|
};
|
|
@@ -1180,6 +1407,14 @@ class ModuleTransform {
|
|
|
1180
1407
|
reason = `<${text}> needs ReactCompatInline to stay inside this paragraph. Install an Octane release that exports it or keep the paragraph in React.`;
|
|
1181
1408
|
if (reason === null) {
|
|
1182
1409
|
port.islands++;
|
|
1410
|
+
const importedName = ts.isPropertyAccessExpression(member)
|
|
1411
|
+
? member.name.text
|
|
1412
|
+
: binding.imported;
|
|
1413
|
+
if (binding.module === 'react-intl' &&
|
|
1414
|
+
(name === 'FormattedMessage' ||
|
|
1415
|
+
(this.options.reactIntlAdapter && INTL_FORMATTER.test(importedName))))
|
|
1416
|
+
this.reportIntlFormatterOwner(element, 'generated island');
|
|
1417
|
+
this.checkIslandLayoutRef(parent);
|
|
1183
1418
|
this.islands.set(element, paragraph ? 'ReactCompatInline' : 'ReactCompat');
|
|
1184
1419
|
}
|
|
1185
1420
|
else {
|
|
@@ -1190,12 +1425,106 @@ class ModuleTransform {
|
|
|
1190
1425
|
}
|
|
1191
1426
|
else if (ts.isCallExpression(parent) && parent.expression === member) {
|
|
1192
1427
|
if (/^use[A-Z0-9]/.test(name))
|
|
1193
|
-
block(`\`${text}\` is a React hook
|
|
1428
|
+
block(`\`${text}\` is a React hook. Keep this hook call under a React renderer.`);
|
|
1194
1429
|
}
|
|
1195
1430
|
else if (ts.isCallExpression(parent) &&
|
|
1196
1431
|
/^(?:use|useContext)$/.test(this.calleeName(parent.expression) ?? '')) {
|
|
1197
|
-
block(`\`${text}\` is a React context
|
|
1432
|
+
block(`\`${text}\` is a React context. Keep this context read under a React renderer.`);
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
/** A layout setup can depend on the child ref from the original shared commit. */
|
|
1436
|
+
checkIslandLayoutRef(tag) {
|
|
1437
|
+
const attr = tag.attributes.properties.findLast((p) => ts.isJsxSpreadAttribute(p) || (ts.isIdentifier(p.name) && p.name.text === 'ref'));
|
|
1438
|
+
const value = attr && ts.isJsxAttribute(attr) && attr.initializer;
|
|
1439
|
+
if (!attr || !value || !ts.isJsxExpression(value) || !value.expression)
|
|
1440
|
+
return;
|
|
1441
|
+
const element = ts.isJsxOpeningElement(tag) ? tag.parent : tag;
|
|
1442
|
+
const owner = ts.findAncestor(element, isFunctionLike);
|
|
1443
|
+
if (!owner)
|
|
1444
|
+
return;
|
|
1445
|
+
const layoutRead = (ref) => {
|
|
1446
|
+
if (directMember(ref)?.text !== 'current')
|
|
1447
|
+
return false;
|
|
1448
|
+
const setup = ts.findAncestor(ref, ts.isFunctionLike);
|
|
1449
|
+
if (!setup || !ts.isArrowFunction(setup) || setup.modifiers?.length)
|
|
1450
|
+
return false;
|
|
1451
|
+
const call = setup.parent;
|
|
1452
|
+
if (!ts.isCallExpression(call) ||
|
|
1453
|
+
call.arguments[0] !== setup ||
|
|
1454
|
+
call.questionDotToken ||
|
|
1455
|
+
this.reactHook(call.expression) !== 'useLayoutEffect' ||
|
|
1456
|
+
!ts.isExpressionStatement(call.parent) ||
|
|
1457
|
+
call.parent.parent !== owner.body)
|
|
1458
|
+
return false;
|
|
1459
|
+
let value = ref.parent;
|
|
1460
|
+
while (isWrapper(value.parent) ||
|
|
1461
|
+
ts.isPropertyAccessExpression(value.parent) ||
|
|
1462
|
+
ts.isTemplateSpan(value.parent) ||
|
|
1463
|
+
ts.isTemplateExpression(value.parent) ||
|
|
1464
|
+
(ts.isPrefixUnaryExpression(value.parent) &&
|
|
1465
|
+
value.parent.operator === ts.SyntaxKind.ExclamationToken) ||
|
|
1466
|
+
(ts.isCallExpression(value.parent) &&
|
|
1467
|
+
(value.parent.expression === value || !ts.isOptionalChain(value.parent))))
|
|
1468
|
+
value = value.parent;
|
|
1469
|
+
const step = value.parent;
|
|
1470
|
+
return (value === setup.body ||
|
|
1471
|
+
(ts.isExpressionStatement(step) &&
|
|
1472
|
+
step.parent === setup.body &&
|
|
1473
|
+
ts.isBlock(setup.body) &&
|
|
1474
|
+
setup.body.statements[0] === step));
|
|
1475
|
+
};
|
|
1476
|
+
const readsInLayout = (name) => {
|
|
1477
|
+
const init = this.constValue(name);
|
|
1478
|
+
if (!init ||
|
|
1479
|
+
!ts.isCallExpression(init) ||
|
|
1480
|
+
init.questionDotToken ||
|
|
1481
|
+
init.arguments.length !== 1 ||
|
|
1482
|
+
init.arguments[0].kind !== ts.SyntaxKind.NullKeyword ||
|
|
1483
|
+
this.reactHook(init.expression) !== 'useRef' ||
|
|
1484
|
+
!fixedBinding(this.checker, name, element) ||
|
|
1485
|
+
!initializedBefore(this.checker, name, element))
|
|
1486
|
+
return false;
|
|
1487
|
+
return this.localUses(name).some(layoutRead);
|
|
1488
|
+
};
|
|
1489
|
+
const ref = skipWrappers(value.expression);
|
|
1490
|
+
let late = ts.isIdentifier(ref) && readsInLayout(ref);
|
|
1491
|
+
if (ts.isArrowFunction(ref) &&
|
|
1492
|
+
!ref.modifiers?.length &&
|
|
1493
|
+
ref.parameters.length === 1 &&
|
|
1494
|
+
!ref.parameters[0].initializer &&
|
|
1495
|
+
!ref.parameters[0].dotDotDotToken) {
|
|
1496
|
+
const body = ref.body;
|
|
1497
|
+
const assign = (value) => {
|
|
1498
|
+
const write = skipWrappers(value);
|
|
1499
|
+
if (!ts.isBinaryExpression(write) || write.operatorToken.kind !== ts.SyntaxKind.EqualsToken)
|
|
1500
|
+
return false;
|
|
1501
|
+
const left = skipWrappers(write.left);
|
|
1502
|
+
const right = skipWrappers(write.right);
|
|
1503
|
+
if (ts.isPropertyAccessExpression(left) &&
|
|
1504
|
+
left.name.text === 'current' &&
|
|
1505
|
+
ts.isIdentifier(left.expression) &&
|
|
1506
|
+
ts.isIdentifier(right) &&
|
|
1507
|
+
this.declarationOf(right) === ref.parameters[0] &&
|
|
1508
|
+
fixedBinding(this.checker, right, write))
|
|
1509
|
+
return readsInLayout(left.expression);
|
|
1510
|
+
return false;
|
|
1511
|
+
};
|
|
1512
|
+
if (ts.isBlock(body)) {
|
|
1513
|
+
for (const step of body.statements) {
|
|
1514
|
+
if (!ts.isExpressionStatement(step))
|
|
1515
|
+
break;
|
|
1516
|
+
if ((late = assign(step.expression)))
|
|
1517
|
+
break;
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
else
|
|
1521
|
+
late = assign(body);
|
|
1198
1522
|
}
|
|
1523
|
+
if (late)
|
|
1524
|
+
this.note('react-island-layout-ref', 'error', attr, 'This layout effect can run before the generated ReactCompat island sets its child ref. Keep both in React or move the layout work to the ref callback.');
|
|
1525
|
+
}
|
|
1526
|
+
reportIntlFormatterOwner(node, boundary) {
|
|
1527
|
+
this.note(RULE.bindings, 'error', node, `A React Intl formatter can depend on ancestor contexts or schedule React DOM resources outside this ${boundary}. Verify the provider and its formatters, or keep the formatter under its original React ancestors.`);
|
|
1199
1528
|
}
|
|
1200
1529
|
fixedSvgColor(ref) {
|
|
1201
1530
|
const tag = ref.parent;
|
|
@@ -1265,7 +1594,7 @@ class ModuleTransform {
|
|
|
1265
1594
|
const plan = linkedRenderState(owner, this.checker, (name) => this.localUses(name), (expr) => {
|
|
1266
1595
|
const hook = this.callee(expr);
|
|
1267
1596
|
return hook?.module === 'react' && hook.name === 'useState';
|
|
1268
|
-
});
|
|
1597
|
+
}, (expr) => this.strictTypes && isBooleanGuardType(this.typeOf(expr)));
|
|
1269
1598
|
if (!plan)
|
|
1270
1599
|
return;
|
|
1271
1600
|
const fresh = (base) => {
|
|
@@ -1471,7 +1800,7 @@ class ModuleTransform {
|
|
|
1471
1800
|
while (this.declared.has(cell) || this.readsGlobal(cell))
|
|
1472
1801
|
cell += '_';
|
|
1473
1802
|
this.declared.add(cell);
|
|
1474
|
-
const edits = (this.
|
|
1803
|
+
const edits = (this.effectEdits ??= new Map());
|
|
1475
1804
|
const joined = decl.name.getStart(this.sf) === decl.parent.getStart(this.sf) + 'const'.length;
|
|
1476
1805
|
edits.set(decl.name, joined ? ' ' + cell : cell);
|
|
1477
1806
|
edits.set(call, this.require('octane', 'useLazyRef', false));
|
|
@@ -1583,19 +1912,11 @@ class ModuleTransform {
|
|
|
1583
1912
|
return text.text.trim() !== '' && !/[!;]/.test(text.text);
|
|
1584
1913
|
if (!ts.isTemplateExpression(text) || /[!;]/.test(text.head.text))
|
|
1585
1914
|
return false;
|
|
1586
|
-
const numeric = (node) => {
|
|
1587
|
-
node = skipWrappers(node);
|
|
1588
|
-
return (ts.isNumericLiteral(node) ||
|
|
1589
|
-
(ts.isPrefixUnaryExpression(node) &&
|
|
1590
|
-
(node.operator === ts.SyntaxKind.MinusToken ||
|
|
1591
|
-
node.operator === ts.SyntaxKind.PlusToken) &&
|
|
1592
|
-
ts.isNumericLiteral(skipWrappers(node.operand))));
|
|
1593
|
-
};
|
|
1594
1915
|
return text.templateSpans.every(({ expression, literal }) => {
|
|
1595
1916
|
if (/[!;]/.test(literal.text))
|
|
1596
1917
|
return false;
|
|
1597
1918
|
const part = skipWrappers(expression);
|
|
1598
|
-
if (
|
|
1919
|
+
if (isNumericConstant(part))
|
|
1599
1920
|
return true;
|
|
1600
1921
|
if (!ts.isConditionalExpression(part))
|
|
1601
1922
|
return false;
|
|
@@ -1603,13 +1924,31 @@ class ModuleTransform {
|
|
|
1603
1924
|
return (ts.isIdentifier(test) &&
|
|
1604
1925
|
fixedBinding(this.checker, test, part, (this.fixedSvgParameters ??= new Map())) &&
|
|
1605
1926
|
initializedBefore(this.checker, test, part) &&
|
|
1606
|
-
|
|
1607
|
-
|
|
1927
|
+
isNumericConstant(part.whenTrue) &&
|
|
1928
|
+
isNumericConstant(part.whenFalse));
|
|
1608
1929
|
});
|
|
1609
1930
|
}
|
|
1610
1931
|
svgRead;
|
|
1611
1932
|
nativeSvgProps;
|
|
1612
1933
|
provenSvgOwners;
|
|
1934
|
+
provenOutputOwners;
|
|
1935
|
+
/** A direct component-value entry into one of the installed native server renderers. */
|
|
1936
|
+
nativeSvgServerEntry(ref) {
|
|
1937
|
+
let argument = ref;
|
|
1938
|
+
while (ts.isParenthesizedExpression(argument.parent))
|
|
1939
|
+
argument = argument.parent;
|
|
1940
|
+
const call = argument.parent;
|
|
1941
|
+
if (!ts.isCallExpression(call) ||
|
|
1942
|
+
call.arguments[0] !== argument ||
|
|
1943
|
+
call.arguments.length > 3 ||
|
|
1944
|
+
ts.isOptionalChain(call) ||
|
|
1945
|
+
call.arguments.some(ts.isSpreadElement))
|
|
1946
|
+
return false;
|
|
1947
|
+
const entry = this.directImportedValue(skipParens(call.expression))?.entry;
|
|
1948
|
+
return (entry?.module === 'octane/server' &&
|
|
1949
|
+
(entry.imported === 'renderToString' || entry.imported === 'renderToStaticMarkup') &&
|
|
1950
|
+
this.options.originalSvgServer === true);
|
|
1951
|
+
}
|
|
1613
1952
|
nativeOutput(value, uses, unproven) {
|
|
1614
1953
|
const typeOf = (node) => this.typeOf(node);
|
|
1615
1954
|
const destination = {
|
|
@@ -1628,9 +1967,11 @@ class ModuleTransform {
|
|
|
1628
1967
|
if (!first)
|
|
1629
1968
|
return false;
|
|
1630
1969
|
const pending = [first];
|
|
1631
|
-
const proven =
|
|
1970
|
+
const proven = unproven
|
|
1971
|
+
? (this.provenSvgOwners ??= new Set())
|
|
1972
|
+
: (this.provenOutputOwners ??= new Set());
|
|
1632
1973
|
const seen = new Set();
|
|
1633
|
-
let
|
|
1974
|
+
let reachesRenderer = false;
|
|
1634
1975
|
// A local function can hand its return value to another renderer. Follow every
|
|
1635
1976
|
// use of its binding, including uses of a component that also has an export.
|
|
1636
1977
|
while (pending.length) {
|
|
@@ -1641,7 +1982,7 @@ class ModuleTransform {
|
|
|
1641
1982
|
(ts.isFunctionExpression(fn) && fn.name))
|
|
1642
1983
|
return false;
|
|
1643
1984
|
if (proven.has(fn)) {
|
|
1644
|
-
|
|
1985
|
+
reachesRenderer = true;
|
|
1645
1986
|
continue;
|
|
1646
1987
|
}
|
|
1647
1988
|
if (seen.has(fn))
|
|
@@ -1649,7 +1990,7 @@ class ModuleTransform {
|
|
|
1649
1990
|
seen.add(fn);
|
|
1650
1991
|
const parent = fn.parent;
|
|
1651
1992
|
if (ts.isExportAssignment(parent) && parent.parent === this.sf) {
|
|
1652
|
-
|
|
1993
|
+
reachesRenderer = true;
|
|
1653
1994
|
continue;
|
|
1654
1995
|
}
|
|
1655
1996
|
const variable = ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name) ? parent : null;
|
|
@@ -1663,27 +2004,31 @@ class ModuleTransform {
|
|
|
1663
2004
|
if (!refs.length && !directExport)
|
|
1664
2005
|
return false;
|
|
1665
2006
|
if (directExport)
|
|
1666
|
-
|
|
2007
|
+
reachesRenderer = true;
|
|
1667
2008
|
for (const ref of refs) {
|
|
1668
2009
|
const tag = ref.parent;
|
|
1669
2010
|
if (ts.isExportSpecifier(tag) || ts.isExportAssignment(tag)) {
|
|
1670
|
-
|
|
2011
|
+
reachesRenderer = true;
|
|
1671
2012
|
continue;
|
|
1672
2013
|
}
|
|
1673
2014
|
if (!(ts.isJsxSelfClosingElement(tag) ||
|
|
1674
2015
|
ts.isJsxOpeningElement(tag) ||
|
|
1675
2016
|
ts.isJsxClosingElement(tag)) ||
|
|
1676
|
-
tag.tagName !== ref)
|
|
1677
|
-
|
|
2017
|
+
tag.tagName !== ref) {
|
|
2018
|
+
if (!unproven || !this.nativeSvgServerEntry(ref))
|
|
2019
|
+
return false;
|
|
2020
|
+
reachesRenderer = true;
|
|
2021
|
+
continue;
|
|
2022
|
+
}
|
|
1678
2023
|
const owner = nativeOutputOwner(ts.isJsxSelfClosingElement(tag) ? tag : tag.parent, slot, destination);
|
|
1679
2024
|
if (!owner)
|
|
1680
2025
|
return false;
|
|
1681
2026
|
pending.push(owner);
|
|
1682
2027
|
}
|
|
1683
2028
|
}
|
|
1684
|
-
if (
|
|
2029
|
+
if (reachesRenderer)
|
|
1685
2030
|
proven.add(first);
|
|
1686
|
-
return
|
|
2031
|
+
return reachesRenderer;
|
|
1687
2032
|
}
|
|
1688
2033
|
/** Keep the imported React component as the type of the element the service receives. */
|
|
1689
2034
|
storeReactDescriptor(element) {
|
|
@@ -1722,17 +2067,29 @@ class ModuleTransform {
|
|
|
1722
2067
|
}
|
|
1723
2068
|
/**
|
|
1724
2069
|
* Whether a React component's element can be the root of a `ReactCompat`
|
|
1725
|
-
* island:
|
|
1726
|
-
* holds an element
|
|
1727
|
-
* by its type.
|
|
2070
|
+
* island: its children are text or a single primitive literal, and no prop
|
|
2071
|
+
* holds an element here, through a local value, or by its type.
|
|
1728
2072
|
*/
|
|
1729
2073
|
hostsInReact(element) {
|
|
1730
2074
|
// By type: a cast to `any` or `ReactNode` proves nothing.
|
|
1731
2075
|
const text = (child) => ts.isJsxText(child) ||
|
|
1732
2076
|
(ts.isJsxExpression(child) &&
|
|
1733
2077
|
(!child.expression || textKind(this.typeOf(child.expression)) !== 0));
|
|
1734
|
-
if (ts.isJsxElement(element) && !element.children.every(text))
|
|
1735
|
-
|
|
2078
|
+
if (ts.isJsxElement(element) && !element.children.every(text)) {
|
|
2079
|
+
// Mixed text and expression children can be normalized differently by JSX emitters.
|
|
2080
|
+
const [child, extra] = withoutJsxIndentation(element.children).filter((node) => !ts.isJsxExpression(node) || node.expression);
|
|
2081
|
+
if (extra ||
|
|
2082
|
+
!child ||
|
|
2083
|
+
!ts.isJsxExpression(child) ||
|
|
2084
|
+
!child.expression ||
|
|
2085
|
+
child.dotDotDotToken)
|
|
2086
|
+
return false;
|
|
2087
|
+
const kind = skipWrappers(child.expression).kind;
|
|
2088
|
+
if (kind !== ts.SyntaxKind.TrueKeyword &&
|
|
2089
|
+
kind !== ts.SyntaxKind.FalseKeyword &&
|
|
2090
|
+
kind !== ts.SyntaxKind.NullKeyword)
|
|
2091
|
+
return false;
|
|
2092
|
+
}
|
|
1736
2093
|
const attributes = ts.isJsxElement(element)
|
|
1737
2094
|
? element.openingElement.attributes
|
|
1738
2095
|
: element.attributes;
|
|
@@ -2223,6 +2580,10 @@ class ModuleTransform {
|
|
|
2223
2580
|
calleeName(node) {
|
|
2224
2581
|
return this.callee(node)?.name ?? null;
|
|
2225
2582
|
}
|
|
2583
|
+
reactHook(node) {
|
|
2584
|
+
const callee = this.callee(node);
|
|
2585
|
+
return callee?.module === 'react' ? callee.name : null;
|
|
2586
|
+
}
|
|
2226
2587
|
callee(node) {
|
|
2227
2588
|
if (ts.isIdentifier(node)) {
|
|
2228
2589
|
const entry = this.importOf(node);
|
|
@@ -2236,8 +2597,60 @@ class ModuleTransform {
|
|
|
2236
2597
|
return null;
|
|
2237
2598
|
return { name: node.name.text, module: entry.module };
|
|
2238
2599
|
}
|
|
2600
|
+
if (ts.isElementAccessExpression(node) && ts.isStringLiteralLike(node.argumentExpression)) {
|
|
2601
|
+
const name = node.argumentExpression.text;
|
|
2602
|
+
if (this.canMigrateComputedReactCall(node, name))
|
|
2603
|
+
return { name, module: 'react' };
|
|
2604
|
+
}
|
|
2239
2605
|
return null;
|
|
2240
2606
|
}
|
|
2607
|
+
canMigrateComputedReactCall(node, name) {
|
|
2608
|
+
const parent = node.parent;
|
|
2609
|
+
if (!isComputedReactHookOrRef(name) ||
|
|
2610
|
+
name === 'useFormState' ||
|
|
2611
|
+
name === 'useFormStatus' ||
|
|
2612
|
+
!ts.isCallExpression(parent) ||
|
|
2613
|
+
parent.expression !== node ||
|
|
2614
|
+
ts.isOptionalChain(parent) ||
|
|
2615
|
+
!ts.isIdentifier(node.expression))
|
|
2616
|
+
return false;
|
|
2617
|
+
const declaration = this.declarationOf(node.expression);
|
|
2618
|
+
if (!declaration ||
|
|
2619
|
+
!ts.isNamespaceImport(declaration) ||
|
|
2620
|
+
this.imports.get(declaration)?.module !== 'react' ||
|
|
2621
|
+
declaration.parent.isTypeOnly ||
|
|
2622
|
+
declaration.parent.parent.attributes ||
|
|
2623
|
+
this.lostComments(node.getStart(this.sf), node.end, '').length)
|
|
2624
|
+
return false;
|
|
2625
|
+
return this.closedReactNamespace(declaration);
|
|
2626
|
+
}
|
|
2627
|
+
reactNamespaceCalls;
|
|
2628
|
+
closedReactNamespace(declaration) {
|
|
2629
|
+
// Bundlers can expose the mutable CommonJS object through a default import
|
|
2630
|
+
// or the namespace's default member. Require the namespace to stay local.
|
|
2631
|
+
return (this.reactNamespaceCalls ??=
|
|
2632
|
+
[...this.imports].every(([binding, item]) => item.module !== 'react' ||
|
|
2633
|
+
item.typeOnly ||
|
|
2634
|
+
binding === declaration ||
|
|
2635
|
+
(!item.namespace && item.imported !== 'default')) &&
|
|
2636
|
+
!(/eval|\\u/.test(this.source) && hasEval(this.sf)) &&
|
|
2637
|
+
this.namespaceMemberCalls(declaration));
|
|
2638
|
+
}
|
|
2639
|
+
namespaceMemberCalls(declaration) {
|
|
2640
|
+
return this.localUses(declaration.name).every((ref) => {
|
|
2641
|
+
if (inTypeNode(ref))
|
|
2642
|
+
return true;
|
|
2643
|
+
const member = ref.parent;
|
|
2644
|
+
return ((ts.isPropertyAccessExpression(member) || ts.isElementAccessExpression(member)) &&
|
|
2645
|
+
member.expression === ref &&
|
|
2646
|
+
ts.isCallExpression(member.parent) &&
|
|
2647
|
+
member.parent.expression === member &&
|
|
2648
|
+
(ts.isPropertyAccessExpression(member)
|
|
2649
|
+
? member.name.text !== 'default'
|
|
2650
|
+
: ts.isStringLiteralLike(member.argumentExpression) &&
|
|
2651
|
+
member.argumentExpression.text !== 'default'));
|
|
2652
|
+
});
|
|
2653
|
+
}
|
|
2241
2654
|
line(node) {
|
|
2242
2655
|
return this.sf.getLineAndCharacterOfPosition(node.getStart(this.sf)).line + 1;
|
|
2243
2656
|
}
|
|
@@ -2262,8 +2675,17 @@ class ModuleTransform {
|
|
|
2262
2675
|
}
|
|
2263
2676
|
// A React import of the same name becomes this import.
|
|
2264
2677
|
const prior = this.apiLocals.get(name);
|
|
2265
|
-
const
|
|
2266
|
-
|
|
2678
|
+
const originalFactory = module === 'octane' && name === 'createElement' && prior?.module === 'react';
|
|
2679
|
+
const canReuse = prior &&
|
|
2680
|
+
mapReactImport(prior.module, prior.imported, prior.typeOnly)?.name === name &&
|
|
2681
|
+
![...this.imports].some(([decl, entry]) => entry === prior &&
|
|
2682
|
+
ts.isImportSpecifier(decl) &&
|
|
2683
|
+
(decl.parent.parent.parent.attributes ||
|
|
2684
|
+
(entry.module === 'react' &&
|
|
2685
|
+
entry.imported === 'Touch' &&
|
|
2686
|
+
this.localUses(decl.name).some(needsTouchValueOrNamespace)) ||
|
|
2687
|
+
(originalFactory && this.retainedReactFactoryImport(decl))));
|
|
2688
|
+
const collides = (this.declared.has(name) || this.readsGlobal(name)) && !canReuse;
|
|
2267
2689
|
let local = collides ? `Octane${name[0].toUpperCase()}${name.slice(1)}` : name;
|
|
2268
2690
|
while (collides && (this.declared.has(local) || this.readsGlobal(local)))
|
|
2269
2691
|
local += '_';
|
|
@@ -2372,11 +2794,16 @@ class ModuleTransform {
|
|
|
2372
2794
|
if (linked)
|
|
2373
2795
|
return linked;
|
|
2374
2796
|
}
|
|
2375
|
-
|
|
2376
|
-
|
|
2797
|
+
if (this.strong && this.target === 'tsrx' && ts.isVariableStatement(node)) {
|
|
2798
|
+
const state = this.nativeRefState(node);
|
|
2799
|
+
if (state)
|
|
2800
|
+
return state;
|
|
2801
|
+
}
|
|
2802
|
+
const effectEdit = this.effectEdits?.get(node);
|
|
2803
|
+
if (effectEdit)
|
|
2377
2804
|
return ts.isCallExpression(node)
|
|
2378
|
-
?
|
|
2379
|
-
:
|
|
2805
|
+
? effectEdit + this.span(node, node.expression.end, node.end, ctx)
|
|
2806
|
+
: effectEdit;
|
|
2380
2807
|
const stored = ts.isArrowFunction(node) ? this.snapshotCaptures?.get(node) : undefined;
|
|
2381
2808
|
const aliases = this.effectAliases;
|
|
2382
2809
|
if (aliases || stored) {
|
|
@@ -2395,10 +2822,7 @@ class ModuleTransform {
|
|
|
2395
2822
|
?.declarations?.some((declaration) => this.imports.get(declaration) === importedEvent));
|
|
2396
2823
|
let layoutEvent = false;
|
|
2397
2824
|
const captures = aliases
|
|
2398
|
-
? effectReceivers(node, this.checker, (name) =>
|
|
2399
|
-
const hook = this.callee(name);
|
|
2400
|
-
return hook?.module === 'react' ? hook.name : null;
|
|
2401
|
-
}, aliases, snapshots &&
|
|
2825
|
+
? effectReceivers(node, this.checker, (name) => this.reactHook(name), aliases, snapshots &&
|
|
2402
2826
|
((statement, member, name) => {
|
|
2403
2827
|
if (aliases.has(member))
|
|
2404
2828
|
return;
|
|
@@ -2413,8 +2837,12 @@ class ModuleTransform {
|
|
|
2413
2837
|
snapshots.get(statement).push(`const ${name} = ${event}(${setup});`);
|
|
2414
2838
|
layoutEvent = true;
|
|
2415
2839
|
}
|
|
2416
|
-
: undefined
|
|
2840
|
+
: undefined)
|
|
2417
2841
|
: [];
|
|
2842
|
+
for (const statement of this.effectHelperCaptures?.get(node) ?? []) {
|
|
2843
|
+
const declaration = this.span(statement, statement.getStart(this.sf), statement.end, PLAIN);
|
|
2844
|
+
captures.push(declaration.endsWith(';') ? declaration : declaration + ';');
|
|
2845
|
+
}
|
|
2418
2846
|
if (stored)
|
|
2419
2847
|
captures.unshift(...stored);
|
|
2420
2848
|
if (captures.length) {
|
|
@@ -2452,14 +2880,34 @@ class ModuleTransform {
|
|
|
2452
2880
|
return this.rewriteReexport(node);
|
|
2453
2881
|
if (ts.isCallExpression(node))
|
|
2454
2882
|
return this.rewriteCall(node);
|
|
2883
|
+
if (ts.isAwaitExpression(node) && ts.isPropertyAccessExpression(node.expression))
|
|
2884
|
+
this.checkServerStream(node);
|
|
2455
2885
|
if (ts.isPropertyAccessExpression(node)) {
|
|
2456
2886
|
const text = this.rewriteMember(node);
|
|
2457
2887
|
if (text === null)
|
|
2458
2888
|
this.checkProjectReactEventMember(node, node.name.text);
|
|
2459
2889
|
return text;
|
|
2460
2890
|
}
|
|
2461
|
-
if (ts.isElementAccessExpression(node) && ts.isStringLiteralLike(node.argumentExpression))
|
|
2462
|
-
|
|
2891
|
+
if (ts.isElementAccessExpression(node) && ts.isStringLiteralLike(node.argumentExpression)) {
|
|
2892
|
+
const name = node.argumentExpression.text;
|
|
2893
|
+
const mapped = this.checkReactNamespaceMember(node, name);
|
|
2894
|
+
const module = mapped && this.importOf(node.expression)?.module;
|
|
2895
|
+
const formHook = name === 'useFormState' || name === 'useFormStatus';
|
|
2896
|
+
if (mapped &&
|
|
2897
|
+
(module === 'react' || (module === 'react-dom' && formHook)) &&
|
|
2898
|
+
isComputedReactHookOrRef(name)) {
|
|
2899
|
+
if (this.canMigrateComputedReactCall(node, name)) {
|
|
2900
|
+
this.count(RULE.import);
|
|
2901
|
+
return this.require(mapped.module, mapped.name, false);
|
|
2902
|
+
}
|
|
2903
|
+
this.note(RULE.unsupported, 'error', node, formHook && module === 'react'
|
|
2904
|
+
? `React DOM exports \`${name}\`. Check this computed React namespace access before migrating it.`
|
|
2905
|
+
: formHook
|
|
2906
|
+
? `Import React DOM's \`${name}\` by name before migrating this computed access.`
|
|
2907
|
+
: `Computed React \`${name}\` migration requires a direct call on a local React namespace import. Review the receiver and the call before migrating it.`);
|
|
2908
|
+
}
|
|
2909
|
+
this.checkProjectReactEventMember(node, name);
|
|
2910
|
+
}
|
|
2463
2911
|
if (ts.isQualifiedName(node))
|
|
2464
2912
|
return this.rewriteQualifiedType(node);
|
|
2465
2913
|
if (ts.isImportTypeNode(node))
|
|
@@ -2521,7 +2969,7 @@ class ModuleTransform {
|
|
|
2521
2969
|
const name = ts.isPropertyAccessExpression(tag) ? tag.name.text : imported?.imported;
|
|
2522
2970
|
if (this.options.reactIntlAdapter &&
|
|
2523
2971
|
imported?.module === 'react-intl' &&
|
|
2524
|
-
|
|
2972
|
+
INTL_FORMATTER.test(name ?? '')) {
|
|
2525
2973
|
const boundary = this.require(this.options.reactIntlAdapter, 'IntlBoundary', false);
|
|
2526
2974
|
const contexts = this.require(this.options.reactIntlAdapter, 'intlContexts', false);
|
|
2527
2975
|
this.count('react-intl');
|
|
@@ -2569,7 +3017,176 @@ class ModuleTransform {
|
|
|
2569
3017
|
}
|
|
2570
3018
|
return null;
|
|
2571
3019
|
}
|
|
3020
|
+
retainedRegistryFactoryImport(react) {
|
|
3021
|
+
const resolveRegistry = this.options.retainedRegistry;
|
|
3022
|
+
if (!resolveRegistry)
|
|
3023
|
+
return false;
|
|
3024
|
+
// Check the registration shape without the inert exported constants.
|
|
3025
|
+
const statements = this.sf.statements.filter((statement) => {
|
|
3026
|
+
if (!ts.isVariableStatement(statement) ||
|
|
3027
|
+
statement.modifiers?.length !== 1 ||
|
|
3028
|
+
statement.modifiers[0].kind !== ts.SyntaxKind.ExportKeyword ||
|
|
3029
|
+
(statement.declarationList.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
3030
|
+
statement.declarationList.declarations.length !== 1)
|
|
3031
|
+
return true;
|
|
3032
|
+
const { name, initializer, type } = statement.declarationList.declarations[0];
|
|
3033
|
+
return !ts.isIdentifier(name) || !!type || !initializer || !isPrimitiveLiteral(initializer);
|
|
3034
|
+
});
|
|
3035
|
+
const bindings = react.importClause?.namedBindings;
|
|
3036
|
+
if (statements.length !== 3 ||
|
|
3037
|
+
!statements.slice(0, 2).every(ts.isImportDeclaration) ||
|
|
3038
|
+
react.attributes ||
|
|
3039
|
+
react.importClause?.name ||
|
|
3040
|
+
!bindings ||
|
|
3041
|
+
!ts.isNamedImports(bindings) ||
|
|
3042
|
+
bindings.elements.length !== 1 ||
|
|
3043
|
+
!this.options.originalReact)
|
|
3044
|
+
return false;
|
|
3045
|
+
const binding = this.importOf(bindings.elements[0].name);
|
|
3046
|
+
if (!binding || binding.typeOnly || binding.imported !== 'createElement')
|
|
3047
|
+
return false;
|
|
3048
|
+
// Only a registration module can keep the descriptor without also moving
|
|
3049
|
+
// a consumer of it in the same file.
|
|
3050
|
+
const declaration = statements[2];
|
|
3051
|
+
if (!ts.isVariableStatement(declaration) ||
|
|
3052
|
+
!(declaration.declarationList.flags & ts.NodeFlags.Const))
|
|
3053
|
+
return false;
|
|
3054
|
+
const [entry, extra] = declaration.declarationList.declarations;
|
|
3055
|
+
const use = entry?.initializer;
|
|
3056
|
+
if (extra ||
|
|
3057
|
+
!entry ||
|
|
3058
|
+
!ts.isIdentifier(entry.name) ||
|
|
3059
|
+
entry.type ||
|
|
3060
|
+
!use ||
|
|
3061
|
+
!ts.isCallExpression(use) ||
|
|
3062
|
+
use.questionDotToken ||
|
|
3063
|
+
use.typeArguments ||
|
|
3064
|
+
!ts.isIdentifier(use.expression))
|
|
3065
|
+
return false;
|
|
3066
|
+
const owner = this.importOf(use.expression);
|
|
3067
|
+
if (!owner || owner.namespace || owner.typeOnly)
|
|
3068
|
+
return false;
|
|
3069
|
+
const [name, object, trailing] = use.arguments;
|
|
3070
|
+
if (!name ||
|
|
3071
|
+
!ts.isStringLiteral(name) ||
|
|
3072
|
+
!object ||
|
|
3073
|
+
!ts.isObjectLiteralExpression(object) ||
|
|
3074
|
+
trailing ||
|
|
3075
|
+
object.properties.length !== 1)
|
|
3076
|
+
return false;
|
|
3077
|
+
const [property] = object.properties;
|
|
3078
|
+
if (!ts.isPropertyAssignment(property) || !ts.isIdentifier(property.name))
|
|
3079
|
+
return false;
|
|
3080
|
+
const arrow = property.initializer;
|
|
3081
|
+
if (!ts.isArrowFunction(arrow) ||
|
|
3082
|
+
arrow.type ||
|
|
3083
|
+
arrow.modifiers ||
|
|
3084
|
+
arrow.typeParameters ||
|
|
3085
|
+
arrow.parameters.length)
|
|
3086
|
+
return false;
|
|
3087
|
+
const output = singleCallbackExpression(arrow.body);
|
|
3088
|
+
if (!output)
|
|
3089
|
+
return false;
|
|
3090
|
+
const call = skipParens(output);
|
|
3091
|
+
if (!ts.isCallExpression(call) ||
|
|
3092
|
+
call.questionDotToken ||
|
|
3093
|
+
call.typeArguments ||
|
|
3094
|
+
this.importOf(call.expression) !== binding)
|
|
3095
|
+
return false;
|
|
3096
|
+
const [tag, props, ...children] = call.arguments;
|
|
3097
|
+
if (!tag ||
|
|
3098
|
+
!ts.isStringLiteral(tag) ||
|
|
3099
|
+
(props &&
|
|
3100
|
+
props.kind !== ts.SyntaxKind.NullKeyword &&
|
|
3101
|
+
(!ts.isObjectLiteralExpression(props) ||
|
|
3102
|
+
!props.properties.every((item) => ts.isPropertyAssignment(item) &&
|
|
3103
|
+
ts.isIdentifier(item.name) &&
|
|
3104
|
+
ts.isStringLiteral(item.initializer)))) ||
|
|
3105
|
+
!children.every(ts.isStringLiteral))
|
|
3106
|
+
return false;
|
|
3107
|
+
const types = this.options.types;
|
|
3108
|
+
if (!types || types.sourceFile.text !== this.source)
|
|
3109
|
+
return false;
|
|
3110
|
+
const file = resolveRegistry(owner.module);
|
|
3111
|
+
if (!file)
|
|
3112
|
+
return false;
|
|
3113
|
+
const { sourceFile, checker } = types;
|
|
3114
|
+
const peer = nodeSpanning(sourceFile, call.getStart(this.sf), call.end);
|
|
3115
|
+
const callee = nodeSpanning(sourceFile, use.expression.getStart(this.sf), use.expression.end);
|
|
3116
|
+
const specifier = nodeSpanning(sourceFile, react.moduleSpecifier.getStart(this.sf), react.moduleSpecifier.end);
|
|
3117
|
+
if (!peer || !ts.isCallExpression(peer) || !callee || !specifier)
|
|
3118
|
+
return false;
|
|
3119
|
+
const signatures = checker.getTypeAtLocation(callee).getCallSignatures();
|
|
3120
|
+
const signature = signatures[0];
|
|
3121
|
+
const decl = signature?.declaration;
|
|
3122
|
+
if (signatures.length !== 1 ||
|
|
3123
|
+
signature.typeParameters?.length ||
|
|
3124
|
+
!decl ||
|
|
3125
|
+
!ts.isFunctionDeclaration(decl) ||
|
|
3126
|
+
!decl.body ||
|
|
3127
|
+
decl.getSourceFile() !== file ||
|
|
3128
|
+
!(ts.getCombinedModifierFlags(decl) & ts.ModifierFlags.Export))
|
|
3129
|
+
return false;
|
|
3130
|
+
const module = checker.getSymbolAtLocation(specifier);
|
|
3131
|
+
const reactNode = module && checker.getExportsOfModule(module).find((symbol) => symbol.name === 'ReactNode');
|
|
3132
|
+
return !!reactNode && checker.getContextualType(peer)?.aliasSymbol === reactNode;
|
|
3133
|
+
}
|
|
2572
3134
|
// ---------------------------------------------------------------- imports
|
|
3135
|
+
retainedHookSetup() {
|
|
3136
|
+
return {
|
|
3137
|
+
calleeName: (callee) => this.calleeName(callee),
|
|
3138
|
+
nativeOutput: (element) => this.nativeOutput(element, this.localUses),
|
|
3139
|
+
originalReact: () => this.options.originalReact === true,
|
|
3140
|
+
};
|
|
3141
|
+
}
|
|
3142
|
+
warnRetainedPrivateContext(module, name, site) {
|
|
3143
|
+
const resolution = module.endsWith('.js')
|
|
3144
|
+
? ' and the import resolves to its TypeScript source'
|
|
3145
|
+
: '';
|
|
3146
|
+
this.note(RULE.bindings, 'warning', site, `If native setup reaches the unchanged ${module} export ${name} after its module initializes${resolution}, its private helper calls React's useContext. Provide a compiled Octane hook or keep the component under React.`);
|
|
3147
|
+
}
|
|
3148
|
+
warnRetainedStore(module, name, site) {
|
|
3149
|
+
this.note(RULE.bindings, 'warning', site, `If native setup reaches the unchanged ${module} export ${name} through the original workspace entry, its store hook calls React. Provide a compiled Octane hook or keep the component under React.`);
|
|
3150
|
+
}
|
|
3151
|
+
checkRetainedNamespaceHooks(module, binding) {
|
|
3152
|
+
const retainedHook = this.options.localReactHook;
|
|
3153
|
+
if (!retainedHook)
|
|
3154
|
+
return;
|
|
3155
|
+
let reported;
|
|
3156
|
+
for (const ref of renderUses(this.localUses(binding))) {
|
|
3157
|
+
const member = ref.parent;
|
|
3158
|
+
if (!ts.isPropertyAccessExpression(member) && !ts.isElementAccessExpression(member))
|
|
3159
|
+
continue;
|
|
3160
|
+
const call = member.parent;
|
|
3161
|
+
if (member.expression !== ref ||
|
|
3162
|
+
ts.isOptionalChain(member) ||
|
|
3163
|
+
!ts.isCallExpression(call) ||
|
|
3164
|
+
call.expression !== member ||
|
|
3165
|
+
ts.isOptionalChain(call))
|
|
3166
|
+
continue;
|
|
3167
|
+
const name = ts.isPropertyAccessExpression(member)
|
|
3168
|
+
? member.name.text
|
|
3169
|
+
: ts.isStringLiteralLike(member.argumentExpression) && member.argumentExpression.text;
|
|
3170
|
+
if (!name || !/^use[A-Z0-9]/.test(name) || reported?.has(name))
|
|
3171
|
+
continue;
|
|
3172
|
+
const setup = this.retainedHookSetup();
|
|
3173
|
+
if (!nativeSetupHook(member, this.localUses, this.checker) &&
|
|
3174
|
+
!nativeSetupHook(member, this.localUses, this.checker, setup) &&
|
|
3175
|
+
!nativeSetupHook(member, this.localUses, this.checker, { ...setup, contextSetup: true }))
|
|
3176
|
+
continue;
|
|
3177
|
+
const local = retainedHook(module, name, false);
|
|
3178
|
+
if (!local || typeof local === 'string')
|
|
3179
|
+
continue;
|
|
3180
|
+
if ('retainedStore' in local)
|
|
3181
|
+
this.warnRetainedStore(module, name, member);
|
|
3182
|
+
else if ('privateContext' in local &&
|
|
3183
|
+
nativeSetupHook(member, this.localUses, this.checker, { ...setup, privateContext: true }))
|
|
3184
|
+
this.warnRetainedPrivateContext(module, name, member);
|
|
3185
|
+
else
|
|
3186
|
+
continue;
|
|
3187
|
+
(reported ??= new Set()).add(name);
|
|
3188
|
+
}
|
|
3189
|
+
}
|
|
2573
3190
|
rewriteImport(node) {
|
|
2574
3191
|
const native = this.pathSvgImports.get(node);
|
|
2575
3192
|
if (native) {
|
|
@@ -2585,15 +3202,43 @@ class ModuleTransform {
|
|
|
2585
3202
|
this.note(RULE.unsupported, 'error', node, "Three.js elements compile only in a `.three.tsrx` module, Octane's three renderer: move the scene components there and mount them with `root.render(Scene, props)`.");
|
|
2586
3203
|
}
|
|
2587
3204
|
if (module in REACT_MODULES) {
|
|
3205
|
+
if (module === 'react' && this.retainedRegistryFactoryImport(node))
|
|
3206
|
+
return null;
|
|
3207
|
+
const clause = node.importClause;
|
|
3208
|
+
const bindings = clause?.namedBindings;
|
|
3209
|
+
if (module === 'react' &&
|
|
3210
|
+
node.attributes &&
|
|
3211
|
+
clause &&
|
|
3212
|
+
bindings &&
|
|
3213
|
+
ts.isNamedImports(bindings)) {
|
|
3214
|
+
const touch = bindings.elements.filter((element) => (clause.isTypeOnly || element.isTypeOnly) &&
|
|
3215
|
+
(element.propertyName ?? element.name).text === 'Touch');
|
|
3216
|
+
// An erased default import may also be written inside a named import list.
|
|
3217
|
+
const defaultTouch = bindings.elements.some((element) => (clause.isTypeOnly || element.isTypeOnly) &&
|
|
3218
|
+
element.propertyName?.text === 'default' &&
|
|
3219
|
+
this.localUses(element.name).some((ref) => directMember(ref)?.text === 'Touch'));
|
|
3220
|
+
if (touch.length > 0) {
|
|
3221
|
+
// Preserve the authored resolution condition and its React export.
|
|
3222
|
+
for (const element of touch) {
|
|
3223
|
+
if (this.localUses(element.name).some(needsTouchValueOrNamespace))
|
|
3224
|
+
this.note(RULE.unsupported, 'error', element, "`Touch` from 'react' has no Octane equivalent for this use.");
|
|
3225
|
+
}
|
|
3226
|
+
return null;
|
|
3227
|
+
}
|
|
3228
|
+
if (defaultTouch)
|
|
3229
|
+
return null;
|
|
3230
|
+
}
|
|
2588
3231
|
this.importSlots.push(node);
|
|
2589
3232
|
return `\u0000IMPORT${this.importSlots.length - 1}\u0000`;
|
|
2590
3233
|
}
|
|
2591
3234
|
const clause = node.importClause;
|
|
2592
3235
|
const bindings = clause?.namedBindings;
|
|
2593
|
-
if (bindings &&
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
3236
|
+
if (clause && bindings && ts.isNamespaceImport(bindings)) {
|
|
3237
|
+
if (this.radixSlotImports?.get(bindings.name) === 'original')
|
|
3238
|
+
return null;
|
|
3239
|
+
if (!clause.isTypeOnly && !node.attributes)
|
|
3240
|
+
this.checkRetainedNamespaceHooks(module, bindings.name);
|
|
3241
|
+
}
|
|
2597
3242
|
if (clause && bindings && ts.isNamedImports(bindings)) {
|
|
2598
3243
|
const moved = new Map();
|
|
2599
3244
|
const kept = [];
|
|
@@ -2620,20 +3265,35 @@ class ModuleTransform {
|
|
|
2620
3265
|
const nativeOnly = sites.length > 0 &&
|
|
2621
3266
|
sites.every(Boolean) &&
|
|
2622
3267
|
(!sites.includes('afterRefs') || this.options.originalReact === true);
|
|
3268
|
+
const setup = this.retainedHookSetup();
|
|
2623
3269
|
const call = refs[sites.indexOf('first')] ??
|
|
2624
3270
|
(nativeOnly
|
|
2625
3271
|
? refs[0]
|
|
2626
|
-
: refs.find((ref) => nativeSetupHook(ref, this.localUses, this.checker,
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
3272
|
+
: refs.find((ref) => nativeSetupHook(ref, this.localUses, this.checker, setup)));
|
|
3273
|
+
const site = call ??
|
|
3274
|
+
refs.find((ref) => nativeSetupHook(ref, this.localUses, this.checker, {
|
|
3275
|
+
...setup,
|
|
3276
|
+
contextSetup: true,
|
|
3277
|
+
}));
|
|
3278
|
+
const local = site && this.options.localReactHook(module, name, !!call && slotKeyed && nativeOnly);
|
|
3279
|
+
if (call && local && typeof local !== 'string' && 'from' in local) {
|
|
2632
3280
|
dest = local.from;
|
|
2633
3281
|
this.count('native-external-store');
|
|
2634
3282
|
}
|
|
2635
|
-
else if (
|
|
2636
|
-
this.note(RULE.bindings, 'error', call, `
|
|
3283
|
+
else if (call && (local === 'useEffect' || local === 'useEffectEvent'))
|
|
3284
|
+
this.note(RULE.bindings, 'error', call, `If setup reaches React's ${local} in the unchanged ${module} export ${name}, the call requires a React renderer. Provide a compiled Octane hook for native callers or keep this component under a React renderer.`);
|
|
3285
|
+
else if (site && typeof local === 'string' && (call || local === 'useContext'))
|
|
3286
|
+
this.note(RULE.bindings, 'error', site, `The unchanged ${module} export ${name} calls ${local === 'useIntl' ? "React Intl's" : "React's"} ${local}. Provide a compiled Octane hook for native callers or keep this component under a React renderer.`);
|
|
3287
|
+
else if (local && typeof local !== 'string' && 'privateContext' in local) {
|
|
3288
|
+
const advisorySite = refs.find((ref) => nativeSetupHook(ref, this.localUses, this.checker, {
|
|
3289
|
+
...setup,
|
|
3290
|
+
privateContext: true,
|
|
3291
|
+
}));
|
|
3292
|
+
if (advisorySite)
|
|
3293
|
+
this.warnRetainedPrivateContext(module, name, advisorySite);
|
|
3294
|
+
}
|
|
3295
|
+
else if (site && local && typeof local !== 'string' && 'retainedStore' in local)
|
|
3296
|
+
this.warnRetainedStore(module, name, site);
|
|
2637
3297
|
}
|
|
2638
3298
|
if (!dest) {
|
|
2639
3299
|
kept.push(item);
|
|
@@ -2680,7 +3340,7 @@ class ModuleTransform {
|
|
|
2680
3340
|
if (!port ||
|
|
2681
3341
|
ts
|
|
2682
3342
|
.getLeadingCommentRanges(this.source, node.pos)
|
|
2683
|
-
?.some((
|
|
3343
|
+
?.some(({ pos, end }) => /(?:No Octane port|Retained React import) of/.test(this.source.slice(pos, end))))
|
|
2684
3344
|
return text;
|
|
2685
3345
|
return `// ${portComment(port)}\n${text ?? node.getText(this.sf)}`;
|
|
2686
3346
|
}
|
|
@@ -2704,6 +3364,16 @@ class ModuleTransform {
|
|
|
2704
3364
|
const type = clause.isTypeOnly ? 'type ' : '';
|
|
2705
3365
|
const bindings = ts.isImportClause(clause) ? clause.namedBindings : clause.exportClause;
|
|
2706
3366
|
if (bindings && ts.isNamespaceImport(bindings)) {
|
|
3367
|
+
if (next === '@octanejs/remix-router' &&
|
|
3368
|
+
clause.isTypeOnly &&
|
|
3369
|
+
this.localUses(bindings.name).some((ref) => {
|
|
3370
|
+
const member = ref.parent;
|
|
3371
|
+
return (ts.isQualifiedName(member) &&
|
|
3372
|
+
member.left === ref &&
|
|
3373
|
+
!isValueTypeQuery(member) &&
|
|
3374
|
+
routerLoadContext(module, member.right.text));
|
|
3375
|
+
}))
|
|
3376
|
+
return node.getText(this.sf);
|
|
2707
3377
|
if (namespace === null)
|
|
2708
3378
|
return null;
|
|
2709
3379
|
const local = bindings.name.text;
|
|
@@ -2715,10 +3385,19 @@ class ModuleTransform {
|
|
|
2715
3385
|
const kept = [];
|
|
2716
3386
|
const values = [];
|
|
2717
3387
|
const types = [];
|
|
3388
|
+
let retainedRouterTypes;
|
|
2718
3389
|
let renamed = false;
|
|
2719
3390
|
for (const element of bindings.elements) {
|
|
2720
3391
|
const imported = (element.propertyName ?? element.name).text;
|
|
2721
3392
|
const local = element.name.text;
|
|
3393
|
+
if (next === '@octanejs/remix-router' &&
|
|
3394
|
+
(clause.isTypeOnly || element.isTypeOnly) &&
|
|
3395
|
+
routerLoadContext(module, imported)) {
|
|
3396
|
+
(retainedRouterTypes ??= new Set()).add(element);
|
|
3397
|
+
const name = element.propertyName ?? element.name;
|
|
3398
|
+
kept.push(this.source.slice(name.getStart(this.sf), element.end));
|
|
3399
|
+
continue;
|
|
3400
|
+
}
|
|
2722
3401
|
if (module === 'radix-ui' && imported === 'Slot' && next === '@octanejs/radix') {
|
|
2723
3402
|
if (!ts.isImportSpecifier(element)) {
|
|
2724
3403
|
this.note(RULE.binding, 'error', element, 'The Octane binding has no Radix Slot namespace to re-export.');
|
|
@@ -2756,8 +3435,19 @@ class ModuleTransform {
|
|
|
2756
3435
|
}
|
|
2757
3436
|
if (namespace === null && kept.length === 0 && !renamed)
|
|
2758
3437
|
return null;
|
|
3438
|
+
if (retainedRouterTypes) {
|
|
3439
|
+
if (values.length === 0 || node.attributes)
|
|
3440
|
+
return node.getText(this.sf);
|
|
3441
|
+
const available = this.options.bindingExports?.(module, next);
|
|
3442
|
+
if (available && !this.hasBindingExports(node, next, available, retainedRouterTypes))
|
|
3443
|
+
return node.getText(this.sf);
|
|
3444
|
+
}
|
|
2759
3445
|
this.count(RULE.import);
|
|
2760
|
-
const lines = kept.length > 0
|
|
3446
|
+
const lines = kept.length > 0
|
|
3447
|
+
? [
|
|
3448
|
+
`${keyword} ${retainedRouterTypes ? 'type ' : type}{ ${kept.join(', ')} } ${from(module)}`,
|
|
3449
|
+
]
|
|
3450
|
+
: [];
|
|
2761
3451
|
if (values.length > 0) {
|
|
2762
3452
|
lines.push(namespace === null
|
|
2763
3453
|
? `${keyword} ${type}{ ${values.join(', ')} } ${from(next)}`
|
|
@@ -2803,7 +3493,9 @@ class ModuleTransform {
|
|
|
2803
3493
|
for (const [index, item] of exports.elements.entries()) {
|
|
2804
3494
|
const text = this.source.slice(item.getFullStart(), exports.elements[index + 1]?.getFullStart() ?? exports.end - 1);
|
|
2805
3495
|
const name = (item.propertyName ?? item.name).text;
|
|
2806
|
-
|
|
3496
|
+
const typeOnly = node.isTypeOnly || item.isTypeOnly;
|
|
3497
|
+
const mapped = mapReactImport(module, name, typeOnly);
|
|
3498
|
+
if (mapped?.module === module) {
|
|
2807
3499
|
retained.push(text);
|
|
2808
3500
|
continue;
|
|
2809
3501
|
}
|
|
@@ -2814,6 +3506,10 @@ class ModuleTransform {
|
|
|
2814
3506
|
this.note(RULE.unsupported, 'error', item, 'Review the React event re-export and define an explicit native event type.');
|
|
2815
3507
|
}
|
|
2816
3508
|
else {
|
|
3509
|
+
if (typeOnly &&
|
|
3510
|
+
mapped?.name !== name &&
|
|
3511
|
+
mapReactImport(module, name, false)?.name !== name)
|
|
3512
|
+
this.note(REACT_TYPE_PROJECTION, 'error', item, `Octane has no export \`${name}\`.`);
|
|
2817
3513
|
migrated.push(text);
|
|
2818
3514
|
}
|
|
2819
3515
|
}
|
|
@@ -2842,6 +3538,16 @@ class ModuleTransform {
|
|
|
2842
3538
|
const next = this.resolve(specifier.text);
|
|
2843
3539
|
if (next === null || next === specifier.text)
|
|
2844
3540
|
return null;
|
|
3541
|
+
if (next === '@octanejs/remix-router' &&
|
|
3542
|
+
ts.isModuleDeclaration(node) &&
|
|
3543
|
+
node.body &&
|
|
3544
|
+
ts.isModuleBlock(node.body) &&
|
|
3545
|
+
node.body.statements.some((statement) => ts.isInterfaceDeclaration(statement) &&
|
|
3546
|
+
routerLoadContext(specifier.text, statement.name.text)))
|
|
3547
|
+
return null;
|
|
3548
|
+
const available = this.options.bindingExports?.(specifier.text, next);
|
|
3549
|
+
if (available && !this.hasBindingExports(node, next, available))
|
|
3550
|
+
return null;
|
|
2845
3551
|
const namespace = radixNamespace(specifier.text);
|
|
2846
3552
|
if (namespace !== null) {
|
|
2847
3553
|
// A re-export, `import()`, or type of the package would be the whole binding.
|
|
@@ -2860,9 +3566,82 @@ class ModuleTransform {
|
|
|
2860
3566
|
return null;
|
|
2861
3567
|
}
|
|
2862
3568
|
}
|
|
3569
|
+
if (available)
|
|
3570
|
+
this.options.recordBindingRewrite?.(specifier.text, next);
|
|
2863
3571
|
this.count(RULE.import);
|
|
2864
3572
|
return this.replaceSpecifier(node, specifier, next);
|
|
2865
3573
|
}
|
|
3574
|
+
/** Check known exports without guessing at a whole namespace. */
|
|
3575
|
+
hasBindingExports(node, binding, available, retained) {
|
|
3576
|
+
const hasDifferentType = (name) => binding === '@octanejs/motion' && name === 'MotionConfigContext';
|
|
3577
|
+
const check = (site, name, needsType = false) => {
|
|
3578
|
+
const missingType = needsType && hasDifferentType(name);
|
|
3579
|
+
if (name !== undefined && available.has(name) && !missingType)
|
|
3580
|
+
return true;
|
|
3581
|
+
this.note(RULE.binding, 'error', site, name === undefined
|
|
3582
|
+
? `\`${binding}\` exposes a subset of the original module. Import specific supported exports before migrating this reference.`
|
|
3583
|
+
: missingType
|
|
3584
|
+
? `\`${binding}\` does not export the React Motion \`${name}\` type. Review the context fields before migrating this reference.`
|
|
3585
|
+
: `\`${binding}\` has no \`${name}\` export.`);
|
|
3586
|
+
return false;
|
|
3587
|
+
};
|
|
3588
|
+
const checkNamed = (elements) => {
|
|
3589
|
+
if (!elements.length)
|
|
3590
|
+
return check(node);
|
|
3591
|
+
let supported = true;
|
|
3592
|
+
for (const element of elements) {
|
|
3593
|
+
if (retained?.has(element))
|
|
3594
|
+
continue;
|
|
3595
|
+
const name = (element.propertyName ?? element.name).text;
|
|
3596
|
+
// A re-export can expose both the type and the value to another module.
|
|
3597
|
+
const typeUse = hasDifferentType(name) &&
|
|
3598
|
+
(ts.isExportSpecifier(element)
|
|
3599
|
+
? element
|
|
3600
|
+
: this.localUses(element.name).find((ref) => ts.isPartOfTypeNode(ref) ||
|
|
3601
|
+
ts.isExportSpecifier(ref.parent) ||
|
|
3602
|
+
ts.isExportAssignment(ref.parent)));
|
|
3603
|
+
if (!check(typeUse || element, name, !!typeUse))
|
|
3604
|
+
supported = false;
|
|
3605
|
+
}
|
|
3606
|
+
return supported;
|
|
3607
|
+
};
|
|
3608
|
+
if (ts.isImportDeclaration(node)) {
|
|
3609
|
+
const clause = node.importClause;
|
|
3610
|
+
if (!clause)
|
|
3611
|
+
return check(node);
|
|
3612
|
+
if (clause.name)
|
|
3613
|
+
return check(clause.name, 'default');
|
|
3614
|
+
const bindings = clause.namedBindings;
|
|
3615
|
+
if (!bindings)
|
|
3616
|
+
return check(node);
|
|
3617
|
+
if (ts.isNamedImports(bindings))
|
|
3618
|
+
return checkNamed(bindings.elements);
|
|
3619
|
+
if ((this.source.includes('eval') || this.source.includes('\\u')) && hasEval(this.sf))
|
|
3620
|
+
return check(bindings);
|
|
3621
|
+
return this.localUses(bindings.name).every((ref) => {
|
|
3622
|
+
const parent = ref.parent;
|
|
3623
|
+
if (ts.isPropertyAccessExpression(parent) && parent.expression === ref)
|
|
3624
|
+
return check(parent.name, parent.name.text, ts.isPartOfTypeNode(parent.name) || ts.isExportAssignment(parent.parent));
|
|
3625
|
+
if (ts.isQualifiedName(parent) && parent.left === ref)
|
|
3626
|
+
return check(parent.right, parent.right.text, ts.isPartOfTypeNode(parent.right) || ts.isImportEqualsDeclaration(parent.parent));
|
|
3627
|
+
if (ts.isElementAccessExpression(parent) &&
|
|
3628
|
+
parent.expression === ref &&
|
|
3629
|
+
ts.isStringLiteralLike(parent.argumentExpression))
|
|
3630
|
+
return check(parent.argumentExpression, parent.argumentExpression.text);
|
|
3631
|
+
return check(ref);
|
|
3632
|
+
});
|
|
3633
|
+
}
|
|
3634
|
+
if (ts.isExportDeclaration(node) && node.exportClause && ts.isNamedExports(node.exportClause))
|
|
3635
|
+
return checkNamed(node.exportClause.elements);
|
|
3636
|
+
if (ts.isImportTypeNode(node)) {
|
|
3637
|
+
let name = node.qualifier;
|
|
3638
|
+
while (name && ts.isQualifiedName(name))
|
|
3639
|
+
name = name.left;
|
|
3640
|
+
if (name)
|
|
3641
|
+
return check(name, name.text, !node.isTypeOf);
|
|
3642
|
+
}
|
|
3643
|
+
return check(node);
|
|
3644
|
+
}
|
|
2866
3645
|
/**
|
|
2867
3646
|
* The new spelling of an import specifier, or null to keep it.
|
|
2868
3647
|
*/
|
|
@@ -2910,15 +3689,37 @@ class ModuleTransform {
|
|
|
2910
3689
|
return null;
|
|
2911
3690
|
const specifier = argument.literal;
|
|
2912
3691
|
const module = REACT_MODULES[specifier.text];
|
|
2913
|
-
if (module === undefined)
|
|
2914
|
-
return this.rewriteSpecifier(node, specifier);
|
|
2915
3692
|
const qualifier = node.qualifier;
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
3693
|
+
if (module === undefined) {
|
|
3694
|
+
if (!node.isTypeOf &&
|
|
3695
|
+
qualifier &&
|
|
3696
|
+
routerLoadContext(specifier.text, firstQualifiedName(qualifier).text) &&
|
|
3697
|
+
this.resolve(specifier.text) === '@octanejs/remix-router')
|
|
3698
|
+
return null;
|
|
3699
|
+
return this.rewriteSpecifier(node, specifier);
|
|
3700
|
+
}
|
|
3701
|
+
if (qualifier &&
|
|
3702
|
+
firstQualifiedName(qualifier).text === 'Touch' &&
|
|
3703
|
+
(specifier.text === 'react-dom' ||
|
|
3704
|
+
(specifier.text === 'react' &&
|
|
3705
|
+
(node.attributes || node.isTypeOf || ts.isQualifiedName(qualifier))))) {
|
|
3706
|
+
this.reportRetainedTouchType(specifier.text, node, node.isTypeOf, ts.isQualifiedName(qualifier));
|
|
2920
3707
|
return null;
|
|
2921
3708
|
}
|
|
3709
|
+
const name = qualifier && ts.isIdentifier(qualifier) ? qualifier.text : null;
|
|
3710
|
+
let missingValue = false;
|
|
3711
|
+
if (specifier.text === 'react' && node.isTypeOf && qualifier) {
|
|
3712
|
+
let value = qualifier;
|
|
3713
|
+
while (ts.isQualifiedName(value))
|
|
3714
|
+
value = value.left;
|
|
3715
|
+
const retained = mapReactImport('react', value.text, true)?.module === 'react';
|
|
3716
|
+
if (retained)
|
|
3717
|
+
return null;
|
|
3718
|
+
if (!hasOctaneValue(value.text)) {
|
|
3719
|
+
missingValue = true;
|
|
3720
|
+
this.note(REACT_TYPE_PROJECTION, 'error', value, `Octane has no value export \`${value.text}\`.`);
|
|
3721
|
+
}
|
|
3722
|
+
}
|
|
2922
3723
|
if (specifier.text === 'react' && !node.isTypeOf) {
|
|
2923
3724
|
const event = this.nativeEventType(node, name);
|
|
2924
3725
|
if (event !== undefined) {
|
|
@@ -2928,9 +3729,12 @@ class ModuleTransform {
|
|
|
2928
3729
|
}
|
|
2929
3730
|
}
|
|
2930
3731
|
this.count(RULE.import);
|
|
2931
|
-
const renamed = name === null
|
|
2932
|
-
|
|
2933
|
-
|
|
3732
|
+
const renamed = name === null || missingValue
|
|
3733
|
+
? null
|
|
3734
|
+
: mapQualifiedType(name, specifier.text, node.isTypeOf, node.typeArguments?.length ?? 0);
|
|
3735
|
+
if (name !== null && renamed === null && !missingValue) {
|
|
3736
|
+
const unresolvedProjection = specifier.text === 'react' && !node.isTypeOf;
|
|
3737
|
+
this.note(unresolvedProjection ? REACT_TYPE_PROJECTION : RULE.unsupported, unresolvedProjection ? 'error' : 'warning', node, `Octane has no type \`${name}\`.`);
|
|
2934
3738
|
}
|
|
2935
3739
|
const quote = this.source[specifier.getStart(this.sf)];
|
|
2936
3740
|
const head = this.span(node, node.getStart(this.sf), specifier.getStart(this.sf), PLAIN) +
|
|
@@ -2948,6 +3752,99 @@ class ModuleTransform {
|
|
|
2948
3752
|
renamed +
|
|
2949
3753
|
this.span(node, qualifier.end, node.end, PLAIN));
|
|
2950
3754
|
}
|
|
3755
|
+
retainedReactFactory(ref) {
|
|
3756
|
+
const call = ref.parent;
|
|
3757
|
+
if (!ts.isCallExpression(call) ||
|
|
3758
|
+
call.expression !== ref ||
|
|
3759
|
+
ts.isOptionalChain(call) ||
|
|
3760
|
+
call.typeArguments?.length)
|
|
3761
|
+
return false;
|
|
3762
|
+
const [tag, props, ...children] = call.arguments;
|
|
3763
|
+
if (!tag ||
|
|
3764
|
+
!ts.isStringLiteral(tag) ||
|
|
3765
|
+
props?.kind !== ts.SyntaxKind.NullKeyword ||
|
|
3766
|
+
!children.every(ts.isStringLiteral))
|
|
3767
|
+
return false;
|
|
3768
|
+
let value = call;
|
|
3769
|
+
while (ts.isParenthesizedExpression(value.parent))
|
|
3770
|
+
value = value.parent;
|
|
3771
|
+
const direct = value.parent;
|
|
3772
|
+
const block = ts.isReturnStatement(direct) &&
|
|
3773
|
+
ts.isBlock(direct.parent) &&
|
|
3774
|
+
direct.parent.statements.length === 1
|
|
3775
|
+
? direct.parent
|
|
3776
|
+
: null;
|
|
3777
|
+
const callback = block ? block.parent : direct;
|
|
3778
|
+
if (!ts.isArrowFunction(callback) ||
|
|
3779
|
+
callback.body !== (block ?? value) ||
|
|
3780
|
+
callback.parameters.length ||
|
|
3781
|
+
callback.type ||
|
|
3782
|
+
callback.typeParameters?.length ||
|
|
3783
|
+
callback.modifiers?.length)
|
|
3784
|
+
return false;
|
|
3785
|
+
const property = callback.parent;
|
|
3786
|
+
if (!ts.isPropertyAssignment(property) ||
|
|
3787
|
+
property.initializer !== callback ||
|
|
3788
|
+
!ts.isIdentifier(property.name) ||
|
|
3789
|
+
property.name.text === '__proto__' ||
|
|
3790
|
+
property.parent.properties.length !== 1)
|
|
3791
|
+
return false;
|
|
3792
|
+
const expression = property.parent.parent;
|
|
3793
|
+
const attribute = expression.parent;
|
|
3794
|
+
if (!ts.isJsxExpression(expression) ||
|
|
3795
|
+
expression.dotDotDotToken ||
|
|
3796
|
+
!ts.isJsxAttribute(attribute) ||
|
|
3797
|
+
attribute.initializer !== expression ||
|
|
3798
|
+
!ts.isIdentifier(attribute.name) ||
|
|
3799
|
+
attribute.name.text !== 'props')
|
|
3800
|
+
return false;
|
|
3801
|
+
const island = attribute.parent.parent;
|
|
3802
|
+
if (!ts.isJsxSelfClosingElement(island) || island.attributes.properties.length !== 2)
|
|
3803
|
+
return false;
|
|
3804
|
+
const component = island.attributes.properties.find((item) => item !== attribute);
|
|
3805
|
+
if (!component ||
|
|
3806
|
+
!ts.isJsxAttribute(component) ||
|
|
3807
|
+
!ts.isIdentifier(component.name) ||
|
|
3808
|
+
component.name.text !== 'component' ||
|
|
3809
|
+
!component.initializer ||
|
|
3810
|
+
!ts.isJsxExpression(component.initializer) ||
|
|
3811
|
+
!component.initializer.expression)
|
|
3812
|
+
return false;
|
|
3813
|
+
const named = (id) => {
|
|
3814
|
+
if (!ts.isIdentifier(id))
|
|
3815
|
+
return;
|
|
3816
|
+
const decl = this.declarationOf(id);
|
|
3817
|
+
const entry = decl && this.imports.get(decl);
|
|
3818
|
+
if (decl &&
|
|
3819
|
+
ts.isImportSpecifier(decl) &&
|
|
3820
|
+
entry &&
|
|
3821
|
+
!entry.typeOnly &&
|
|
3822
|
+
!decl.parent.parent.parent.attributes)
|
|
3823
|
+
return entry;
|
|
3824
|
+
};
|
|
3825
|
+
const bridge = named(island.tagName);
|
|
3826
|
+
const owner = named(component.initializer.expression);
|
|
3827
|
+
return (!!bridge &&
|
|
3828
|
+
!!owner &&
|
|
3829
|
+
nativeReactCompat(bridge.module, bridge.imported) &&
|
|
3830
|
+
this.options.retainedReactBridgeComponent?.(bridge.module, owner.module, owner.imported) ===
|
|
3831
|
+
true);
|
|
3832
|
+
}
|
|
3833
|
+
retainedReactFactoryImport(element) {
|
|
3834
|
+
const binding = this.imports.get(element);
|
|
3835
|
+
if (binding?.module !== 'react' ||
|
|
3836
|
+
binding.imported !== 'createElement' ||
|
|
3837
|
+
binding.typeOnly ||
|
|
3838
|
+
element.parent.parent.parent.attributes ||
|
|
3839
|
+
this.target !== 'tsrx' ||
|
|
3840
|
+
!this.hasReactBridge ||
|
|
3841
|
+
!this.options.retainedReactBridgeComponent ||
|
|
3842
|
+
!this.options.originalReact ||
|
|
3843
|
+
hasEval(this.sf))
|
|
3844
|
+
return false;
|
|
3845
|
+
const refs = this.localUses(element.name).filter((ref) => !inTypeNode(ref));
|
|
3846
|
+
return refs.length > 0 && refs.every((ref) => this.retainedReactFactory(ref));
|
|
3847
|
+
}
|
|
2951
3848
|
/**
|
|
2952
3849
|
* Fill each React import placeholder. The first one carries every Octane
|
|
2953
3850
|
* import the module needs, the module's own specifiers first; the others
|
|
@@ -2967,8 +3864,10 @@ class ModuleTransform {
|
|
|
2967
3864
|
};
|
|
2968
3865
|
/** React specifiers with no Octane export stay on React, for a person. */
|
|
2969
3866
|
const kept = new Map();
|
|
3867
|
+
const typeNamespaces = [];
|
|
2970
3868
|
/** Locals of React's `jsx` factories, which a JSX runtime consumer calls. */
|
|
2971
3869
|
const factories = [];
|
|
3870
|
+
let retainsCreateElement = false;
|
|
2972
3871
|
for (const node of this.importSlots) {
|
|
2973
3872
|
const module = node.moduleSpecifier.text;
|
|
2974
3873
|
const clause = node.importClause;
|
|
@@ -2978,17 +3877,46 @@ class ModuleTransform {
|
|
|
2978
3877
|
const namespace = clause.name?.text ??
|
|
2979
3878
|
(bindings && ts.isNamespaceImport(bindings) ? bindings.name.text : null);
|
|
2980
3879
|
// Members the rewrite could not map keep the namespace alive.
|
|
2981
|
-
if (namespace && this.
|
|
3880
|
+
if (namespace && this.retainedTypeNamespaces.has(clause)) {
|
|
3881
|
+
if (node.attributes) {
|
|
3882
|
+
typeNamespaces.push(node.getText(this.sf));
|
|
3883
|
+
continue;
|
|
3884
|
+
}
|
|
3885
|
+
// A static type query can follow a value chain that an import() type cannot.
|
|
3886
|
+
typeNamespaces.push(clause.name && bindings && ts.isNamedImports(bindings)
|
|
3887
|
+
? this.source.slice(node.getStart(this.sf), clause.name.end) +
|
|
3888
|
+
this.source.slice(clause.end, node.end).replace(/^from\b/, ' from')
|
|
3889
|
+
: node.getText(this.sf));
|
|
3890
|
+
}
|
|
3891
|
+
else if (namespace && this.used.has(namespace)) {
|
|
2982
3892
|
add(REACT_MODULES[module], '*', namespace, false);
|
|
3893
|
+
}
|
|
2983
3894
|
if (!bindings || !ts.isNamedImports(bindings))
|
|
2984
3895
|
continue;
|
|
2985
3896
|
for (const element of bindings.elements) {
|
|
2986
3897
|
const local = element.name.text;
|
|
2987
|
-
if (!this.used.has(local))
|
|
2988
|
-
continue;
|
|
2989
3898
|
const imported = (element.propertyName ?? element.name).text;
|
|
2990
|
-
|
|
2991
|
-
|
|
3899
|
+
let typeOnly = clause.isTypeOnly || element.isTypeOnly;
|
|
3900
|
+
let typeReferences = false;
|
|
3901
|
+
let unsupportedTouchUse = false;
|
|
3902
|
+
if (module === 'react' && imported === 'Touch' && !node.attributes && this.isTypeScript()) {
|
|
3903
|
+
// A type query still needs a value export, which Touch does not have.
|
|
3904
|
+
const references = this.localUses(element.name);
|
|
3905
|
+
typeReferences =
|
|
3906
|
+
references.length > 0 &&
|
|
3907
|
+
references.every((ref) => ts.isPartOfTypeNode(ref) || ts.isTypeOnlyExportDeclaration(ref.parent));
|
|
3908
|
+
unsupportedTouchUse = references.some(needsTouchValueOrNamespace);
|
|
3909
|
+
if (!unsupportedTouchUse)
|
|
3910
|
+
typeOnly ||= typeReferences;
|
|
3911
|
+
}
|
|
3912
|
+
if (!this.used.has(local) && !typeReferences)
|
|
3913
|
+
continue;
|
|
3914
|
+
if (imported === 'createElement' && this.retainedReactFactoryImport(element)) {
|
|
3915
|
+
add(module, imported, local, false);
|
|
3916
|
+
retainsCreateElement ||= local === 'createElement';
|
|
3917
|
+
continue;
|
|
3918
|
+
}
|
|
3919
|
+
const mapped = unsupportedTouchUse ? null : mapReactImport(module, imported, typeOnly);
|
|
2992
3920
|
if (mapped) {
|
|
2993
3921
|
// A name read only in types imports as a type, which bundlers elide.
|
|
2994
3922
|
add(mapped.module, mapped.name, local, typeOnly || !this.valueUses.has(local));
|
|
@@ -3001,15 +3929,25 @@ class ModuleTransform {
|
|
|
3001
3929
|
}
|
|
3002
3930
|
this.note(RULE.unsupported, 'error', element, UNSUPPORTED_VALUES[imported] ??
|
|
3003
3931
|
`\`${imported}\` from '${module}' has no Octane equivalent.`);
|
|
3932
|
+
if (unsupportedTouchUse && clause.isTypeOnly) {
|
|
3933
|
+
const binding = imported === local ? local : `${imported} as ${local}`;
|
|
3934
|
+
typeNamespaces.push(`import type { ${binding} } from ${this.quote}${module}${this.quote};`);
|
|
3935
|
+
continue;
|
|
3936
|
+
}
|
|
3004
3937
|
const spec = (typeOnly ? 'type ' : '') + (imported === local ? local : `${imported} as ${local}`);
|
|
3005
3938
|
kept.set(module, [...(kept.get(module) ?? []), spec]);
|
|
3006
3939
|
}
|
|
3007
3940
|
}
|
|
3941
|
+
const jsxFactory = retainsCreateElement && factories.length > 0
|
|
3942
|
+
? this.require('octane', 'createElement', false)
|
|
3943
|
+
: 'createElement';
|
|
3944
|
+
if (jsxFactory !== 'createElement')
|
|
3945
|
+
imports.get('octane')?.delete('createElement createElement');
|
|
3008
3946
|
for (const [module, names] of this.added) {
|
|
3009
3947
|
for (const [name, { local, typeOnly }] of names)
|
|
3010
3948
|
add(module, name, local, typeOnly);
|
|
3011
3949
|
}
|
|
3012
|
-
const lines =
|
|
3950
|
+
const lines = typeNamespaces;
|
|
3013
3951
|
const quote = (module) => this.quote + module + this.quote;
|
|
3014
3952
|
for (const [module, names] of imports) {
|
|
3015
3953
|
const specs = [...names.values()];
|
|
@@ -3030,10 +3968,10 @@ class ModuleTransform {
|
|
|
3030
3968
|
// React's runtime takes the key apart from the props; Octane's
|
|
3031
3969
|
// `createElement` reads it from them.
|
|
3032
3970
|
const params = this.isTypeScript()
|
|
3033
|
-
?
|
|
3971
|
+
? `type: Parameters<typeof ${jsxFactory}>[0], props: Record<string, unknown>, key?: string | number`
|
|
3034
3972
|
: 'type, props, key';
|
|
3035
3973
|
const [first, ...rest] = factories;
|
|
3036
|
-
lines.push(`function ${first}(${params}) {`, `${this.indentUnit}return
|
|
3974
|
+
lines.push(`function ${first}(${params}) {`, `${this.indentUnit}return ${jsxFactory}(type, key === undefined ? props : { ...props, key });`, '}', ...rest.map((local) => `const ${local} = ${first};`));
|
|
3037
3975
|
}
|
|
3038
3976
|
if (this.intrinsicPropsHelper) {
|
|
3039
3977
|
lines.push(`type ${this.intrinsicPropsHelper}<Props> = {`, `${this.indentUnit}[K in keyof Props]: K extends 'className' ? string | undefined`, `${this.indentUnit}${this.indentUnit}: K extends 'style' ? import(${quote('octane')}).CSSProperties | undefined : Props[K];`, '};');
|
|
@@ -3073,7 +4011,7 @@ class ModuleTransform {
|
|
|
3073
4011
|
// line, and directive lines. Anything inserted here starts a line, so
|
|
3074
4012
|
// the Strong fallback can find its own directive line again. The
|
|
3075
4013
|
// comment on an unported package's import stays with it.
|
|
3076
|
-
const prologue = /^(?:#![^\n]*\n)?(?:[ \t]*(?:\/\/(?! No Octane port of)[^\n]*|\/\*[\s\S]*?\*\/[ \t]*|(['"])use [a-z ]+\1;?[^\n]*)?\r?\n)*/.exec(code);
|
|
4014
|
+
const prologue = /^(?:#![^\n]*\n)?(?:[ \t]*(?:\/\/(?! (?:No Octane port|Retained React import) of)[^\n]*|\/\*[\s\S]*?\*\/[ \t]*|(['"])use [a-z ]+\1;?[^\n]*)?\r?\n)*/.exec(code);
|
|
3077
4015
|
return prologue ? prologue[0].length : 0;
|
|
3078
4016
|
}
|
|
3079
4017
|
addStrongDirective(code) {
|
|
@@ -3088,6 +4026,13 @@ class ModuleTransform {
|
|
|
3088
4026
|
return replaced !== code ? replaced : '/** @jsxImportSource octane */\n' + code;
|
|
3089
4027
|
}
|
|
3090
4028
|
// ---------------------------------------------------------------- APIs
|
|
4029
|
+
checkServerStream(wait) {
|
|
4030
|
+
const originalServer = this.options.originalReactServerEntry;
|
|
4031
|
+
if (originalServer &&
|
|
4032
|
+
unreadServerStream(wait, (id) => this.declarationOf(id), (id) => this.localUses(id), originalServer) &&
|
|
4033
|
+
!hasEval(this.sf))
|
|
4034
|
+
this.note(RULE.serverRender, 'warning', wait, "Octane's renderToReadableStream can wait for a reader when it writes deferred output. Start reading before awaiting allReady, or use prerender for fully resolved HTML.");
|
|
4035
|
+
}
|
|
3091
4036
|
rewriteMember(node) {
|
|
3092
4037
|
// `<Ctx.Provider value>` is `<Ctx value>` in Octane, as in React 19.
|
|
3093
4038
|
const tag = node.parent;
|
|
@@ -3104,22 +4049,36 @@ class ModuleTransform {
|
|
|
3104
4049
|
const isValue = !isTag &&
|
|
3105
4050
|
!(ts.isPropertyAccessExpression(tag) && tag.expression === node) &&
|
|
3106
4051
|
!(ts.isBinaryExpression(tag) && tag.left === node);
|
|
3107
|
-
if (node.name.text === 'Provider' &&
|
|
3108
|
-
|
|
4052
|
+
if (node.name.text === 'Provider' &&
|
|
4053
|
+
(isTag || isValue) &&
|
|
4054
|
+
this.isContext(node.expression, node)) {
|
|
4055
|
+
if (!ts.isJsxClosingElement(tag)) {
|
|
4056
|
+
let value = node;
|
|
4057
|
+
while (isWrapper(value.parent) && value.parent.expression === value)
|
|
4058
|
+
value = value.parent;
|
|
4059
|
+
const declaration = value.parent;
|
|
4060
|
+
const topLevelInitializer = ts.isVariableDeclaration(declaration) &&
|
|
4061
|
+
declaration.initializer === value &&
|
|
4062
|
+
ts.isVariableDeclarationList(declaration.parent) &&
|
|
4063
|
+
ts.isVariableStatement(declaration.parent.parent) &&
|
|
4064
|
+
declaration.parent.parent.parent === this.sf;
|
|
4065
|
+
const change = (isTag || topLevelInitializer) && ts.isIdentifier(node.expression)
|
|
4066
|
+
? this.localProviderReadChange(node.expression, topLevelInitializer && declaration.parent.declarations[0] === declaration
|
|
4067
|
+
? declaration.parent.parent
|
|
4068
|
+
: undefined)
|
|
4069
|
+
: undefined;
|
|
4070
|
+
if (change)
|
|
4071
|
+
this.note(CONTEXT_PROVIDER_GETTER, 'error', node, `Replacing this React context Provider read can skip an authored ${change}. Review the custom Provider before migrating it.`);
|
|
3109
4072
|
this.count(RULE.provider);
|
|
4073
|
+
}
|
|
3110
4074
|
return this.emit(node.expression, PLAIN);
|
|
3111
4075
|
}
|
|
3112
|
-
const
|
|
3113
|
-
if (
|
|
3114
|
-
|
|
3115
|
-
const mapped = mapReactImport(namespace.module, name, false);
|
|
3116
|
-
if (!mapped) {
|
|
3117
|
-
this.used.add(namespace.local);
|
|
3118
|
-
this.note(RULE.unsupported, 'error', node, UNSUPPORTED_VALUES[name] ?? `\`${node.getText(this.sf)}\` has no Octane equivalent.`);
|
|
4076
|
+
const reactMember = this.checkReactNamespaceMember(node, node.name.text);
|
|
4077
|
+
if (reactMember !== undefined) {
|
|
4078
|
+
if (!reactMember)
|
|
3119
4079
|
return null;
|
|
3120
|
-
}
|
|
3121
4080
|
this.count(RULE.import);
|
|
3122
|
-
return this.require(
|
|
4081
|
+
return this.require(reactMember.module, reactMember.name, false);
|
|
3123
4082
|
}
|
|
3124
4083
|
const member = this.rewritePackageMember(node.expression, node.name, 'error');
|
|
3125
4084
|
if (member !== null)
|
|
@@ -3135,6 +4094,18 @@ class ModuleTransform {
|
|
|
3135
4094
|
}
|
|
3136
4095
|
return null;
|
|
3137
4096
|
}
|
|
4097
|
+
checkReactNamespaceMember(node, name) {
|
|
4098
|
+
const namespace = this.importOf(node.expression);
|
|
4099
|
+
if (!namespace?.namespace || !(namespace.module in REACT_MODULES))
|
|
4100
|
+
return;
|
|
4101
|
+
const mapped = mapReactImport(namespace.module, name, false);
|
|
4102
|
+
if (!mapped) {
|
|
4103
|
+
this.used.add(namespace.local);
|
|
4104
|
+
this.note(RULE.unsupported, 'error', node, UNSUPPORTED_VALUES[name] ?? `\`${node.getText(this.sf)}\` has no Octane equivalent.`);
|
|
4105
|
+
return null;
|
|
4106
|
+
}
|
|
4107
|
+
return mapped;
|
|
4108
|
+
}
|
|
3138
4109
|
/**
|
|
3139
4110
|
* Whether `node` names a global namespace React's types declare, such as
|
|
3140
4111
|
* `React` in `React.ReactNode` written without an import.
|
|
@@ -3150,6 +4121,17 @@ class ModuleTransform {
|
|
|
3150
4121
|
? entry.namespace && entry.module === 'react'
|
|
3151
4122
|
: this.isGlobalNamespace(node, 'React');
|
|
3152
4123
|
}
|
|
4124
|
+
reportRetainedTouchType(module, node, query, nested) {
|
|
4125
|
+
const message = module === 'react-dom'
|
|
4126
|
+
? "`Touch` from 'react-dom' stays on React DOM. Octane only maps the type from 'react'."
|
|
4127
|
+
: query
|
|
4128
|
+
? 'Octane has no Touch value for this type query.'
|
|
4129
|
+
: nested
|
|
4130
|
+
? 'Octane has no Touch namespace for this type reference.'
|
|
4131
|
+
: null;
|
|
4132
|
+
if (message)
|
|
4133
|
+
this.note(RULE.unsupported, 'warning', node, message);
|
|
4134
|
+
}
|
|
3153
4135
|
rewriteQualifiedType(node) {
|
|
3154
4136
|
const member = this.rewritePackageMember(node.left, node.right, 'warning');
|
|
3155
4137
|
if (member !== null)
|
|
@@ -3160,26 +4142,117 @@ class ModuleTransform {
|
|
|
3160
4142
|
: !this.isGlobalNamespace(node.left, 'React')) {
|
|
3161
4143
|
return null;
|
|
3162
4144
|
}
|
|
3163
|
-
if (ts.
|
|
3164
|
-
|
|
4145
|
+
if (ts.isIdentifier(node.left) &&
|
|
4146
|
+
node.right.text === 'Touch' &&
|
|
4147
|
+
(!namespace || namespace.module === 'react-dom' || namespace.module === 'react')) {
|
|
4148
|
+
const declaration = this.declarationOf(node.left);
|
|
4149
|
+
if (!namespace && declaration)
|
|
4150
|
+
return null;
|
|
4151
|
+
const clause = declaration && (ts.isNamespaceImport(declaration) ? declaration.parent : declaration);
|
|
4152
|
+
const importClause = clause && ts.isImportClause(clause) ? clause : null;
|
|
4153
|
+
if (!namespace || importClause) {
|
|
4154
|
+
let access = node;
|
|
4155
|
+
while (ts.isQualifiedName(access.parent) && access.parent.left === access)
|
|
4156
|
+
access = access.parent;
|
|
4157
|
+
const query = isValueTypeQuery(access);
|
|
4158
|
+
const nested = access !== node;
|
|
4159
|
+
const attributes = importClause?.parent.attributes;
|
|
4160
|
+
if (query ||
|
|
4161
|
+
(ts.isTypeReferenceNode(access.parent) &&
|
|
4162
|
+
(namespace?.module === 'react-dom' || nested || attributes))) {
|
|
4163
|
+
this.reportRetainedTouchType(namespace?.module ?? 'react', node, query, nested);
|
|
4164
|
+
if (importClause)
|
|
4165
|
+
this.retainedTypeNamespaces.add(importClause);
|
|
4166
|
+
return node.getText(this.sf);
|
|
4167
|
+
}
|
|
4168
|
+
}
|
|
4169
|
+
}
|
|
4170
|
+
let queried = node;
|
|
4171
|
+
if (namespace?.module === 'react') {
|
|
4172
|
+
while (ts.isQualifiedName(queried.parent) && queried.parent.left === queried)
|
|
4173
|
+
queried = queried.parent;
|
|
4174
|
+
}
|
|
4175
|
+
if (ts.isTypeQueryNode(queried.parent) && queried.parent.exprName === queried) {
|
|
4176
|
+
const name = node.right.text;
|
|
4177
|
+
const mapped = mapReactImport(namespace?.module ?? 'react', name, true);
|
|
3165
4178
|
if (mapped?.module === 'react') {
|
|
3166
4179
|
if (!namespace)
|
|
3167
4180
|
return null;
|
|
4181
|
+
const declaration = ts.isIdentifier(node.left) ? this.declarationOf(node.left) : null;
|
|
4182
|
+
const clause = declaration && (ts.isNamespaceImport(declaration) ? declaration.parent : declaration);
|
|
4183
|
+
if (clause && ts.isImportClause(clause) && (queried !== node || clause.parent.attributes)) {
|
|
4184
|
+
this.retainedTypeNamespaces.add(clause);
|
|
4185
|
+
return node.getText(this.sf);
|
|
4186
|
+
}
|
|
3168
4187
|
this.count(RULE.import);
|
|
3169
4188
|
return this.require(mapped.module, mapped.name, true);
|
|
3170
4189
|
}
|
|
4190
|
+
if (namespace?.module === 'react' && !hasOctaneValue(name)) {
|
|
4191
|
+
this.note(REACT_TYPE_PROJECTION, 'error', node, `Octane has no value export \`${name}\`.`);
|
|
4192
|
+
return null;
|
|
4193
|
+
}
|
|
3171
4194
|
}
|
|
3172
|
-
const name = mapQualifiedType(node.right.text);
|
|
4195
|
+
const name = mapQualifiedType(node.right.text, namespace?.module ?? 'react', ts.isTypeQueryNode(node.parent), ts.isTypeReferenceNode(node.parent) ? (node.parent.typeArguments?.length ?? 0) : 0);
|
|
3173
4196
|
if (!name) {
|
|
3174
4197
|
if (namespace)
|
|
3175
4198
|
this.used.add(namespace.local);
|
|
3176
|
-
|
|
4199
|
+
const unresolvedProjection = namespace?.module === 'react' && ts.isTypeReferenceNode(node.parent);
|
|
4200
|
+
this.note(unresolvedProjection ? REACT_TYPE_PROJECTION : RULE.unsupported, unresolvedProjection ? 'error' : 'warning', node, `Octane has no type \`${node.getText(this.sf)}\`.`);
|
|
3177
4201
|
return null;
|
|
3178
4202
|
}
|
|
3179
4203
|
this.count(RULE.import);
|
|
3180
4204
|
return this.require('octane', name, true);
|
|
3181
4205
|
}
|
|
3182
4206
|
rewriteTypeReference(node, name) {
|
|
4207
|
+
if (ts.isExpressionWithTypeArguments(node)) {
|
|
4208
|
+
const root = typeHeritageRoot(node);
|
|
4209
|
+
const entry = root && this.importOf(root);
|
|
4210
|
+
const member = root && directMember(root);
|
|
4211
|
+
if (root && entry && member && (entry.module === 'react' || entry.module === 'react-dom')) {
|
|
4212
|
+
if (entry.namespace && member.text === 'Touch' && member.parent !== name) {
|
|
4213
|
+
const decl = this.declarationOf(root);
|
|
4214
|
+
const clause = decl && (ts.isNamespaceImport(decl) ? decl.parent : decl);
|
|
4215
|
+
if (clause && ts.isImportClause(clause)) {
|
|
4216
|
+
this.reportRetainedTouchType(entry.module, member, false, true);
|
|
4217
|
+
this.retainedTypeNamespaces.add(clause);
|
|
4218
|
+
return name.getText(this.sf) + this.span(node, name.end, node.end, PLAIN);
|
|
4219
|
+
}
|
|
4220
|
+
}
|
|
4221
|
+
if (entry.module === 'react' && entry.imported === 'Touch') {
|
|
4222
|
+
this.used.add(entry.local);
|
|
4223
|
+
return name.getText(this.sf) + this.span(node, name.end, node.end, PLAIN);
|
|
4224
|
+
}
|
|
4225
|
+
}
|
|
4226
|
+
}
|
|
4227
|
+
// Type heritage parses a qualified name as a property access expression.
|
|
4228
|
+
if (ts.isExpressionWithTypeArguments(node) &&
|
|
4229
|
+
ts.isPartOfTypeNode(node) &&
|
|
4230
|
+
ts.isPropertyAccessExpression(name) &&
|
|
4231
|
+
!name.questionDotToken &&
|
|
4232
|
+
ts.isIdentifier(name.expression) &&
|
|
4233
|
+
name.name.text === 'Touch') {
|
|
4234
|
+
const receiver = name.expression;
|
|
4235
|
+
const entry = this.importOf(receiver);
|
|
4236
|
+
const declaration = this.declarationOf(receiver);
|
|
4237
|
+
if (entry?.namespace && (entry.module === 'react' || entry.module === 'react-dom')) {
|
|
4238
|
+
const clause = declaration && (ts.isNamespaceImport(declaration) ? declaration.parent : declaration);
|
|
4239
|
+
if (clause &&
|
|
4240
|
+
ts.isImportClause(clause) &&
|
|
4241
|
+
(clause.parent.attributes || entry.module === 'react-dom')) {
|
|
4242
|
+
this.reportRetainedTouchType(entry.module, name, false, false);
|
|
4243
|
+
this.retainedTypeNamespaces.add(clause);
|
|
4244
|
+
return name.getText(this.sf) + this.span(node, name.end, node.end, PLAIN);
|
|
4245
|
+
}
|
|
4246
|
+
}
|
|
4247
|
+
if ((entry?.namespace && entry.module === 'react') ||
|
|
4248
|
+
(!entry &&
|
|
4249
|
+
!declaration &&
|
|
4250
|
+
this.isGlobalNamespace(receiver, 'React') &&
|
|
4251
|
+
ts.isExternalModule(this.sf))) {
|
|
4252
|
+
this.count(RULE.import);
|
|
4253
|
+
return this.require('octane', 'Touch', true) + this.span(node, name.end, node.end, PLAIN);
|
|
4254
|
+
}
|
|
4255
|
+
}
|
|
3183
4256
|
// The global `JSX` namespace React's types declared is Octane's export.
|
|
3184
4257
|
if (ts.isQualifiedName(name) && this.isGlobalNamespace(name.left, 'JSX'))
|
|
3185
4258
|
this.require('octane', 'JSX', true);
|
|
@@ -3250,15 +4323,22 @@ class ModuleTransform {
|
|
|
3250
4323
|
}
|
|
3251
4324
|
if (ts.isPropertyAccessExpression(node.expression) &&
|
|
3252
4325
|
node.expression.name.text === 'render' &&
|
|
3253
|
-
node.arguments.length === 1
|
|
3254
|
-
|
|
3255
|
-
|
|
4326
|
+
node.arguments.length === 1) {
|
|
4327
|
+
const receiver = skipParens(node.expression.expression);
|
|
4328
|
+
const root = ts.isIdentifier(receiver) ? this.constValue(receiver) : receiver;
|
|
4329
|
+
if (root && ts.isCallExpression(root) && this.calleeName(root.expression) === 'createRoot') {
|
|
4330
|
+
const rewritten = this.rewriteRootRender(node, node.arguments[0]);
|
|
4331
|
+
this.checkLocalReactRoot(node, receiver, root, rewritten === null);
|
|
4332
|
+
return rewritten;
|
|
4333
|
+
}
|
|
3256
4334
|
}
|
|
3257
4335
|
const callee = this.callee(node.expression);
|
|
3258
4336
|
// An Octane hook without a list already means "inferred".
|
|
3259
4337
|
if (!callee || callee.module === 'octane')
|
|
3260
4338
|
return null;
|
|
3261
4339
|
const name = callee.name;
|
|
4340
|
+
if (name === 'useState' && callee.module === 'react' && node.arguments[0])
|
|
4341
|
+
this.checkInitializerResourceHints(node.arguments[0]);
|
|
3262
4342
|
if (name === 'useSyncExternalStore' && this.strong && this.target === 'tsrx')
|
|
3263
4343
|
return this.nativeSignalRead(node);
|
|
3264
4344
|
if (name === 'forwardRef')
|
|
@@ -3269,8 +4349,14 @@ class ModuleTransform {
|
|
|
3269
4349
|
return this.rewriteRootRender(node, node.arguments[1]);
|
|
3270
4350
|
}
|
|
3271
4351
|
if ((name === 'renderToString' || name === 'renderToStaticMarkup') &&
|
|
3272
|
-
callee.module.startsWith('react-dom/server')
|
|
3273
|
-
node.arguments.length
|
|
4352
|
+
callee.module.startsWith('react-dom/server')) {
|
|
4353
|
+
if (node.arguments.length > 1) {
|
|
4354
|
+
this.note(BUFFERED_SSR_OPTIONS, 'error', node, 'Keep this buffered server render in React. Migrating the call can change when arguments are evaluated and which server options the renderer reads.');
|
|
4355
|
+
return null;
|
|
4356
|
+
}
|
|
4357
|
+
if (node.arguments.length !== 1)
|
|
4358
|
+
return null;
|
|
4359
|
+
this.checkBufferedReactComponent(node, node.arguments[0], callee.module);
|
|
3274
4360
|
// React returns the HTML; Octane returns `{ html, css }`.
|
|
3275
4361
|
this.count(RULE.serverRender);
|
|
3276
4362
|
return ((this.rewriteRootRender(node, node.arguments[0]) ??
|
|
@@ -3278,33 +4364,251 @@ class ModuleTransform {
|
|
|
3278
4364
|
}
|
|
3279
4365
|
if (name === 'flushSync') {
|
|
3280
4366
|
this.note(RULE.flushSync, 'warning', node, "Octane's `flushSync` commits synchronously but leaves the commit's `useEffect`s queued, where React runs them before returning. If code after it reads what an effect did, use `useLayoutEffect` for that effect.");
|
|
4367
|
+
if (callee.module === 'react-dom' &&
|
|
4368
|
+
!node.typeArguments?.length &&
|
|
4369
|
+
!ts.isOptionalChain(node) &&
|
|
4370
|
+
!(ts.isPropertyAccessExpression(node.expression) &&
|
|
4371
|
+
this.importOf(node.expression.expression)?.typeOnly)) {
|
|
4372
|
+
const [argument] = node.arguments;
|
|
4373
|
+
const value = argument && skipParens(argument);
|
|
4374
|
+
if (!value || (node.arguments.length === 1 && value.kind === ts.SyntaxKind.NullKeyword)) {
|
|
4375
|
+
// Older Octane versions need a callback even for a drain-only call.
|
|
4376
|
+
const close = node.getChildren(this.sf).at(-1);
|
|
4377
|
+
if (close?.kind === ts.SyntaxKind.CloseParenToken && close.getWidth(this.sf) > 0) {
|
|
4378
|
+
const start = (value ?? close).getStart(this.sf);
|
|
4379
|
+
return (this.span(node, node.getStart(this.sf), start, PLAIN) +
|
|
4380
|
+
'() => {}' +
|
|
4381
|
+
this.source.slice(value?.end ?? start, node.end));
|
|
4382
|
+
}
|
|
4383
|
+
}
|
|
4384
|
+
}
|
|
3281
4385
|
return null;
|
|
3282
4386
|
}
|
|
3283
|
-
if (name in DEPENDENCY_HOOKS)
|
|
4387
|
+
if (name in DEPENDENCY_HOOKS) {
|
|
4388
|
+
const depsIndex = DEPENDENCY_HOOKS[name];
|
|
4389
|
+
if (callee.module === 'react' && node.arguments.length === depsIndex + 1)
|
|
4390
|
+
this.checkHookDependencyReads(node, node.arguments[depsIndex]);
|
|
3284
4391
|
return this.rewriteDependencies(node, name);
|
|
4392
|
+
}
|
|
3285
4393
|
return null;
|
|
3286
4394
|
}
|
|
3287
|
-
|
|
3288
|
-
|
|
3289
|
-
if (
|
|
3290
|
-
return
|
|
3291
|
-
|
|
3292
|
-
const
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
4395
|
+
checkIntlProviderResources(tag) {
|
|
4396
|
+
const resourceHint = this.options.initializerResourceHint;
|
|
4397
|
+
if (!resourceHint)
|
|
4398
|
+
return;
|
|
4399
|
+
// Provider admission already excludes spreads. JSX takes the last duplicate prop.
|
|
4400
|
+
const attribute = (name) => tag.attributes.properties.findLast((attr) => ts.isJsxAttribute(attr) && ts.isIdentifier(attr.name) && attr.name.text === name);
|
|
4401
|
+
const locale = attribute('locale')?.initializer;
|
|
4402
|
+
const expression = locale && ts.isJsxExpression(locale) ? locale.expression : locale;
|
|
4403
|
+
const value = expression && skipWrappers(expression);
|
|
4404
|
+
if (!value || !ts.isStringLiteralLike(value) || value.text !== '')
|
|
4405
|
+
return;
|
|
4406
|
+
const callback = attribute('onError');
|
|
4407
|
+
const handler = callback?.initializer;
|
|
4408
|
+
if (!callback || !handler || !ts.isJsxExpression(handler) || !handler.expression)
|
|
4409
|
+
return;
|
|
4410
|
+
const callee = skipWrappers(handler.expression);
|
|
4411
|
+
if (!ts.isIdentifier(callee))
|
|
4412
|
+
return;
|
|
4413
|
+
const declaration = this.declarationOf(callee);
|
|
4414
|
+
const imported = declaration && this.imports.get(declaration);
|
|
4415
|
+
if (!declaration ||
|
|
4416
|
+
!imported ||
|
|
4417
|
+
imported.typeOnly ||
|
|
4418
|
+
imported.namespace ||
|
|
4419
|
+
ts.findAncestor(declaration, ts.isImportDeclaration)?.attributes)
|
|
4420
|
+
return;
|
|
4421
|
+
const hint = resourceHint(imported.module, imported.imported, true);
|
|
4422
|
+
if (hint)
|
|
4423
|
+
this.note(RULE.bindings, 'error', callback, `IntlProvider can call \`${imported.imported}\` for an empty locale. The retained callback uses React DOM ${hint} outside the original React server request. Move it into Octane-compiled code or retain the React provider.`);
|
|
4424
|
+
}
|
|
4425
|
+
checkInitializerResourceHints(initializer) {
|
|
4426
|
+
const resourceHint = this.options.initializerResourceHint;
|
|
4427
|
+
if (!resourceHint)
|
|
4428
|
+
return;
|
|
4429
|
+
const value = skipWrappers(initializer);
|
|
4430
|
+
const check = (callee, site) => {
|
|
4431
|
+
callee = skipWrappers(callee);
|
|
4432
|
+
if (!ts.isIdentifier(callee))
|
|
4433
|
+
return;
|
|
4434
|
+
const declaration = this.declarationOf(callee);
|
|
4435
|
+
const imported = declaration && this.imports.get(declaration);
|
|
4436
|
+
if (!imported || imported.typeOnly || imported.namespace)
|
|
4437
|
+
return;
|
|
4438
|
+
if (site !== initializer && ts.findAncestor(declaration, ts.isImportDeclaration)?.attributes)
|
|
4439
|
+
return;
|
|
4440
|
+
const hint = resourceHint(imported.module, imported.imported);
|
|
4441
|
+
if (hint)
|
|
4442
|
+
this.note(RULE.bindings, 'error', site, `The initializer calls \`${imported.imported}\`, which uses React DOM ${hint} from plain TypeScript. Move the resource hint into Octane-compiled code so the server renderer can collect it.`);
|
|
4443
|
+
};
|
|
4444
|
+
if (!ts.isArrowFunction(value) && !ts.isFunctionExpression(value)) {
|
|
4445
|
+
if (ts.isIdentifier(value))
|
|
4446
|
+
check(value, initializer);
|
|
4447
|
+
return;
|
|
4448
|
+
}
|
|
4449
|
+
if (value.modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword))
|
|
4450
|
+
return;
|
|
4451
|
+
if (ts.isFunctionExpression(value) && value.asteriskToken)
|
|
4452
|
+
return;
|
|
4453
|
+
// Follow eager expressions only. A returned callback or a lazy operand may never run.
|
|
4454
|
+
const visit = (node) => {
|
|
4455
|
+
if (isFunctionLike(node) || ts.isClassLike(node) || ts.isTypeNode(node))
|
|
4456
|
+
return;
|
|
4457
|
+
if (ts.isConditionalExpression(node))
|
|
4458
|
+
return visit(node.condition);
|
|
4459
|
+
if (ts.isOptionalChain(node))
|
|
4460
|
+
return visit(node.expression);
|
|
4461
|
+
if (ts.isBinaryExpression(node)) {
|
|
4462
|
+
const kind = node.operatorToken.kind;
|
|
4463
|
+
if (kind === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
4464
|
+
kind === ts.SyntaxKind.BarBarToken ||
|
|
4465
|
+
kind === ts.SyntaxKind.QuestionQuestionToken ||
|
|
4466
|
+
kind === ts.SyntaxKind.AmpersandAmpersandEqualsToken ||
|
|
4467
|
+
kind === ts.SyntaxKind.BarBarEqualsToken ||
|
|
4468
|
+
kind === ts.SyntaxKind.QuestionQuestionEqualsToken)
|
|
4469
|
+
return visit(node.left);
|
|
4470
|
+
}
|
|
4471
|
+
if (ts.isCallExpression(node))
|
|
4472
|
+
check(node.expression, node);
|
|
4473
|
+
ts.forEachChild(node, visit);
|
|
4474
|
+
};
|
|
4475
|
+
if (!ts.isBlock(value.body))
|
|
4476
|
+
return visit(value.body);
|
|
4477
|
+
for (const statement of value.body.statements) {
|
|
4478
|
+
if (ts.isReturnStatement(statement)) {
|
|
4479
|
+
if (statement.expression)
|
|
4480
|
+
visit(statement.expression);
|
|
4481
|
+
break;
|
|
4482
|
+
}
|
|
4483
|
+
if (ts.isExpressionStatement(statement))
|
|
4484
|
+
visit(statement.expression);
|
|
4485
|
+
else if (ts.isVariableStatement(statement)) {
|
|
4486
|
+
for (const { initializer } of statement.declarationList.declarations) {
|
|
4487
|
+
if (initializer)
|
|
4488
|
+
visit(initializer);
|
|
4489
|
+
}
|
|
4490
|
+
}
|
|
4491
|
+
else
|
|
4492
|
+
break;
|
|
4493
|
+
}
|
|
4494
|
+
}
|
|
4495
|
+
/** Publish an exclusive native host ref through the existing external-store hook. */
|
|
4496
|
+
nativeRefState(statement) {
|
|
4497
|
+
const { declarationList } = statement;
|
|
4498
|
+
if (!(declarationList.flags & ts.NodeFlags.Const) || declarationList.declarations.length !== 1)
|
|
4499
|
+
return null;
|
|
4500
|
+
const [{ name, initializer, type: annotation }] = declarationList.declarations;
|
|
4501
|
+
if (annotation ||
|
|
4502
|
+
!ts.isArrayBindingPattern(name) ||
|
|
4503
|
+
name.elements.length !== 2 ||
|
|
4504
|
+
!initializer ||
|
|
4505
|
+
!ts.isCallExpression(initializer) ||
|
|
4506
|
+
initializer.questionDotToken ||
|
|
4507
|
+
initializer.arguments.length !== 1 ||
|
|
4508
|
+
initializer.arguments[0].kind !== ts.SyntaxKind.NullKeyword ||
|
|
4509
|
+
initializer.typeArguments?.length !== 1)
|
|
4510
|
+
return null;
|
|
4511
|
+
const hook = this.callee(initializer.expression);
|
|
4512
|
+
if (hook?.module !== 'react' || hook.name !== 'useState' || !this.options.automaticReactJsx)
|
|
4513
|
+
return null;
|
|
4514
|
+
const [value, setter] = name.elements;
|
|
4515
|
+
if (!ts.isBindingElement(value) ||
|
|
4516
|
+
!ts.isBindingElement(setter) ||
|
|
4517
|
+
!ts.isIdentifier(value.name) ||
|
|
4518
|
+
!ts.isIdentifier(setter.name) ||
|
|
4519
|
+
value.initializer ||
|
|
4520
|
+
setter.initializer ||
|
|
4521
|
+
value.dotDotDotToken ||
|
|
4522
|
+
setter.dotDotDotToken)
|
|
4523
|
+
return null;
|
|
4524
|
+
const refs = this.localUses(setter.name);
|
|
4525
|
+
if (refs.length !== 1)
|
|
4526
|
+
return null;
|
|
4527
|
+
const [ref] = refs;
|
|
4528
|
+
const expression = ref.parent;
|
|
4529
|
+
if (!ts.isJsxExpression(expression) || expression.expression !== ref)
|
|
4530
|
+
return null;
|
|
4531
|
+
const attribute = expression.parent;
|
|
4532
|
+
if (!ts.isJsxAttribute(attribute) ||
|
|
4533
|
+
attribute.initializer !== expression ||
|
|
4534
|
+
!ts.isIdentifier(attribute.name) ||
|
|
4535
|
+
attribute.name.text !== 'ref')
|
|
4536
|
+
return null;
|
|
4537
|
+
const host = attribute.parent.parent;
|
|
4538
|
+
if (!ts.isIdentifier(host.tagName) ||
|
|
4539
|
+
!/^[a-z]/.test(host.tagName.text) ||
|
|
4540
|
+
host.attributes.properties.some(ts.isJsxSpreadAttribute))
|
|
4541
|
+
return null;
|
|
4542
|
+
const owner = ts.findAncestor(statement, isFunctionLike);
|
|
4543
|
+
const output = ts.isJsxOpeningElement(host) ? host.parent : host;
|
|
4544
|
+
if (!owner ||
|
|
4545
|
+
!(ts.isFunctionDeclaration(owner) ||
|
|
4546
|
+
ts.isFunctionExpression(owner) ||
|
|
4547
|
+
ts.isArrowFunction(owner)) ||
|
|
4548
|
+
owner.body !== statement.parent ||
|
|
4549
|
+
owner.asteriskToken ||
|
|
4550
|
+
owner.modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword) ||
|
|
4551
|
+
!this.isComponent(owner) ||
|
|
4552
|
+
nativeOutputOwner(output, () => false, {
|
|
4553
|
+
typeOf: (node) => this.typeOf(node),
|
|
4554
|
+
text: (node) => scalar(this.typeOf(node)),
|
|
4555
|
+
}) !== owner ||
|
|
4556
|
+
reads(owner, (name) => name.text === 'eval') ||
|
|
4557
|
+
this.lostComments(statement.getStart(this.sf), statement.end, '').length)
|
|
4558
|
+
return null;
|
|
4559
|
+
let binding = owner.name;
|
|
4560
|
+
if (!ts.isFunctionDeclaration(owner)) {
|
|
4561
|
+
const declaration = owner.parent;
|
|
4562
|
+
if (!ts.isVariableDeclaration(declaration) ||
|
|
4563
|
+
declaration.initializer !== owner ||
|
|
4564
|
+
!ts.isIdentifier(declaration.name) ||
|
|
4565
|
+
!(ts.getCombinedNodeFlags(declaration) & ts.NodeFlags.Const))
|
|
4566
|
+
return null;
|
|
4567
|
+
binding = declaration.name;
|
|
4568
|
+
}
|
|
4569
|
+
if ((binding && !this.localUses(binding).every(componentTagOrExport)) ||
|
|
4570
|
+
(owner.name &&
|
|
4571
|
+
owner.name !== binding &&
|
|
4572
|
+
!this.localUses(owner.name).every(componentTagOrExport)))
|
|
4573
|
+
return null;
|
|
4574
|
+
const fresh = (base) => {
|
|
4575
|
+
while (this.declared.has(base) || this.readsGlobal(base))
|
|
4576
|
+
base += '_';
|
|
4577
|
+
this.declared.add(base);
|
|
4578
|
+
return base;
|
|
4579
|
+
};
|
|
4580
|
+
const factory = (this.nativeRefStoreFactory ??= fresh('createNativeRefStore'));
|
|
4581
|
+
const cell = fresh(value.name.text + 'RefStore');
|
|
4582
|
+
const memo = this.require('octane', 'useMemo', false);
|
|
4583
|
+
const read = this.require('octane', 'useSyncExternalStore', false);
|
|
4584
|
+
const type = this.emit(initializer.typeArguments[0], PLAIN);
|
|
4585
|
+
const line = '\n' + this.indentOf(statement);
|
|
4586
|
+
this.count(RULE.nativeRefState);
|
|
4587
|
+
return (`const ${cell} = ${memo}(() => ${factory}<${type}>(null));${line}` +
|
|
4588
|
+
`const ${value.name.getText(this.sf)} = ${read}(${cell}.subscribe, ${cell}.get, ${cell}.get);${line}` +
|
|
4589
|
+
`const ${setter.name.getText(this.sf)} = ${cell}.attach;`);
|
|
4590
|
+
}
|
|
4591
|
+
/** Replace only a direct render subscription to one native writable cell, with identical SSR and client reads. */
|
|
4592
|
+
nativeSignalRead(node) {
|
|
4593
|
+
if (node.arguments.length !== 3 || node.questionDotToken || node.typeArguments)
|
|
4594
|
+
return null;
|
|
4595
|
+
const [subscribe, get, server] = node.arguments;
|
|
4596
|
+
const receiver = (fn, method) => {
|
|
4597
|
+
if (!ts.isArrowFunction(fn) ||
|
|
4598
|
+
fn.modifiers ||
|
|
4599
|
+
fn.type ||
|
|
4600
|
+
fn.typeParameters ||
|
|
4601
|
+
!ts.isCallExpression(fn.body))
|
|
4602
|
+
return null;
|
|
4603
|
+
const call = fn.body;
|
|
4604
|
+
const member = call.expression;
|
|
4605
|
+
if (call.typeArguments ||
|
|
4606
|
+
call.questionDotToken ||
|
|
4607
|
+
!ts.isPropertyAccessExpression(member) ||
|
|
4608
|
+
member.questionDotToken ||
|
|
4609
|
+
member.name.text !== method ||
|
|
4610
|
+
!ts.isIdentifier(member.expression))
|
|
4611
|
+
return null;
|
|
3308
4612
|
const arity = method === 'get' ? 0 : 1;
|
|
3309
4613
|
if (fn.parameters.length !== arity || call.arguments.length !== arity)
|
|
3310
4614
|
return null;
|
|
@@ -3430,6 +4734,281 @@ class ModuleTransform {
|
|
|
3430
4734
|
return;
|
|
3431
4735
|
return `[${field.name.getText(this.sf)}]`;
|
|
3432
4736
|
}
|
|
4737
|
+
/** Prove the state record is never exposed and every update owns the dependency field. */
|
|
4738
|
+
ownStateDependency(receiver, key, hook, dependencies, nestedRead) {
|
|
4739
|
+
const state = this.declarationOf(receiver);
|
|
4740
|
+
if (!state ||
|
|
4741
|
+
!ts.isBindingElement(state) ||
|
|
4742
|
+
!ts.isIdentifier(state.name) ||
|
|
4743
|
+
state.initializer ||
|
|
4744
|
+
state.dotDotDotToken)
|
|
4745
|
+
return false;
|
|
4746
|
+
const tuple = state.parent;
|
|
4747
|
+
if (!ts.isArrayBindingPattern(tuple) ||
|
|
4748
|
+
tuple.elements.length !== 2 ||
|
|
4749
|
+
tuple.elements[0] !== state)
|
|
4750
|
+
return false;
|
|
4751
|
+
const setter = tuple.elements[1];
|
|
4752
|
+
const declaration = tuple.parent;
|
|
4753
|
+
if (!ts.isBindingElement(setter) ||
|
|
4754
|
+
!ts.isIdentifier(setter.name) ||
|
|
4755
|
+
setter.initializer ||
|
|
4756
|
+
setter.dotDotDotToken ||
|
|
4757
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
4758
|
+
!declaration.initializer ||
|
|
4759
|
+
!fixedBinding(this.checker, receiver, hook) ||
|
|
4760
|
+
!initializedBefore(this.checker, receiver, hook) ||
|
|
4761
|
+
(/eval|\\u/.test(this.source) && hasEval(this.sf)))
|
|
4762
|
+
return false;
|
|
4763
|
+
const init = skipWrappers(declaration.initializer);
|
|
4764
|
+
if (!ts.isCallExpression(init) ||
|
|
4765
|
+
init.questionDotToken ||
|
|
4766
|
+
init.arguments.length !== 1 ||
|
|
4767
|
+
!ts.isIdentifier(init.expression) ||
|
|
4768
|
+
this.reactHook(init.expression) !== 'useState' ||
|
|
4769
|
+
this.options.originalReact === false ||
|
|
4770
|
+
[...this.imports.values()].some((item) => item.module === 'react' &&
|
|
4771
|
+
!item.typeOnly &&
|
|
4772
|
+
(item.namespace || item.imported === 'default')))
|
|
4773
|
+
return false;
|
|
4774
|
+
// A CommonJS import or dynamic import can expose React's mutable export object.
|
|
4775
|
+
const staticReactImports = this.sf.statements.filter((statement) => ts.isImportDeclaration(statement) &&
|
|
4776
|
+
ts.isStringLiteral(statement.moduleSpecifier) &&
|
|
4777
|
+
statement.moduleSpecifier.text === 'react').length;
|
|
4778
|
+
const reactLoads = ts.preProcessFile(this.source, true, true).importedFiles;
|
|
4779
|
+
if (reactLoads.filter((entry) => entry.fileName === 'react').length !== staticReactImports)
|
|
4780
|
+
return false;
|
|
4781
|
+
const simpleName = (name) => ts.isIdentifier(name) || ts.isStringLiteral(name) ? name.text : undefined;
|
|
4782
|
+
const required = new Set([key]);
|
|
4783
|
+
const numeric = new Set();
|
|
4784
|
+
const addField = (name) => {
|
|
4785
|
+
if (name === undefined || name === '__proto__')
|
|
4786
|
+
return false;
|
|
4787
|
+
required.add(name);
|
|
4788
|
+
return true;
|
|
4789
|
+
};
|
|
4790
|
+
const closed = renderUses(this.localUses(state.name)).every((use) => {
|
|
4791
|
+
const parent = use.parent;
|
|
4792
|
+
if (ts.isVariableDeclaration(parent) &&
|
|
4793
|
+
parent.initializer === use &&
|
|
4794
|
+
ts.isObjectBindingPattern(parent.name)) {
|
|
4795
|
+
return parent.name.elements.every((binding) => {
|
|
4796
|
+
const name = simpleName(binding.propertyName ?? binding.name);
|
|
4797
|
+
return (!binding.initializer &&
|
|
4798
|
+
!binding.dotDotDotToken &&
|
|
4799
|
+
ts.isIdentifier(binding.name) &&
|
|
4800
|
+
addField(name));
|
|
4801
|
+
});
|
|
4802
|
+
}
|
|
4803
|
+
if (!ts.isPropertyAccessExpression(parent) ||
|
|
4804
|
+
parent.expression !== use ||
|
|
4805
|
+
!addField(parent.name.text))
|
|
4806
|
+
return false;
|
|
4807
|
+
let value = parent;
|
|
4808
|
+
while (isWrapper(value.parent))
|
|
4809
|
+
value = value.parent;
|
|
4810
|
+
const outer = value.parent;
|
|
4811
|
+
if (ts.isPrefixUnaryExpression(outer) &&
|
|
4812
|
+
outer.operator !== ts.SyntaxKind.PlusPlusToken &&
|
|
4813
|
+
outer.operator !== ts.SyntaxKind.MinusMinusToken) {
|
|
4814
|
+
if (outer.operator !== ts.SyntaxKind.ExclamationToken)
|
|
4815
|
+
numeric.add(parent.name.text);
|
|
4816
|
+
return true;
|
|
4817
|
+
}
|
|
4818
|
+
if (ts.isTypeOfExpression(outer) ||
|
|
4819
|
+
ts.isVoidExpression(outer) ||
|
|
4820
|
+
(ts.isBinaryExpression(outer) &&
|
|
4821
|
+
(outer.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken ||
|
|
4822
|
+
outer.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsEqualsToken)) ||
|
|
4823
|
+
(ts.isIfStatement(outer) && outer.expression === value) ||
|
|
4824
|
+
(ts.isJsxExpression(outer) && ts.isJsxAttribute(outer.parent)) ||
|
|
4825
|
+
outer === dependencies)
|
|
4826
|
+
return true;
|
|
4827
|
+
const coercesNumber = nestedRead(parent);
|
|
4828
|
+
if (coercesNumber === undefined)
|
|
4829
|
+
return false;
|
|
4830
|
+
if (coercesNumber)
|
|
4831
|
+
numeric.add(parent.name.text);
|
|
4832
|
+
return true;
|
|
4833
|
+
});
|
|
4834
|
+
if (!closed)
|
|
4835
|
+
return false;
|
|
4836
|
+
const ownsFields = (expression, prior) => {
|
|
4837
|
+
const record = skipWrappers(expression);
|
|
4838
|
+
if (!ts.isObjectLiteralExpression(record))
|
|
4839
|
+
return false;
|
|
4840
|
+
let firstField = 0;
|
|
4841
|
+
if (prior) {
|
|
4842
|
+
const [spread] = record.properties;
|
|
4843
|
+
const uses = this.localUses(prior);
|
|
4844
|
+
if (!spread ||
|
|
4845
|
+
!ts.isSpreadAssignment(spread) ||
|
|
4846
|
+
uses.length !== 1 ||
|
|
4847
|
+
uses[0] !== spread.expression)
|
|
4848
|
+
return false;
|
|
4849
|
+
firstField = 1;
|
|
4850
|
+
}
|
|
4851
|
+
const missing = new Set(prior ? [] : required);
|
|
4852
|
+
for (const field of record.properties.slice(firstField)) {
|
|
4853
|
+
if (!ts.isPropertyAssignment(field))
|
|
4854
|
+
return false;
|
|
4855
|
+
const name = simpleName(field.name);
|
|
4856
|
+
if (name === undefined || name === '__proto__')
|
|
4857
|
+
return false;
|
|
4858
|
+
if (numeric.has(name) && !isNumericConstant(field.initializer))
|
|
4859
|
+
return false;
|
|
4860
|
+
missing.delete(name);
|
|
4861
|
+
}
|
|
4862
|
+
return missing.size === 0;
|
|
4863
|
+
};
|
|
4864
|
+
const initializer = skipWrappers(init.arguments[0]);
|
|
4865
|
+
const birth = ts.isArrowFunction(initializer) ? initializer.body : initializer;
|
|
4866
|
+
if ((ts.isArrowFunction(initializer) &&
|
|
4867
|
+
(initializer.modifiers?.length || initializer.parameters.length)) ||
|
|
4868
|
+
ts.isBlock(birth) ||
|
|
4869
|
+
!ownsFields(birth))
|
|
4870
|
+
return false;
|
|
4871
|
+
return this.localUses(setter.name).every((use) => {
|
|
4872
|
+
const call = use.parent;
|
|
4873
|
+
if (!ts.isCallExpression(call) ||
|
|
4874
|
+
call.expression !== use ||
|
|
4875
|
+
call.questionDotToken ||
|
|
4876
|
+
!ts.isExpressionStatement(call.parent) ||
|
|
4877
|
+
call.arguments.length !== 1)
|
|
4878
|
+
return false;
|
|
4879
|
+
const next = skipWrappers(call.arguments[0]);
|
|
4880
|
+
if (!ts.isArrowFunction(next))
|
|
4881
|
+
return ownsFields(next);
|
|
4882
|
+
const [prior] = next.parameters;
|
|
4883
|
+
return (!next.modifiers?.length &&
|
|
4884
|
+
next.parameters.length === 1 &&
|
|
4885
|
+
ts.isIdentifier(prior.name) &&
|
|
4886
|
+
!prior.initializer &&
|
|
4887
|
+
!prior.dotDotDotToken &&
|
|
4888
|
+
!ts.isBlock(next.body) &&
|
|
4889
|
+
ownsFields(next.body, prior.name));
|
|
4890
|
+
});
|
|
4891
|
+
}
|
|
4892
|
+
ownDataDependency(member, hook, dependencies, coercesNumber, nestedRead) {
|
|
4893
|
+
const receiver = skipWrappers(member.expression);
|
|
4894
|
+
const name = ts.isPropertyAccessExpression(member)
|
|
4895
|
+
? member.name
|
|
4896
|
+
: skipWrappers(member.argumentExpression);
|
|
4897
|
+
if (!ts.isStringLiteralLike(name) &&
|
|
4898
|
+
!(ts.isPropertyAccessExpression(member) && ts.isIdentifier(name)))
|
|
4899
|
+
return false;
|
|
4900
|
+
const key = name.text;
|
|
4901
|
+
if (key === '__proto__' || !ts.isIdentifier(receiver))
|
|
4902
|
+
return false;
|
|
4903
|
+
if (this.ownStateDependency(receiver, key, hook, dependencies, nestedRead))
|
|
4904
|
+
return true;
|
|
4905
|
+
const init = this.constValue(receiver);
|
|
4906
|
+
const object = init && skipWrappers(init);
|
|
4907
|
+
const owner = ts.findAncestor(hook, ts.isFunctionLike);
|
|
4908
|
+
if (!object ||
|
|
4909
|
+
!ts.isObjectLiteralExpression(object) ||
|
|
4910
|
+
!owner ||
|
|
4911
|
+
!fixedBinding(this.checker, receiver, hook) ||
|
|
4912
|
+
this.localUses(receiver).some((name) => !declaresName(name)) ||
|
|
4913
|
+
reads(owner, (name) => name.text === 'eval'))
|
|
4914
|
+
return false;
|
|
4915
|
+
// Spreads, computed names and accessors can redefine the property.
|
|
4916
|
+
let owns = false;
|
|
4917
|
+
for (const field of object.properties) {
|
|
4918
|
+
const name = field.name;
|
|
4919
|
+
if (!(ts.isPropertyAssignment(field) && !ts.isComputedPropertyName(field.name)) &&
|
|
4920
|
+
!(ts.isShorthandPropertyAssignment(field) && !field.objectAssignmentInitializer))
|
|
4921
|
+
return false;
|
|
4922
|
+
if (!name || (!ts.isIdentifier(name) && !ts.isStringLiteralLike(name))) {
|
|
4923
|
+
// A numeric name can override the corresponding string name.
|
|
4924
|
+
if (coercesNumber)
|
|
4925
|
+
return false;
|
|
4926
|
+
continue;
|
|
4927
|
+
}
|
|
4928
|
+
if (name.text !== key)
|
|
4929
|
+
continue;
|
|
4930
|
+
owns = true;
|
|
4931
|
+
if (coercesNumber &&
|
|
4932
|
+
!(ts.isPropertyAssignment(field) && isNumericConstant(field.initializer)))
|
|
4933
|
+
return false;
|
|
4934
|
+
}
|
|
4935
|
+
return owns;
|
|
4936
|
+
}
|
|
4937
|
+
checkHookDependencyReads(node, deps) {
|
|
4938
|
+
const list = skipWrappers(deps);
|
|
4939
|
+
if (!this.emitsTsrx || !ts.isArrayLiteralExpression(list))
|
|
4940
|
+
return;
|
|
4941
|
+
const read = (expr, visit, coercesNumber = false) => {
|
|
4942
|
+
const member = skipWrappers(expr);
|
|
4943
|
+
if (ts.isTypeOfExpression(member) || ts.isVoidExpression(member))
|
|
4944
|
+
return read(member.expression, visit);
|
|
4945
|
+
if (ts.isPrefixUnaryExpression(member) &&
|
|
4946
|
+
member.operator !== ts.SyntaxKind.PlusPlusToken &&
|
|
4947
|
+
member.operator !== ts.SyntaxKind.MinusMinusToken)
|
|
4948
|
+
return read(member.operand, visit, member.operator !== ts.SyntaxKind.ExclamationToken);
|
|
4949
|
+
if (ts.isConditionalExpression(member)) {
|
|
4950
|
+
const condition = skipWrappers(member.condition);
|
|
4951
|
+
if (condition.kind === ts.SyntaxKind.TrueKeyword)
|
|
4952
|
+
return read(member.whenTrue, visit, coercesNumber);
|
|
4953
|
+
if (condition.kind === ts.SyntaxKind.FalseKeyword)
|
|
4954
|
+
return read(member.whenFalse, visit, coercesNumber);
|
|
4955
|
+
return (read(condition, visit) ??
|
|
4956
|
+
read(member.whenTrue, visit, coercesNumber) ??
|
|
4957
|
+
read(member.whenFalse, visit, coercesNumber));
|
|
4958
|
+
}
|
|
4959
|
+
if (ts.isBinaryExpression(member)) {
|
|
4960
|
+
const op = member.operatorToken.kind;
|
|
4961
|
+
if (op === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
4962
|
+
op === ts.SyntaxKind.BarBarToken ||
|
|
4963
|
+
op === ts.SyntaxKind.QuestionQuestionToken) {
|
|
4964
|
+
// Only a falsy left value can become the result of &&.
|
|
4965
|
+
const left = read(member.left, visit, coercesNumber && op !== ts.SyntaxKind.AmpersandAmpersandToken);
|
|
4966
|
+
if (left)
|
|
4967
|
+
return left;
|
|
4968
|
+
const condition = literalCondition(member.left);
|
|
4969
|
+
if (condition === undefined ||
|
|
4970
|
+
(op === ts.SyntaxKind.AmpersandAmpersandToken && condition === 'truthy') ||
|
|
4971
|
+
(op === ts.SyntaxKind.BarBarToken && condition !== 'truthy') ||
|
|
4972
|
+
(op === ts.SyntaxKind.QuestionQuestionToken && condition === 'nullish'))
|
|
4973
|
+
return read(member.right, visit, coercesNumber);
|
|
4974
|
+
}
|
|
4975
|
+
}
|
|
4976
|
+
if (ts.isPropertyAccessExpression(member) || ts.isElementAccessExpression(member))
|
|
4977
|
+
return visit(member, coercesNumber);
|
|
4978
|
+
};
|
|
4979
|
+
// The closed-state proof must account for every field read in this list.
|
|
4980
|
+
let members;
|
|
4981
|
+
const nestedRead = (member) => {
|
|
4982
|
+
if (!members) {
|
|
4983
|
+
const reachable = new Map();
|
|
4984
|
+
for (const entry of list.elements) {
|
|
4985
|
+
read(entry, (dep, coercesNumber) => {
|
|
4986
|
+
reachable.set(dep, coercesNumber);
|
|
4987
|
+
return undefined;
|
|
4988
|
+
});
|
|
4989
|
+
}
|
|
4990
|
+
members = reachable;
|
|
4991
|
+
}
|
|
4992
|
+
return members.get(member);
|
|
4993
|
+
};
|
|
4994
|
+
let opaqueCoercion = false;
|
|
4995
|
+
const check = (member, coercesNumber) => {
|
|
4996
|
+
if (!this.ownDataDependency(member, node, list, coercesNumber, nestedRead)) {
|
|
4997
|
+
opaqueCoercion = coercesNumber;
|
|
4998
|
+
return member;
|
|
4999
|
+
}
|
|
5000
|
+
};
|
|
5001
|
+
let opaque;
|
|
5002
|
+
for (const entry of list.elements) {
|
|
5003
|
+
opaque = read(entry, check);
|
|
5004
|
+
if (opaque)
|
|
5005
|
+
break;
|
|
5006
|
+
}
|
|
5007
|
+
if (opaque)
|
|
5008
|
+
this.note(RULE.deps, 'error', opaque, opaqueCoercion
|
|
5009
|
+
? 'This hook dependency reads a property during render. It may run a getter owned by React or execute user code during numeric coercion. Keep the component in React or verify the property before moving it to native SSR.'
|
|
5010
|
+
: 'This hook dependency reads a property during render and may run a getter owned by React. Keep the component in React or verify the getter before moving it to native SSR.');
|
|
5011
|
+
}
|
|
3433
5012
|
/**
|
|
3434
5013
|
* React runs a hook with an omitted or undefined list on every render.
|
|
3435
5014
|
* Octane spells that schedule with `null` or a fresh dependency.
|
|
@@ -3571,20 +5150,141 @@ class ModuleTransform {
|
|
|
3571
5150
|
? `${this.require('octane', 'createRoot', false)}(${containerText}).render(${elementText})`
|
|
3572
5151
|
: `${this.require('octane', 'hydrateRoot', false)}(${containerText}, ${elementText})`;
|
|
3573
5152
|
}
|
|
3574
|
-
/**
|
|
3575
|
-
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
const
|
|
3581
|
-
|
|
5153
|
+
/** Report a retained React hook when the server render receives its JSX descriptor. */
|
|
5154
|
+
checkBufferedReactComponent(call, argument, module) {
|
|
5155
|
+
const check = this.options.localReactHook;
|
|
5156
|
+
if (!check || ts.isOptionalChain(call))
|
|
5157
|
+
return;
|
|
5158
|
+
const callee = call.expression;
|
|
5159
|
+
const receiver = ts.isPropertyAccessExpression(callee) ? callee.expression : callee;
|
|
5160
|
+
if (!ts.isIdentifier(receiver))
|
|
5161
|
+
return;
|
|
5162
|
+
const binding = this.importOf(receiver);
|
|
5163
|
+
const declaration = this.declarationOf(receiver);
|
|
5164
|
+
if (!binding ||
|
|
5165
|
+
binding.typeOnly ||
|
|
5166
|
+
!declaration ||
|
|
5167
|
+
!(ts.isImportSpecifier(declaration) || ts.isNamespaceImport(declaration)) ||
|
|
5168
|
+
ts.findAncestor(declaration, ts.isImportDeclaration)?.attributes ||
|
|
5169
|
+
!this.options.automaticReactJsx ||
|
|
5170
|
+
!this.options.originalReactServerEntry?.(module) ||
|
|
5171
|
+
((this.source.includes('eval') || this.source.includes('\\')) && hasEval(this.sf)))
|
|
5172
|
+
return;
|
|
5173
|
+
// Another import of the module can expose its mutable CommonJS exports.
|
|
5174
|
+
if ([...this.imports].some(([decl, entry]) => REACT_MODULES[entry.module] === 'octane/server' &&
|
|
5175
|
+
!entry.typeOnly &&
|
|
5176
|
+
(ts.isNamespaceImport(decl)
|
|
5177
|
+
? !this.namespaceMemberCalls(decl)
|
|
5178
|
+
: entry.namespace || entry.imported === 'default')))
|
|
5179
|
+
return;
|
|
5180
|
+
const retained = (expression) => {
|
|
5181
|
+
const value = skipParens(expression);
|
|
5182
|
+
if (!ts.isJsxSelfClosingElement(value) || this.retainedReactOutput(value))
|
|
5183
|
+
return;
|
|
5184
|
+
const imported = this.directImportedValue(value.tagName);
|
|
5185
|
+
if (!imported || !isComponentName(imported.tag.text))
|
|
5186
|
+
return;
|
|
5187
|
+
const hook = check(imported.entry.module, imported.entry.imported, false);
|
|
5188
|
+
if (typeof hook === 'string')
|
|
5189
|
+
return { tag: imported.tag.text, hook };
|
|
5190
|
+
};
|
|
5191
|
+
const value = skipParens(argument);
|
|
5192
|
+
let component = retained(value);
|
|
5193
|
+
if (ts.isIdentifier(value)) {
|
|
5194
|
+
const local = this.declarationOf(value);
|
|
5195
|
+
if (!local ||
|
|
5196
|
+
!ts.isVariableDeclaration(local) ||
|
|
5197
|
+
!ts.isIdentifier(local.name) ||
|
|
5198
|
+
!local.initializer ||
|
|
5199
|
+
!initializedBefore(this.checker, value, call) ||
|
|
5200
|
+
ts.findAncestor(local, ts.isFunctionLike) !== ts.findAncestor(call, ts.isFunctionLike))
|
|
5201
|
+
return;
|
|
5202
|
+
component = retained(local.initializer);
|
|
5203
|
+
if (!component || !this.closedBufferedValue(local, value, call, (expr) => !!retained(expr)))
|
|
5204
|
+
return;
|
|
5205
|
+
}
|
|
5206
|
+
if (component)
|
|
5207
|
+
this.note(BUFFERED_SSR_REACT_COMPONENT, 'error', call, `The migrated server renderer can render ${component.tag}, which stays on React and calls ${component.hook}. Migrate the component with this file or keep the render in React.`);
|
|
5208
|
+
}
|
|
5209
|
+
/** The descriptor can only go to this renderer or be replaced by another proven descriptor. */
|
|
5210
|
+
closedBufferedValue(local, value, call, retained) {
|
|
5211
|
+
const block = local.parent.parent.parent;
|
|
5212
|
+
const kind = ts.getCombinedNodeFlags(local) & ts.NodeFlags.BlockScoped;
|
|
5213
|
+
const canAssign = kind === ts.NodeFlags.Let &&
|
|
5214
|
+
ts.isReturnStatement(call.parent) &&
|
|
5215
|
+
call.parent.parent === block;
|
|
5216
|
+
if (!canAssign && kind !== ts.NodeFlags.Const)
|
|
5217
|
+
return false;
|
|
5218
|
+
return renderUses(this.localUses(value)).every((ref) => {
|
|
5219
|
+
if (ref === local.name)
|
|
5220
|
+
return true;
|
|
5221
|
+
const write = ref.parent;
|
|
5222
|
+
const statement = write.parent;
|
|
5223
|
+
return (canAssign &&
|
|
5224
|
+
ts.isBinaryExpression(write) &&
|
|
5225
|
+
write.left === ref &&
|
|
5226
|
+
write.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
5227
|
+
ts.isExpressionStatement(statement) &&
|
|
5228
|
+
statement.end <= call.pos &&
|
|
5229
|
+
(statement.parent === block ||
|
|
5230
|
+
(ts.isIfStatement(statement.parent) && statement.parent.parent === block)) &&
|
|
5231
|
+
retained(write.right));
|
|
5232
|
+
});
|
|
5233
|
+
}
|
|
5234
|
+
/** JSX retained for a prop spread or quoted reference still uses the migrated root. */
|
|
5235
|
+
checkLocalReactRoot(call, receiver, root, retained) {
|
|
5236
|
+
const local = this.options.localReactRootComponent;
|
|
5237
|
+
const element = skipParens(call.arguments[0]);
|
|
5238
|
+
if (!local || !ts.isJsxSelfClosingElement(element) || !ts.isIdentifier(element.tagName))
|
|
5239
|
+
return;
|
|
5240
|
+
const tag = element.tagName;
|
|
5241
|
+
const leaf = this.importOf(tag);
|
|
5242
|
+
const attributes = element.attributes.properties;
|
|
5243
|
+
const factoryName = ts.isPropertyAccessExpression(root.expression)
|
|
5244
|
+
? root.expression.expression
|
|
5245
|
+
: root.expression;
|
|
5246
|
+
const rootImport = this.importOf(factoryName);
|
|
5247
|
+
const factoryBinding = ts.isIdentifier(factoryName) && this.declarationOf(factoryName);
|
|
5248
|
+
if (!isComponentName(tag.text) ||
|
|
5249
|
+
(retained &&
|
|
5250
|
+
(!(attributes.some(ts.isJsxSpreadAttribute) ||
|
|
5251
|
+
(attributes.some(quotedPropWithReference) &&
|
|
5252
|
+
!attributes.some((attr) => ts.isJsxAttribute(attr) && /^(key|ref)$/.test(attr.name.getText(this.sf))))) ||
|
|
5253
|
+
this.islands.has(element) ||
|
|
5254
|
+
this.reactRendererBodies.has(element) ||
|
|
5255
|
+
this.unrenderable.has(element))) ||
|
|
5256
|
+
!leaf ||
|
|
5257
|
+
leaf.typeOnly ||
|
|
5258
|
+
ts.findAncestor(this.declarationOf(tag), ts.isImportDeclaration)?.attributes ||
|
|
5259
|
+
rootImport?.module !== 'react-dom/client' ||
|
|
5260
|
+
rootImport.typeOnly ||
|
|
5261
|
+
!factoryBinding ||
|
|
5262
|
+
!(ts.isImportSpecifier(factoryBinding) || ts.isNamespaceImport(factoryBinding)) ||
|
|
5263
|
+
ts.findAncestor(factoryBinding, ts.isImportDeclaration)?.attributes ||
|
|
5264
|
+
ts.isOptionalChain(call) ||
|
|
5265
|
+
ts.isOptionalChain(root) ||
|
|
5266
|
+
!this.options.automaticReactJsx ||
|
|
5267
|
+
((this.source.includes('eval') || this.source.includes('\\u')) && hasEval(this.sf)))
|
|
5268
|
+
return;
|
|
5269
|
+
if (ts.isIdentifier(receiver)) {
|
|
5270
|
+
const declaration = root.parent;
|
|
5271
|
+
if (!ts.isVariableDeclaration(declaration) ||
|
|
5272
|
+
!ts.isIdentifier(declaration.name) ||
|
|
5273
|
+
ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export ||
|
|
5274
|
+
(ts.getCombinedNodeFlags(declaration) & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const)
|
|
5275
|
+
return;
|
|
5276
|
+
if ((!atInitializedModuleHandle(declaration, call) &&
|
|
5277
|
+
!initializedBefore(this.checker, receiver, call)) ||
|
|
5278
|
+
rootHandleEscapes(this.localUses(declaration.name)))
|
|
5279
|
+
return;
|
|
5280
|
+
}
|
|
5281
|
+
const hook = local(leaf.module, leaf.imported);
|
|
5282
|
+
if (hook)
|
|
5283
|
+
this.note(RULE.rootRender, 'error', tag, `The migrated root renders ${tag.text}, but the unchanged ${leaf.module} export calls ${hook === 'useIntl' ? "React Intl's" : "React's"} ${hook}. Migrate the component or keep this root under React DOM.`);
|
|
3582
5284
|
}
|
|
3583
5285
|
/**
|
|
3584
|
-
*
|
|
3585
|
-
*
|
|
3586
|
-
* element: the generic element renderer stays out of the bundle. So are
|
|
3587
|
-
* `hydrateRoot(c, <App />)` and the server renderers' element argument.
|
|
5286
|
+
* Mount component roots directly when the element carries no key or
|
|
5287
|
+
* factory configuration. The same applies to hydration and server roots.
|
|
3588
5288
|
*
|
|
3589
5289
|
* @param argument the element, the call's last argument
|
|
3590
5290
|
*/
|
|
@@ -3615,12 +5315,12 @@ class ModuleTransform {
|
|
|
3615
5315
|
return null;
|
|
3616
5316
|
if (!isComponentName(tag.getText(this.sf).split('.').pop() ?? ''))
|
|
3617
5317
|
return null;
|
|
5318
|
+
const attributes = node.attributes.properties;
|
|
5319
|
+
// A spread can carry a key. Keep quoted references for the JSX parser.
|
|
5320
|
+
if (!attributes.every(ts.isJsxAttribute) || attributes.some(quotedPropWithReference))
|
|
5321
|
+
return null;
|
|
3618
5322
|
const props = [];
|
|
3619
|
-
for (const property of
|
|
3620
|
-
if (ts.isJsxSpreadAttribute(property)) {
|
|
3621
|
-
props.push(`...${this.emit(property.expression, PLAIN)}`);
|
|
3622
|
-
continue;
|
|
3623
|
-
}
|
|
5323
|
+
for (const property of attributes) {
|
|
3624
5324
|
const name = property.name.getText(this.sf);
|
|
3625
5325
|
if (name === 'key' || name === 'ref')
|
|
3626
5326
|
return null;
|
|
@@ -3628,8 +5328,12 @@ class ModuleTransform {
|
|
|
3628
5328
|
const value = property.initializer;
|
|
3629
5329
|
if (!value)
|
|
3630
5330
|
props.push(`${key}: true`);
|
|
3631
|
-
else if (ts.isStringLiteral(value))
|
|
3632
|
-
|
|
5331
|
+
else if (ts.isStringLiteral(value)) {
|
|
5332
|
+
const text = /[\\\r\n]/.test(value.text)
|
|
5333
|
+
? JSON.stringify(value.text)
|
|
5334
|
+
: value.getText(this.sf);
|
|
5335
|
+
props.push(`${key}: ${text}`);
|
|
5336
|
+
}
|
|
3633
5337
|
else if (ts.isJsxExpression(value) && value.expression)
|
|
3634
5338
|
props.push(`${key}: ${this.emit(value.expression, PLAIN)}`);
|
|
3635
5339
|
else
|
|
@@ -3646,13 +5350,69 @@ class ModuleTransform {
|
|
|
3646
5350
|
return null;
|
|
3647
5351
|
if (!isComponentName(type.getText(this.sf).split('.').pop() ?? ''))
|
|
3648
5352
|
return null;
|
|
3649
|
-
|
|
3650
|
-
|
|
5353
|
+
// The element factory extracts the config's key and snapshots its enumerable props.
|
|
5354
|
+
if (props && (!isNullish(props) || (ts.isIdentifier(props) && this.declarationOf(props))))
|
|
5355
|
+
return null;
|
|
5356
|
+
return this.emit(type, PLAIN);
|
|
3651
5357
|
}
|
|
3652
5358
|
return null;
|
|
3653
5359
|
}
|
|
5360
|
+
rewriteFreshHandle(statement, call) {
|
|
5361
|
+
const [ref, factory, argument] = call.arguments;
|
|
5362
|
+
const deps = argument && skipParens(argument);
|
|
5363
|
+
const body = statement.parent;
|
|
5364
|
+
if (!this.strong ||
|
|
5365
|
+
this.target !== 'tsrx' ||
|
|
5366
|
+
!ts.isIdentifier(call.expression) ||
|
|
5367
|
+
this.reactHook(call.expression) !== 'useImperativeHandle' ||
|
|
5368
|
+
call.questionDotToken ||
|
|
5369
|
+
call.arguments.length < 2 ||
|
|
5370
|
+
call.arguments.length > 3 ||
|
|
5371
|
+
(deps && (!ts.isIdentifier(deps) || deps.text !== 'undefined' || this.declarationOf(deps))) ||
|
|
5372
|
+
!ts.isIdentifier(ref) ||
|
|
5373
|
+
!ts.isArrowFunction(factory) ||
|
|
5374
|
+
factory.modifiers?.length ||
|
|
5375
|
+
factory.typeParameters?.length ||
|
|
5376
|
+
factory.parameters.length ||
|
|
5377
|
+
!factory.type ||
|
|
5378
|
+
ts.isBlock(factory.body) ||
|
|
5379
|
+
!ts.isBlock(body) ||
|
|
5380
|
+
!ts.isFunctionDeclaration(body.parent) ||
|
|
5381
|
+
body.parent.parent !== this.sf ||
|
|
5382
|
+
body.parent.asteriskToken ||
|
|
5383
|
+
body.parent.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) ||
|
|
5384
|
+
!this.isComponent(body.parent))
|
|
5385
|
+
return null;
|
|
5386
|
+
const value = skipParens(factory.body);
|
|
5387
|
+
if (!ts.isObjectLiteralExpression(value) ||
|
|
5388
|
+
!value.properties.length ||
|
|
5389
|
+
!value.properties.every((field) => ts.isPropertyAssignment(field) &&
|
|
5390
|
+
(ts.isIdentifier(field.name) || ts.isStringLiteral(field.name)) &&
|
|
5391
|
+
field.name.text !== '__proto__' &&
|
|
5392
|
+
ts.isArrowFunction(field.initializer)) ||
|
|
5393
|
+
!fixedBinding(this.checker, ref, call) ||
|
|
5394
|
+
!initializedBefore(this.checker, ref, call) ||
|
|
5395
|
+
reads(body.parent, (id) => id.text === 'eval') ||
|
|
5396
|
+
this.lostComments(statement.getFullStart(), call.end, '').length)
|
|
5397
|
+
return null;
|
|
5398
|
+
// Strong tracks this reference. A const arrow could be auto-memoized.
|
|
5399
|
+
let name = 'createHandle';
|
|
5400
|
+
while (this.declared.has(name) || this.readsGlobal(name))
|
|
5401
|
+
name += '_';
|
|
5402
|
+
this.declared.add(name);
|
|
5403
|
+
this.count(RULE.deps);
|
|
5404
|
+
return (`let ${name} = ${this.emit(factory, PLAIN)};\n${this.indentOf(statement)}` +
|
|
5405
|
+
this.span(call, call.getStart(this.sf), factory.getStart(this.sf), PLAIN) +
|
|
5406
|
+
`${name}, [${name}]` +
|
|
5407
|
+
this.source.slice((argument ?? factory).end, statement.end));
|
|
5408
|
+
}
|
|
3654
5409
|
rewriteStatement(node) {
|
|
3655
5410
|
const call = node.expression;
|
|
5411
|
+
if (ts.isCallExpression(call)) {
|
|
5412
|
+
const handle = this.rewriteFreshHandle(node, call);
|
|
5413
|
+
if (handle !== null)
|
|
5414
|
+
return handle;
|
|
5415
|
+
}
|
|
3656
5416
|
if (ts.isCallExpression(call) &&
|
|
3657
5417
|
call.arguments.length === 0 &&
|
|
3658
5418
|
ts.isPropertyAccessExpression(call.expression) &&
|
|
@@ -3977,11 +5737,30 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
3977
5737
|
: `Class component \`${node.name?.text ?? 'class'}\` needs a rewrite as a function component.`);
|
|
3978
5738
|
}
|
|
3979
5739
|
checkDefaultProps(node) {
|
|
3980
|
-
if (node.operatorToken.kind
|
|
3981
|
-
ts.isPropertyAccessExpression(node.left)
|
|
3982
|
-
|
|
3983
|
-
|
|
5740
|
+
if (node.operatorToken.kind !== ts.SyntaxKind.EqualsToken ||
|
|
5741
|
+
!ts.isPropertyAccessExpression(node.left))
|
|
5742
|
+
return;
|
|
5743
|
+
const member = node.left.name.text === 'defaultProps' ? node.left : node.left.expression;
|
|
5744
|
+
if (ts.isExpressionStatement(node.parent) &&
|
|
5745
|
+
node.parent.parent === this.sf &&
|
|
5746
|
+
ts.isPropertyAccessExpression(member) &&
|
|
5747
|
+
member.name.text === 'defaultProps' &&
|
|
5748
|
+
ts.isIdentifier(member.expression) &&
|
|
5749
|
+
[...this.nativeIntl].some((item) => (item.propertyName ?? item.name).text === 'IntlProvider')) {
|
|
5750
|
+
const declaration = this.declarationOf(member.expression);
|
|
5751
|
+
const imported = declaration && this.imports.get(declaration);
|
|
5752
|
+
if (declaration &&
|
|
5753
|
+
imported?.module === 'react-intl' &&
|
|
5754
|
+
imported.imported === 'IntlProvider' &&
|
|
5755
|
+
!imported.typeOnly &&
|
|
5756
|
+
!ts.findAncestor(declaration, ts.isImportDeclaration)?.attributes) {
|
|
5757
|
+
this.note(INTL_PROVIDER_DEFAULTS, 'error', node, 'The generated IntlProvider cannot share the React class defaults assigned here. Keep the provider and its consumers in the original React render.');
|
|
5758
|
+
return;
|
|
5759
|
+
}
|
|
3984
5760
|
}
|
|
5761
|
+
if (node.left.name.text !== 'defaultProps')
|
|
5762
|
+
return;
|
|
5763
|
+
this.note(RULE.defaultProps, 'warning', node, 'Function components ignore `defaultProps`; move the defaults into the props destructuring.');
|
|
3985
5764
|
}
|
|
3986
5765
|
// ---------------------------------------------------------------- JSX
|
|
3987
5766
|
rewriteAttributes(node, ctx) {
|
|
@@ -4037,8 +5816,22 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4037
5816
|
}
|
|
4038
5817
|
else if ((name === 'onChange' || name === 'onChangeCapture') && this.isTextEntry(element)) {
|
|
4039
5818
|
const input = name === 'onChange' ? 'onInput' : 'onInputCapture';
|
|
4040
|
-
|
|
4041
|
-
|
|
5819
|
+
const existing = attributeOf(element, input);
|
|
5820
|
+
const changesOrder = !spread &&
|
|
5821
|
+
name === 'onChangeCapture' &&
|
|
5822
|
+
(tag === 'input' || tag === 'textarea') &&
|
|
5823
|
+
!attributes.some((peer) => peer !== attribute &&
|
|
5824
|
+
ts.isJsxAttribute(peer) &&
|
|
5825
|
+
ts.isIdentifier(peer.name) &&
|
|
5826
|
+
peer.name.text === name) &&
|
|
5827
|
+
this.mayHaveJsxHandler(attribute) &&
|
|
5828
|
+
this.mayHaveJsxHandler(attributes.findLast((peer) => ts.isJsxAttribute(peer) &&
|
|
5829
|
+
ts.isIdentifier(peer.name) &&
|
|
5830
|
+
peer.name.text === 'onInput'));
|
|
5831
|
+
if (existing || changesOrder) {
|
|
5832
|
+
this.note(RULE.nativeChange, 'warning', attribute, existing
|
|
5833
|
+
? `\`${name}\` beside \`${input}\` now fires on commit (blur), not per edit.`
|
|
5834
|
+
: 'Renaming `onChangeCapture` beside `onInput` can change callback order. Review the event scheduling. The retained `onChangeCapture` runs on native change.');
|
|
4042
5835
|
out += this.emit(attribute, PLAIN);
|
|
4043
5836
|
}
|
|
4044
5837
|
else {
|
|
@@ -4050,8 +5843,24 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4050
5843
|
out += this.emit(attribute, PLAIN);
|
|
4051
5844
|
}
|
|
4052
5845
|
}
|
|
5846
|
+
if (ts.isJsxSelfClosingElement(element) && this.inlineIntlMessages.has(element))
|
|
5847
|
+
out += ` inlineHost={${this.require('octane/react', 'ReactCompatInline', false)}}`;
|
|
4053
5848
|
return out + this.source.slice(cursor, node.end);
|
|
4054
5849
|
}
|
|
5850
|
+
mayHaveJsxHandler(attribute) {
|
|
5851
|
+
const initializer = attribute?.initializer;
|
|
5852
|
+
if (!initializer || !ts.isJsxExpression(initializer) || !initializer.expression)
|
|
5853
|
+
return false;
|
|
5854
|
+
const value = skipWrappers(initializer.expression);
|
|
5855
|
+
if (ts.isExternalModule(this.sf) && ts.isIdentifier(value) && value.text === 'undefined') {
|
|
5856
|
+
const symbol = this.checker.getSymbolAtLocation(value);
|
|
5857
|
+
if (symbol && this.checker.isUndefinedSymbol(symbol))
|
|
5858
|
+
return false;
|
|
5859
|
+
}
|
|
5860
|
+
return (value.kind !== ts.SyntaxKind.NullKeyword &&
|
|
5861
|
+
value.kind !== ts.SyntaxKind.FalseKeyword &&
|
|
5862
|
+
!ts.isVoidExpression(value));
|
|
5863
|
+
}
|
|
4055
5864
|
emitInitializer(attribute) {
|
|
4056
5865
|
if (!attribute.initializer)
|
|
4057
5866
|
return '';
|
|
@@ -4074,6 +5883,18 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4074
5883
|
const value = type.initializer;
|
|
4075
5884
|
if (value && ts.isStringLiteral(value))
|
|
4076
5885
|
return TEXT_INPUT_TYPES.has(value.text);
|
|
5886
|
+
if (tag === 'input' && value && ts.isJsxExpression(value) && value.expression) {
|
|
5887
|
+
const expression = skipWrappers(value.expression);
|
|
5888
|
+
// A spread can also supply the input handler that a rename would overwrite.
|
|
5889
|
+
if (ts.isStringLiteralLike(expression) &&
|
|
5890
|
+
TEXT_INPUT_TYPES.has(expression.text) &&
|
|
5891
|
+
!element.attributes.properties.some((attribute) => ts.isJsxSpreadAttribute(attribute) ||
|
|
5892
|
+
(attribute !== type &&
|
|
5893
|
+
ts.isIdentifier(attribute.name) &&
|
|
5894
|
+
attribute.name.text === 'type'))) {
|
|
5895
|
+
return true;
|
|
5896
|
+
}
|
|
5897
|
+
}
|
|
4077
5898
|
this.note(RULE.nativeChange, 'warning', element, 'This `<input>` has a dynamic `type`; if it can be a text field, use `onInput` for per-edit updates.');
|
|
4078
5899
|
return false;
|
|
4079
5900
|
}
|
|
@@ -4088,12 +5909,12 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4088
5909
|
// in React; `@if` or `@for` there would hand it a fragment instead.
|
|
4089
5910
|
const declaration = ts.isIdentifier(tag) ? this.declarationOf(tag) : null;
|
|
4090
5911
|
const entry = this.importOf(tag);
|
|
4091
|
-
const imported = entry && !entry.namespace && !entry.typeOnly
|
|
5912
|
+
const imported = entry && !entry.namespace && !entry.typeOnly ? entry : null;
|
|
4092
5913
|
if ((declaration &&
|
|
4093
5914
|
(ts.isVariableDeclaration(declaration) || ts.isFunctionDeclaration(declaration)) &&
|
|
4094
5915
|
this.descriptorComponents.has(declaration)) ||
|
|
4095
5916
|
(this.hasReactBridge && this.reactCompatTag(tag)) ||
|
|
4096
|
-
(imported && this.options.descriptorChild?.(imported))) {
|
|
5917
|
+
(imported?.module.startsWith('.') && this.options.descriptorChild?.(imported))) {
|
|
4097
5918
|
return this.span(node, node.getStart(this.sf), node.end, DESCRIPTOR);
|
|
4098
5919
|
}
|
|
4099
5920
|
const name = this.calleeName(tag);
|
|
@@ -4103,16 +5924,22 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4103
5924
|
const binding = declaration && ts.isFunctionDeclaration(declaration)
|
|
4104
5925
|
? declaration.getStart(this.sf)
|
|
4105
5926
|
: imported;
|
|
4106
|
-
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
5927
|
+
const alias = imported && !imported.module.startsWith('.') ? imported : null;
|
|
5928
|
+
const plan = radixSlot || (binding !== null && this.options.elementChild?.(binding));
|
|
5929
|
+
const child = plan && this.singleElementChild(node);
|
|
5930
|
+
if (child && plan === true && !alias) {
|
|
5931
|
+
this.count(RULE.elementChildProp);
|
|
5932
|
+
return (this.span(opening, opening.getStart(this.sf), opening.end - 1, ctx) +
|
|
5933
|
+
' children={' +
|
|
5934
|
+
this.emit(child, PLAIN) +
|
|
5935
|
+
'} />');
|
|
4115
5936
|
}
|
|
5937
|
+
if (child &&
|
|
5938
|
+
plan === 'review-alias' &&
|
|
5939
|
+
alias &&
|
|
5940
|
+
declaration &&
|
|
5941
|
+
!ts.findAncestor(declaration, ts.isImportDeclaration)?.attributes)
|
|
5942
|
+
this.note(ELEMENT_CHILD_ALIAS, 'error', opening, `TypeScript resolves \`${alias.module}\` to a selected component with an element-only slot. Verify the runtime alias before passing the element with \`children={...}\`.`);
|
|
4116
5943
|
}
|
|
4117
5944
|
if (name === 'StrictMode') {
|
|
4118
5945
|
// Octane's StrictMode is a pass-through wrapper.
|
|
@@ -4121,8 +5948,8 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4121
5948
|
}
|
|
4122
5949
|
if (name === 'Suspense' && ctx.template)
|
|
4123
5950
|
return this.rewriteSuspense(node);
|
|
4124
|
-
if (ts.isPropertyAccessExpression(tag) &&
|
|
4125
|
-
if (tag.
|
|
5951
|
+
if (ts.isPropertyAccessExpression(tag) && tag.name.text === 'Consumer') {
|
|
5952
|
+
if (this.isContext(tag.expression, tag))
|
|
4126
5953
|
return this.rewriteConsumer(node, ctx, tag);
|
|
4127
5954
|
}
|
|
4128
5955
|
return null;
|
|
@@ -4132,7 +5959,7 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4132
5959
|
* `createContext` call here, or an export the project knows is one. Only
|
|
4133
5960
|
* a package's export, which the codemod cannot see, is judged by its name.
|
|
4134
5961
|
*/
|
|
4135
|
-
isContext(node) {
|
|
5962
|
+
isContext(node, member) {
|
|
4136
5963
|
if (!ts.isIdentifier(node))
|
|
4137
5964
|
return false;
|
|
4138
5965
|
const declaration = this.declarationOf(node);
|
|
@@ -4140,10 +5967,15 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4140
5967
|
return !!declaration.initializer && this.createsContext(declaration.initializer);
|
|
4141
5968
|
}
|
|
4142
5969
|
const entry = declaration ? this.imports.get(declaration) : null;
|
|
4143
|
-
if (!entry || entry.namespace)
|
|
5970
|
+
if (!entry || entry.namespace || entry.typeOnly)
|
|
4144
5971
|
return false;
|
|
4145
5972
|
const name = entry.imported === 'default' ? entry.local : entry.imported;
|
|
4146
|
-
|
|
5973
|
+
const context = this.options.isContext?.(entry.module, entry.imported, entry.local);
|
|
5974
|
+
if (context !== 'review-alias')
|
|
5975
|
+
return context ?? /Context$/.test(name);
|
|
5976
|
+
if (member && !ts.isJsxClosingElement(member.parent))
|
|
5977
|
+
this.note(CONTEXT_ALIAS, 'error', member, `TypeScript resolves \`${entry.module}\` to a selected local export. Verify the runtime alias and its ${member.name.text} member before migrating it.`);
|
|
5978
|
+
return false;
|
|
4147
5979
|
}
|
|
4148
5980
|
/**
|
|
4149
5981
|
* Whether `node` evaluates to a `createContext` result on every branch.
|
|
@@ -4155,6 +5987,114 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4155
5987
|
}
|
|
4156
5988
|
return ts.isCallExpression(node) && this.calleeName(node.expression) === 'createContext';
|
|
4157
5989
|
}
|
|
5990
|
+
localProviderReadChange(context, capture) {
|
|
5991
|
+
if (!this.source.includes('defineProperty') &&
|
|
5992
|
+
!(capture && this.source.includes('assign')) &&
|
|
5993
|
+
!this.source.includes('\\u'))
|
|
5994
|
+
return;
|
|
5995
|
+
const initial = this.constValue(context);
|
|
5996
|
+
const factory = initial && skipParens(initial);
|
|
5997
|
+
if (!factory ||
|
|
5998
|
+
!ts.isCallExpression(factory) ||
|
|
5999
|
+
this.reactHook(factory.expression) !== 'createContext')
|
|
6000
|
+
return;
|
|
6001
|
+
let change = 'getter';
|
|
6002
|
+
const declaration = this.declarationOf(context);
|
|
6003
|
+
const references = this.localUses(context);
|
|
6004
|
+
const found = references.some((reference) => {
|
|
6005
|
+
let target = reference;
|
|
6006
|
+
while (isWrapper(target.parent) && target.parent.expression === target)
|
|
6007
|
+
target = target.parent;
|
|
6008
|
+
const call = target.parent;
|
|
6009
|
+
if (!ts.isCallExpression(call) ||
|
|
6010
|
+
call.arguments[0] !== target ||
|
|
6011
|
+
call.questionDotToken ||
|
|
6012
|
+
!ts.isExpressionStatement(call.parent) ||
|
|
6013
|
+
call.parent.parent !== this.sf ||
|
|
6014
|
+
factory.end > call.pos ||
|
|
6015
|
+
call.end > context.pos)
|
|
6016
|
+
return false;
|
|
6017
|
+
const method = skipWrappers(call.expression);
|
|
6018
|
+
const object = ts.isPropertyAccessExpression(method) && skipWrappers(method.expression);
|
|
6019
|
+
if (!ts.isPropertyAccessExpression(method) ||
|
|
6020
|
+
method.questionDotToken ||
|
|
6021
|
+
!object ||
|
|
6022
|
+
!ts.isIdentifier(object) ||
|
|
6023
|
+
object.text !== 'Object' ||
|
|
6024
|
+
this.declarationOf(object)?.getSourceFile().hasNoDefaultLib !== true)
|
|
6025
|
+
return false;
|
|
6026
|
+
const [, keyArgument, descriptorArgument] = call.arguments;
|
|
6027
|
+
if (capture && method.name.text === 'assign' && call.arguments.length === 2) {
|
|
6028
|
+
if (this.sf.statements[this.sf.statements.indexOf(capture) - 1] !== call.parent ||
|
|
6029
|
+
hasEval(this.sf) ||
|
|
6030
|
+
this.localUses(object).some((use) => !inTypeNode(use)) ||
|
|
6031
|
+
references.some((use) => use !== reference &&
|
|
6032
|
+
!(declaration && ts.isVariableDeclaration(declaration) && use === declaration.name) &&
|
|
6033
|
+
!inTypeNode(use)))
|
|
6034
|
+
return false;
|
|
6035
|
+
const data = skipWrappers(keyArgument);
|
|
6036
|
+
if (!ts.isObjectLiteralExpression(data) || data.properties.length !== 1)
|
|
6037
|
+
return false;
|
|
6038
|
+
const [property] = data.properties;
|
|
6039
|
+
const name = property.name;
|
|
6040
|
+
if (!name ||
|
|
6041
|
+
(!ts.isIdentifier(name) && !ts.isStringLiteral(name)) ||
|
|
6042
|
+
name.text !== 'Provider' ||
|
|
6043
|
+
!ts.isPropertyAssignment(property))
|
|
6044
|
+
return false;
|
|
6045
|
+
const value = skipWrappers(property.initializer);
|
|
6046
|
+
const binding = ts.isIdentifier(value) && this.declarationOf(value);
|
|
6047
|
+
if (!ts.isArrowFunction(value) &&
|
|
6048
|
+
!ts.isFunctionExpression(value) &&
|
|
6049
|
+
!(binding &&
|
|
6050
|
+
ts.isFunctionDeclaration(binding) &&
|
|
6051
|
+
binding.parent === this.sf &&
|
|
6052
|
+
binding.body &&
|
|
6053
|
+
binding.name &&
|
|
6054
|
+
!binding.modifiers?.length &&
|
|
6055
|
+
renderUses(this.localUses(binding.name)).every((ref) => ref === value)))
|
|
6056
|
+
return false;
|
|
6057
|
+
change = 'Provider replacement';
|
|
6058
|
+
return true;
|
|
6059
|
+
}
|
|
6060
|
+
const key = keyArgument && skipWrappers(keyArgument);
|
|
6061
|
+
const descriptor = descriptorArgument && skipWrappers(descriptorArgument);
|
|
6062
|
+
if (method.name.text !== 'defineProperty' ||
|
|
6063
|
+
!key ||
|
|
6064
|
+
!ts.isStringLiteralLike(key) ||
|
|
6065
|
+
key.text !== 'Provider' ||
|
|
6066
|
+
!descriptor ||
|
|
6067
|
+
!ts.isObjectLiteralExpression(descriptor))
|
|
6068
|
+
return false;
|
|
6069
|
+
let getter = false;
|
|
6070
|
+
let setter = false;
|
|
6071
|
+
for (const property of descriptor.properties) {
|
|
6072
|
+
const name = property.name;
|
|
6073
|
+
if (!name || (!ts.isIdentifier(name) && !ts.isStringLiteral(name)))
|
|
6074
|
+
return false;
|
|
6075
|
+
if (name.text === 'configurable' || name.text === 'enumerable')
|
|
6076
|
+
continue;
|
|
6077
|
+
if (name.text !== 'get' && name.text !== 'set')
|
|
6078
|
+
return false;
|
|
6079
|
+
const value = ts.isPropertyAssignment(property) ? skipWrappers(property.initializer) : null;
|
|
6080
|
+
if (!ts.isMethodDeclaration(property) &&
|
|
6081
|
+
!(value && (ts.isArrowFunction(value) || ts.isFunctionExpression(value))))
|
|
6082
|
+
return false;
|
|
6083
|
+
if (name.text === 'get') {
|
|
6084
|
+
if (getter)
|
|
6085
|
+
return false;
|
|
6086
|
+
getter = true;
|
|
6087
|
+
}
|
|
6088
|
+
else {
|
|
6089
|
+
if (setter)
|
|
6090
|
+
return false;
|
|
6091
|
+
setter = true;
|
|
6092
|
+
}
|
|
6093
|
+
}
|
|
6094
|
+
return getter;
|
|
6095
|
+
});
|
|
6096
|
+
return found ? change : undefined;
|
|
6097
|
+
}
|
|
4158
6098
|
/**
|
|
4159
6099
|
* `typeof children === 'function'` also holds for a compiled children
|
|
4160
6100
|
* block, which renders through Octane rather than being called, so the
|
|
@@ -4220,12 +6160,9 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4220
6160
|
const output = isJsx(body) ? this.emit(body, TEMPLATE) : `<>{${this.emit(body, PLAIN)}}</>`;
|
|
4221
6161
|
return `@{ const ${param} = ${useContext}(${this.emit(tag.expression, PLAIN)}); ${output} }`;
|
|
4222
6162
|
}
|
|
4223
|
-
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
*/
|
|
4227
|
-
rewriteSuspense(node) {
|
|
4228
|
-
const attributes = node.openingElement.attributes.properties.filter((a) => !this.removed.has(a));
|
|
6163
|
+
suspenseFallback(node, allowKey = false) {
|
|
6164
|
+
const attributes = node.openingElement.attributes.properties.filter((a) => !this.removed.has(a) &&
|
|
6165
|
+
!(allowKey && ts.isJsxAttribute(a) && a.name.getText(this.sf) === 'key'));
|
|
4229
6166
|
const fallback = attributes[0];
|
|
4230
6167
|
if (attributes.length !== 1 ||
|
|
4231
6168
|
!ts.isJsxAttribute(fallback) ||
|
|
@@ -4233,8 +6170,20 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4233
6170
|
!fallback.initializer) {
|
|
4234
6171
|
return null;
|
|
4235
6172
|
}
|
|
4236
|
-
let pending;
|
|
4237
6173
|
const value = fallback.initializer;
|
|
6174
|
+
return ts.isStringLiteral(value) || (ts.isJsxExpression(value) && value.expression)
|
|
6175
|
+
? value
|
|
6176
|
+
: null;
|
|
6177
|
+
}
|
|
6178
|
+
/**
|
|
6179
|
+
* `<Suspense fallback={f}>…</Suspense>` is the template boundary
|
|
6180
|
+
* `@try { … } @pending { f }`.
|
|
6181
|
+
*/
|
|
6182
|
+
rewriteSuspense(node) {
|
|
6183
|
+
const value = this.suspenseFallback(node);
|
|
6184
|
+
if (!value)
|
|
6185
|
+
return null;
|
|
6186
|
+
let pending;
|
|
4238
6187
|
if (ts.isStringLiteral(value))
|
|
4239
6188
|
pending = `<>{${value.getText(this.sf)}}</>`;
|
|
4240
6189
|
else if (ts.isJsxExpression(value) && value.expression) {
|
|
@@ -4441,22 +6390,23 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4441
6390
|
* The template directive equivalent to a renderable expression, as
|
|
4442
6391
|
* relative text, or null.
|
|
4443
6392
|
*/
|
|
4444
|
-
directive(expression) {
|
|
6393
|
+
directive(expression, componentRoot = false) {
|
|
4445
6394
|
const node = skipParens(expression);
|
|
4446
6395
|
if (ts.isConditionalExpression(node))
|
|
4447
|
-
return this.ifDirective(node);
|
|
6396
|
+
return this.ifDirective(node, componentRoot);
|
|
4448
6397
|
if (ts.isBinaryExpression(node) &&
|
|
4449
6398
|
node.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken &&
|
|
4450
6399
|
(this.isBoolean(node.left) ||
|
|
4451
6400
|
(this.strictTypes && isBooleanGuardType(this.typeOf(node.left))))) {
|
|
4452
|
-
const
|
|
6401
|
+
const condition = componentRoot ? literalCondition(node.left) : undefined;
|
|
6402
|
+
const arm = this.arm(node.right, componentRoot && (condition === undefined || condition === 'truthy'));
|
|
4453
6403
|
if (arm === null || arm === '')
|
|
4454
6404
|
return null;
|
|
4455
6405
|
this.count(RULE.if);
|
|
4456
6406
|
return this.block(`@if (${this.emit(node.left, PLAIN)})`, arm);
|
|
4457
6407
|
}
|
|
4458
6408
|
if (ts.isCallExpression(node))
|
|
4459
|
-
return this.forDirective(node);
|
|
6409
|
+
return this.forDirective(node, componentRoot);
|
|
4460
6410
|
return null;
|
|
4461
6411
|
}
|
|
4462
6412
|
/**
|
|
@@ -4464,13 +6414,18 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4464
6414
|
* directive, or '' for nothing. An arm that is only a value is null; the
|
|
4465
6415
|
* caller decides whether it may become a `{value}` hole.
|
|
4466
6416
|
*/
|
|
4467
|
-
arm(expression) {
|
|
6417
|
+
arm(expression, componentRoot = false) {
|
|
4468
6418
|
const node = skipParens(expression);
|
|
4469
|
-
if (isJsx(node))
|
|
6419
|
+
if (isJsx(node)) {
|
|
6420
|
+
if (componentRoot)
|
|
6421
|
+
this.checkNativeOutput(node);
|
|
4470
6422
|
return this.relative(this.emit(node, TEMPLATE), node);
|
|
6423
|
+
}
|
|
4471
6424
|
if (isNullish(node))
|
|
4472
6425
|
return '';
|
|
4473
|
-
|
|
6426
|
+
const directive = this.directive(node, componentRoot);
|
|
6427
|
+
// A retained map is a JSX hole, which needs a fragment inside a directive arm.
|
|
6428
|
+
return directive?.startsWith('{') ? `<>${directive}</>` : directive;
|
|
4474
6429
|
}
|
|
4475
6430
|
/** The same unkeyed input keeps its live value when only its default changes. */
|
|
4476
6431
|
conditionalInputDefault(node) {
|
|
@@ -4501,12 +6456,13 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4501
6456
|
const condition = this.emit(test, PLAIN);
|
|
4502
6457
|
return `<input defaultValue={${condition} ? ${JSON.stringify(yes)} : ${JSON.stringify(no)}} />`;
|
|
4503
6458
|
}
|
|
4504
|
-
ifDirective(node) {
|
|
6459
|
+
ifDirective(node, componentRoot = false) {
|
|
4505
6460
|
const shared = this.conditionalInputDefault(node);
|
|
4506
6461
|
if (shared !== null)
|
|
4507
6462
|
return shared;
|
|
4508
|
-
|
|
4509
|
-
let
|
|
6463
|
+
const literal = componentRoot ? literalCondition(node.condition) : undefined;
|
|
6464
|
+
let whenTrue = this.arm(node.whenTrue, componentRoot && (literal === undefined || literal === 'truthy'));
|
|
6465
|
+
let whenFalse = this.arm(node.whenFalse, componentRoot && literal !== 'truthy');
|
|
4510
6466
|
// A ternary between two values is a value; one with an element on
|
|
4511
6467
|
// either side is control flow, and a value arm is a hole in a fragment:
|
|
4512
6468
|
// a bare `{value}` arm is a JavaScript block, not a hole.
|
|
@@ -4525,19 +6481,29 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4525
6481
|
const then = this.block(`@if (${condition})`, whenTrue);
|
|
4526
6482
|
return whenFalse === '' ? then : then + ' ' + this.block('@else', whenFalse);
|
|
4527
6483
|
}
|
|
6484
|
+
reportOpaqueMap(node) {
|
|
6485
|
+
this.note(RULE.for, 'error', node, 'This receiver can override .map() and inspect the React elements its callback returns. Keep this list in React or verify its map behavior before porting it.');
|
|
6486
|
+
}
|
|
4528
6487
|
/**
|
|
4529
6488
|
* `items.map((item, i) => <Row key={item.id} />)` is
|
|
4530
6489
|
* `@for (const item of items; index i; key item.id) { <Row /> }`.
|
|
4531
6490
|
*/
|
|
4532
|
-
forDirective(node) {
|
|
6491
|
+
forDirective(node, componentRoot) {
|
|
4533
6492
|
const callee = node.expression;
|
|
4534
|
-
if (!ts.isPropertyAccessExpression(callee) ||
|
|
4535
|
-
callee.name.text !== 'map' ||
|
|
4536
|
-
node.arguments.length !== 1) {
|
|
6493
|
+
if (!ts.isPropertyAccessExpression(callee) || callee.name.text !== 'map')
|
|
4537
6494
|
return null;
|
|
4538
|
-
}
|
|
4539
6495
|
const fn = node.arguments[0];
|
|
4540
|
-
if (!
|
|
6496
|
+
if (!fn)
|
|
6497
|
+
return null;
|
|
6498
|
+
const callback = skipWrappers(fn);
|
|
6499
|
+
// Even a retained arrow can observe the enclosing renderer's invocation.
|
|
6500
|
+
if (ts.isArrowFunction(callback) &&
|
|
6501
|
+
(ts.isBlock(callback.body) ? returnsJsx(callback.body.statements) : hasJsx(callback.body)) &&
|
|
6502
|
+
readsFunctionContext(callback, this.checker)) {
|
|
6503
|
+
this.note(MAP_CALLBACK_SCOPE, 'error', node, 'The migrated component can change this arrow callback’s access to this, arguments, or new.target. Review these reads before porting the map.');
|
|
6504
|
+
return null;
|
|
6505
|
+
}
|
|
6506
|
+
if (node.arguments.length !== 1 || !(ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)))
|
|
4541
6507
|
return null;
|
|
4542
6508
|
if (fn.parameters.length === 0 || fn.parameters.length > 2)
|
|
4543
6509
|
return null;
|
|
@@ -4549,13 +6515,13 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4549
6515
|
// `@for (const items of items)` would read `items` in its own TDZ.
|
|
4550
6516
|
const bound = new Set([...bindingNames(item.name), ...(index ? bindingNames(index.name) : [])]);
|
|
4551
6517
|
const iterable = callee.expression;
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
6518
|
+
const receiver = skipWrappers(iterable);
|
|
6519
|
+
const literal = ts.isArrayLiteralExpression(receiver) ? receiver : null;
|
|
6520
|
+
if (literal?.elements.some(ts.isOmittedExpression))
|
|
6521
|
+
return null;
|
|
6522
|
+
if (literal && reads(iterable, (name) => bound.has(name.text))) {
|
|
4557
6523
|
// Direct eval can observe the callback arguments or the generated loop's bindings.
|
|
4558
|
-
if (
|
|
6524
|
+
if (reads(node, (name) => name.text === 'eval' && !isPropertyName(name)))
|
|
4559
6525
|
return null;
|
|
4560
6526
|
if (reads(iterable, (name) => {
|
|
4561
6527
|
if (!bound.has(name.text) || isPropertyName(name))
|
|
@@ -4565,8 +6531,8 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4565
6531
|
}))
|
|
4566
6532
|
return null;
|
|
4567
6533
|
}
|
|
4568
|
-
//
|
|
4569
|
-
if (ts.isFunctionExpression(fn) &&
|
|
6534
|
+
// Retaining an ordinary callback keeps its own function context.
|
|
6535
|
+
if (ts.isFunctionExpression(fn) && readsFunctionContext(fn.body, this.checker))
|
|
4570
6536
|
return null;
|
|
4571
6537
|
// A row that writes an outer variable (`n++`) needs every row to run in
|
|
4572
6538
|
// order; an `@for` row can be skipped.
|
|
@@ -4578,6 +6544,15 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4578
6544
|
if (ts.isBlock(fn.body)) {
|
|
4579
6545
|
const statements = fn.body.statements;
|
|
4580
6546
|
const last = statements[statements.length - 1];
|
|
6547
|
+
if (last &&
|
|
6548
|
+
ts.isSwitchStatement(last) &&
|
|
6549
|
+
!literal &&
|
|
6550
|
+
!isPrimitiveLiteral(last.expression) &&
|
|
6551
|
+
!fn.asteriskToken &&
|
|
6552
|
+
!fn.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) &&
|
|
6553
|
+
last.caseBlock.clauses.some((clause) => clauseReturnsJsx(clause.statements) === true)) {
|
|
6554
|
+
this.reportOpaqueMap(node);
|
|
6555
|
+
}
|
|
4581
6556
|
if (!last || !ts.isReturnStatement(last) || !last.expression)
|
|
4582
6557
|
return null;
|
|
4583
6558
|
setup = statements.slice(0, -1);
|
|
@@ -4592,22 +6567,6 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4592
6567
|
const root = skipParens(output);
|
|
4593
6568
|
if (!isJsx(root) && !ts.isConditionalExpression(root))
|
|
4594
6569
|
return null;
|
|
4595
|
-
// Custom maps and explicit array holes keep the original callback dispatch.
|
|
4596
|
-
const receiver = skipWrappers(iterable);
|
|
4597
|
-
const initializer = ts.isIdentifier(receiver) ? this.constValue(receiver) : null;
|
|
4598
|
-
const array = initializer ? skipWrappers(initializer) : receiver;
|
|
4599
|
-
if (ts.isArrayLiteralExpression(array) && array.elements.some(ts.isOmittedExpression))
|
|
4600
|
-
return null;
|
|
4601
|
-
const declarations = this.typeOf(iterable)
|
|
4602
|
-
.getNonNullableType()
|
|
4603
|
-
.getProperty('map')?.declarations;
|
|
4604
|
-
if (declarations?.some((declaration) => {
|
|
4605
|
-
const file = declaration.getSourceFile();
|
|
4606
|
-
return ((file !== esLib() && file.fileName !== ES5_LIB_PATH) ||
|
|
4607
|
-
!ts.isInterfaceDeclaration(declaration.parent) ||
|
|
4608
|
-
!['Array', 'ReadonlyArray'].includes(declaration.parent.name.text));
|
|
4609
|
-
}))
|
|
4610
|
-
return null;
|
|
4611
6570
|
const attribute = rowKey(root);
|
|
4612
6571
|
let key = null;
|
|
4613
6572
|
if (attribute) {
|
|
@@ -4626,6 +6585,46 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4626
6585
|
this.note(RULE.for, 'warning', attribute, 'This row runs setup before reading its key, so the list stays a `.map()`.');
|
|
4627
6586
|
return null;
|
|
4628
6587
|
}
|
|
6588
|
+
}
|
|
6589
|
+
if (!literal)
|
|
6590
|
+
this.reportOpaqueMap(node);
|
|
6591
|
+
if (attribute && key && !this.stableRowKey(key, item, iterable)) {
|
|
6592
|
+
const open = attribute.parent.parent;
|
|
6593
|
+
const children = ts.isJsxElement(root) ? root.children : [];
|
|
6594
|
+
const dynamic = (value) => {
|
|
6595
|
+
if (!value || ts.isJsxText(value) || ts.isStringLiteral(value))
|
|
6596
|
+
return false;
|
|
6597
|
+
if (!ts.isJsxExpression(value))
|
|
6598
|
+
return true;
|
|
6599
|
+
if (!value.expression)
|
|
6600
|
+
return false;
|
|
6601
|
+
const expression = skipWrappers(value.expression);
|
|
6602
|
+
return !(isPrimitiveLiteral(expression) ||
|
|
6603
|
+
(ts.isIdentifier(expression) &&
|
|
6604
|
+
fixedBinding(this.checker, expression, expression) &&
|
|
6605
|
+
initializedBefore(this.checker, expression, expression)));
|
|
6606
|
+
};
|
|
6607
|
+
if (!ts.isIdentifier(open.tagName) ||
|
|
6608
|
+
open.attributes.properties.some((a) => a !== attribute && (ts.isJsxSpreadAttribute(a) || dynamic(a.initializer))) ||
|
|
6609
|
+
children.some(dynamic)) {
|
|
6610
|
+
const ordered = this.orderedMapRow(node, fn, open, attribute, children);
|
|
6611
|
+
if (ordered !== null)
|
|
6612
|
+
return ordered;
|
|
6613
|
+
this.note(RULE.for, 'error', attribute, 'This row can evaluate JSX props or children before its key. Keep their original evaluation order before porting the list.');
|
|
6614
|
+
return null;
|
|
6615
|
+
}
|
|
6616
|
+
}
|
|
6617
|
+
if (!literal)
|
|
6618
|
+
return null;
|
|
6619
|
+
if (fn.asteriskToken || fn.modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword)) {
|
|
6620
|
+
this.note(RULE.for, 'error', node, 'An @for row would discard this callback’s promise or iterator. Review the retained .map() call for suspension or iteration.');
|
|
6621
|
+
return null;
|
|
6622
|
+
}
|
|
6623
|
+
if (fn.parameters.some((parameter) => readsFunctionContext(parameter, this.checker))) {
|
|
6624
|
+
this.note(MAP_CALLBACK_SCOPE, 'error', node, 'An @for row cannot preserve this callback’s access to this, arguments, or new.target. Keep the original map callback until these reads are migrated explicitly.');
|
|
6625
|
+
return null;
|
|
6626
|
+
}
|
|
6627
|
+
if (attribute) {
|
|
4629
6628
|
this.removed.add(attribute);
|
|
4630
6629
|
const opening = attribute.parent.parent;
|
|
4631
6630
|
if (ts.isJsxOpeningElement(opening) &&
|
|
@@ -4633,11 +6632,24 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4633
6632
|
this.calleeName(opening.tagName) === 'Fragment')
|
|
4634
6633
|
this.bare.add(opening.parent);
|
|
4635
6634
|
}
|
|
6635
|
+
if ((componentRoot || this.nativeOutputMaps?.has(node)) &&
|
|
6636
|
+
literal.elements.some((element) => !ts.isSpreadElement(element)) &&
|
|
6637
|
+
(!skipsRow || this.mapRowCanRender(item, literal, setup)))
|
|
6638
|
+
this.checkNativeOutput(root);
|
|
4636
6639
|
const arm = ts.isConditionalExpression(root)
|
|
4637
6640
|
? this.ifDirective(root)
|
|
4638
6641
|
: this.relative(this.emit(root, TEMPLATE), root);
|
|
4639
6642
|
if (arm === null)
|
|
4640
6643
|
return null;
|
|
6644
|
+
if (index && this.patternReadsOrdinal(item.name, index)) {
|
|
6645
|
+
this.note(MAP_PARAMETER_TDZ, 'error', node, 'An @for row initializes its index before this parameter pattern can read it. Review the original callback’s initialization order.');
|
|
6646
|
+
}
|
|
6647
|
+
const mutable = (name) => !fixedBinding(this.checker, name, name, undefined, (use) => !isPropertyName(use));
|
|
6648
|
+
if ((index && ts.isIdentifier(index.name) && mutable(index.name)) ||
|
|
6649
|
+
(!ts.isIdentifier(item.name) &&
|
|
6650
|
+
reads(item.name, (name) => ts.isBindingElement(name.parent) && name.parent.name === name && mutable(name)))) {
|
|
6651
|
+
this.note(MAP_PARAMETER_WRITE, 'error', node, 'Cannot prove this map parameter stays unchanged in an @for row. Exclude the source from migration or keep the parameter unchanged.');
|
|
6652
|
+
}
|
|
4641
6653
|
this.count(RULE.for);
|
|
4642
6654
|
let source = this.emit(callee.expression, PLAIN);
|
|
4643
6655
|
if (callee.questionDotToken)
|
|
@@ -4656,17 +6668,198 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4656
6668
|
const statements = this.relative(this.span(fn.body, setup[0].getStart(this.sf), setup[setup.length - 1].end, PLAIN), setup[0]);
|
|
4657
6669
|
return this.block(header, statements + '\n' + arm);
|
|
4658
6670
|
}
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
6671
|
+
patternReadsOrdinal(pattern, ordinal) {
|
|
6672
|
+
const nonCoercingBinary = (op) => op === ts.SyntaxKind.CommaToken ||
|
|
6673
|
+
op === ts.SyntaxKind.EqualsEqualsEqualsToken ||
|
|
6674
|
+
op === ts.SyntaxKind.ExclamationEqualsEqualsToken;
|
|
6675
|
+
const safeObjectSpread = (expression) => {
|
|
6676
|
+
const source = skipWrappers(expression);
|
|
6677
|
+
// A fresh record proven below has only own data properties to copy.
|
|
6678
|
+
return ts.isObjectLiteralExpression(source) && safePrefix(source);
|
|
6679
|
+
};
|
|
6680
|
+
const safePrefix = (expr) => {
|
|
6681
|
+
const value = skipWrappers(expr);
|
|
6682
|
+
if (isPrimitiveLiteral(value))
|
|
6683
|
+
return true;
|
|
6684
|
+
if (ts.isVoidExpression(value))
|
|
6685
|
+
return safePrefix(value.expression);
|
|
6686
|
+
if (ts.isBinaryExpression(value))
|
|
6687
|
+
return (nonCoercingBinary(value.operatorToken.kind) &&
|
|
6688
|
+
safePrefix(value.left) &&
|
|
6689
|
+
safePrefix(value.right));
|
|
6690
|
+
if (ts.isArrayLiteralExpression(value))
|
|
6691
|
+
return value.elements.every((element) => ts.isOmittedExpression(element) || safePrefix(element));
|
|
6692
|
+
return (ts.isObjectLiteralExpression(value) &&
|
|
6693
|
+
value.properties.every((property) => ts.isSpreadAssignment(property)
|
|
6694
|
+
? safeObjectSpread(property.expression)
|
|
6695
|
+
: ts.isPropertyAssignment(property) &&
|
|
6696
|
+
(!ts.isComputedPropertyName(property.name) ||
|
|
6697
|
+
isPrimitiveLiteral(property.name.expression)) &&
|
|
6698
|
+
safePrefix(property.initializer)));
|
|
6699
|
+
};
|
|
6700
|
+
const branchCondition = (expression) => {
|
|
6701
|
+
const value = skipWrappers(expression);
|
|
6702
|
+
// A fresh array with literal spreads is truthy if its iterators finish.
|
|
6703
|
+
// Branch detection can use that result even when iteration can throw.
|
|
6704
|
+
if (ts.isArrayLiteralExpression(value) &&
|
|
6705
|
+
value.elements.every((element) => {
|
|
6706
|
+
if (!ts.isSpreadElement(element))
|
|
6707
|
+
return ts.isOmittedExpression(element) || safePrefix(element);
|
|
6708
|
+
const source = skipWrappers(element.expression);
|
|
6709
|
+
return ts.isArrayLiteralExpression(source) && safePrefix(source);
|
|
6710
|
+
}))
|
|
6711
|
+
return 'truthy';
|
|
6712
|
+
if (!safePrefix(value))
|
|
6713
|
+
return undefined;
|
|
6714
|
+
// For the allowed unary + and -, the operand has the same truthiness.
|
|
6715
|
+
const literal = ts.isPrefixUnaryExpression(value) ? skipWrappers(value.operand) : value;
|
|
6716
|
+
if (ts.isBigIntLiteral(literal))
|
|
6717
|
+
return /^(?:0[xXoObB])?0+n$/.test(literal.text) ? 'falsy' : 'truthy';
|
|
6718
|
+
return literalCondition(literal);
|
|
6719
|
+
};
|
|
6720
|
+
// Once a value can interrupt evaluation, a later read is no longer proven.
|
|
6721
|
+
const readsInOrder = (expressions) => {
|
|
6722
|
+
for (const expression of expressions) {
|
|
6723
|
+
// The source is evaluated before iteration or property copying.
|
|
6724
|
+
if (ts.isSpreadElement(expression))
|
|
6725
|
+
return readsOrdinal(expression.expression);
|
|
6726
|
+
if (readsOrdinal(expression))
|
|
6727
|
+
return true;
|
|
6728
|
+
if (!safePrefix(expression))
|
|
6729
|
+
return false;
|
|
6730
|
+
}
|
|
4664
6731
|
return false;
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
|
|
4668
|
-
|
|
4669
|
-
|
|
6732
|
+
};
|
|
6733
|
+
const readsOrdinal = (expr) => {
|
|
6734
|
+
if (!expr)
|
|
6735
|
+
return false;
|
|
6736
|
+
const read = skipWrappers(expr);
|
|
6737
|
+
if (ts.isIdentifier(read))
|
|
6738
|
+
return this.declarationOf(read) === ordinal;
|
|
6739
|
+
if (ts.isTypeOfExpression(read) || ts.isVoidExpression(read) || ts.isDeleteExpression(read))
|
|
6740
|
+
return readsOrdinal(read.expression);
|
|
6741
|
+
if (ts.isPrefixUnaryExpression(read))
|
|
6742
|
+
return (read.operator !== ts.SyntaxKind.PlusPlusToken &&
|
|
6743
|
+
read.operator !== ts.SyntaxKind.MinusMinusToken &&
|
|
6744
|
+
readsOrdinal(read.operand));
|
|
6745
|
+
if (ts.isPropertyAccessExpression(read))
|
|
6746
|
+
return readsOrdinal(read.expression);
|
|
6747
|
+
if (ts.isConditionalExpression(read)) {
|
|
6748
|
+
if (readsOrdinal(read.condition))
|
|
6749
|
+
return true;
|
|
6750
|
+
const condition = branchCondition(read.condition);
|
|
6751
|
+
return (condition !== undefined &&
|
|
6752
|
+
readsOrdinal(condition === 'truthy' ? read.whenTrue : read.whenFalse));
|
|
6753
|
+
}
|
|
6754
|
+
if (ts.isTemplateExpression(read)) {
|
|
6755
|
+
for (const { expression } of read.templateSpans) {
|
|
6756
|
+
if (readsOrdinal(expression))
|
|
6757
|
+
return true;
|
|
6758
|
+
// Each interpolation is coerced to text before the next one runs.
|
|
6759
|
+
if (!isPrimitiveLiteral(expression))
|
|
6760
|
+
return false;
|
|
6761
|
+
}
|
|
6762
|
+
return false;
|
|
6763
|
+
}
|
|
6764
|
+
if (ts.isArrayLiteralExpression(read))
|
|
6765
|
+
return readsInOrder(read.elements.filter((element) => !ts.isOmittedExpression(element)));
|
|
6766
|
+
if (ts.isObjectLiteralExpression(read)) {
|
|
6767
|
+
for (const property of read.properties) {
|
|
6768
|
+
if (ts.isSpreadAssignment(property)) {
|
|
6769
|
+
if (readsOrdinal(property.expression))
|
|
6770
|
+
return true;
|
|
6771
|
+
if (!safeObjectSpread(property.expression))
|
|
6772
|
+
return false;
|
|
6773
|
+
continue;
|
|
6774
|
+
}
|
|
6775
|
+
if (ts.isComputedPropertyName(property.name)) {
|
|
6776
|
+
const key = property.name.expression;
|
|
6777
|
+
if (readsOrdinal(key))
|
|
6778
|
+
return true;
|
|
6779
|
+
if (!isPrimitiveLiteral(key))
|
|
6780
|
+
return false;
|
|
6781
|
+
}
|
|
6782
|
+
if (ts.isShorthandPropertyAssignment(property))
|
|
6783
|
+
return readsOrdinal(property.name);
|
|
6784
|
+
if (ts.isPropertyAssignment(property)) {
|
|
6785
|
+
if (readsOrdinal(property.initializer))
|
|
6786
|
+
return true;
|
|
6787
|
+
if (!safePrefix(property.initializer))
|
|
6788
|
+
return false;
|
|
6789
|
+
}
|
|
6790
|
+
}
|
|
6791
|
+
return false;
|
|
6792
|
+
}
|
|
6793
|
+
if (ts.isElementAccessExpression(read)) {
|
|
6794
|
+
if (readsOrdinal(read.expression))
|
|
6795
|
+
return true;
|
|
6796
|
+
const receiver = skipWrappers(read.expression);
|
|
6797
|
+
// A safe prefix can still produce a nullish receiver.
|
|
6798
|
+
return (safePrefix(receiver) &&
|
|
6799
|
+
(!ts.isOptionalChain(read) ||
|
|
6800
|
+
(!ts.isBinaryExpression(receiver) && literalCondition(receiver) !== 'nullish')) &&
|
|
6801
|
+
readsOrdinal(read.argumentExpression));
|
|
6802
|
+
}
|
|
6803
|
+
if (ts.isCallExpression(read)) {
|
|
6804
|
+
if (readsOrdinal(read.expression))
|
|
6805
|
+
return true;
|
|
6806
|
+
const callee = skipWrappers(read.expression);
|
|
6807
|
+
if (ts.isArrowFunction(callee) &&
|
|
6808
|
+
!callee.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) &&
|
|
6809
|
+
callee.parameters.length === 0 &&
|
|
6810
|
+
read.arguments.length === 0) {
|
|
6811
|
+
if (!ts.isBlock(callee.body))
|
|
6812
|
+
return readsOrdinal(callee.body);
|
|
6813
|
+
if (callee.body.statements.length !== 1)
|
|
6814
|
+
return false;
|
|
6815
|
+
const [statement] = callee.body.statements;
|
|
6816
|
+
return ts.isReturnStatement(statement) && readsOrdinal(statement.expression);
|
|
6817
|
+
}
|
|
6818
|
+
// Optional calls can skip their arguments, and earlier arguments or a
|
|
6819
|
+
// member getter can interrupt evaluation before the ordinal read.
|
|
6820
|
+
if (!ts.isArrowFunction(callee) && (ts.isOptionalChain(read) || !ts.isIdentifier(callee)))
|
|
6821
|
+
return false;
|
|
6822
|
+
// A binding in the current parameter list may still be uninitialized.
|
|
6823
|
+
const declaration = ts.isIdentifier(callee) && this.declarationOf(callee);
|
|
6824
|
+
if (declaration &&
|
|
6825
|
+
(ts.isParameter(declaration) || ts.isBindingElement(declaration)) &&
|
|
6826
|
+
ts.findAncestor(declaration, ts.isParameter)?.parent === ordinal.parent) {
|
|
6827
|
+
const element = ts.findAncestor(read, ts.isBindingElement);
|
|
6828
|
+
if (!element || declaration.end > element.pos)
|
|
6829
|
+
return false;
|
|
6830
|
+
}
|
|
6831
|
+
return readsInOrder(read.arguments);
|
|
6832
|
+
}
|
|
6833
|
+
if (!ts.isBinaryExpression(read))
|
|
6834
|
+
return false;
|
|
6835
|
+
const op = read.operatorToken.kind;
|
|
6836
|
+
if (op === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
6837
|
+
op === ts.SyntaxKind.BarBarToken ||
|
|
6838
|
+
op === ts.SyntaxKind.QuestionQuestionToken) {
|
|
6839
|
+
if (readsOrdinal(read.left))
|
|
6840
|
+
return true;
|
|
6841
|
+
const condition = branchCondition(read.left);
|
|
6842
|
+
if (condition === undefined)
|
|
6843
|
+
return false;
|
|
6844
|
+
const reachesRight = op === ts.SyntaxKind.AmpersandAmpersandToken
|
|
6845
|
+
? condition === 'truthy'
|
|
6846
|
+
: op === ts.SyntaxKind.BarBarToken
|
|
6847
|
+
? condition !== 'truthy'
|
|
6848
|
+
: condition === 'nullish';
|
|
6849
|
+
return reachesRight && readsOrdinal(read.right);
|
|
6850
|
+
}
|
|
6851
|
+
// Ordinary non-assignment operators read operands before coercion.
|
|
6852
|
+
// A private brand operand is not a proven prefix, so the scan stops there.
|
|
6853
|
+
return ((op < ts.SyntaxKind.FirstAssignment || op > ts.SyntaxKind.LastAssignment) &&
|
|
6854
|
+
readsInOrder([read.left, read.right]));
|
|
6855
|
+
};
|
|
6856
|
+
return (!ts.isIdentifier(pattern) &&
|
|
6857
|
+
pattern.elements.some((element) => ts.isBindingElement(element) &&
|
|
6858
|
+
(readsOrdinal(element.initializer) ||
|
|
6859
|
+
(element.propertyName &&
|
|
6860
|
+
ts.isComputedPropertyName(element.propertyName) &&
|
|
6861
|
+
readsOrdinal(element.propertyName.expression)) ||
|
|
6862
|
+
this.patternReadsOrdinal(element.name, ordinal))));
|
|
4670
6863
|
}
|
|
4671
6864
|
/**
|
|
4672
6865
|
* Whether `fn` assigns a variable declared outside it.
|
|
@@ -4708,25 +6901,99 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4708
6901
|
};
|
|
4709
6902
|
return statements.some(exitsWithValue) ? null : exits;
|
|
4710
6903
|
}
|
|
4711
|
-
/**
|
|
4712
|
-
|
|
4713
|
-
|
|
6904
|
+
/** A literal row remains after a single numeric equality guard skips its match. */
|
|
6905
|
+
mapRowCanRender(item, literal, setup) {
|
|
6906
|
+
const [guard] = setup;
|
|
6907
|
+
if (setup.length !== 1 || !ts.isIfStatement(guard) || guard.elseStatement)
|
|
6908
|
+
return false;
|
|
6909
|
+
const branch = guard.thenStatement;
|
|
6910
|
+
const exit = ts.isBlock(branch) && branch.statements.length === 1 ? branch.statements[0] : branch;
|
|
6911
|
+
const test = skipParens(guard.expression);
|
|
6912
|
+
if (!ts.isReturnStatement(exit) ||
|
|
6913
|
+
!ts.isBinaryExpression(test) ||
|
|
6914
|
+
test.operatorToken.kind !== ts.SyntaxKind.EqualsEqualsEqualsToken)
|
|
4714
6915
|
return false;
|
|
4715
|
-
|
|
6916
|
+
const left = skipWrappers(test.left);
|
|
6917
|
+
const right = skipWrappers(test.right);
|
|
6918
|
+
const compared = ts.isIdentifier(left) && this.declarationOf(left) === item
|
|
6919
|
+
? right
|
|
6920
|
+
: ts.isIdentifier(right) && this.declarationOf(right) === item
|
|
6921
|
+
? left
|
|
6922
|
+
: null;
|
|
6923
|
+
const skipped = compared === null ? undefined : numericConstantValue(compared);
|
|
6924
|
+
return (skipped !== undefined &&
|
|
6925
|
+
literal.elements.some((element) => {
|
|
6926
|
+
const value = numericConstantValue(element);
|
|
6927
|
+
return value !== undefined && value !== skipped;
|
|
6928
|
+
}));
|
|
6929
|
+
}
|
|
6930
|
+
// The automatic JSX runtime receives props (including children) before its
|
|
6931
|
+
// separate key argument. Keep a block-body map so the compiler builds every
|
|
6932
|
+
// row value before rendering any of them.
|
|
6933
|
+
orderedMapRow(call, fn, open, key, children) {
|
|
6934
|
+
const tag = open.tagName;
|
|
6935
|
+
const props = open.attributes.properties;
|
|
6936
|
+
const declaration = ts.isIdentifier(tag) && this.declarationOf(tag);
|
|
6937
|
+
if (!ts.isArrowFunction(fn) ||
|
|
6938
|
+
ts.isBlock(fn.body) ||
|
|
6939
|
+
fn.modifiers?.length ||
|
|
6940
|
+
!ts.isIdentifier(tag) ||
|
|
6941
|
+
open.typeArguments ||
|
|
6942
|
+
!((declaration && ts.isFunctionDeclaration(declaration)) ||
|
|
6943
|
+
/^[a-z][a-z0-9]*$/.test(tag.text)) ||
|
|
6944
|
+
!this.options.automaticReactJsx ||
|
|
6945
|
+
this.lostComments(call.getStart(this.sf), call.end, '').length)
|
|
6946
|
+
return null;
|
|
6947
|
+
const names = new Set();
|
|
6948
|
+
for (const prop of props) {
|
|
6949
|
+
if (!ts.isJsxAttribute(prop) ||
|
|
6950
|
+
!ts.isIdentifier(prop.name) ||
|
|
6951
|
+
names.has(prop.name.text) ||
|
|
6952
|
+
(prop.initializer && hasJsx(prop.initializer)) ||
|
|
6953
|
+
/^(?:children|ref|dangerouslySetInnerHTML)$|^on[A-Z]/.test(prop.name.text))
|
|
6954
|
+
return null;
|
|
6955
|
+
names.add(prop.name.text);
|
|
6956
|
+
}
|
|
6957
|
+
const content = withoutJsxIndentation(children);
|
|
6958
|
+
const [child] = content;
|
|
6959
|
+
if (content.length &&
|
|
6960
|
+
!(content.length === 1 &&
|
|
6961
|
+
ts.isJsxExpression(child) &&
|
|
6962
|
+
child.expression &&
|
|
6963
|
+
!child.dotDotDotToken &&
|
|
6964
|
+
!hasJsx(child.expression)))
|
|
6965
|
+
return null;
|
|
6966
|
+
const fields = props.filter((prop) => prop !== key).map((prop) => this.emit(prop, PLAIN));
|
|
6967
|
+
if (child)
|
|
6968
|
+
fields.push('children=' + this.emit(child, DESCRIPTOR));
|
|
6969
|
+
fields.push(this.emit(key, PLAIN));
|
|
6970
|
+
const row = '<' + this.emit(tag, PLAIN) + ' ' + fields.join(' ') + ' />';
|
|
6971
|
+
const head = this.span(call, call.getStart(this.sf), fn.getStart(this.sf), PLAIN) +
|
|
6972
|
+
this.span(fn, fn.getStart(this.sf), fn.body.getStart(this.sf), PLAIN);
|
|
6973
|
+
const tail = this.span(call, fn.end, call.end, PLAIN);
|
|
6974
|
+
return '{' + head + '{ return ' + row + '; }' + tail + '}';
|
|
6975
|
+
}
|
|
6976
|
+
stableRowKey(key, item, iterable) {
|
|
6977
|
+
if (isPrimitiveLiteral(key))
|
|
4716
6978
|
return true;
|
|
4717
6979
|
key = skipWrappers(key);
|
|
4718
6980
|
const source = skipWrappers(iterable);
|
|
4719
|
-
|
|
6981
|
+
return (ts.isIdentifier(key) &&
|
|
4720
6982
|
this.declarationOf(key) === item &&
|
|
4721
6983
|
ts.isArrayLiteralExpression(source) &&
|
|
4722
|
-
source.elements.every(
|
|
4723
|
-
fixedBinding(this.checker, key, key))
|
|
6984
|
+
source.elements.every(isPrimitiveLiteral) &&
|
|
6985
|
+
fixedBinding(this.checker, key, key));
|
|
6986
|
+
}
|
|
6987
|
+
/** An @for header evaluates its key before the row body runs. */
|
|
6988
|
+
rowKeyCanMove(key, item, iterable, setup) {
|
|
6989
|
+
if (!ts.isIdentifier(item.name))
|
|
6990
|
+
return false;
|
|
6991
|
+
if (this.stableRowKey(key, item, iterable))
|
|
4724
6992
|
return true;
|
|
4725
|
-
}
|
|
4726
6993
|
// Even an accessor key can precede initialization that reads nothing.
|
|
4727
6994
|
return (setup.every((statement) => ts.isVariableStatement(statement) &&
|
|
4728
6995
|
(statement.declarationList.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const &&
|
|
4729
|
-
statement.declarationList.declarations.every(({ name, initializer }) => ts.isIdentifier(name) && initializer !== undefined &&
|
|
6996
|
+
statement.declarationList.declarations.every(({ name, initializer }) => ts.isIdentifier(name) && initializer !== undefined && isPrimitiveLiteral(initializer))) && !reads(key, (name) => name.text === 'eval' && !isPropertyName(name)));
|
|
4730
6997
|
}
|
|
4731
6998
|
readsLocals(expression, statements) {
|
|
4732
6999
|
const declared = new Set(statements
|
|
@@ -4769,13 +7036,27 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4769
7036
|
if (!last || !ts.isReturnStatement(last) || !last.expression)
|
|
4770
7037
|
return null;
|
|
4771
7038
|
const guard = statements.findIndex((s, i) => i < statements.length - 1 && guardReturn(s));
|
|
4772
|
-
if (guard !== -1)
|
|
4773
|
-
|
|
7039
|
+
if (guard !== -1) {
|
|
7040
|
+
const output = this.rewriteGuards(node, body, guard);
|
|
7041
|
+
if (output !== null)
|
|
7042
|
+
return output;
|
|
7043
|
+
}
|
|
7044
|
+
const inner = skipParens(last.expression);
|
|
4774
7045
|
// A template body has one output; any other early `return <A />` would
|
|
4775
7046
|
// render through runtime descriptors, so the plain function stays.
|
|
4776
|
-
|
|
7047
|
+
const setupStatements = statements.slice(0, -1);
|
|
7048
|
+
if (guard !== -1 || returnsJsx(setupStatements)) {
|
|
7049
|
+
if (this.options.localReactClass &&
|
|
7050
|
+
isJsx(inner) &&
|
|
7051
|
+
this.moduleComponent(node) &&
|
|
7052
|
+
setupStatements.every((statement) => ts.isVariableStatement(statement) ||
|
|
7053
|
+
(ts.isIfStatement(statement) &&
|
|
7054
|
+
!statement.elseStatement &&
|
|
7055
|
+
literalCondition(statement.expression) !== 'truthy')) &&
|
|
7056
|
+
this.options.automaticReactJsx)
|
|
7057
|
+
this.checkNativeOutput(inner, false, true);
|
|
4777
7058
|
return null;
|
|
4778
|
-
|
|
7059
|
+
}
|
|
4779
7060
|
let output = this.output(inner);
|
|
4780
7061
|
if (output === null)
|
|
4781
7062
|
return null;
|
|
@@ -4814,7 +7095,14 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4814
7095
|
const rest = statements.slice(first + 1, -1).filter((statement) => !guardReturn(statement));
|
|
4815
7096
|
if (returnsJsx(statements.slice(0, first)) || this.emptyArmExit(rest) === null)
|
|
4816
7097
|
return null;
|
|
4817
|
-
const
|
|
7098
|
+
const checkRoot = this.options.localReactClass !== undefined || this.options.localReactContext !== undefined;
|
|
7099
|
+
const terminal = checkRoot
|
|
7100
|
+
? statements.findIndex((statement, index) => index >= first &&
|
|
7101
|
+
ts.isIfStatement(statement) &&
|
|
7102
|
+
guardReturn(statement) &&
|
|
7103
|
+
literalCondition(statement.expression) === 'truthy')
|
|
7104
|
+
: -1;
|
|
7105
|
+
const output = this.arm(last.expression, checkRoot && terminal === -1);
|
|
4818
7106
|
if (output === null)
|
|
4819
7107
|
return null;
|
|
4820
7108
|
const chain = (from) => {
|
|
@@ -4832,8 +7120,11 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4832
7120
|
if (at === statements.length - 1)
|
|
4833
7121
|
return prefix + output;
|
|
4834
7122
|
const guard = statements[at];
|
|
7123
|
+
const condition = checkRoot ? literalCondition(guard.expression) : undefined;
|
|
4835
7124
|
this.count(RULE.if);
|
|
4836
|
-
const then = this.block(`@if (${this.emit(guard.expression, PLAIN)})`, this.arm(guardReturn(guard)
|
|
7125
|
+
const then = this.block(`@if (${this.emit(guard.expression, PLAIN)})`, this.arm(guardReturn(guard), checkRoot &&
|
|
7126
|
+
(terminal === -1 || at <= terminal) &&
|
|
7127
|
+
(condition === undefined || condition === 'truthy')));
|
|
4837
7128
|
const rest = chain(at + 1);
|
|
4838
7129
|
return prefix + (rest === '' ? then : then + ' ' + this.block('@else', rest));
|
|
4839
7130
|
};
|
|
@@ -4854,17 +7145,290 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4854
7145
|
* and indentation `node` had, or null when it is not a single renderable.
|
|
4855
7146
|
*/
|
|
4856
7147
|
output(node) {
|
|
4857
|
-
if (isJsx(node))
|
|
7148
|
+
if (isJsx(node)) {
|
|
7149
|
+
this.checkNativeOutput(node);
|
|
4858
7150
|
return this.emit(node, TEMPLATE);
|
|
7151
|
+
}
|
|
4859
7152
|
if (ts.isConditionalExpression(node) || ts.isCallExpression(node)) {
|
|
4860
|
-
const directive = this.directive(node);
|
|
7153
|
+
const directive = this.directive(node, this.options.localReactClass !== undefined || this.options.localReactContext !== undefined);
|
|
4861
7154
|
return directive === null ? null : this.absolute(directive, node);
|
|
4862
7155
|
}
|
|
4863
7156
|
return null;
|
|
4864
7157
|
}
|
|
7158
|
+
/** Possible values if the expression completes, grouped by short-circuit behavior. */
|
|
7159
|
+
logicalResult(expression) {
|
|
7160
|
+
const node = skipWrappers(expression);
|
|
7161
|
+
if (isJsx(node))
|
|
7162
|
+
return TRUTHY;
|
|
7163
|
+
switch (literalCondition(node)) {
|
|
7164
|
+
case 'truthy':
|
|
7165
|
+
return TRUTHY;
|
|
7166
|
+
case 'falsy':
|
|
7167
|
+
return FALSY;
|
|
7168
|
+
case 'nullish':
|
|
7169
|
+
return NULLISH;
|
|
7170
|
+
}
|
|
7171
|
+
if (ts.isBinaryExpression(node)) {
|
|
7172
|
+
const rightCases = logicalRightCases(node.operatorToken.kind);
|
|
7173
|
+
if (rightCases) {
|
|
7174
|
+
// Left-associated chains are also visited as possible template outputs.
|
|
7175
|
+
const cached = this.logicalResults?.get(node);
|
|
7176
|
+
if (cached !== undefined)
|
|
7177
|
+
return cached;
|
|
7178
|
+
const left = this.logicalResult(node.left);
|
|
7179
|
+
let result = left & ~rightCases;
|
|
7180
|
+
if (left & rightCases)
|
|
7181
|
+
result |= this.logicalResult(node.right);
|
|
7182
|
+
(this.logicalResults ??= new WeakMap()).set(node, result);
|
|
7183
|
+
return result;
|
|
7184
|
+
}
|
|
7185
|
+
}
|
|
7186
|
+
return TRUTHY | FALSY | NULLISH;
|
|
7187
|
+
}
|
|
7188
|
+
checkNativeOutput(node, conditional = false, retained = false, descriptorValue = false) {
|
|
7189
|
+
if (!this.options.localReactClass && !this.options.localReactContext)
|
|
7190
|
+
return;
|
|
7191
|
+
if (!retained) {
|
|
7192
|
+
while (isWrapper(node)) {
|
|
7193
|
+
// String assertions select text in template holes. Descriptor values erase them.
|
|
7194
|
+
if (!descriptorValue &&
|
|
7195
|
+
(ts.isAsExpression(node) ||
|
|
7196
|
+
ts.isSatisfiesExpression(node) ||
|
|
7197
|
+
ts.isTypeAssertionExpression(node)) &&
|
|
7198
|
+
node.type.kind === ts.SyntaxKind.StringKeyword)
|
|
7199
|
+
return;
|
|
7200
|
+
node = node.expression;
|
|
7201
|
+
}
|
|
7202
|
+
if (ts.isConditionalExpression(node)) {
|
|
7203
|
+
const condition = literalCondition(node.condition);
|
|
7204
|
+
if (condition === undefined || condition === 'truthy')
|
|
7205
|
+
this.checkNativeOutput(node.whenTrue, conditional, false, descriptorValue);
|
|
7206
|
+
if (condition !== 'truthy')
|
|
7207
|
+
this.checkNativeOutput(node.whenFalse, conditional, false, descriptorValue);
|
|
7208
|
+
return;
|
|
7209
|
+
}
|
|
7210
|
+
if (ts.isBinaryExpression(node)) {
|
|
7211
|
+
const operator = node.operatorToken.kind;
|
|
7212
|
+
const rightCases = logicalRightCases(operator);
|
|
7213
|
+
if (!rightCases)
|
|
7214
|
+
return;
|
|
7215
|
+
const left = this.logicalResult(node.left);
|
|
7216
|
+
// The left result of && is falsy and cannot render JSX.
|
|
7217
|
+
if (operator !== ts.SyntaxKind.AmpersandAmpersandToken && left & ~rightCases)
|
|
7218
|
+
this.checkNativeOutput(node.left, conditional, false, descriptorValue);
|
|
7219
|
+
if (left & rightCases)
|
|
7220
|
+
this.checkNativeOutput(node.right, conditional, false, descriptorValue);
|
|
7221
|
+
return;
|
|
7222
|
+
}
|
|
7223
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
7224
|
+
for (const element of node.elements)
|
|
7225
|
+
this.checkNativeOutput(element, conditional, false, true);
|
|
7226
|
+
return;
|
|
7227
|
+
}
|
|
7228
|
+
if (ts.isCallExpression(node)) {
|
|
7229
|
+
const callee = node.expression;
|
|
7230
|
+
if (!ts.isPropertyAccessExpression(callee) || callee.name.text !== 'map')
|
|
7231
|
+
return;
|
|
7232
|
+
if (descriptorValue) {
|
|
7233
|
+
if (this.options.localReactClass)
|
|
7234
|
+
this.checkDescriptorMapClassOutput(node, callee.expression);
|
|
7235
|
+
}
|
|
7236
|
+
else if (!conditional)
|
|
7237
|
+
(this.nativeOutputMaps ??= new WeakSet()).add(node);
|
|
7238
|
+
return;
|
|
7239
|
+
}
|
|
7240
|
+
}
|
|
7241
|
+
if (!isJsx(node))
|
|
7242
|
+
return;
|
|
7243
|
+
if (!ts.isJsxFragment(node)) {
|
|
7244
|
+
if (this.retainedReactOutput(node))
|
|
7245
|
+
return;
|
|
7246
|
+
if (!retained && !descriptorValue) {
|
|
7247
|
+
this.checkRetainedReactContext(node);
|
|
7248
|
+
this.checkRetainedReactTemplateComponent(node);
|
|
7249
|
+
}
|
|
7250
|
+
if (ts.isJsxSelfClosingElement(node))
|
|
7251
|
+
return this.checkReactClassRoot(node, conditional, retained);
|
|
7252
|
+
if (!retained && rendersOrdinaryHtmlChildren(node.openingElement)) {
|
|
7253
|
+
this.checkNativeChildren(node, conditional, false, descriptorValue);
|
|
7254
|
+
return;
|
|
7255
|
+
}
|
|
7256
|
+
const tag = node.openingElement.tagName;
|
|
7257
|
+
const callee = this.callee(tag);
|
|
7258
|
+
if (callee?.module !== 'react' && callee?.module !== 'octane')
|
|
7259
|
+
return this.checkReactClassRoot(node, conditional, retained);
|
|
7260
|
+
if (callee.name === 'Fragment' || callee.name === 'Suspense') {
|
|
7261
|
+
const member = ts.isPropertyAccessExpression(tag);
|
|
7262
|
+
const reference = member ? tag.expression : tag;
|
|
7263
|
+
const declaration = ts.isIdentifier(reference) && this.declarationOf(reference);
|
|
7264
|
+
if (!declaration ||
|
|
7265
|
+
this.imports.get(declaration)?.typeOnly !== false ||
|
|
7266
|
+
(member && callee.module === 'octane' && !ts.isNamespaceImport(declaration)))
|
|
7267
|
+
return this.checkReactClassRoot(node, conditional, retained);
|
|
7268
|
+
const imp = ts.findAncestor(declaration, ts.isImportDeclaration);
|
|
7269
|
+
if (!imp || imp.attributes)
|
|
7270
|
+
return this.checkReactClassRoot(node, conditional, retained);
|
|
7271
|
+
if (callee.name === 'Suspense') {
|
|
7272
|
+
const fallback = !retained && this.suspenseFallback(node, true);
|
|
7273
|
+
if (!fallback)
|
|
7274
|
+
return this.checkReactClassRoot(node, conditional, retained);
|
|
7275
|
+
if (ts.isJsxExpression(fallback) && fallback.expression) {
|
|
7276
|
+
const expression = skipParens(fallback.expression);
|
|
7277
|
+
if (isJsx(expression))
|
|
7278
|
+
this.checkNativeOutput(expression, conditional === 'map' ? 'map' : true, false, descriptorValue);
|
|
7279
|
+
}
|
|
7280
|
+
}
|
|
7281
|
+
}
|
|
7282
|
+
else if (callee.name !== 'StrictMode') {
|
|
7283
|
+
return this.checkReactClassRoot(node, conditional, retained);
|
|
7284
|
+
}
|
|
7285
|
+
}
|
|
7286
|
+
this.checkNativeChildren(node, conditional, retained, descriptorValue);
|
|
7287
|
+
}
|
|
7288
|
+
checkDescriptorMapClassOutput(call, receiver) {
|
|
7289
|
+
const array = skipWrappers(receiver);
|
|
7290
|
+
const [argument] = call.arguments;
|
|
7291
|
+
if (!ts.isArrayLiteralExpression(array) ||
|
|
7292
|
+
!array.elements.some((element) => !ts.isSpreadElement(element) && !ts.isOmittedExpression(element)) ||
|
|
7293
|
+
call.arguments.length !== 1)
|
|
7294
|
+
return;
|
|
7295
|
+
const fn = skipWrappers(argument);
|
|
7296
|
+
if ((!ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) ||
|
|
7297
|
+
fn.modifiers?.length ||
|
|
7298
|
+
fn.asteriskToken)
|
|
7299
|
+
return;
|
|
7300
|
+
const output = singleCallbackExpression(fn.body);
|
|
7301
|
+
// The retained map can be overridden and discard its callback's result.
|
|
7302
|
+
if (output)
|
|
7303
|
+
this.checkNativeOutput(output, 'map', false, true);
|
|
7304
|
+
}
|
|
7305
|
+
checkNativeChildren(node, conditional, retained, descriptorValue) {
|
|
7306
|
+
for (const child of node.children) {
|
|
7307
|
+
if (isJsx(child))
|
|
7308
|
+
this.checkNativeOutput(child, conditional, retained, descriptorValue);
|
|
7309
|
+
else if (!retained && ts.isJsxExpression(child) && child.expression && !child.dotDotDotToken)
|
|
7310
|
+
this.checkNativeOutput(child.expression, conditional, false, descriptorValue);
|
|
7311
|
+
}
|
|
7312
|
+
}
|
|
7313
|
+
retainedReactOutput(node) {
|
|
7314
|
+
return (this.islands.has(node) || this.unrenderable.has(node) || this.reactRendererBodies.has(node));
|
|
7315
|
+
}
|
|
7316
|
+
directImportedValue(tag) {
|
|
7317
|
+
if (!ts.isIdentifier(tag))
|
|
7318
|
+
return;
|
|
7319
|
+
const entry = this.importOf(tag);
|
|
7320
|
+
if (!entry || entry.namespace || entry.typeOnly)
|
|
7321
|
+
return;
|
|
7322
|
+
const declaration = ts.findAncestor(this.declarationOf(tag), ts.isImportDeclaration);
|
|
7323
|
+
if (declaration && !declaration.attributes)
|
|
7324
|
+
return { tag, entry };
|
|
7325
|
+
}
|
|
7326
|
+
checkRetainedReactTemplateComponent(node) {
|
|
7327
|
+
const check = this.options.localReactTemplateComponent;
|
|
7328
|
+
if (!check)
|
|
7329
|
+
return;
|
|
7330
|
+
const tag = ts.isJsxElement(node) ? node.openingElement.tagName : node.tagName;
|
|
7331
|
+
if (!ts.isIdentifier(tag) || !isComponentName(tag.text))
|
|
7332
|
+
return;
|
|
7333
|
+
const binding = this.directImportedValue(tag);
|
|
7334
|
+
if (!binding || !check(binding.entry.module, binding.entry.imported))
|
|
7335
|
+
return;
|
|
7336
|
+
this.note('retained-react-component', 'warning', tag, `This native template can render ${tag.text} from the unchanged ${binding.entry.module} module. Its JSX returns a React element under the project's React JSX settings. Migrate the component too or keep its render under React.`);
|
|
7337
|
+
}
|
|
7338
|
+
checkRetainedReactContext(node) {
|
|
7339
|
+
const check = this.options.localReactContext;
|
|
7340
|
+
if (!check)
|
|
7341
|
+
return;
|
|
7342
|
+
const tag = ts.isJsxElement(node) ? node.openingElement.tagName : node.tagName;
|
|
7343
|
+
const provider = ts.isPropertyAccessExpression(tag) && tag.name.text === 'Provider';
|
|
7344
|
+
if (!provider && (!ts.isIdentifier(tag) || !isComponentName(tag.text)))
|
|
7345
|
+
return;
|
|
7346
|
+
const binding = this.directImportedValue(provider ? tag.expression : tag);
|
|
7347
|
+
if (!binding)
|
|
7348
|
+
return;
|
|
7349
|
+
const { tag: context, entry } = binding;
|
|
7350
|
+
if (!check(entry.module, entry.imported, false))
|
|
7351
|
+
return;
|
|
7352
|
+
const retainedMember = provider && !this.isContext(context);
|
|
7353
|
+
if (retainedMember &&
|
|
7354
|
+
(!check(entry.module, entry.imported, true) || !this.hasUnchangedProviderUse(context)))
|
|
7355
|
+
return;
|
|
7356
|
+
this.note('retained-react-context', 'warning', tag, retainedMember
|
|
7357
|
+
? `If \`${context.text}.Provider\` from \`${entry.module}\` still exposes the React provider, this native template cannot render it. Verify the runtime import and keep this render in React or migrate the context producer.`
|
|
7358
|
+
: `If this native template resolves \`${entry.module}\` to the retained React context \`${context.text}\`, Octane cannot render it as a provider. Verify the runtime import and keep this render in React or migrate the context producer.`);
|
|
7359
|
+
}
|
|
7360
|
+
hasUnchangedProviderUse(context) {
|
|
7361
|
+
let originalReact;
|
|
7362
|
+
const readContext = (callee) => {
|
|
7363
|
+
if (!ts.isIdentifier(callee))
|
|
7364
|
+
return false;
|
|
7365
|
+
const hook = this.directImportedValue(callee)?.entry;
|
|
7366
|
+
return (hook?.module === 'react' &&
|
|
7367
|
+
hook.imported === 'useContext' &&
|
|
7368
|
+
(originalReact ??= this.options.originalReact === true));
|
|
7369
|
+
};
|
|
7370
|
+
return ((!/eval|\\/.test(this.source) || !hasEval(this.sf)) &&
|
|
7371
|
+
this.localUses(context).every((ref) => declaresName(ref) || unchangedContextProviderUse(ref, readContext)));
|
|
7372
|
+
}
|
|
7373
|
+
checkReactClassRoot(node, conditional, retained) {
|
|
7374
|
+
const check = this.options.localReactClass;
|
|
7375
|
+
if (!check)
|
|
7376
|
+
return;
|
|
7377
|
+
const tagName = ts.isJsxElement(node) ? node.openingElement.tagName : node.tagName;
|
|
7378
|
+
if (!ts.isIdentifier(tagName) || !isComponentName(tagName.text))
|
|
7379
|
+
return;
|
|
7380
|
+
const binding = this.directImportedValue(tagName);
|
|
7381
|
+
if (!binding)
|
|
7382
|
+
return;
|
|
7383
|
+
const { tag, entry } = binding;
|
|
7384
|
+
const proof = check(entry.module, entry.imported);
|
|
7385
|
+
if (!proof)
|
|
7386
|
+
return;
|
|
7387
|
+
const severity = conditional === 'map' ? 'warning' : proof;
|
|
7388
|
+
this.note(RULE.classComponent, severity, tag, severity === 'error'
|
|
7389
|
+
? `The migration resolves \`${entry.module}\` to the imported React class \`${tag.text}\`, ${retained ? 'which fails if Octane renders this output' : `which this native template ${conditional ? 'can render' : 'renders'}`}. Keep this render in React or replace the class with an Octane function component.`
|
|
7390
|
+
: `If ${retained ? 'Octane' : 'this native template'} renders the imported React class \`${tag.text}\` resolved from \`${entry.module}\`, the call will fail. Verify the runtime import, keep the render in React, or replace the class with an Octane function component.`);
|
|
7391
|
+
}
|
|
4865
7392
|
startsLine(node) {
|
|
4866
7393
|
return this.linePrefix(node).trim() === '';
|
|
4867
7394
|
}
|
|
7395
|
+
/** A direct module component, without going through an arbitrary wrapper or callback. */
|
|
7396
|
+
moduleComponent(node) {
|
|
7397
|
+
const parent = node.parent;
|
|
7398
|
+
if (ts.isExportAssignment(parent))
|
|
7399
|
+
return !parent.isExportEquals && parent.parent === this.sf;
|
|
7400
|
+
let statement;
|
|
7401
|
+
let name;
|
|
7402
|
+
if (ts.isFunctionDeclaration(node)) {
|
|
7403
|
+
statement = node;
|
|
7404
|
+
name = node.name;
|
|
7405
|
+
}
|
|
7406
|
+
else {
|
|
7407
|
+
if (!ts.isVariableDeclaration(parent) ||
|
|
7408
|
+
parent.initializer !== node ||
|
|
7409
|
+
!ts.isIdentifier(parent.name) ||
|
|
7410
|
+
!isComponentName(parent.name.text) ||
|
|
7411
|
+
(ts.getCombinedNodeFlags(parent) & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
7412
|
+
!ts.isVariableStatement(parent.parent.parent))
|
|
7413
|
+
return false;
|
|
7414
|
+
statement = parent.parent.parent;
|
|
7415
|
+
name = parent.name;
|
|
7416
|
+
}
|
|
7417
|
+
if (statement.parent !== this.sf)
|
|
7418
|
+
return false;
|
|
7419
|
+
if (statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword))
|
|
7420
|
+
return true;
|
|
7421
|
+
if (!name)
|
|
7422
|
+
return false;
|
|
7423
|
+
const refs = renderUses(this.localUses(name));
|
|
7424
|
+
return (refs.some((ref) => ts.isExportSpecifier(ref.parent)
|
|
7425
|
+
? !ref.parent.parent.parent.moduleSpecifier
|
|
7426
|
+
: ts.isExportAssignment(ref.parent) && !ref.parent.isExportEquals) ||
|
|
7427
|
+
(refs.length > 0 &&
|
|
7428
|
+
refs.every((ref) => !ts.isExportAssignment(ref.parent) &&
|
|
7429
|
+
!ts.isExportSpecifier(ref.parent) &&
|
|
7430
|
+
componentTagOrExport(ref))));
|
|
7431
|
+
}
|
|
4868
7432
|
/**
|
|
4869
7433
|
* Whether `node` is a component definition: a capitalized function, or the
|
|
4870
7434
|
* function passed to `memo` / `forwardRef`, or a default-exported one.
|