@octanejs/codemod 0.2.1 → 0.2.3

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.
@@ -1,9 +1,12 @@
1
1
  import ts from 'typescript';
2
+ import { parseSync } from '@tsrx/oxc/parser';
2
3
  import { hasRuntimeDependency } from './scoped-store-contract.js';
3
- import { skipWrappers } from './expression-wrappers.js';
4
- import { hasEval } from './forwarded-props.js';
5
- import { componentTagOrExport, localUses } from './local-uses.js';
6
- import { bind, nativeReactCompat, withoutJsxIndentation } from './transform.js';
4
+ import { literalCondition, skipWrappers } from './expression-wrappers.js';
5
+ import { atInitializedModuleHandle, initializedBefore } from './effect-receivers.js';
6
+ import { callsRootMethod, contains, hasEval, hasPrototypeAccess, rendersElementChildren, rootHandleEscapes, } from './forwarded-props.js';
7
+ import { componentTagOrExport, inTypeNode, localUses, unchangedContextProviderUse, } from './local-uses.js';
8
+ import { staticReactProps } from './react-renderer.js';
9
+ import { bind, nativeReactCompat, skipParens, withoutJsxIndentation } from './transform.js';
7
10
  export function importsReactBridge(source) {
8
11
  return (((source.includes('ReactCompat') && source.includes('octane/react')) ||
9
12
  source.includes('\\')) &&
@@ -27,7 +30,7 @@ async function bindIsland(file, source) {
27
30
  }
28
31
  return bind(file.endsWith('.tsrx') ? file + '.tsx' : file, code);
29
32
  }
30
- function imported(checker, node) {
33
+ function imported(checker, node, defaultMember = false) {
31
34
  const ref = ts.isPropertyAccessExpression(node) ? node.expression : node;
32
35
  if (!ts.isIdentifier(ref))
33
36
  return;
@@ -35,7 +38,7 @@ function imported(checker, node) {
35
38
  if (!decl)
36
39
  return;
37
40
  const name = ts.isPropertyAccessExpression(node)
38
- ? ts.isNamespaceImport(decl) && node.name.text
41
+ ? (ts.isNamespaceImport(decl) || (defaultMember && ts.isImportClause(decl))) && node.name.text
39
42
  : ts.isImportSpecifier(decl) && !decl.isTypeOnly
40
43
  ? (decl.propertyName ?? decl.name).text
41
44
  : ts.isImportClause(decl) && 'default';
@@ -46,6 +49,199 @@ function imported(checker, node) {
46
49
  ts.isStringLiteral(imp.moduleSpecifier))
47
50
  return { name, module: imp.moduleSpecifier.text };
48
51
  }
52
+ function isReactWrapper(name) {
53
+ return name === 'Fragment' || name === 'Suspense' || name === 'StrictMode';
54
+ }
55
+ function closedReactReceiver(receiver, uses, proveWrapper = false) {
56
+ return uses(receiver).every((ref) => {
57
+ if (ts.isImportClause(ref.parent) || ts.isNamespaceImport(ref.parent) || inTypeNode(ref))
58
+ return true;
59
+ const member = ref.parent;
60
+ if (!ts.isPropertyAccessExpression(member) || member.expression !== ref)
61
+ return false;
62
+ const parent = member.parent;
63
+ return ((ts.isExpressionWithTypeArguments(parent) &&
64
+ (!proveWrapper || ts.isHeritageClause(parent.parent))) ||
65
+ ((ts.isJsxOpeningElement(parent) ||
66
+ ts.isJsxClosingElement(parent) ||
67
+ ts.isJsxSelfClosingElement(parent)) &&
68
+ parent.tagName === member &&
69
+ isReactWrapper(member.name.text)) ||
70
+ (ts.isCallExpression(parent) &&
71
+ parent.expression === member &&
72
+ member.name.text !== 'default' &&
73
+ // React's mutable CommonJS object inherits methods that can replace a wrapper.
74
+ (!proveWrapper || !Object.hasOwn(Object.prototype, member.name.text))));
75
+ });
76
+ }
77
+ function soleReactReceiver(checker, receiver) {
78
+ const declaration = checker.getSymbolAtLocation(receiver)?.declarations?.[0];
79
+ return (declaration &&
80
+ (ts.isNamespaceImport(declaration)
81
+ ? !declaration.parent.name
82
+ : ts.isImportClause(declaration) && !declaration.namedBindings));
83
+ }
84
+ /** A direct assignment of inert metadata does not change the exported constructor. */
85
+ function classMetadataAssignment(ref, decl) {
86
+ const access = ref.parent;
87
+ if (!ts.isPropertyAccessExpression(access) || access.expression !== ref)
88
+ return false;
89
+ const name = access.name.text;
90
+ if (name !== 'displayName' && name !== 'defaultProps')
91
+ return false;
92
+ const assignment = access.parent;
93
+ if (!ts.isBinaryExpression(assignment) ||
94
+ assignment.left !== access ||
95
+ assignment.operatorToken.kind !== ts.SyntaxKind.EqualsToken ||
96
+ !ts.isExpressionStatement(assignment.parent) ||
97
+ assignment.parent.parent !== decl.parent ||
98
+ assignment.getStart() < decl.end ||
99
+ decl.members.some((member) => ts.isClassStaticBlockDeclaration(member) ||
100
+ (member.name && ts.isComputedPropertyName(member.name)) ||
101
+ (ts.canHaveDecorators(member) && ts.getDecorators(member)?.length) ||
102
+ (ts.getCombinedModifierFlags(member) & ts.ModifierFlags.Static &&
103
+ (ts.isPropertyDeclaration(member) ||
104
+ ts.isSetAccessorDeclaration(member) ||
105
+ ts.isGetAccessorDeclaration(member)))))
106
+ return false;
107
+ const value = skipWrappers(assignment.right);
108
+ if (name === 'displayName')
109
+ return ts.isStringLiteralLike(value);
110
+ return (ts.isObjectLiteralExpression(value) &&
111
+ value.properties.every((field) => {
112
+ if (!ts.isPropertyAssignment(field) ||
113
+ !(ts.isIdentifier(field.name) ||
114
+ ts.isStringLiteral(field.name) ||
115
+ ts.isNumericLiteral(field.name)) ||
116
+ field.name.text === '__proto__')
117
+ return false;
118
+ const value = skipWrappers(field.initializer);
119
+ return (ts.isStringLiteralLike(value) ||
120
+ ts.isNumericLiteral(value) ||
121
+ value.kind === ts.SyntaxKind.TrueKeyword ||
122
+ value.kind === ts.SyntaxKind.FalseKeyword ||
123
+ value.kind === ts.SyntaxKind.NullKeyword);
124
+ }));
125
+ }
126
+ function localValueExport(symbol, checker) {
127
+ const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
128
+ if (!declaration || !ts.isExportSpecifier(declaration))
129
+ return declaration;
130
+ const exported = declaration.parent.parent;
131
+ if (declaration.isTypeOnly || exported.isTypeOnly || exported.moduleSpecifier)
132
+ return;
133
+ return checker.getExportSpecifierLocalTargetSymbol(declaration)?.valueDeclaration;
134
+ }
135
+ /** Direct React class exports, including an alias in a local export list. */
136
+ export function reactClassExports(file, source) {
137
+ const { sf, checker } = bind(file, source);
138
+ const names = new Set();
139
+ const module = checker.getSymbolAtLocation(sf);
140
+ if (!module || hasEval(sf))
141
+ return names;
142
+ const uses = localUses(sf, checker);
143
+ let decorators;
144
+ const hasDecorator = (node) => ts.isDecorator(node) ||
145
+ (ts.forEachChild(node, (child) => hasDecorator(child) || undefined) ?? false);
146
+ const erasedClassUse = (ref) => inTypeNode(ref) &&
147
+ (ts.isTypeQueryNode(ref.parent) || !(decorators ??= source.includes('@') && hasDecorator(sf)));
148
+ for (const symbol of checker.getExportsOfModule(module)) {
149
+ let decl = localValueExport(symbol, checker);
150
+ if (decl && ts.isExportAssignment(decl)) {
151
+ const value = decl.expression;
152
+ if (decl.isExportEquals || !ts.isIdentifier(value))
153
+ continue;
154
+ const local = checker.getSymbolAtLocation(value)?.valueDeclaration;
155
+ if (!local || local.parent !== sf || local.end > decl.getStart(sf))
156
+ continue;
157
+ decl = local;
158
+ }
159
+ if (!decl ||
160
+ !ts.isClassDeclaration(decl) ||
161
+ ts.getCombinedModifierFlags(decl) & ts.ModifierFlags.Ambient ||
162
+ ts.getDecorators(decl)?.length)
163
+ continue;
164
+ const base = decl.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword)?.types[0]?.expression;
165
+ if (!base)
166
+ continue;
167
+ let entry = imported(checker, base);
168
+ if (!entry && ts.isPropertyAccessExpression(base) && ts.isIdentifier(base.expression)) {
169
+ const candidate = imported(checker, base, true);
170
+ if (candidate?.module === 'react' && closedReactReceiver(base.expression, uses))
171
+ entry = candidate;
172
+ }
173
+ if (entry?.module !== 'react' || (entry.name !== 'Component' && entry.name !== 'PureComponent'))
174
+ continue;
175
+ if (!decl.name ||
176
+ uses(decl.name).every((ref) => erasedClassUse(ref) || componentTagOrExport(ref) || classMetadataAssignment(ref, decl)))
177
+ names.add(symbol.name);
178
+ }
179
+ return names;
180
+ }
181
+ function hasSingleRuntimeImport(sf, source, module = 'react') {
182
+ const erased = new Set();
183
+ for (const statement of sf.statements) {
184
+ if ((ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) &&
185
+ statement.moduleSpecifier &&
186
+ !hasRuntimeDependency(statement, true))
187
+ erased.add(statement.moduleSpecifier.getStart(sf));
188
+ }
189
+ // Keep dynamic imports and require calls in the scan. Only declaration-level
190
+ // type imports and exports are erased even with verbatim module syntax.
191
+ return (ts
192
+ .preProcessFile(source, true, true)
193
+ .importedFiles.filter(({ fileName, pos }) => fileName === module && !erased.has(pos))
194
+ .length === 1);
195
+ }
196
+ /** Direct module-level const exports initialized by the real React context factory. */
197
+ export function retainedReactContextExports(file, source) {
198
+ const { sf, checker } = bind(file, source);
199
+ const module = checker.getSymbolAtLocation(sf);
200
+ const names = new Map();
201
+ if (!module)
202
+ return names;
203
+ let uses;
204
+ let singleReactImport;
205
+ let dynamicScope;
206
+ const readContext = (callee) => {
207
+ const hook = ts.isIdentifier(callee) && imported(checker, callee);
208
+ return !!hook && hook.module === 'react' && hook.name === 'useContext';
209
+ };
210
+ for (const symbol of checker.getExportsOfModule(module)) {
211
+ const declaration = localValueExport(symbol, checker);
212
+ if (!declaration || !ts.isVariableDeclaration(declaration) || !declaration.initializer)
213
+ continue;
214
+ const list = declaration.parent;
215
+ if ((list.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
216
+ !ts.isVariableStatement(list.parent) ||
217
+ list.parent.parent !== sf ||
218
+ ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Ambient)
219
+ continue;
220
+ const init = skipWrappers(declaration.initializer);
221
+ if (!ts.isCallExpression(init) || ts.isOptionalChain(init))
222
+ continue;
223
+ const factory = init.expression;
224
+ const entry = imported(checker, factory, true);
225
+ if (entry?.module !== 'react' || entry.name !== 'createContext')
226
+ continue;
227
+ if (ts.isPropertyAccessExpression(factory)) {
228
+ // With interop the default and namespace can share the same mutable React object.
229
+ singleReactImport ??= !(dynamicScope ??= hasEval(sf)) && hasSingleRuntimeImport(sf, source);
230
+ if (!singleReactImport ||
231
+ !ts.isIdentifier(factory.expression) ||
232
+ !soleReactReceiver(checker, factory.expression) ||
233
+ !closedReactReceiver(factory.expression, (uses ??= localUses(sf, checker))))
234
+ continue;
235
+ }
236
+ // The context itself stays a React value even if its Provider member is reassigned.
237
+ // For a retained member read, require the producer to keep that object local.
238
+ const unchangedProvider = ts.isIdentifier(declaration.name) &&
239
+ !(dynamicScope ??= hasEval(sf)) &&
240
+ (uses ??= localUses(sf, checker))(declaration.name).every((ref) => unchangedContextProviderUse(ref, readContext));
241
+ names.set(symbol.name, unchangedProvider);
242
+ }
243
+ return names;
244
+ }
49
245
  /** Original imported components handed to an authored React island. */
50
246
  export async function islandComponents(file, source, original) {
51
247
  const bound = await bindIsland(file, source);
@@ -127,7 +323,7 @@ export async function islandComponents(file, source, original) {
127
323
  visit(sf);
128
324
  return [...components];
129
325
  }
130
- function* directExportReturns({ sf, checker }) {
326
+ function* exportReturns({ sf, checker }, allowSetup = false) {
131
327
  const module = checker.getSymbolAtLocation(sf);
132
328
  if (!module || hasEval(sf))
133
329
  return;
@@ -136,6 +332,19 @@ function* directExportReturns({ sf, checker }) {
136
332
  let decl = symbol.valueDeclaration ?? symbol.declarations?.[0];
137
333
  if (!decl)
138
334
  continue;
335
+ let splitExport = false;
336
+ if (ts.isExportSpecifier(decl)) {
337
+ if (!allowSetup ||
338
+ decl.isTypeOnly ||
339
+ decl.parent.parent.isTypeOnly ||
340
+ decl.parent.parent.moduleSpecifier)
341
+ continue;
342
+ const local = checker.getExportSpecifierLocalTargetSymbol(decl)?.valueDeclaration;
343
+ if (!local || !ts.isFunctionDeclaration(local))
344
+ continue;
345
+ splitExport = true;
346
+ decl = local;
347
+ }
139
348
  let value = decl;
140
349
  let defaultRef;
141
350
  if (ts.isExportAssignment(decl)) {
@@ -162,28 +371,84 @@ function* directExportReturns({ sf, checker }) {
162
371
  continue;
163
372
  value = skipWrappers(value.initializer);
164
373
  }
165
- const output = returnedJsx(value);
166
- if (output)
167
- yield { name: symbol.name, output, localName: name?.text };
374
+ const returned = singleReturn(value, allowSetup);
375
+ if (returned)
376
+ yield {
377
+ name: symbol.name,
378
+ output: skipWrappers(returned),
379
+ localName: name?.text,
380
+ decl,
381
+ symbol,
382
+ splitExport,
383
+ };
384
+ }
385
+ }
386
+ function* directExportReturns(bound, allowSetup = false) {
387
+ for (const entry of exportReturns(bound, allowSetup)) {
388
+ const { output } = entry;
389
+ if (ts.isJsxElement(output) || ts.isJsxSelfClosingElement(output))
390
+ yield { ...entry, output };
168
391
  }
169
392
  }
170
- function returnedJsx(fn) {
393
+ function singleReturn(fn, allowSetup = false) {
171
394
  if (!(ts.isFunctionDeclaration(fn) || ts.isFunctionExpression(fn) || ts.isArrowFunction(fn)) ||
172
395
  fn.asteriskToken ||
173
396
  fn.modifiers?.some((item) => item.kind === ts.SyntaxKind.AsyncKeyword))
174
397
  return;
175
- let value;
176
398
  if (fn.body && ts.isBlock(fn.body)) {
177
- const [statement, extra] = fn.body.statements;
178
- if (!statement || extra || !ts.isReturnStatement(statement))
399
+ const statements = fn.body.statements;
400
+ const last = statements.at(-1);
401
+ if (!last || !ts.isReturnStatement(last) || !last.expression)
179
402
  return;
180
- value = statement.expression;
403
+ if (statements.length === 1)
404
+ return last.expression;
405
+ // The codemod moves this setup and its terminal JSX into one template body.
406
+ if (!allowSetup ||
407
+ (ts.getCombinedModifierFlags(fn) & ts.ModifierFlags.Ambient) !== 0 ||
408
+ (ts.isVariableDeclaration(fn.parent) &&
409
+ (ts.getCombinedModifierFlags(fn.parent) & ts.ModifierFlags.Ambient) !== 0) ||
410
+ (!ts.isFunctionDeclaration(fn) &&
411
+ !ts.isVariableDeclaration(fn.parent) &&
412
+ !ts.isExportAssignment(fn.parent)) ||
413
+ !statements
414
+ .slice(0, -1)
415
+ .every((statement) => (typeof allowSetup === 'function' && allowSetup(statement)) ||
416
+ (ts.isVariableStatement(statement) &&
417
+ !statement.modifiers?.some((item) => item.kind === ts.SyntaxKind.DeclareKeyword) &&
418
+ (statement.declarationList.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const &&
419
+ statement.declarationList.declarations.every((decl) => decl.initializer !== undefined))))
420
+ return;
421
+ if (typeof allowSetup === 'function')
422
+ return last.expression;
423
+ const output = skipParens(last.expression);
424
+ if (ts.isJsxElement(output) || ts.isJsxSelfClosingElement(output))
425
+ return output;
426
+ return;
181
427
  }
182
- else
183
- value = fn.body;
184
- const output = value && skipWrappers(value);
185
- if (output && (ts.isJsxElement(output) || ts.isJsxSelfClosingElement(output)))
186
- return output;
428
+ return fn.body;
429
+ }
430
+ function conditionalFallback(statement, checker) {
431
+ if (!ts.isIfStatement(statement) || statement.elseStatement)
432
+ return false;
433
+ const test = skipWrappers(statement.expression);
434
+ const negated = ts.isPrefixUnaryExpression(test) && test.operator === ts.SyntaxKind.ExclamationToken;
435
+ const identifier = negated ? skipWrappers(test.operand) : test;
436
+ if (!ts.isIdentifier(identifier))
437
+ return false;
438
+ const declaration = checker.getSymbolAtLocation(identifier)?.valueDeclaration;
439
+ if (declaration &&
440
+ ts.isVariableDeclaration(declaration) &&
441
+ ts.getCombinedNodeFlags(declaration) & ts.NodeFlags.Const &&
442
+ declaration.initializer) {
443
+ const value = literalCondition(declaration.initializer);
444
+ if (value !== undefined && negated !== (value === 'truthy'))
445
+ return false;
446
+ }
447
+ const branch = statement.thenStatement;
448
+ return (ts.isReturnStatement(branch) ||
449
+ (ts.isBlock(branch) &&
450
+ branch.statements.length === 1 &&
451
+ ts.isReturnStatement(branch.statements[0])));
187
452
  }
188
453
  function openingTag(element) {
189
454
  return ts.isJsxElement(element) ? element.openingElement : element;
@@ -197,7 +462,15 @@ function hostedLeaf(output) {
197
462
  return;
198
463
  const name = attr.name.text;
199
464
  if (seen.has(name) ||
200
- !['component', 'props', 'key', 'hostClassName', 'hostStyle'].includes(name))
465
+ ![
466
+ 'component',
467
+ 'props',
468
+ 'key',
469
+ 'hostClassName',
470
+ 'hostStyle',
471
+ 'hostRole',
472
+ 'hostAriaLabel',
473
+ ].includes(name))
201
474
  return;
202
475
  seen.add(name);
203
476
  if (name === 'component') {
@@ -223,29 +496,119 @@ function ordinaryReactJsx(source) {
223
496
  [...source.matchAll(/@jsxRuntime\s+(\S+)/gi)].some(([, mode]) => mode !== 'automatic') ||
224
497
  [...source.matchAll(/@jsxImportSource\s+(\S+)/gi)].some(([, name]) => name !== 'react'));
225
498
  }
226
- /** Runtime imports that an explicit server host returns directly. */
227
- export function hostedServerComponents(file, source) {
499
+ /** Runtime imports that an explicit React host returns directly. */
500
+ export function hostedComponents(file, source, bridge, selected = false) {
228
501
  if (!ordinaryReactJsx(source))
229
502
  return [];
230
503
  const bound = bind(file, source);
231
504
  const { sf, checker } = bound;
232
505
  const edges = [];
233
- for (const { output } of directExportReturns(bound)) {
234
- const tag = openingTag(output).tagName;
235
- if (ts.isIdentifier(tag) && (/^[a-z]/.test(tag.text) || tag.text.includes('-')))
236
- continue;
506
+ function visit(output) {
507
+ if (selected && ts.isJsxFragment(output)) {
508
+ output.children.forEach(visit);
509
+ return;
510
+ }
511
+ if (!ts.isJsxElement(output) && !ts.isJsxSelfClosingElement(output))
512
+ return;
513
+ const opening = openingTag(output);
514
+ const tag = opening.tagName;
515
+ if (ts.isIdentifier(tag) && (/^[a-z]/.test(tag.text) || tag.text.includes('-'))) {
516
+ // Intrinsic children reach the returned output; custom slots can capture JSX.
517
+ if (selected && ts.isJsxElement(output) && rendersElementChildren(opening))
518
+ output.children.forEach(visit);
519
+ return;
520
+ }
237
521
  const host = imported(checker, tag);
238
- if (host?.module !== 'octane/react/server' ||
522
+ if (host?.module !== bridge ||
239
523
  (host.name !== 'OctaneCompat' && host.name !== 'OctaneCompatInline'))
240
- continue;
524
+ return;
241
525
  const ref = hostedLeaf(output);
242
526
  const leaf = ref && imported(checker, ref);
243
527
  if (leaf)
244
528
  edges.push({ ...leaf, line: sf.getLineAndCharacterOfPosition(output.getStart(sf)).line + 1 });
245
529
  }
530
+ const allowSetup = selected && ((statement) => conditionalFallback(statement, checker));
531
+ for (const { output } of exportReturns(bound, allowSetup))
532
+ visit(output);
246
533
  return edges;
247
534
  }
248
- /** A retained component that returns JSX for a directly imported component. */
535
+ function literalImport(expr) {
536
+ const call = skipWrappers(expr);
537
+ if (!ts.isCallExpression(call) || call.expression.kind !== ts.SyntaxKind.ImportKeyword)
538
+ return;
539
+ const [specifier, extra] = call.arguments;
540
+ if (specifier &&
541
+ !extra &&
542
+ ts.isStringLiteralLike(specifier) &&
543
+ /^\.{1,2}\//.test(specifier.text) &&
544
+ !/[!%?#\\]/.test(specifier.text))
545
+ return specifier.text;
546
+ }
547
+ function lazyComponentImport(checker, tag) {
548
+ if (!ts.isIdentifier(tag))
549
+ return;
550
+ const symbol = checker.getSymbolAtLocation(tag);
551
+ const decl = symbol?.valueDeclaration;
552
+ if (symbol?.declarations?.length !== 1 ||
553
+ !decl ||
554
+ !ts.isVariableDeclaration(decl) ||
555
+ ts.getCombinedModifierFlags(decl) & ts.ModifierFlags.Ambient ||
556
+ !decl.initializer ||
557
+ !ts.isVariableDeclarationList(decl.parent) ||
558
+ (decl.parent.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
559
+ !ts.isVariableStatement(decl.parent.parent) ||
560
+ !ts.isSourceFile(decl.parent.parent.parent))
561
+ return;
562
+ const call = skipWrappers(decl.initializer);
563
+ if (!ts.isCallExpression(call) || ts.isOptionalChain(call) || call.arguments.length !== 1)
564
+ return;
565
+ const callee = skipWrappers(call.expression);
566
+ if (ts.isOptionalChain(callee))
567
+ return;
568
+ const factory = imported(checker, callee);
569
+ if (factory?.module !== 'react' || factory.name !== 'lazy')
570
+ return;
571
+ const loader = skipWrappers(call.arguments[0]);
572
+ if (!ts.isArrowFunction(loader) || loader.parameters.length)
573
+ return;
574
+ const result = singleReturn(loader);
575
+ if (!result)
576
+ return;
577
+ const direct = literalImport(result);
578
+ if (direct)
579
+ return { module: direct, name: 'default', lazy: true };
580
+ const map = skipWrappers(result);
581
+ if (!ts.isCallExpression(map) ||
582
+ ts.isOptionalChain(map) ||
583
+ map.arguments.length !== 1 ||
584
+ !ts.isPropertyAccessExpression(map.expression) ||
585
+ map.expression.name.text !== 'then')
586
+ return;
587
+ const module = literalImport(map.expression.expression);
588
+ if (!module)
589
+ return;
590
+ const mapper = skipWrappers(map.arguments[0]);
591
+ if (!ts.isArrowFunction(mapper) || mapper.parameters.length !== 1)
592
+ return;
593
+ const parameter = mapper.parameters[0];
594
+ if (!ts.isIdentifier(parameter.name) || parameter.initializer || parameter.dotDotDotToken)
595
+ return;
596
+ const mapped = singleReturn(mapper);
597
+ const object = mapped && skipWrappers(mapped);
598
+ if (!object || !ts.isObjectLiteralExpression(object) || object.properties.length !== 1)
599
+ return;
600
+ const field = object.properties[0];
601
+ if (!ts.isPropertyAssignment(field) ||
602
+ !ts.isIdentifier(field.name) ||
603
+ field.name.text !== 'default')
604
+ return;
605
+ const value = skipWrappers(field.initializer);
606
+ if (ts.isPropertyAccessExpression(value) &&
607
+ !ts.isOptionalChain(value) &&
608
+ checker.getSymbolAtLocation(value.expression) === checker.getSymbolAtLocation(parameter.name))
609
+ return { module, name: value.name.text, lazy: true };
610
+ }
611
+ /** Imports rendered directly by a retained component. */
249
612
  export function retainedReactComponentReturns(file, source) {
250
613
  if (!ordinaryReactJsx(source))
251
614
  return [];
@@ -253,45 +616,209 @@ export function retainedReactComponentReturns(file, source) {
253
616
  const { sf, checker } = bound;
254
617
  const edges = [];
255
618
  const seen = new Set();
256
- for (const { output, localName } of directExportReturns(bound)) {
619
+ const mayBeLazy = source.includes('import') && (source.includes('lazy') || source.includes('\\'));
620
+ for (const { output, localName, decl } of directExportReturns(bound)) {
257
621
  if (seen.has(output))
258
622
  continue;
259
623
  const tag = openingTag(output).tagName;
260
624
  if (localName && !/^[A-Z]/.test(localName))
261
625
  continue;
626
+ seen.add(output);
627
+ if (ts.isIdentifier(tag) && /^[a-z]/.test(tag.text)) {
628
+ if (!ts.isJsxElement(output) ||
629
+ (ts.getCombinedModifierFlags(decl) & ts.ModifierFlags.Ambient) !== 0 ||
630
+ !rendersElementChildren(output.openingElement) ||
631
+ output.openingElement.attributes.properties.some(ts.isJsxSpreadAttribute))
632
+ continue;
633
+ for (const child of output.children) {
634
+ if (!ts.isJsxExpression(child) || !child.expression || child.dotDotDotToken)
635
+ continue;
636
+ const call = skipWrappers(child.expression);
637
+ if (!ts.isCallExpression(call) || ts.isOptionalChain(call) || call.arguments.length)
638
+ continue;
639
+ const callee = skipWrappers(call.expression);
640
+ const leaf = ts.isIdentifier(callee) && imported(checker, callee);
641
+ if (leaf && /^\.{1,2}\/[^!%?#\\]*\.tsx$/.test(leaf.module))
642
+ edges.push({
643
+ ...leaf,
644
+ factory: true,
645
+ line: sf.getLineAndCharacterOfPosition(call.getStart(sf)).line + 1,
646
+ });
647
+ }
648
+ continue;
649
+ }
262
650
  if (ts.isIdentifier(tag) && !/^[A-Z]/.test(tag.text))
263
651
  continue;
264
- const leaf = imported(checker, tag);
265
- if (leaf?.module.startsWith('.') && /\.[jt]sx$/.test(leaf.module)) {
266
- seen.add(output);
652
+ const leaf = imported(checker, tag) ?? (mayBeLazy ? lazyComponentImport(checker, tag) : undefined);
653
+ if (leaf?.module.startsWith('.'))
267
654
  edges.push({ ...leaf, line: sf.getLineAndCharacterOfPosition(output.getStart(sf)).line + 1 });
268
- }
269
655
  }
270
656
  return edges;
271
657
  }
272
- /** Existing Octane roots that render a directly imported component body. */
273
- export function octaneRootComponents(file, source) {
274
- const { sf, checker } = bind(file, source);
658
+ /** Named React factories returned by an otherwise retained function. */
659
+ export function retainedReactElementReturns(file, source) {
660
+ const bound = bind(file, source);
661
+ const { sf, checker } = bound;
275
662
  const edges = [];
276
- function clientComponent(call, callee, hydrate) {
277
- if (hydrate && call.arguments.length <= 4)
278
- return call.arguments[1];
663
+ const seen = new Set();
664
+ for (const { output } of exportReturns(bound)) {
665
+ if (seen.has(output) ||
666
+ !ts.isCallExpression(output) ||
667
+ ts.isOptionalChain(output) ||
668
+ !ts.isIdentifier(output.expression) ||
669
+ !output.arguments.length ||
670
+ output.arguments.some(ts.isSpreadElement))
671
+ continue;
672
+ const factory = imported(checker, output.expression);
673
+ if (factory?.module !== 'react' || factory.name !== 'createElement')
674
+ continue;
675
+ const component = skipWrappers(output.arguments[0]);
676
+ if (!ts.isIdentifier(component))
677
+ continue;
678
+ const leaf = imported(checker, component);
679
+ if (leaf?.module.startsWith('.')) {
680
+ seen.add(output);
681
+ edges.push({
682
+ ...leaf,
683
+ line: sf.getLineAndCharacterOfPosition(output.getStart(sf)).line + 1,
684
+ });
685
+ }
686
+ }
687
+ return edges;
688
+ }
689
+ function privateRootField(access) {
690
+ if (ts.isPropertyAccessExpression(access) &&
691
+ ts.isPrivateIdentifier(access.name) &&
692
+ access.expression.kind === ts.SyntaxKind.ThisKeyword)
693
+ return access.name;
694
+ }
695
+ /** Find the createRoot initializer of a closed, branded instance field. */
696
+ function privateRootInitializer(name, checker) {
697
+ const symbol = checker.getSymbolAtLocation(name);
698
+ const field = symbol?.valueDeclaration;
699
+ if (!field || !ts.isPropertyDeclaration(field) || field.initializer)
700
+ return;
701
+ const owner = field.parent;
702
+ if (!ts.isClassDeclaration(owner) ||
703
+ owner.heritageClauses?.length ||
704
+ field.modifiers?.some((modifier) => modifier.kind !== ts.SyntaxKind.ReadonlyKeyword))
705
+ return;
706
+ const constructor = owner.members.find(ts.isConstructorDeclaration);
707
+ const first = constructor?.body?.statements[0];
708
+ const assignment = first && ts.isExpressionStatement(first) && first.expression;
709
+ if (!assignment ||
710
+ !ts.isBinaryExpression(assignment) ||
711
+ assignment.operatorToken.kind !== ts.SyntaxKind.EqualsToken)
712
+ return;
713
+ const initialized = privateRootField(assignment.left);
714
+ if (!initialized || checker.getSymbolAtLocation(initialized) !== symbol)
715
+ return;
716
+ if (contains(owner, (node) => {
717
+ if (ts.isDecorator(node))
718
+ return true;
719
+ if (!ts.isPrivateIdentifier(node) ||
720
+ node.text !== name.text ||
721
+ node === field.name ||
722
+ node === initialized)
723
+ return false;
724
+ // An any-typed receiver can still access the current class brand at runtime.
725
+ const accessed = checker.getSymbolAtLocation(node);
726
+ if (accessed && accessed !== symbol)
727
+ return false;
728
+ const use = node.parent;
729
+ const method = ts.findAncestor(use, ts.isFunctionLike);
730
+ return (!ts.isPropertyAccessExpression(use) ||
731
+ privateRootField(use) !== node ||
732
+ !method ||
733
+ !ts.isMethodDeclaration(method) ||
734
+ method.parent !== owner ||
735
+ !method.body ||
736
+ use.pos < method.body.pos ||
737
+ !!(ts.getCombinedModifierFlags(method) & ts.ModifierFlags.Static) ||
738
+ !callsRootMethod(use));
739
+ }))
740
+ return;
741
+ return skipWrappers(assignment.right);
742
+ }
743
+ function closedNamedImport(checker, reference) {
744
+ const declaration = ts.isIdentifier(reference) && checker.getSymbolAtLocation(reference)?.declarations?.[0];
745
+ return (declaration &&
746
+ ts.isImportSpecifier(declaration) &&
747
+ !declaration.parent.parent.name &&
748
+ !declaration.parent.elements.some((item) => !item.isTypeOnly && (item.propertyName ?? item.name).text === 'default'));
749
+ }
750
+ /** Direct createRoot handles and hydrateRoot calls with verified import provenance. */
751
+ function clientRootArgument({ sf, checker }, source, runtime) {
752
+ let uses;
753
+ let closedClientImport;
754
+ return (call, callee) => {
755
+ const entry = imported(checker, callee);
756
+ if (entry?.module === runtime && entry.name === 'hydrateRoot') {
757
+ const maxArgs = runtime === 'octane' ? 4 : 3;
758
+ if (call.arguments.length <= maxArgs)
759
+ return call.arguments[1];
760
+ }
279
761
  if (!ts.isPropertyAccessExpression(callee) ||
280
762
  ts.isOptionalChain(callee) ||
281
763
  callee.name.text !== 'render' ||
282
- call.arguments.length > 2)
764
+ call.arguments.length > (runtime === 'octane' ? 2 : 1))
283
765
  return;
284
- const root = skipWrappers(callee.expression);
285
- if (ts.isCallExpression(root) &&
286
- !ts.isOptionalChain(root) &&
287
- root.arguments.length >= 1 &&
288
- root.arguments.length <= 2 &&
289
- !root.arguments.some(ts.isSpreadElement)) {
290
- const factory = imported(checker, skipWrappers(root.expression));
291
- if (factory?.module === 'octane' && factory.name === 'createRoot')
292
- return call.arguments[0];
766
+ const receiver = skipWrappers(callee.expression);
767
+ const privateField = runtime === 'react-dom/client' && privateRootField(receiver);
768
+ let root = receiver;
769
+ let handle;
770
+ if (ts.isIdentifier(receiver)) {
771
+ const declaration = checker.getSymbolAtLocation(receiver)?.valueDeclaration;
772
+ if (!declaration ||
773
+ !ts.isVariableDeclaration(declaration) ||
774
+ !ts.isIdentifier(declaration.name) ||
775
+ !declaration.initializer ||
776
+ !(declaration.parent.flags & ts.NodeFlags.Const) ||
777
+ ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export ||
778
+ (!atInitializedModuleHandle(declaration, call) &&
779
+ !initializedBefore(checker, receiver, call)))
780
+ return;
781
+ handle = declaration.name;
782
+ root = skipWrappers(declaration.initializer);
293
783
  }
294
- }
784
+ else if (privateField) {
785
+ const initializer = privateRootInitializer(privateField, checker);
786
+ // A CommonJS export or root prototype can be changed through another reference.
787
+ if (!initializer ||
788
+ !ts.isCallExpression(initializer) ||
789
+ !closedNamedImport(checker, initializer.expression) ||
790
+ !(closedClientImport ??=
791
+ !hasEval(sf) && !hasPrototypeAccess(sf) && hasSingleRuntimeImport(sf, source, runtime)))
792
+ return;
793
+ root = initializer;
794
+ }
795
+ if (!ts.isCallExpression(root) ||
796
+ ts.isOptionalChain(root) ||
797
+ root.arguments.length < 1 ||
798
+ root.arguments.length > 2 ||
799
+ root.arguments.some(ts.isSpreadElement))
800
+ return;
801
+ const factory = imported(checker, skipWrappers(root.expression));
802
+ if (factory?.module !== runtime || factory.name !== 'createRoot')
803
+ return;
804
+ if (handle) {
805
+ if ((source.includes('eval') || source.includes('\\u')) && hasEval(sf))
806
+ return;
807
+ const rootScope = (node) => ts.isFunctionLike(node) || ts.isClassLike(node);
808
+ const owner = ts.findAncestor(handle, rootScope);
809
+ const refs = (uses ??= localUses(sf, checker))(handle);
810
+ if (rootHandleEscapes(refs, (ref) => ts.findAncestor(ref, rootScope) === owner))
811
+ return;
812
+ }
813
+ return call.arguments[0];
814
+ };
815
+ }
816
+ /** Existing Octane roots that render a directly imported component body. */
817
+ export function octaneRootComponents(file, source) {
818
+ const bound = bind(file, source);
819
+ const { sf, checker } = bound;
820
+ const edges = [];
821
+ const clientArgument = clientRootArgument(bound, source, 'octane');
295
822
  function visit(node) {
296
823
  if (ts.isCallExpression(node) &&
297
824
  !ts.isOptionalChain(node) &&
@@ -303,7 +830,7 @@ export function octaneRootComponents(file, source) {
303
830
  ? node.arguments.length <= 3 &&
304
831
  (entry.name === 'renderToString' || entry.name === 'renderToStaticMarkup') &&
305
832
  node.arguments[0]
306
- : clientComponent(node, callee, entry?.module === 'octane' && entry.name === 'hydrateRoot');
833
+ : clientArgument(node, callee);
307
834
  const leaf = value ? imported(checker, skipWrappers(value)) : undefined;
308
835
  if (leaf?.module.startsWith('.') && /\.[jt]sx$/.test(leaf.module))
309
836
  edges.push({
@@ -317,8 +844,94 @@ export function octaneRootComponents(file, source) {
317
844
  visit(sf);
318
845
  return edges;
319
846
  }
847
+ /** Retained React DOM client roots that render a directly imported React element. */
848
+ export function reactClientComponents(file, source, original) {
849
+ const bound = bind(file, source);
850
+ const { sf, checker } = bound;
851
+ const clientArgument = clientRootArgument(bound, source, 'react-dom/client');
852
+ const edges = [];
853
+ let reactJsx;
854
+ let reactModule;
855
+ let singleReactImport;
856
+ let uses;
857
+ const isReact = () => (reactModule ??= original('react'));
858
+ function builtin(tag) {
859
+ const entry = imported(checker, tag, true);
860
+ if (entry?.module !== 'react' ||
861
+ !isReactWrapper(entry.name) ||
862
+ !isReact() ||
863
+ !(singleReactImport ??= !hasEval(sf) && hasSingleRuntimeImport(sf, source)))
864
+ return false;
865
+ if (ts.isPropertyAccessExpression(tag))
866
+ return (ts.isIdentifier(tag.expression) &&
867
+ soleReactReceiver(checker, tag.expression) &&
868
+ closedReactReceiver(tag.expression, (uses ??= localUses(sf, checker)), true));
869
+ // A CJS React member can be mutated through a default import from the same clause.
870
+ return closedNamedImport(checker, tag);
871
+ }
872
+ function rendered(expression, line) {
873
+ const value = ts.isExpression(expression) ? skipWrappers(expression) : expression;
874
+ let ref;
875
+ if (ts.isJsxExpression(value) && value.expression && !value.dotDotDotToken) {
876
+ rendered(value.expression, line);
877
+ return;
878
+ }
879
+ if (ts.isJsxFragment(value) || ts.isJsxElement(value) || ts.isJsxSelfClosingElement(value)) {
880
+ if (!(reactJsx ??= ordinaryReactJsx(source) && original('react/jsx-runtime')))
881
+ return;
882
+ if (ts.isJsxFragment(value) ||
883
+ (ts.isJsxElement(value) && builtin(value.openingElement.tagName))) {
884
+ for (const child of value.children)
885
+ rendered(child, line);
886
+ return;
887
+ }
888
+ const { tagName } = openingTag(value);
889
+ if (!ts.isIdentifier(tagName) || /^[A-Z]/.test(tagName.text))
890
+ ref = tagName;
891
+ }
892
+ else if (ts.isCallExpression(value) && !ts.isOptionalChain(value)) {
893
+ const [type] = value.arguments;
894
+ if (!type || ts.isSpreadElement(type))
895
+ return;
896
+ const factory = imported(checker, skipWrappers(value.expression));
897
+ if (factory?.module !== 'react' || factory.name !== 'createElement' || !isReact())
898
+ return;
899
+ ref = skipWrappers(type);
900
+ if (builtin(ref)) {
901
+ if (!value.arguments.some(ts.isSpreadElement))
902
+ for (const child of value.arguments.slice(2))
903
+ rendered(child, line);
904
+ return;
905
+ }
906
+ }
907
+ const leaf = ref && imported(checker, ref);
908
+ if (leaf?.module.startsWith('.'))
909
+ edges.push({ ...leaf, line });
910
+ }
911
+ const closedPrivateRootElement = (value) => ts.isCallExpression(value) &&
912
+ closedNamedImport(checker, skipWrappers(value.expression)) &&
913
+ !!value.arguments[0] &&
914
+ imported(checker, skipWrappers(value.arguments[0]))?.module.startsWith('.') &&
915
+ (singleReactImport ??= !hasEval(sf) && hasSingleRuntimeImport(sf, source));
916
+ function visit(node) {
917
+ if (ts.isCallExpression(node) &&
918
+ !ts.isOptionalChain(node) &&
919
+ !node.arguments.some(ts.isSpreadElement)) {
920
+ const callee = skipWrappers(node.expression);
921
+ const argument = clientArgument(node, callee);
922
+ if (argument &&
923
+ (!ts.isPropertyAccessExpression(callee) ||
924
+ !privateRootField(skipWrappers(callee.expression)) ||
925
+ closedPrivateRootElement(skipWrappers(argument))))
926
+ rendered(argument, sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1);
927
+ }
928
+ ts.forEachChild(node, visit);
929
+ }
930
+ visit(sf);
931
+ return edges;
932
+ }
320
933
  /** React server calls that render directly imported components. */
321
- export function reactServerComponents(file, source, original, topLevel = false) {
934
+ export function reactServerComponents(file, source, original, selected = false) {
322
935
  const { sf, checker } = bind(file, source);
323
936
  const edges = [];
324
937
  function visit(node) {
@@ -335,7 +948,13 @@ export function reactServerComponents(file, source, original, topLevel = false)
335
948
  : undefined;
336
949
  if (jsx &&
337
950
  (!ts.isIdentifier(jsx.tagName) || /^[A-Z][\w$]*$/.test(jsx.tagName.text)) &&
338
- jsx.attributes.properties.length === 0 &&
951
+ (jsx.attributes.properties.length === 0 ||
952
+ (selected &&
953
+ ts.isJsxSelfClosingElement(jsx) &&
954
+ staticReactProps(jsx) !== null &&
955
+ !jsx.attributes.properties.some((attr) => ts.isJsxAttribute(attr) &&
956
+ ts.isIdentifier(attr.name) &&
957
+ attr.name.text === 'children'))) &&
339
958
  ordinaryReactJsx(source) &&
340
959
  original('react/jsx-runtime'))
341
960
  ref = jsx.tagName;
@@ -343,52 +962,165 @@ export function reactServerComponents(file, source, original, topLevel = false)
343
962
  !ts.isOptionalChain(value) &&
344
963
  (value.arguments.length === 1 ||
345
964
  (value.arguments.length === 2 &&
346
- skipWrappers(value.arguments[1]).kind === ts.SyntaxKind.NullKeyword))) {
965
+ (selected || skipWrappers(value.arguments[1]).kind === ts.SyntaxKind.NullKeyword)))) {
347
966
  const factory = imported(checker, value.expression);
348
967
  if (factory?.module === 'react' && factory.name === 'createElement' && original('react'))
349
968
  ref = skipWrappers(value.arguments[0]);
350
969
  }
351
970
  const leaf = ref && imported(checker, ref);
352
- if (leaf?.module.startsWith('.') && /\.[jt]sx$/.test(leaf.module))
971
+ if (leaf?.module.startsWith('.') && (!selected || /\.[jt]sx$/.test(leaf.module)))
353
972
  edges.push({
354
973
  ...leaf,
355
974
  line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1,
356
975
  });
357
976
  }
358
977
  }
359
- ts.forEachChild(node, visit);
978
+ if (!selected)
979
+ ts.forEachChild(node, visit);
360
980
  }
361
- if (topLevel) {
981
+ if (selected) {
982
+ function exportedRoot(expression) {
983
+ const value = expression && skipParens(expression);
984
+ return value && (ts.isArrowFunction(value) || ts.isFunctionExpression(value))
985
+ ? singleReturn(value)
986
+ : expression;
987
+ }
988
+ function visitDirectRoot(expression) {
989
+ const call = expression && skipParens(expression);
990
+ if (!call || !ts.isCallExpression(call))
991
+ return;
992
+ const argument = call.arguments[0] && skipParens(call.arguments[0]);
993
+ if (!argument)
994
+ return;
995
+ if (ts.isJsxSelfClosingElement(argument) && ts.isIdentifier(argument.tagName)) {
996
+ visit(call);
997
+ return;
998
+ }
999
+ const target = ts.isJsxSelfClosingElement(argument)
1000
+ ? argument.tagName
1001
+ : ts.isCallExpression(argument) && argument.arguments[0];
1002
+ if (target &&
1003
+ (ts.isIdentifier(target) || ts.isPropertyAccessExpression(target)) &&
1004
+ /^[A-Z]/.test(target.getText(sf).split('.').pop() ?? ''))
1005
+ visit(call);
1006
+ }
1007
+ function visitEntryStatement(statement) {
1008
+ if (ts.isVariableStatement(statement)) {
1009
+ for (const declaration of statement.declarationList.declarations)
1010
+ visitDirectRoot(declaration.initializer);
1011
+ }
1012
+ else if (ts.isExpressionStatement(statement) ||
1013
+ (ts.isExportAssignment(statement) && !statement.isExportEquals)) {
1014
+ visitDirectRoot(statement.expression);
1015
+ }
1016
+ }
362
1017
  for (const statement of sf.statements) {
363
- if (!ts.isVariableStatement(statement))
364
- continue;
365
- for (const declaration of statement.declarationList.declarations) {
366
- const call = declaration.initializer;
367
- if (!call || !ts.isCallExpression(call))
368
- continue;
369
- const [argument] = call.arguments;
370
- if (argument && ts.isJsxSelfClosingElement(argument) && ts.isIdentifier(argument.tagName))
371
- visit(call);
1018
+ if (ts.isVariableStatement(statement) &&
1019
+ (statement.declarationList.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const &&
1020
+ statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
1021
+ for (const { name, initializer } of statement.declarationList.declarations)
1022
+ visitDirectRoot(ts.isIdentifier(name) ? exportedRoot(initializer) : initializer);
1023
+ }
1024
+ else if (ts.isExportAssignment(statement) && !statement.isExportEquals) {
1025
+ visitDirectRoot(exportedRoot(statement.expression));
1026
+ }
1027
+ else if (ts.isIfStatement(statement)) {
1028
+ const condition = skipParens(statement.expression);
1029
+ if (ts.isPropertyAccessExpression(condition) &&
1030
+ !ts.isOptionalChain(condition) &&
1031
+ condition.name.text === 'main' &&
1032
+ ts.isMetaProperty(condition.expression) &&
1033
+ condition.expression.keywordToken === ts.SyntaxKind.ImportKeyword &&
1034
+ condition.expression.name.text === 'meta') {
1035
+ const body = statement.thenStatement;
1036
+ if (ts.isBlock(body))
1037
+ body.statements.forEach(visitEntryStatement);
1038
+ else
1039
+ visitEntryStatement(body);
1040
+ }
1041
+ }
1042
+ else if (ts.isFunctionDeclaration(statement) &&
1043
+ statement.body &&
1044
+ !statement.asteriskToken &&
1045
+ !statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword)) {
1046
+ for (const bodyStatement of statement.body.statements)
1047
+ if (ts.isReturnStatement(bodyStatement))
1048
+ visitDirectRoot(bodyStatement.expression);
372
1049
  }
1050
+ else
1051
+ visitEntryStatement(statement);
373
1052
  }
374
1053
  }
375
1054
  else
376
1055
  visit(sf);
377
1056
  return edges;
378
1057
  }
379
- /** Local exports whose original body returns an intrinsic React element. */
380
- export function intrinsicReactExports(file, source) {
381
- const names = new Set();
1058
+ /** Local exports whose original body returns React JSX directly. */
1059
+ export function directReactExports(file, source) {
1060
+ const intrinsics = new Set();
1061
+ const wrappers = new Set();
1062
+ let splitIntrinsicDefault = false;
1063
+ let mayExportThen = false;
382
1064
  if (ordinaryReactJsx(source)) {
383
- for (const { name, output, localName } of directExportReturns(bind(file, source))) {
1065
+ const bound = bind(file, source);
1066
+ const { sf, checker } = bound;
1067
+ const module = checker.getSymbolAtLocation(sf);
1068
+ const then = module && checker.getExportsOfModule(module).find((symbol) => symbol.name === 'then');
1069
+ // import() assimilates a callable namespace then. This local checker cannot resolve wildcard re-exports.
1070
+ mayExportThen =
1071
+ !!then &&
1072
+ (!!(then.flags & ts.SymbolFlags.Value) ||
1073
+ !!then.declarations?.some((decl) => (ts.isExportSpecifier(decl) && !decl.isTypeOnly && !decl.parent.parent.isTypeOnly) ||
1074
+ (ts.isImportEqualsDeclaration(decl) && !decl.isTypeOnly)));
1075
+ mayExportThen ||= sf.statements.some((stmt) => ts.isExportDeclaration(stmt) && !stmt.isTypeOnly && !stmt.exportClause);
1076
+ for (const { name, output, localName, decl, splitExport } of directExportReturns(bound, true)) {
384
1077
  const tag = openingTag(output).tagName;
385
1078
  if (ts.isIdentifier(tag) &&
386
1079
  /^[a-z]/.test(tag.text) &&
387
- (!localName || /^[A-Z]/.test(localName)))
1080
+ (!localName || /^[A-Z]/.test(localName))) {
1081
+ if (!splitExport)
1082
+ intrinsics.add(name);
1083
+ else if (name === 'default')
1084
+ splitIntrinsicDefault = true;
1085
+ }
1086
+ else if (ts.isFunctionDeclaration(decl) &&
1087
+ (name === 'default'
1088
+ ? !localName || /^[A-Z]/.test(localName)
1089
+ : localName === name && /^[A-Z]/.test(name)) &&
1090
+ ts.isIdentifier(tag) &&
1091
+ /^[A-Z]/.test(tag.text))
1092
+ wrappers.add(name);
1093
+ }
1094
+ }
1095
+ return { intrinsics, wrappers, splitIntrinsicDefault, mayExportThen };
1096
+ }
1097
+ /** Factories whose intrinsic JSX return survives as Octane JSX in the emitted file. */
1098
+ export function migratedJsxFactories(file, source, emitted) {
1099
+ if (!ordinaryReactJsx(source))
1100
+ return new Set();
1101
+ function factories(code) {
1102
+ const names = new Set();
1103
+ for (const { name, output, decl, symbol } of directExportReturns(bind(file, code))) {
1104
+ const tag = openingTag(output).tagName;
1105
+ if (ts.isFunctionDeclaration(decl) &&
1106
+ symbol.declarations?.length === 1 &&
1107
+ decl.name?.text === name &&
1108
+ /^[a-z]/.test(name) &&
1109
+ decl.parameters.length === 0 &&
1110
+ (ts.getCombinedModifierFlags(decl) &
1111
+ (ts.ModifierFlags.Export | ts.ModifierFlags.Ambient)) ===
1112
+ ts.ModifierFlags.Export &&
1113
+ ts.isIdentifier(tag) &&
1114
+ /^[a-z]/.test(tag.text))
388
1115
  names.add(name);
389
1116
  }
1117
+ return names;
390
1118
  }
391
- return names;
1119
+ const original = factories(source);
1120
+ // TSX parsing proves the emitted return directly. Volar can hoist it out of the function.
1121
+ if (!original.size || parseSync(file, emitted).errors.length)
1122
+ return new Set();
1123
+ return original.intersection(factories(emitted));
392
1124
  }
393
1125
  /** Inspect the emitted local export before assigning a reverse island to a server host. */
394
1126
  export async function reverseIslandExports(file, source, original) {
@@ -417,6 +1149,12 @@ export function executableImports(file, source, verbatim) {
417
1149
  imports.push(node.moduleSpecifier.text);
418
1150
  return;
419
1151
  }
1152
+ if (ts.isImportEqualsDeclaration(node) && !node.isTypeOnly) {
1153
+ const reference = node.moduleReference;
1154
+ if (ts.isExternalModuleReference(reference) && ts.isStringLiteral(reference.expression)) {
1155
+ imports.push(reference.expression.text);
1156
+ }
1157
+ }
420
1158
  if (ts.isCallExpression(node) &&
421
1159
  node.expression.kind === ts.SyntaxKind.ImportKeyword &&
422
1160
  node.arguments[0] &&