@octanejs/codemod 0.2.1 → 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 +97 -8
- package/dist/bin.js +2 -2
- package/dist/effect-receivers.d.ts +2 -0
- package/dist/effect-receivers.js +10 -0
- 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/local-react-hook.d.ts +7 -2
- package/dist/local-react-hook.js +99 -44
- package/dist/local-uses.d.ts +2 -0
- package/dist/local-uses.js +22 -0
- package/dist/mappings.d.ts +5 -1
- package/dist/mappings.js +16 -3
- package/dist/path-svg.d.ts +5 -0
- package/dist/path-svg.js +19 -2
- package/dist/project.js +868 -122
- package/dist/react-intl.d.ts +1 -1
- package/dist/react-intl.js +23 -3
- package/dist/react-island-sources.d.ts +26 -7
- package/dist/react-island-sources.js +614 -64
- package/dist/react-renderer.d.ts +2 -0
- package/dist/react-renderer.js +94 -0
- package/dist/scoped-store-contract.d.ts +6 -1
- package/dist/scoped-store-contract.js +199 -31
- package/dist/store-hook-uses.js +1 -20
- package/dist/store-inputs.js +3 -1
- package/dist/transform.d.ts +6 -0
- package/dist/transform.js +1922 -251
- package/dist/types.d.ts +24 -8
- package/package.json +4 -3
- package/src/path-svg.ts +20 -2
- package/templates/octane-path-svg.ts +4 -0
- 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, hasEval, 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 { componentTagOrExport, inTypeNode, localSymbol, 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 { effectHelpers, effectReceivers, fixedBinding, initializedBefore, stableExportedProp, } 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
|
/**
|
|
@@ -49,6 +94,11 @@ export const MAP_PARAMETER_WRITE = 'map-parameter-write';
|
|
|
49
94
|
export const MAP_CALLBACK_SCOPE = 'map-callback-scope';
|
|
50
95
|
export const CONTEXT_ALIAS = 'context-path-alias';
|
|
51
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';
|
|
52
102
|
/** Rule names, as reported. */
|
|
53
103
|
const RULE = {
|
|
54
104
|
binding: 'bindings',
|
|
@@ -122,6 +172,8 @@ export function projectTypeAt(node, types) {
|
|
|
122
172
|
* members.
|
|
123
173
|
*/
|
|
124
174
|
export function bind(filename, source) {
|
|
175
|
+
// The program asks its host for slash-delimited paths even on Windows.
|
|
176
|
+
filename = filename.replaceAll('\\', '/');
|
|
125
177
|
const lib = esLib();
|
|
126
178
|
const sf = ts.createSourceFile(filename, source, ts.ScriptTarget.Latest, true, scriptKind(filename));
|
|
127
179
|
const host = {
|
|
@@ -180,6 +232,14 @@ function isDOMTag(tag) {
|
|
|
180
232
|
}
|
|
181
233
|
return domTagNames.has(tag);
|
|
182
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
|
+
}
|
|
183
243
|
function scriptKind(filename) {
|
|
184
244
|
if (filename.endsWith('.tsx'))
|
|
185
245
|
return ts.ScriptKind.TSX;
|
|
@@ -194,7 +254,7 @@ export function nativeReactCompat(module, name) {
|
|
|
194
254
|
return ((name === 'ReactCompat' || name === 'ReactCompatInline') &&
|
|
195
255
|
(module === 'octane/react' || module === 'octane/react/server'));
|
|
196
256
|
}
|
|
197
|
-
function skipParens(node) {
|
|
257
|
+
export function skipParens(node) {
|
|
198
258
|
while (ts.isParenthesizedExpression(node))
|
|
199
259
|
node = node.expression;
|
|
200
260
|
return node;
|
|
@@ -213,6 +273,37 @@ function asRest(declaration) {
|
|
|
213
273
|
function isJsx(node) {
|
|
214
274
|
return ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node) || ts.isJsxFragment(node);
|
|
215
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
|
+
}
|
|
216
307
|
/** A deferred clsx call may test a binding only if its truth value stays fixed. */
|
|
217
308
|
function svgClassToken(expr, fixedToken, truthTest = false) {
|
|
218
309
|
expr = skipWrappers(expr);
|
|
@@ -256,16 +347,15 @@ function readsCssToken(ref) {
|
|
|
256
347
|
use.operatorToken.kind === ts.SyntaxKind.BarBarToken)));
|
|
257
348
|
}
|
|
258
349
|
/**
|
|
259
|
-
*
|
|
350
|
+
* Describe a package import that the codemod keeps on React.
|
|
260
351
|
*/
|
|
261
352
|
function portComment({ name, islands, blocked }) {
|
|
262
|
-
const
|
|
263
|
-
if (blocked > 0)
|
|
264
|
-
return `${
|
|
265
|
-
}
|
|
353
|
+
const retained = `Retained React import of \`${name}\``;
|
|
354
|
+
if (blocked > 0)
|
|
355
|
+
return `${retained}. Review the uses that need a React renderer.`;
|
|
266
356
|
return islands > 0
|
|
267
|
-
? `${
|
|
268
|
-
: `${
|
|
357
|
+
? `${retained}. Its components render in React through ReactCompat.`
|
|
358
|
+
: `${retained}. Keep React as a dependency.`;
|
|
269
359
|
}
|
|
270
360
|
/**
|
|
271
361
|
* Whether a hole is already spelled as text, or as a literal the cast would
|
|
@@ -339,7 +429,7 @@ function nodeSpanning(sf, start, end) {
|
|
|
339
429
|
node = child;
|
|
340
430
|
}
|
|
341
431
|
}
|
|
342
|
-
function
|
|
432
|
+
function isPrimitiveLiteral(node) {
|
|
343
433
|
node = skipWrappers(node);
|
|
344
434
|
if (ts.isPrefixUnaryExpression(node)) {
|
|
345
435
|
const operand = skipWrappers(node.operand);
|
|
@@ -405,6 +495,20 @@ function returnsJsx(statements) {
|
|
|
405
495
|
};
|
|
406
496
|
return statements.some(visit);
|
|
407
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
|
+
}
|
|
408
512
|
/** Document tags a router shell renders through `@octanejs/tanstack-router`. */
|
|
409
513
|
const DOCUMENT_TAGS = {
|
|
410
514
|
html: 'Html',
|
|
@@ -579,6 +683,9 @@ class ModuleTransform {
|
|
|
579
683
|
removed = new Set();
|
|
580
684
|
/** `<Fragment key>` rows whose key moved to the `@for` header. */
|
|
581
685
|
bare = new Set();
|
|
686
|
+
/** Map calls in output with a proven native renderer. */
|
|
687
|
+
nativeOutputMaps;
|
|
688
|
+
logicalResults;
|
|
582
689
|
/** Functions whose parameter list a rule replaced. */
|
|
583
690
|
parameters = new Map();
|
|
584
691
|
/** Rest props that give up `onChange` to a text field. */
|
|
@@ -597,6 +704,7 @@ class ModuleTransform {
|
|
|
597
704
|
unported = new Map();
|
|
598
705
|
/** Elements to host in a `ReactCompat` island. */
|
|
599
706
|
islands = new Map();
|
|
707
|
+
inlineIntlMessages = new Set();
|
|
600
708
|
reactRendererBodies = new Map();
|
|
601
709
|
originalStoreTags = new Map();
|
|
602
710
|
storeSourceNames;
|
|
@@ -604,6 +712,7 @@ class ModuleTransform {
|
|
|
604
712
|
unrenderable = new Map();
|
|
605
713
|
/** Placeholder index -> import declaration. */
|
|
606
714
|
importSlots = [];
|
|
715
|
+
retainedTypeNamespaces = new Set();
|
|
607
716
|
intrinsicPropsHelper = null;
|
|
608
717
|
hasJsx = false;
|
|
609
718
|
/** Whether the module imports React, rather than already being Octane. */
|
|
@@ -768,7 +877,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
768
877
|
// Added imports quote their module as the module's own imports do.
|
|
769
878
|
const first = this.sf.statements.find(ts.isImportDeclaration);
|
|
770
879
|
this.quote = first ? first.moduleSpecifier.getText(this.sf)[0] : "'";
|
|
771
|
-
/** Locals
|
|
880
|
+
/** Locals whose package imports stay on React. */
|
|
772
881
|
const portLocals = new Set();
|
|
773
882
|
const reactSvgImports = new Set();
|
|
774
883
|
for (const statement of this.sf.statements) {
|
|
@@ -778,7 +887,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
778
887
|
ts.isStringLiteral(statement.moduleSpecifier)) {
|
|
779
888
|
const port = this.portOf(statement.moduleSpecifier.text);
|
|
780
889
|
if (port) {
|
|
781
|
-
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.`);
|
|
782
891
|
}
|
|
783
892
|
}
|
|
784
893
|
if (!ts.isImportDeclaration(statement) || !statement.importClause)
|
|
@@ -834,7 +943,12 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
834
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, {
|
|
835
944
|
typeOf: (n) => this.typeOf(n),
|
|
836
945
|
text: (n) => textKind(this.typeOf(n)) !== 0,
|
|
837
|
-
}, 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) => {
|
|
838
952
|
if (method === 'formatMessage' || this.options.automaticReactJsx)
|
|
839
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.`);
|
|
840
954
|
});
|
|
@@ -848,7 +962,9 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
848
962
|
this.options.resolveStoreHook &&
|
|
849
963
|
ts.isImportSpecifier(declaration) &&
|
|
850
964
|
/^use[A-Z0-9]/.test(entry.imported) &&
|
|
851
|
-
|
|
965
|
+
// The compiler only assigns custom hook slots to these local names.
|
|
966
|
+
/^use[A-Z]/.test(entry.local) &&
|
|
967
|
+
entry.local !== 'useContext') {
|
|
852
968
|
const statement = declaration.parent.parent.parent;
|
|
853
969
|
if (!statement.attributes && !/\/\*|\/\//.test(statement.getText(this.sf))) {
|
|
854
970
|
const pending = new Map();
|
|
@@ -890,12 +1006,22 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
890
1006
|
}
|
|
891
1007
|
if (this.target === 'tsrx') {
|
|
892
1008
|
for (const item of this.nativeIntl) {
|
|
893
|
-
|
|
1009
|
+
const name = (item.propertyName ?? item.name).text;
|
|
1010
|
+
if (name !== 'FormattedMessage' && name !== 'IntlProvider')
|
|
894
1011
|
continue;
|
|
895
1012
|
for (const ref of this.localUses(item.name)) {
|
|
896
1013
|
const element = ref.parent;
|
|
897
|
-
if (ts.
|
|
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)) {
|
|
898
1021
|
this.reportIntlFormatterOwner(element, 'generated native adapter');
|
|
1022
|
+
if (this.inParagraph(element) && this.options.reactCompatInline)
|
|
1023
|
+
this.inlineIntlMessages.add(element);
|
|
1024
|
+
}
|
|
899
1025
|
}
|
|
900
1026
|
}
|
|
901
1027
|
}
|
|
@@ -1185,8 +1311,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1185
1311
|
});
|
|
1186
1312
|
}
|
|
1187
1313
|
/**
|
|
1188
|
-
* The package
|
|
1189
|
-
* 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.
|
|
1190
1315
|
*/
|
|
1191
1316
|
portOf(module) {
|
|
1192
1317
|
let port = this.ports.get(module);
|
|
@@ -1228,7 +1353,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1228
1353
|
}
|
|
1229
1354
|
}
|
|
1230
1355
|
/**
|
|
1231
|
-
* A component from a
|
|
1356
|
+
* A component from a retained React import renders in React, inside
|
|
1232
1357
|
* a `ReactCompat` island, when its element hands React nothing Octane
|
|
1233
1358
|
* renders. Its hooks and contexts cannot run in an Octane component, so
|
|
1234
1359
|
* they are reported.
|
|
@@ -1255,7 +1380,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1255
1380
|
const parent = member.parent;
|
|
1256
1381
|
const block = (message) => {
|
|
1257
1382
|
port.blocked++;
|
|
1258
|
-
const error = `${message} \`${port.name}\`
|
|
1383
|
+
const error = `${message} \`${port.name}\` stays on React.`;
|
|
1259
1384
|
this.note(RULE.bindings, 'error', member, error);
|
|
1260
1385
|
return error;
|
|
1261
1386
|
};
|
|
@@ -1289,6 +1414,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1289
1414
|
(name === 'FormattedMessage' ||
|
|
1290
1415
|
(this.options.reactIntlAdapter && INTL_FORMATTER.test(importedName))))
|
|
1291
1416
|
this.reportIntlFormatterOwner(element, 'generated island');
|
|
1417
|
+
this.checkIslandLayoutRef(parent);
|
|
1292
1418
|
this.islands.set(element, paragraph ? 'ReactCompatInline' : 'ReactCompat');
|
|
1293
1419
|
}
|
|
1294
1420
|
else {
|
|
@@ -1299,12 +1425,103 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1299
1425
|
}
|
|
1300
1426
|
else if (ts.isCallExpression(parent) && parent.expression === member) {
|
|
1301
1427
|
if (/^use[A-Z0-9]/.test(name))
|
|
1302
|
-
block(`\`${text}\` is a React hook
|
|
1428
|
+
block(`\`${text}\` is a React hook. Keep this hook call under a React renderer.`);
|
|
1303
1429
|
}
|
|
1304
1430
|
else if (ts.isCallExpression(parent) &&
|
|
1305
1431
|
/^(?:use|useContext)$/.test(this.calleeName(parent.expression) ?? '')) {
|
|
1306
|
-
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);
|
|
1307
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.');
|
|
1308
1525
|
}
|
|
1309
1526
|
reportIntlFormatterOwner(node, boundary) {
|
|
1310
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.`);
|
|
@@ -1695,19 +1912,11 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1695
1912
|
return text.text.trim() !== '' && !/[!;]/.test(text.text);
|
|
1696
1913
|
if (!ts.isTemplateExpression(text) || /[!;]/.test(text.head.text))
|
|
1697
1914
|
return false;
|
|
1698
|
-
const numeric = (node) => {
|
|
1699
|
-
node = skipWrappers(node);
|
|
1700
|
-
return (ts.isNumericLiteral(node) ||
|
|
1701
|
-
(ts.isPrefixUnaryExpression(node) &&
|
|
1702
|
-
(node.operator === ts.SyntaxKind.MinusToken ||
|
|
1703
|
-
node.operator === ts.SyntaxKind.PlusToken) &&
|
|
1704
|
-
ts.isNumericLiteral(skipWrappers(node.operand))));
|
|
1705
|
-
};
|
|
1706
1915
|
return text.templateSpans.every(({ expression, literal }) => {
|
|
1707
1916
|
if (/[!;]/.test(literal.text))
|
|
1708
1917
|
return false;
|
|
1709
1918
|
const part = skipWrappers(expression);
|
|
1710
|
-
if (
|
|
1919
|
+
if (isNumericConstant(part))
|
|
1711
1920
|
return true;
|
|
1712
1921
|
if (!ts.isConditionalExpression(part))
|
|
1713
1922
|
return false;
|
|
@@ -1715,13 +1924,31 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1715
1924
|
return (ts.isIdentifier(test) &&
|
|
1716
1925
|
fixedBinding(this.checker, test, part, (this.fixedSvgParameters ??= new Map())) &&
|
|
1717
1926
|
initializedBefore(this.checker, test, part) &&
|
|
1718
|
-
|
|
1719
|
-
|
|
1927
|
+
isNumericConstant(part.whenTrue) &&
|
|
1928
|
+
isNumericConstant(part.whenFalse));
|
|
1720
1929
|
});
|
|
1721
1930
|
}
|
|
1722
1931
|
svgRead;
|
|
1723
1932
|
nativeSvgProps;
|
|
1724
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
|
+
}
|
|
1725
1952
|
nativeOutput(value, uses, unproven) {
|
|
1726
1953
|
const typeOf = (node) => this.typeOf(node);
|
|
1727
1954
|
const destination = {
|
|
@@ -1740,9 +1967,11 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1740
1967
|
if (!first)
|
|
1741
1968
|
return false;
|
|
1742
1969
|
const pending = [first];
|
|
1743
|
-
const proven =
|
|
1970
|
+
const proven = unproven
|
|
1971
|
+
? (this.provenSvgOwners ??= new Set())
|
|
1972
|
+
: (this.provenOutputOwners ??= new Set());
|
|
1744
1973
|
const seen = new Set();
|
|
1745
|
-
let
|
|
1974
|
+
let reachesRenderer = false;
|
|
1746
1975
|
// A local function can hand its return value to another renderer. Follow every
|
|
1747
1976
|
// use of its binding, including uses of a component that also has an export.
|
|
1748
1977
|
while (pending.length) {
|
|
@@ -1753,7 +1982,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1753
1982
|
(ts.isFunctionExpression(fn) && fn.name))
|
|
1754
1983
|
return false;
|
|
1755
1984
|
if (proven.has(fn)) {
|
|
1756
|
-
|
|
1985
|
+
reachesRenderer = true;
|
|
1757
1986
|
continue;
|
|
1758
1987
|
}
|
|
1759
1988
|
if (seen.has(fn))
|
|
@@ -1761,7 +1990,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1761
1990
|
seen.add(fn);
|
|
1762
1991
|
const parent = fn.parent;
|
|
1763
1992
|
if (ts.isExportAssignment(parent) && parent.parent === this.sf) {
|
|
1764
|
-
|
|
1993
|
+
reachesRenderer = true;
|
|
1765
1994
|
continue;
|
|
1766
1995
|
}
|
|
1767
1996
|
const variable = ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name) ? parent : null;
|
|
@@ -1775,27 +2004,31 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
1775
2004
|
if (!refs.length && !directExport)
|
|
1776
2005
|
return false;
|
|
1777
2006
|
if (directExport)
|
|
1778
|
-
|
|
2007
|
+
reachesRenderer = true;
|
|
1779
2008
|
for (const ref of refs) {
|
|
1780
2009
|
const tag = ref.parent;
|
|
1781
2010
|
if (ts.isExportSpecifier(tag) || ts.isExportAssignment(tag)) {
|
|
1782
|
-
|
|
2011
|
+
reachesRenderer = true;
|
|
1783
2012
|
continue;
|
|
1784
2013
|
}
|
|
1785
2014
|
if (!(ts.isJsxSelfClosingElement(tag) ||
|
|
1786
2015
|
ts.isJsxOpeningElement(tag) ||
|
|
1787
2016
|
ts.isJsxClosingElement(tag)) ||
|
|
1788
|
-
tag.tagName !== ref)
|
|
1789
|
-
|
|
2017
|
+
tag.tagName !== ref) {
|
|
2018
|
+
if (!unproven || !this.nativeSvgServerEntry(ref))
|
|
2019
|
+
return false;
|
|
2020
|
+
reachesRenderer = true;
|
|
2021
|
+
continue;
|
|
2022
|
+
}
|
|
1790
2023
|
const owner = nativeOutputOwner(ts.isJsxSelfClosingElement(tag) ? tag : tag.parent, slot, destination);
|
|
1791
2024
|
if (!owner)
|
|
1792
2025
|
return false;
|
|
1793
2026
|
pending.push(owner);
|
|
1794
2027
|
}
|
|
1795
2028
|
}
|
|
1796
|
-
if (
|
|
2029
|
+
if (reachesRenderer)
|
|
1797
2030
|
proven.add(first);
|
|
1798
|
-
return
|
|
2031
|
+
return reachesRenderer;
|
|
1799
2032
|
}
|
|
1800
2033
|
/** Keep the imported React component as the type of the element the service receives. */
|
|
1801
2034
|
storeReactDescriptor(element) {
|
|
@@ -2364,8 +2597,60 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2364
2597
|
return null;
|
|
2365
2598
|
return { name: node.name.text, module: entry.module };
|
|
2366
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
|
+
}
|
|
2367
2605
|
return null;
|
|
2368
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
|
+
}
|
|
2369
2654
|
line(node) {
|
|
2370
2655
|
return this.sf.getLineAndCharacterOfPosition(node.getStart(this.sf)).line + 1;
|
|
2371
2656
|
}
|
|
@@ -2390,8 +2675,17 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2390
2675
|
}
|
|
2391
2676
|
// A React import of the same name becomes this import.
|
|
2392
2677
|
const prior = this.apiLocals.get(name);
|
|
2393
|
-
const
|
|
2394
|
-
|
|
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;
|
|
2395
2689
|
let local = collides ? `Octane${name[0].toUpperCase()}${name.slice(1)}` : name;
|
|
2396
2690
|
while (collides && (this.declared.has(local) || this.readsGlobal(local)))
|
|
2397
2691
|
local += '_';
|
|
@@ -2586,14 +2880,34 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2586
2880
|
return this.rewriteReexport(node);
|
|
2587
2881
|
if (ts.isCallExpression(node))
|
|
2588
2882
|
return this.rewriteCall(node);
|
|
2883
|
+
if (ts.isAwaitExpression(node) && ts.isPropertyAccessExpression(node.expression))
|
|
2884
|
+
this.checkServerStream(node);
|
|
2589
2885
|
if (ts.isPropertyAccessExpression(node)) {
|
|
2590
2886
|
const text = this.rewriteMember(node);
|
|
2591
2887
|
if (text === null)
|
|
2592
2888
|
this.checkProjectReactEventMember(node, node.name.text);
|
|
2593
2889
|
return text;
|
|
2594
2890
|
}
|
|
2595
|
-
if (ts.isElementAccessExpression(node) && ts.isStringLiteralLike(node.argumentExpression))
|
|
2596
|
-
|
|
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
|
+
}
|
|
2597
2911
|
if (ts.isQualifiedName(node))
|
|
2598
2912
|
return this.rewriteQualifiedType(node);
|
|
2599
2913
|
if (ts.isImportTypeNode(node))
|
|
@@ -2703,7 +3017,176 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2703
3017
|
}
|
|
2704
3018
|
return null;
|
|
2705
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
|
+
}
|
|
2706
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
|
+
}
|
|
2707
3190
|
rewriteImport(node) {
|
|
2708
3191
|
const native = this.pathSvgImports.get(node);
|
|
2709
3192
|
if (native) {
|
|
@@ -2719,15 +3202,43 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2719
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)`.");
|
|
2720
3203
|
}
|
|
2721
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
|
+
}
|
|
2722
3231
|
this.importSlots.push(node);
|
|
2723
3232
|
return `\u0000IMPORT${this.importSlots.length - 1}\u0000`;
|
|
2724
3233
|
}
|
|
2725
3234
|
const clause = node.importClause;
|
|
2726
3235
|
const bindings = clause?.namedBindings;
|
|
2727
|
-
if (bindings &&
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
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
|
+
}
|
|
2731
3242
|
if (clause && bindings && ts.isNamedImports(bindings)) {
|
|
2732
3243
|
const moved = new Map();
|
|
2733
3244
|
const kept = [];
|
|
@@ -2754,11 +3265,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2754
3265
|
const nativeOnly = sites.length > 0 &&
|
|
2755
3266
|
sites.every(Boolean) &&
|
|
2756
3267
|
(!sites.includes('afterRefs') || this.options.originalReact === true);
|
|
2757
|
-
const setup =
|
|
2758
|
-
calleeName: (callee) => this.calleeName(callee),
|
|
2759
|
-
nativeOutput: (element) => this.nativeOutput(element, this.localUses),
|
|
2760
|
-
originalReact: () => this.options.originalReact === true,
|
|
2761
|
-
};
|
|
3268
|
+
const setup = this.retainedHookSetup();
|
|
2762
3269
|
const call = refs[sites.indexOf('first')] ??
|
|
2763
3270
|
(nativeOnly
|
|
2764
3271
|
? refs[0]
|
|
@@ -2783,8 +3290,10 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2783
3290
|
privateContext: true,
|
|
2784
3291
|
}));
|
|
2785
3292
|
if (advisorySite)
|
|
2786
|
-
this.
|
|
3293
|
+
this.warnRetainedPrivateContext(module, name, advisorySite);
|
|
2787
3294
|
}
|
|
3295
|
+
else if (site && local && typeof local !== 'string' && 'retainedStore' in local)
|
|
3296
|
+
this.warnRetainedStore(module, name, site);
|
|
2788
3297
|
}
|
|
2789
3298
|
if (!dest) {
|
|
2790
3299
|
kept.push(item);
|
|
@@ -2831,7 +3340,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2831
3340
|
if (!port ||
|
|
2832
3341
|
ts
|
|
2833
3342
|
.getLeadingCommentRanges(this.source, node.pos)
|
|
2834
|
-
?.some((
|
|
3343
|
+
?.some(({ pos, end }) => /(?:No Octane port|Retained React import) of/.test(this.source.slice(pos, end))))
|
|
2835
3344
|
return text;
|
|
2836
3345
|
return `// ${portComment(port)}\n${text ?? node.getText(this.sf)}`;
|
|
2837
3346
|
}
|
|
@@ -2855,6 +3364,16 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2855
3364
|
const type = clause.isTypeOnly ? 'type ' : '';
|
|
2856
3365
|
const bindings = ts.isImportClause(clause) ? clause.namedBindings : clause.exportClause;
|
|
2857
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);
|
|
2858
3377
|
if (namespace === null)
|
|
2859
3378
|
return null;
|
|
2860
3379
|
const local = bindings.name.text;
|
|
@@ -2866,10 +3385,19 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2866
3385
|
const kept = [];
|
|
2867
3386
|
const values = [];
|
|
2868
3387
|
const types = [];
|
|
3388
|
+
let retainedRouterTypes;
|
|
2869
3389
|
let renamed = false;
|
|
2870
3390
|
for (const element of bindings.elements) {
|
|
2871
3391
|
const imported = (element.propertyName ?? element.name).text;
|
|
2872
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
|
+
}
|
|
2873
3401
|
if (module === 'radix-ui' && imported === 'Slot' && next === '@octanejs/radix') {
|
|
2874
3402
|
if (!ts.isImportSpecifier(element)) {
|
|
2875
3403
|
this.note(RULE.binding, 'error', element, 'The Octane binding has no Radix Slot namespace to re-export.');
|
|
@@ -2907,8 +3435,19 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2907
3435
|
}
|
|
2908
3436
|
if (namespace === null && kept.length === 0 && !renamed)
|
|
2909
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
|
+
}
|
|
2910
3445
|
this.count(RULE.import);
|
|
2911
|
-
const lines = kept.length > 0
|
|
3446
|
+
const lines = kept.length > 0
|
|
3447
|
+
? [
|
|
3448
|
+
`${keyword} ${retainedRouterTypes ? 'type ' : type}{ ${kept.join(', ')} } ${from(module)}`,
|
|
3449
|
+
]
|
|
3450
|
+
: [];
|
|
2912
3451
|
if (values.length > 0) {
|
|
2913
3452
|
lines.push(namespace === null
|
|
2914
3453
|
? `${keyword} ${type}{ ${values.join(', ')} } ${from(next)}`
|
|
@@ -2954,7 +3493,9 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2954
3493
|
for (const [index, item] of exports.elements.entries()) {
|
|
2955
3494
|
const text = this.source.slice(item.getFullStart(), exports.elements[index + 1]?.getFullStart() ?? exports.end - 1);
|
|
2956
3495
|
const name = (item.propertyName ?? item.name).text;
|
|
2957
|
-
|
|
3496
|
+
const typeOnly = node.isTypeOnly || item.isTypeOnly;
|
|
3497
|
+
const mapped = mapReactImport(module, name, typeOnly);
|
|
3498
|
+
if (mapped?.module === module) {
|
|
2958
3499
|
retained.push(text);
|
|
2959
3500
|
continue;
|
|
2960
3501
|
}
|
|
@@ -2965,6 +3506,10 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2965
3506
|
this.note(RULE.unsupported, 'error', item, 'Review the React event re-export and define an explicit native event type.');
|
|
2966
3507
|
}
|
|
2967
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}\`.`);
|
|
2968
3513
|
migrated.push(text);
|
|
2969
3514
|
}
|
|
2970
3515
|
}
|
|
@@ -2993,6 +3538,13 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
2993
3538
|
const next = this.resolve(specifier.text);
|
|
2994
3539
|
if (next === null || next === specifier.text)
|
|
2995
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;
|
|
2996
3548
|
const available = this.options.bindingExports?.(specifier.text, next);
|
|
2997
3549
|
if (available && !this.hasBindingExports(node, next, available))
|
|
2998
3550
|
return null;
|
|
@@ -3020,7 +3572,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3020
3572
|
return this.replaceSpecifier(node, specifier, next);
|
|
3021
3573
|
}
|
|
3022
3574
|
/** Check known exports without guessing at a whole namespace. */
|
|
3023
|
-
hasBindingExports(node, binding, available) {
|
|
3575
|
+
hasBindingExports(node, binding, available, retained) {
|
|
3024
3576
|
const hasDifferentType = (name) => binding === '@octanejs/motion' && name === 'MotionConfigContext';
|
|
3025
3577
|
const check = (site, name, needsType = false) => {
|
|
3026
3578
|
const missingType = needsType && hasDifferentType(name);
|
|
@@ -3038,6 +3590,8 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3038
3590
|
return check(node);
|
|
3039
3591
|
let supported = true;
|
|
3040
3592
|
for (const element of elements) {
|
|
3593
|
+
if (retained?.has(element))
|
|
3594
|
+
continue;
|
|
3041
3595
|
const name = (element.propertyName ?? element.name).text;
|
|
3042
3596
|
// A re-export can expose both the type and the value to another module.
|
|
3043
3597
|
const typeUse = hasDifferentType(name) &&
|
|
@@ -3135,15 +3689,37 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3135
3689
|
return null;
|
|
3136
3690
|
const specifier = argument.literal;
|
|
3137
3691
|
const module = REACT_MODULES[specifier.text];
|
|
3138
|
-
if (module === undefined)
|
|
3139
|
-
return this.rewriteSpecifier(node, specifier);
|
|
3140
3692
|
const qualifier = node.qualifier;
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
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));
|
|
3145
3707
|
return null;
|
|
3146
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
|
+
}
|
|
3147
3723
|
if (specifier.text === 'react' && !node.isTypeOf) {
|
|
3148
3724
|
const event = this.nativeEventType(node, name);
|
|
3149
3725
|
if (event !== undefined) {
|
|
@@ -3153,9 +3729,12 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3153
3729
|
}
|
|
3154
3730
|
}
|
|
3155
3731
|
this.count(RULE.import);
|
|
3156
|
-
const renamed = name === null
|
|
3157
|
-
|
|
3158
|
-
|
|
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}\`.`);
|
|
3159
3738
|
}
|
|
3160
3739
|
const quote = this.source[specifier.getStart(this.sf)];
|
|
3161
3740
|
const head = this.span(node, node.getStart(this.sf), specifier.getStart(this.sf), PLAIN) +
|
|
@@ -3173,6 +3752,99 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3173
3752
|
renamed +
|
|
3174
3753
|
this.span(node, qualifier.end, node.end, PLAIN));
|
|
3175
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
|
+
}
|
|
3176
3848
|
/**
|
|
3177
3849
|
* Fill each React import placeholder. The first one carries every Octane
|
|
3178
3850
|
* import the module needs, the module's own specifiers first; the others
|
|
@@ -3192,8 +3864,10 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3192
3864
|
};
|
|
3193
3865
|
/** React specifiers with no Octane export stay on React, for a person. */
|
|
3194
3866
|
const kept = new Map();
|
|
3867
|
+
const typeNamespaces = [];
|
|
3195
3868
|
/** Locals of React's `jsx` factories, which a JSX runtime consumer calls. */
|
|
3196
3869
|
const factories = [];
|
|
3870
|
+
let retainsCreateElement = false;
|
|
3197
3871
|
for (const node of this.importSlots) {
|
|
3198
3872
|
const module = node.moduleSpecifier.text;
|
|
3199
3873
|
const clause = node.importClause;
|
|
@@ -3203,17 +3877,46 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3203
3877
|
const namespace = clause.name?.text ??
|
|
3204
3878
|
(bindings && ts.isNamespaceImport(bindings) ? bindings.name.text : null);
|
|
3205
3879
|
// Members the rewrite could not map keep the namespace alive.
|
|
3206
|
-
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)) {
|
|
3207
3892
|
add(REACT_MODULES[module], '*', namespace, false);
|
|
3893
|
+
}
|
|
3208
3894
|
if (!bindings || !ts.isNamedImports(bindings))
|
|
3209
3895
|
continue;
|
|
3210
3896
|
for (const element of bindings.elements) {
|
|
3211
3897
|
const local = element.name.text;
|
|
3212
|
-
if (!this.used.has(local))
|
|
3213
|
-
continue;
|
|
3214
3898
|
const imported = (element.propertyName ?? element.name).text;
|
|
3215
|
-
|
|
3216
|
-
|
|
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);
|
|
3217
3920
|
if (mapped) {
|
|
3218
3921
|
// A name read only in types imports as a type, which bundlers elide.
|
|
3219
3922
|
add(mapped.module, mapped.name, local, typeOnly || !this.valueUses.has(local));
|
|
@@ -3226,15 +3929,25 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3226
3929
|
}
|
|
3227
3930
|
this.note(RULE.unsupported, 'error', element, UNSUPPORTED_VALUES[imported] ??
|
|
3228
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
|
+
}
|
|
3229
3937
|
const spec = (typeOnly ? 'type ' : '') + (imported === local ? local : `${imported} as ${local}`);
|
|
3230
3938
|
kept.set(module, [...(kept.get(module) ?? []), spec]);
|
|
3231
3939
|
}
|
|
3232
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');
|
|
3233
3946
|
for (const [module, names] of this.added) {
|
|
3234
3947
|
for (const [name, { local, typeOnly }] of names)
|
|
3235
3948
|
add(module, name, local, typeOnly);
|
|
3236
3949
|
}
|
|
3237
|
-
const lines =
|
|
3950
|
+
const lines = typeNamespaces;
|
|
3238
3951
|
const quote = (module) => this.quote + module + this.quote;
|
|
3239
3952
|
for (const [module, names] of imports) {
|
|
3240
3953
|
const specs = [...names.values()];
|
|
@@ -3255,10 +3968,10 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3255
3968
|
// React's runtime takes the key apart from the props; Octane's
|
|
3256
3969
|
// `createElement` reads it from them.
|
|
3257
3970
|
const params = this.isTypeScript()
|
|
3258
|
-
?
|
|
3971
|
+
? `type: Parameters<typeof ${jsxFactory}>[0], props: Record<string, unknown>, key?: string | number`
|
|
3259
3972
|
: 'type, props, key';
|
|
3260
3973
|
const [first, ...rest] = factories;
|
|
3261
|
-
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};`));
|
|
3262
3975
|
}
|
|
3263
3976
|
if (this.intrinsicPropsHelper) {
|
|
3264
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];`, '};');
|
|
@@ -3298,7 +4011,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3298
4011
|
// line, and directive lines. Anything inserted here starts a line, so
|
|
3299
4012
|
// the Strong fallback can find its own directive line again. The
|
|
3300
4013
|
// comment on an unported package's import stays with it.
|
|
3301
|
-
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);
|
|
3302
4015
|
return prologue ? prologue[0].length : 0;
|
|
3303
4016
|
}
|
|
3304
4017
|
addStrongDirective(code) {
|
|
@@ -3313,6 +4026,13 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3313
4026
|
return replaced !== code ? replaced : '/** @jsxImportSource octane */\n' + code;
|
|
3314
4027
|
}
|
|
3315
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
|
+
}
|
|
3316
4036
|
rewriteMember(node) {
|
|
3317
4037
|
// `<Ctx.Provider value>` is `<Ctx value>` in Octane, as in React 19.
|
|
3318
4038
|
const tag = node.parent;
|
|
@@ -3333,25 +4053,32 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3333
4053
|
(isTag || isValue) &&
|
|
3334
4054
|
this.isContext(node.expression, node)) {
|
|
3335
4055
|
if (!ts.isJsxClosingElement(tag)) {
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
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.`);
|
|
3340
4072
|
this.count(RULE.provider);
|
|
3341
4073
|
}
|
|
3342
4074
|
return this.emit(node.expression, PLAIN);
|
|
3343
4075
|
}
|
|
3344
|
-
const
|
|
3345
|
-
if (
|
|
3346
|
-
|
|
3347
|
-
const mapped = mapReactImport(namespace.module, name, false);
|
|
3348
|
-
if (!mapped) {
|
|
3349
|
-
this.used.add(namespace.local);
|
|
3350
|
-
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)
|
|
3351
4079
|
return null;
|
|
3352
|
-
}
|
|
3353
4080
|
this.count(RULE.import);
|
|
3354
|
-
return this.require(
|
|
4081
|
+
return this.require(reactMember.module, reactMember.name, false);
|
|
3355
4082
|
}
|
|
3356
4083
|
const member = this.rewritePackageMember(node.expression, node.name, 'error');
|
|
3357
4084
|
if (member !== null)
|
|
@@ -3367,6 +4094,18 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3367
4094
|
}
|
|
3368
4095
|
return null;
|
|
3369
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
|
+
}
|
|
3370
4109
|
/**
|
|
3371
4110
|
* Whether `node` names a global namespace React's types declare, such as
|
|
3372
4111
|
* `React` in `React.ReactNode` written without an import.
|
|
@@ -3382,6 +4121,17 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3382
4121
|
? entry.namespace && entry.module === 'react'
|
|
3383
4122
|
: this.isGlobalNamespace(node, 'React');
|
|
3384
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
|
+
}
|
|
3385
4135
|
rewriteQualifiedType(node) {
|
|
3386
4136
|
const member = this.rewritePackageMember(node.left, node.right, 'warning');
|
|
3387
4137
|
if (member !== null)
|
|
@@ -3392,26 +4142,117 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3392
4142
|
: !this.isGlobalNamespace(node.left, 'React')) {
|
|
3393
4143
|
return null;
|
|
3394
4144
|
}
|
|
3395
|
-
if (ts.
|
|
3396
|
-
|
|
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);
|
|
3397
4178
|
if (mapped?.module === 'react') {
|
|
3398
4179
|
if (!namespace)
|
|
3399
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
|
+
}
|
|
3400
4187
|
this.count(RULE.import);
|
|
3401
4188
|
return this.require(mapped.module, mapped.name, true);
|
|
3402
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
|
+
}
|
|
3403
4194
|
}
|
|
3404
|
-
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);
|
|
3405
4196
|
if (!name) {
|
|
3406
4197
|
if (namespace)
|
|
3407
4198
|
this.used.add(namespace.local);
|
|
3408
|
-
|
|
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)}\`.`);
|
|
3409
4201
|
return null;
|
|
3410
4202
|
}
|
|
3411
4203
|
this.count(RULE.import);
|
|
3412
4204
|
return this.require('octane', name, true);
|
|
3413
4205
|
}
|
|
3414
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
|
+
}
|
|
3415
4256
|
// The global `JSX` namespace React's types declared is Octane's export.
|
|
3416
4257
|
if (ts.isQualifiedName(name) && this.isGlobalNamespace(name.left, 'JSX'))
|
|
3417
4258
|
this.require('octane', 'JSX', true);
|
|
@@ -3487,8 +4328,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3487
4328
|
const root = ts.isIdentifier(receiver) ? this.constValue(receiver) : receiver;
|
|
3488
4329
|
if (root && ts.isCallExpression(root) && this.calleeName(root.expression) === 'createRoot') {
|
|
3489
4330
|
const rewritten = this.rewriteRootRender(node, node.arguments[0]);
|
|
3490
|
-
|
|
3491
|
-
this.checkLocalReactRoot(node, receiver, root);
|
|
4331
|
+
this.checkLocalReactRoot(node, receiver, root, rewritten === null);
|
|
3492
4332
|
return rewritten;
|
|
3493
4333
|
}
|
|
3494
4334
|
}
|
|
@@ -3509,8 +4349,14 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3509
4349
|
return this.rewriteRootRender(node, node.arguments[1]);
|
|
3510
4350
|
}
|
|
3511
4351
|
if ((name === 'renderToString' || name === 'renderToStaticMarkup') &&
|
|
3512
|
-
callee.module.startsWith('react-dom/server')
|
|
3513
|
-
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);
|
|
3514
4360
|
// React returns the HTML; Octane returns `{ html, css }`.
|
|
3515
4361
|
this.count(RULE.serverRender);
|
|
3516
4362
|
return ((this.rewriteRootRender(node, node.arguments[0]) ??
|
|
@@ -3518,6 +4364,24 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3518
4364
|
}
|
|
3519
4365
|
if (name === 'flushSync') {
|
|
3520
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
|
+
}
|
|
3521
4385
|
return null;
|
|
3522
4386
|
}
|
|
3523
4387
|
if (name in DEPENDENCY_HOOKS) {
|
|
@@ -3528,6 +4392,36 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3528
4392
|
}
|
|
3529
4393
|
return null;
|
|
3530
4394
|
}
|
|
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
|
+
}
|
|
3531
4425
|
checkInitializerResourceHints(initializer) {
|
|
3532
4426
|
const resourceHint = this.options.initializerResourceHint;
|
|
3533
4427
|
if (!resourceHint)
|
|
@@ -3840,7 +4734,162 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3840
4734
|
return;
|
|
3841
4735
|
return `[${field.name.getText(this.sf)}]`;
|
|
3842
4736
|
}
|
|
3843
|
-
|
|
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) {
|
|
3844
4893
|
const receiver = skipWrappers(member.expression);
|
|
3845
4894
|
const name = ts.isPropertyAccessExpression(member)
|
|
3846
4895
|
? member.name
|
|
@@ -3851,6 +4900,8 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3851
4900
|
const key = name.text;
|
|
3852
4901
|
if (key === '__proto__' || !ts.isIdentifier(receiver))
|
|
3853
4902
|
return false;
|
|
4903
|
+
if (this.ownStateDependency(receiver, key, hook, dependencies, nestedRead))
|
|
4904
|
+
return true;
|
|
3854
4905
|
const init = this.constValue(receiver);
|
|
3855
4906
|
const object = init && skipWrappers(init);
|
|
3856
4907
|
const owner = ts.findAncestor(hook, ts.isFunctionLike);
|
|
@@ -3862,36 +4913,101 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
3862
4913
|
reads(owner, (name) => name.text === 'eval'))
|
|
3863
4914
|
return false;
|
|
3864
4915
|
// Spreads, computed names and accessors can redefine the property.
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
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;
|
|
3868
4936
|
}
|
|
3869
4937
|
checkHookDependencyReads(node, deps) {
|
|
3870
4938
|
const list = skipWrappers(deps);
|
|
3871
4939
|
if (!this.emitsTsrx || !ts.isArrayLiteralExpression(list))
|
|
3872
4940
|
return;
|
|
3873
|
-
const read = (expr) => {
|
|
4941
|
+
const read = (expr, visit, coercesNumber = false) => {
|
|
3874
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);
|
|
3875
4949
|
if (ts.isConditionalExpression(member)) {
|
|
3876
4950
|
const condition = skipWrappers(member.condition);
|
|
3877
4951
|
if (condition.kind === ts.SyntaxKind.TrueKeyword)
|
|
3878
|
-
return read(member.whenTrue);
|
|
4952
|
+
return read(member.whenTrue, visit, coercesNumber);
|
|
3879
4953
|
if (condition.kind === ts.SyntaxKind.FalseKeyword)
|
|
3880
|
-
return read(member.whenFalse);
|
|
3881
|
-
return read(condition
|
|
4954
|
+
return read(member.whenFalse, visit, coercesNumber);
|
|
4955
|
+
return (read(condition, visit) ??
|
|
4956
|
+
read(member.whenTrue, visit, coercesNumber) ??
|
|
4957
|
+
read(member.whenFalse, visit, coercesNumber));
|
|
3882
4958
|
}
|
|
3883
|
-
if (
|
|
3884
|
-
|
|
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;
|
|
3885
4998
|
return member;
|
|
4999
|
+
}
|
|
3886
5000
|
};
|
|
3887
5001
|
let opaque;
|
|
3888
5002
|
for (const entry of list.elements) {
|
|
3889
|
-
opaque = read(entry);
|
|
5003
|
+
opaque = read(entry, check);
|
|
3890
5004
|
if (opaque)
|
|
3891
5005
|
break;
|
|
3892
5006
|
}
|
|
3893
5007
|
if (opaque)
|
|
3894
|
-
this.note(RULE.deps, 'error', 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.');
|
|
3895
5011
|
}
|
|
3896
5012
|
/**
|
|
3897
5013
|
* React runs a hook with an omitted or undefined list on every render.
|
|
@@ -4034,20 +5150,110 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
4034
5150
|
? `${this.require('octane', 'createRoot', false)}(${containerText}).render(${elementText})`
|
|
4035
5151
|
: `${this.require('octane', 'hydrateRoot', false)}(${containerText}, ${elementText})`;
|
|
4036
5152
|
}
|
|
4037
|
-
/** Report
|
|
4038
|
-
|
|
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) {
|
|
4039
5236
|
const local = this.options.localReactRootComponent;
|
|
4040
5237
|
const element = skipParens(call.arguments[0]);
|
|
4041
5238
|
if (!local || !ts.isJsxSelfClosingElement(element) || !ts.isIdentifier(element.tagName))
|
|
4042
5239
|
return;
|
|
4043
5240
|
const tag = element.tagName;
|
|
4044
5241
|
const leaf = this.importOf(tag);
|
|
5242
|
+
const attributes = element.attributes.properties;
|
|
4045
5243
|
const factoryName = ts.isPropertyAccessExpression(root.expression)
|
|
4046
5244
|
? root.expression.expression
|
|
4047
5245
|
: root.expression;
|
|
4048
5246
|
const rootImport = this.importOf(factoryName);
|
|
4049
5247
|
const factoryBinding = ts.isIdentifier(factoryName) && this.declarationOf(factoryName);
|
|
4050
|
-
if (!
|
|
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 ||
|
|
4051
5257
|
leaf.typeOnly ||
|
|
4052
5258
|
ts.findAncestor(this.declarationOf(tag), ts.isImportDeclaration)?.attributes ||
|
|
4053
5259
|
rootImport?.module !== 'react-dom/client' ||
|
|
@@ -4067,22 +5273,9 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
4067
5273
|
ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export ||
|
|
4068
5274
|
(ts.getCombinedNodeFlags(declaration) & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const)
|
|
4069
5275
|
return;
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
const topLevel = ts.isExpressionStatement(call.parent) &&
|
|
4074
|
-
call.parent.parent === this.sf &&
|
|
4075
|
-
statement.parent === this.sf &&
|
|
4076
|
-
statement.end <= call.getStart(this.sf);
|
|
4077
|
-
if ((!topLevel && !initializedBefore(this.checker, receiver, call)) ||
|
|
4078
|
-
renderUses(this.localUses(declaration.name)).some((ref) => {
|
|
4079
|
-
const access = ref.parent;
|
|
4080
|
-
return (!ts.isPropertyAccessExpression(access) ||
|
|
4081
|
-
access.expression !== ref ||
|
|
4082
|
-
(access.name.text !== 'render' && access.name.text !== 'unmount') ||
|
|
4083
|
-
!ts.isCallExpression(access.parent) ||
|
|
4084
|
-
access.parent.expression !== access);
|
|
4085
|
-
}))
|
|
5276
|
+
if ((!atInitializedModuleHandle(declaration, call) &&
|
|
5277
|
+
!initializedBefore(this.checker, receiver, call)) ||
|
|
5278
|
+
rootHandleEscapes(this.localUses(declaration.name)))
|
|
4086
5279
|
return;
|
|
4087
5280
|
}
|
|
4088
5281
|
const hook = local(leaf.module, leaf.imported);
|
|
@@ -4090,10 +5283,8 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
4090
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.`);
|
|
4091
5284
|
}
|
|
4092
5285
|
/**
|
|
4093
|
-
*
|
|
4094
|
-
*
|
|
4095
|
-
* element: the generic element renderer stays out of the bundle. So are
|
|
4096
|
-
* `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.
|
|
4097
5288
|
*
|
|
4098
5289
|
* @param argument the element, the call's last argument
|
|
4099
5290
|
*/
|
|
@@ -4124,12 +5315,12 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
4124
5315
|
return null;
|
|
4125
5316
|
if (!isComponentName(tag.getText(this.sf).split('.').pop() ?? ''))
|
|
4126
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;
|
|
4127
5322
|
const props = [];
|
|
4128
|
-
for (const property of
|
|
4129
|
-
if (ts.isJsxSpreadAttribute(property)) {
|
|
4130
|
-
props.push(`...${this.emit(property.expression, PLAIN)}`);
|
|
4131
|
-
continue;
|
|
4132
|
-
}
|
|
5323
|
+
for (const property of attributes) {
|
|
4133
5324
|
const name = property.name.getText(this.sf);
|
|
4134
5325
|
if (name === 'key' || name === 'ref')
|
|
4135
5326
|
return null;
|
|
@@ -4137,8 +5328,12 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
4137
5328
|
const value = property.initializer;
|
|
4138
5329
|
if (!value)
|
|
4139
5330
|
props.push(`${key}: true`);
|
|
4140
|
-
else if (ts.isStringLiteral(value))
|
|
4141
|
-
|
|
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
|
+
}
|
|
4142
5337
|
else if (ts.isJsxExpression(value) && value.expression)
|
|
4143
5338
|
props.push(`${key}: ${this.emit(value.expression, PLAIN)}`);
|
|
4144
5339
|
else
|
|
@@ -4155,8 +5350,10 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
|
|
|
4155
5350
|
return null;
|
|
4156
5351
|
if (!isComponentName(type.getText(this.sf).split('.').pop() ?? ''))
|
|
4157
5352
|
return null;
|
|
4158
|
-
|
|
4159
|
-
|
|
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);
|
|
4160
5357
|
}
|
|
4161
5358
|
return null;
|
|
4162
5359
|
}
|
|
@@ -4540,11 +5737,30 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4540
5737
|
: `Class component \`${node.name?.text ?? 'class'}\` needs a rewrite as a function component.`);
|
|
4541
5738
|
}
|
|
4542
5739
|
checkDefaultProps(node) {
|
|
4543
|
-
if (node.operatorToken.kind
|
|
4544
|
-
ts.isPropertyAccessExpression(node.left)
|
|
4545
|
-
|
|
4546
|
-
|
|
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
|
+
}
|
|
4547
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.');
|
|
4548
5764
|
}
|
|
4549
5765
|
// ---------------------------------------------------------------- JSX
|
|
4550
5766
|
rewriteAttributes(node, ctx) {
|
|
@@ -4627,6 +5843,8 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4627
5843
|
out += this.emit(attribute, PLAIN);
|
|
4628
5844
|
}
|
|
4629
5845
|
}
|
|
5846
|
+
if (ts.isJsxSelfClosingElement(element) && this.inlineIntlMessages.has(element))
|
|
5847
|
+
out += ` inlineHost={${this.require('octane/react', 'ReactCompatInline', false)}}`;
|
|
4630
5848
|
return out + this.source.slice(cursor, node.end);
|
|
4631
5849
|
}
|
|
4632
5850
|
mayHaveJsxHandler(attribute) {
|
|
@@ -4665,6 +5883,18 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4665
5883
|
const value = type.initializer;
|
|
4666
5884
|
if (value && ts.isStringLiteral(value))
|
|
4667
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
|
+
}
|
|
4668
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.');
|
|
4669
5899
|
return false;
|
|
4670
5900
|
}
|
|
@@ -4679,12 +5909,12 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4679
5909
|
// in React; `@if` or `@for` there would hand it a fragment instead.
|
|
4680
5910
|
const declaration = ts.isIdentifier(tag) ? this.declarationOf(tag) : null;
|
|
4681
5911
|
const entry = this.importOf(tag);
|
|
4682
|
-
const imported = entry && !entry.namespace && !entry.typeOnly
|
|
5912
|
+
const imported = entry && !entry.namespace && !entry.typeOnly ? entry : null;
|
|
4683
5913
|
if ((declaration &&
|
|
4684
5914
|
(ts.isVariableDeclaration(declaration) || ts.isFunctionDeclaration(declaration)) &&
|
|
4685
5915
|
this.descriptorComponents.has(declaration)) ||
|
|
4686
5916
|
(this.hasReactBridge && this.reactCompatTag(tag)) ||
|
|
4687
|
-
(imported && this.options.descriptorChild?.(imported))) {
|
|
5917
|
+
(imported?.module.startsWith('.') && this.options.descriptorChild?.(imported))) {
|
|
4688
5918
|
return this.span(node, node.getStart(this.sf), node.end, DESCRIPTOR);
|
|
4689
5919
|
}
|
|
4690
5920
|
const name = this.calleeName(tag);
|
|
@@ -4694,16 +5924,22 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4694
5924
|
const binding = declaration && ts.isFunctionDeclaration(declaration)
|
|
4695
5925
|
? declaration.getStart(this.sf)
|
|
4696
5926
|
: imported;
|
|
4697
|
-
|
|
4698
|
-
|
|
4699
|
-
|
|
4700
|
-
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
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
|
+
'} />');
|
|
4706
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={...}\`.`);
|
|
4707
5943
|
}
|
|
4708
5944
|
if (name === 'StrictMode') {
|
|
4709
5945
|
// Octane's StrictMode is a pass-through wrapper.
|
|
@@ -4751,16 +5987,21 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4751
5987
|
}
|
|
4752
5988
|
return ts.isCallExpression(node) && this.calleeName(node.expression) === 'createContext';
|
|
4753
5989
|
}
|
|
4754
|
-
|
|
4755
|
-
if (!this.source.includes('defineProperty') &&
|
|
4756
|
-
|
|
5990
|
+
localProviderReadChange(context, capture) {
|
|
5991
|
+
if (!this.source.includes('defineProperty') &&
|
|
5992
|
+
!(capture && this.source.includes('assign')) &&
|
|
5993
|
+
!this.source.includes('\\u'))
|
|
5994
|
+
return;
|
|
4757
5995
|
const initial = this.constValue(context);
|
|
4758
5996
|
const factory = initial && skipParens(initial);
|
|
4759
5997
|
if (!factory ||
|
|
4760
5998
|
!ts.isCallExpression(factory) ||
|
|
4761
5999
|
this.reactHook(factory.expression) !== 'createContext')
|
|
4762
|
-
return
|
|
4763
|
-
|
|
6000
|
+
return;
|
|
6001
|
+
let change = 'getter';
|
|
6002
|
+
const declaration = this.declarationOf(context);
|
|
6003
|
+
const references = this.localUses(context);
|
|
6004
|
+
const found = references.some((reference) => {
|
|
4764
6005
|
let target = reference;
|
|
4765
6006
|
while (isWrapper(target.parent) && target.parent.expression === target)
|
|
4766
6007
|
target = target.parent;
|
|
@@ -4775,16 +6016,50 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4775
6016
|
return false;
|
|
4776
6017
|
const method = skipWrappers(call.expression);
|
|
4777
6018
|
const object = ts.isPropertyAccessExpression(method) && skipWrappers(method.expression);
|
|
4778
|
-
const [, keyArgument, descriptorArgument] = call.arguments;
|
|
4779
|
-
const key = keyArgument && skipWrappers(keyArgument);
|
|
4780
|
-
const descriptor = descriptorArgument && skipWrappers(descriptorArgument);
|
|
4781
6019
|
if (!ts.isPropertyAccessExpression(method) ||
|
|
4782
6020
|
method.questionDotToken ||
|
|
4783
|
-
method.name.text !== 'defineProperty' ||
|
|
4784
6021
|
!object ||
|
|
4785
6022
|
!ts.isIdentifier(object) ||
|
|
4786
6023
|
object.text !== 'Object' ||
|
|
4787
|
-
this.declarationOf(object)?.getSourceFile().hasNoDefaultLib !== true
|
|
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' ||
|
|
4788
6063
|
!key ||
|
|
4789
6064
|
!ts.isStringLiteralLike(key) ||
|
|
4790
6065
|
key.text !== 'Provider' ||
|
|
@@ -4799,22 +6074,26 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4799
6074
|
return false;
|
|
4800
6075
|
if (name.text === 'configurable' || name.text === 'enumerable')
|
|
4801
6076
|
continue;
|
|
4802
|
-
if (name.text
|
|
4803
|
-
|
|
4804
|
-
|
|
4805
|
-
|
|
4806
|
-
|
|
4807
|
-
|
|
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)
|
|
4808
6090
|
return false;
|
|
4809
6091
|
setter = true;
|
|
4810
|
-
continue;
|
|
4811
6092
|
}
|
|
4812
|
-
if (name.text !== 'get' || getter || !ts.isMethodDeclaration(property))
|
|
4813
|
-
return false;
|
|
4814
|
-
getter = true;
|
|
4815
6093
|
}
|
|
4816
6094
|
return getter;
|
|
4817
6095
|
});
|
|
6096
|
+
return found ? change : undefined;
|
|
4818
6097
|
}
|
|
4819
6098
|
/**
|
|
4820
6099
|
* `typeof children === 'function'` also holds for a compiled children
|
|
@@ -4881,12 +6160,9 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4881
6160
|
const output = isJsx(body) ? this.emit(body, TEMPLATE) : `<>{${this.emit(body, PLAIN)}}</>`;
|
|
4882
6161
|
return `@{ const ${param} = ${useContext}(${this.emit(tag.expression, PLAIN)}); ${output} }`;
|
|
4883
6162
|
}
|
|
4884
|
-
|
|
4885
|
-
|
|
4886
|
-
|
|
4887
|
-
*/
|
|
4888
|
-
rewriteSuspense(node) {
|
|
4889
|
-
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'));
|
|
4890
6166
|
const fallback = attributes[0];
|
|
4891
6167
|
if (attributes.length !== 1 ||
|
|
4892
6168
|
!ts.isJsxAttribute(fallback) ||
|
|
@@ -4894,8 +6170,20 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
4894
6170
|
!fallback.initializer) {
|
|
4895
6171
|
return null;
|
|
4896
6172
|
}
|
|
4897
|
-
let pending;
|
|
4898
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;
|
|
4899
6187
|
if (ts.isStringLiteral(value))
|
|
4900
6188
|
pending = `<>{${value.getText(this.sf)}}</>`;
|
|
4901
6189
|
else if (ts.isJsxExpression(value) && value.expression) {
|
|
@@ -5102,22 +6390,23 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5102
6390
|
* The template directive equivalent to a renderable expression, as
|
|
5103
6391
|
* relative text, or null.
|
|
5104
6392
|
*/
|
|
5105
|
-
directive(expression) {
|
|
6393
|
+
directive(expression, componentRoot = false) {
|
|
5106
6394
|
const node = skipParens(expression);
|
|
5107
6395
|
if (ts.isConditionalExpression(node))
|
|
5108
|
-
return this.ifDirective(node);
|
|
6396
|
+
return this.ifDirective(node, componentRoot);
|
|
5109
6397
|
if (ts.isBinaryExpression(node) &&
|
|
5110
6398
|
node.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken &&
|
|
5111
6399
|
(this.isBoolean(node.left) ||
|
|
5112
6400
|
(this.strictTypes && isBooleanGuardType(this.typeOf(node.left))))) {
|
|
5113
|
-
const
|
|
6401
|
+
const condition = componentRoot ? literalCondition(node.left) : undefined;
|
|
6402
|
+
const arm = this.arm(node.right, componentRoot && (condition === undefined || condition === 'truthy'));
|
|
5114
6403
|
if (arm === null || arm === '')
|
|
5115
6404
|
return null;
|
|
5116
6405
|
this.count(RULE.if);
|
|
5117
6406
|
return this.block(`@if (${this.emit(node.left, PLAIN)})`, arm);
|
|
5118
6407
|
}
|
|
5119
6408
|
if (ts.isCallExpression(node))
|
|
5120
|
-
return this.forDirective(node);
|
|
6409
|
+
return this.forDirective(node, componentRoot);
|
|
5121
6410
|
return null;
|
|
5122
6411
|
}
|
|
5123
6412
|
/**
|
|
@@ -5125,13 +6414,16 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5125
6414
|
* directive, or '' for nothing. An arm that is only a value is null; the
|
|
5126
6415
|
* caller decides whether it may become a `{value}` hole.
|
|
5127
6416
|
*/
|
|
5128
|
-
arm(expression) {
|
|
6417
|
+
arm(expression, componentRoot = false) {
|
|
5129
6418
|
const node = skipParens(expression);
|
|
5130
|
-
if (isJsx(node))
|
|
6419
|
+
if (isJsx(node)) {
|
|
6420
|
+
if (componentRoot)
|
|
6421
|
+
this.checkNativeOutput(node);
|
|
5131
6422
|
return this.relative(this.emit(node, TEMPLATE), node);
|
|
6423
|
+
}
|
|
5132
6424
|
if (isNullish(node))
|
|
5133
6425
|
return '';
|
|
5134
|
-
const directive = this.directive(node);
|
|
6426
|
+
const directive = this.directive(node, componentRoot);
|
|
5135
6427
|
// A retained map is a JSX hole, which needs a fragment inside a directive arm.
|
|
5136
6428
|
return directive?.startsWith('{') ? `<>${directive}</>` : directive;
|
|
5137
6429
|
}
|
|
@@ -5164,12 +6456,13 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5164
6456
|
const condition = this.emit(test, PLAIN);
|
|
5165
6457
|
return `<input defaultValue={${condition} ? ${JSON.stringify(yes)} : ${JSON.stringify(no)}} />`;
|
|
5166
6458
|
}
|
|
5167
|
-
ifDirective(node) {
|
|
6459
|
+
ifDirective(node, componentRoot = false) {
|
|
5168
6460
|
const shared = this.conditionalInputDefault(node);
|
|
5169
6461
|
if (shared !== null)
|
|
5170
6462
|
return shared;
|
|
5171
|
-
|
|
5172
|
-
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');
|
|
5173
6466
|
// A ternary between two values is a value; one with an element on
|
|
5174
6467
|
// either side is control flow, and a value arm is a hole in a fragment:
|
|
5175
6468
|
// a bare `{value}` arm is a JavaScript block, not a hole.
|
|
@@ -5188,11 +6481,14 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5188
6481
|
const then = this.block(`@if (${condition})`, whenTrue);
|
|
5189
6482
|
return whenFalse === '' ? then : then + ' ' + this.block('@else', whenFalse);
|
|
5190
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
|
+
}
|
|
5191
6487
|
/**
|
|
5192
6488
|
* `items.map((item, i) => <Row key={item.id} />)` is
|
|
5193
6489
|
* `@for (const item of items; index i; key item.id) { <Row /> }`.
|
|
5194
6490
|
*/
|
|
5195
|
-
forDirective(node) {
|
|
6491
|
+
forDirective(node, componentRoot) {
|
|
5196
6492
|
const callee = node.expression;
|
|
5197
6493
|
if (!ts.isPropertyAccessExpression(callee) || callee.name.text !== 'map')
|
|
5198
6494
|
return null;
|
|
@@ -5248,6 +6544,15 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5248
6544
|
if (ts.isBlock(fn.body)) {
|
|
5249
6545
|
const statements = fn.body.statements;
|
|
5250
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
|
+
}
|
|
5251
6556
|
if (!last || !ts.isReturnStatement(last) || !last.expression)
|
|
5252
6557
|
return null;
|
|
5253
6558
|
setup = statements.slice(0, -1);
|
|
@@ -5281,9 +6586,8 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5281
6586
|
return null;
|
|
5282
6587
|
}
|
|
5283
6588
|
}
|
|
5284
|
-
if (!literal)
|
|
5285
|
-
this.
|
|
5286
|
-
}
|
|
6589
|
+
if (!literal)
|
|
6590
|
+
this.reportOpaqueMap(node);
|
|
5287
6591
|
if (attribute && key && !this.stableRowKey(key, item, iterable)) {
|
|
5288
6592
|
const open = attribute.parent.parent;
|
|
5289
6593
|
const children = ts.isJsxElement(root) ? root.children : [];
|
|
@@ -5295,7 +6599,7 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5295
6599
|
if (!value.expression)
|
|
5296
6600
|
return false;
|
|
5297
6601
|
const expression = skipWrappers(value.expression);
|
|
5298
|
-
return !(
|
|
6602
|
+
return !(isPrimitiveLiteral(expression) ||
|
|
5299
6603
|
(ts.isIdentifier(expression) &&
|
|
5300
6604
|
fixedBinding(this.checker, expression, expression) &&
|
|
5301
6605
|
initializedBefore(this.checker, expression, expression)));
|
|
@@ -5328,6 +6632,10 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5328
6632
|
this.calleeName(opening.tagName) === 'Fragment')
|
|
5329
6633
|
this.bare.add(opening.parent);
|
|
5330
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);
|
|
5331
6639
|
const arm = ts.isConditionalExpression(root)
|
|
5332
6640
|
? this.ifDirective(root)
|
|
5333
6641
|
: this.relative(this.emit(root, TEMPLATE), root);
|
|
@@ -5361,22 +6669,53 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5361
6669
|
return this.block(header, statements + '\n' + arm);
|
|
5362
6670
|
}
|
|
5363
6671
|
patternReadsOrdinal(pattern, ordinal) {
|
|
5364
|
-
const
|
|
6672
|
+
const nonCoercingBinary = (op) => op === ts.SyntaxKind.CommaToken ||
|
|
5365
6673
|
op === ts.SyntaxKind.EqualsEqualsEqualsToken ||
|
|
5366
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
|
+
};
|
|
5367
6680
|
const safePrefix = (expr) => {
|
|
5368
6681
|
const value = skipWrappers(expr);
|
|
5369
|
-
if (
|
|
6682
|
+
if (isPrimitiveLiteral(value))
|
|
5370
6683
|
return true;
|
|
6684
|
+
if (ts.isVoidExpression(value))
|
|
6685
|
+
return safePrefix(value.expression);
|
|
5371
6686
|
if (ts.isBinaryExpression(value))
|
|
5372
|
-
return (
|
|
6687
|
+
return (nonCoercingBinary(value.operatorToken.kind) &&
|
|
6688
|
+
safePrefix(value.left) &&
|
|
6689
|
+
safePrefix(value.right));
|
|
5373
6690
|
if (ts.isArrayLiteralExpression(value))
|
|
5374
6691
|
return value.elements.every((element) => ts.isOmittedExpression(element) || safePrefix(element));
|
|
5375
6692
|
return (ts.isObjectLiteralExpression(value) &&
|
|
5376
|
-
value.properties.every((property) => ts.
|
|
5377
|
-
(
|
|
5378
|
-
|
|
5379
|
-
|
|
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);
|
|
5380
6719
|
};
|
|
5381
6720
|
// Once a value can interrupt evaluation, a later read is no longer proven.
|
|
5382
6721
|
const readsInOrder = (expressions) => {
|
|
@@ -5397,7 +6736,7 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5397
6736
|
const read = skipWrappers(expr);
|
|
5398
6737
|
if (ts.isIdentifier(read))
|
|
5399
6738
|
return this.declarationOf(read) === ordinal;
|
|
5400
|
-
if (ts.isTypeOfExpression(read) || ts.isVoidExpression(read))
|
|
6739
|
+
if (ts.isTypeOfExpression(read) || ts.isVoidExpression(read) || ts.isDeleteExpression(read))
|
|
5401
6740
|
return readsOrdinal(read.expression);
|
|
5402
6741
|
if (ts.isPrefixUnaryExpression(read))
|
|
5403
6742
|
return (read.operator !== ts.SyntaxKind.PlusPlusToken &&
|
|
@@ -5408,17 +6747,16 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5408
6747
|
if (ts.isConditionalExpression(read)) {
|
|
5409
6748
|
if (readsOrdinal(read.condition))
|
|
5410
6749
|
return true;
|
|
5411
|
-
const
|
|
5412
|
-
|
|
5413
|
-
|
|
5414
|
-
return test === ts.SyntaxKind.FalseKeyword && readsOrdinal(read.whenFalse);
|
|
6750
|
+
const condition = branchCondition(read.condition);
|
|
6751
|
+
return (condition !== undefined &&
|
|
6752
|
+
readsOrdinal(condition === 'truthy' ? read.whenTrue : read.whenFalse));
|
|
5415
6753
|
}
|
|
5416
6754
|
if (ts.isTemplateExpression(read)) {
|
|
5417
6755
|
for (const { expression } of read.templateSpans) {
|
|
5418
6756
|
if (readsOrdinal(expression))
|
|
5419
6757
|
return true;
|
|
5420
6758
|
// Each interpolation is coerced to text before the next one runs.
|
|
5421
|
-
if (!
|
|
6759
|
+
if (!isPrimitiveLiteral(expression))
|
|
5422
6760
|
return false;
|
|
5423
6761
|
}
|
|
5424
6762
|
return false;
|
|
@@ -5427,13 +6765,18 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5427
6765
|
return readsInOrder(read.elements.filter((element) => !ts.isOmittedExpression(element)));
|
|
5428
6766
|
if (ts.isObjectLiteralExpression(read)) {
|
|
5429
6767
|
for (const property of read.properties) {
|
|
5430
|
-
if (ts.isSpreadAssignment(property))
|
|
5431
|
-
|
|
6768
|
+
if (ts.isSpreadAssignment(property)) {
|
|
6769
|
+
if (readsOrdinal(property.expression))
|
|
6770
|
+
return true;
|
|
6771
|
+
if (!safeObjectSpread(property.expression))
|
|
6772
|
+
return false;
|
|
6773
|
+
continue;
|
|
6774
|
+
}
|
|
5432
6775
|
if (ts.isComputedPropertyName(property.name)) {
|
|
5433
6776
|
const key = property.name.expression;
|
|
5434
6777
|
if (readsOrdinal(key))
|
|
5435
6778
|
return true;
|
|
5436
|
-
if (!
|
|
6779
|
+
if (!isPrimitiveLiteral(key))
|
|
5437
6780
|
return false;
|
|
5438
6781
|
}
|
|
5439
6782
|
if (ts.isShorthandPropertyAssignment(property))
|
|
@@ -5451,10 +6794,10 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5451
6794
|
if (readsOrdinal(read.expression))
|
|
5452
6795
|
return true;
|
|
5453
6796
|
const receiver = skipWrappers(read.expression);
|
|
5454
|
-
// A safe
|
|
6797
|
+
// A safe prefix can still produce a nullish receiver.
|
|
5455
6798
|
return (safePrefix(receiver) &&
|
|
5456
6799
|
(!ts.isOptionalChain(read) ||
|
|
5457
|
-
(
|
|
6800
|
+
(!ts.isBinaryExpression(receiver) && literalCondition(receiver) !== 'nullish')) &&
|
|
5458
6801
|
readsOrdinal(read.argumentExpression));
|
|
5459
6802
|
}
|
|
5460
6803
|
if (ts.isCallExpression(read)) {
|
|
@@ -5464,9 +6807,14 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5464
6807
|
if (ts.isArrowFunction(callee) &&
|
|
5465
6808
|
!callee.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) &&
|
|
5466
6809
|
callee.parameters.length === 0 &&
|
|
5467
|
-
read.arguments.length === 0
|
|
5468
|
-
!ts.isBlock(callee.body))
|
|
5469
|
-
|
|
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
|
+
}
|
|
5470
6818
|
// Optional calls can skip their arguments, and earlier arguments or a
|
|
5471
6819
|
// member getter can interrupt evaluation before the ordinal read.
|
|
5472
6820
|
if (!ts.isArrowFunction(callee) && (ts.isOptionalChain(read) || !ts.isIdentifier(callee)))
|
|
@@ -5485,25 +6833,25 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5485
6833
|
if (!ts.isBinaryExpression(read))
|
|
5486
6834
|
return false;
|
|
5487
6835
|
const op = read.operatorToken.kind;
|
|
5488
|
-
if (eagerBinary(op))
|
|
5489
|
-
return readsInOrder([read.left, read.right]);
|
|
5490
6836
|
if (op === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
5491
6837
|
op === ts.SyntaxKind.BarBarToken ||
|
|
5492
6838
|
op === ts.SyntaxKind.QuestionQuestionToken) {
|
|
5493
6839
|
if (readsOrdinal(read.left))
|
|
5494
6840
|
return true;
|
|
5495
|
-
const
|
|
5496
|
-
|
|
5497
|
-
|
|
6841
|
+
const condition = branchCondition(read.left);
|
|
6842
|
+
if (condition === undefined)
|
|
6843
|
+
return false;
|
|
6844
|
+
const reachesRight = op === ts.SyntaxKind.AmpersandAmpersandToken
|
|
6845
|
+
? condition === 'truthy'
|
|
5498
6846
|
: op === ts.SyntaxKind.BarBarToken
|
|
5499
|
-
?
|
|
5500
|
-
:
|
|
5501
|
-
return
|
|
5502
|
-
}
|
|
5503
|
-
|
|
5504
|
-
|
|
5505
|
-
|
|
5506
|
-
|
|
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]));
|
|
5507
6855
|
};
|
|
5508
6856
|
return (!ts.isIdentifier(pattern) &&
|
|
5509
6857
|
pattern.elements.some((element) => ts.isBindingElement(element) &&
|
|
@@ -5553,6 +6901,32 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5553
6901
|
};
|
|
5554
6902
|
return statements.some(exitsWithValue) ? null : exits;
|
|
5555
6903
|
}
|
|
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)
|
|
6915
|
+
return false;
|
|
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
|
+
}
|
|
5556
6930
|
// The automatic JSX runtime receives props (including children) before its
|
|
5557
6931
|
// separate key argument. Keep a block-body map so the compiler builds every
|
|
5558
6932
|
// row value before rendering any of them.
|
|
@@ -5600,14 +6974,14 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5600
6974
|
return '{' + head + '{ return ' + row + '; }' + tail + '}';
|
|
5601
6975
|
}
|
|
5602
6976
|
stableRowKey(key, item, iterable) {
|
|
5603
|
-
if (
|
|
6977
|
+
if (isPrimitiveLiteral(key))
|
|
5604
6978
|
return true;
|
|
5605
6979
|
key = skipWrappers(key);
|
|
5606
6980
|
const source = skipWrappers(iterable);
|
|
5607
6981
|
return (ts.isIdentifier(key) &&
|
|
5608
6982
|
this.declarationOf(key) === item &&
|
|
5609
6983
|
ts.isArrayLiteralExpression(source) &&
|
|
5610
|
-
source.elements.every(
|
|
6984
|
+
source.elements.every(isPrimitiveLiteral) &&
|
|
5611
6985
|
fixedBinding(this.checker, key, key));
|
|
5612
6986
|
}
|
|
5613
6987
|
/** An @for header evaluates its key before the row body runs. */
|
|
@@ -5619,7 +6993,7 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5619
6993
|
// Even an accessor key can precede initialization that reads nothing.
|
|
5620
6994
|
return (setup.every((statement) => ts.isVariableStatement(statement) &&
|
|
5621
6995
|
(statement.declarationList.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const &&
|
|
5622
|
-
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)));
|
|
5623
6997
|
}
|
|
5624
6998
|
readsLocals(expression, statements) {
|
|
5625
6999
|
const declared = new Set(statements
|
|
@@ -5662,13 +7036,27 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5662
7036
|
if (!last || !ts.isReturnStatement(last) || !last.expression)
|
|
5663
7037
|
return null;
|
|
5664
7038
|
const guard = statements.findIndex((s, i) => i < statements.length - 1 && guardReturn(s));
|
|
5665
|
-
if (guard !== -1)
|
|
5666
|
-
|
|
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);
|
|
5667
7045
|
// A template body has one output; any other early `return <A />` would
|
|
5668
7046
|
// render through runtime descriptors, so the plain function stays.
|
|
5669
|
-
|
|
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);
|
|
5670
7058
|
return null;
|
|
5671
|
-
|
|
7059
|
+
}
|
|
5672
7060
|
let output = this.output(inner);
|
|
5673
7061
|
if (output === null)
|
|
5674
7062
|
return null;
|
|
@@ -5707,7 +7095,14 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5707
7095
|
const rest = statements.slice(first + 1, -1).filter((statement) => !guardReturn(statement));
|
|
5708
7096
|
if (returnsJsx(statements.slice(0, first)) || this.emptyArmExit(rest) === null)
|
|
5709
7097
|
return null;
|
|
5710
|
-
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);
|
|
5711
7106
|
if (output === null)
|
|
5712
7107
|
return null;
|
|
5713
7108
|
const chain = (from) => {
|
|
@@ -5725,8 +7120,11 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5725
7120
|
if (at === statements.length - 1)
|
|
5726
7121
|
return prefix + output;
|
|
5727
7122
|
const guard = statements[at];
|
|
7123
|
+
const condition = checkRoot ? literalCondition(guard.expression) : undefined;
|
|
5728
7124
|
this.count(RULE.if);
|
|
5729
|
-
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')));
|
|
5730
7128
|
const rest = chain(at + 1);
|
|
5731
7129
|
return prefix + (rest === '' ? then : then + ' ' + this.block('@else', rest));
|
|
5732
7130
|
};
|
|
@@ -5747,17 +7145,290 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
|
|
|
5747
7145
|
* and indentation `node` had, or null when it is not a single renderable.
|
|
5748
7146
|
*/
|
|
5749
7147
|
output(node) {
|
|
5750
|
-
if (isJsx(node))
|
|
7148
|
+
if (isJsx(node)) {
|
|
7149
|
+
this.checkNativeOutput(node);
|
|
5751
7150
|
return this.emit(node, TEMPLATE);
|
|
7151
|
+
}
|
|
5752
7152
|
if (ts.isConditionalExpression(node) || ts.isCallExpression(node)) {
|
|
5753
|
-
const directive = this.directive(node);
|
|
7153
|
+
const directive = this.directive(node, this.options.localReactClass !== undefined || this.options.localReactContext !== undefined);
|
|
5754
7154
|
return directive === null ? null : this.absolute(directive, node);
|
|
5755
7155
|
}
|
|
5756
7156
|
return null;
|
|
5757
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
|
+
}
|
|
5758
7392
|
startsLine(node) {
|
|
5759
7393
|
return this.linePrefix(node).trim() === '';
|
|
5760
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
|
+
}
|
|
5761
7432
|
/**
|
|
5762
7433
|
* Whether `node` is a component definition: a capitalized function, or the
|
|
5763
7434
|
* function passed to `memo` / `forwardRef`, or a default-exported one.
|