@octanejs/codemod 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +140 -12
- package/dist/bin.js +2 -2
- package/dist/effect-receivers.d.ts +14 -2
- package/dist/effect-receivers.js +171 -22
- package/dist/expression-wrappers.d.ts +2 -0
- package/dist/expression-wrappers.js +19 -0
- package/dist/forwarded-props.d.ts +4 -1
- package/dist/forwarded-props.js +18 -3
- package/dist/linked-state.d.ts +1 -1
- package/dist/linked-state.js +7 -3
- package/dist/local-react-hook.d.ts +13 -6
- package/dist/local-react-hook.js +316 -53
- package/dist/local-uses.d.ts +10 -1
- package/dist/local-uses.js +87 -5
- package/dist/mappings.d.ts +7 -1
- package/dist/mappings.js +21 -3
- package/dist/path-svg.d.ts +68 -4
- package/dist/path-svg.js +162 -56
- package/dist/project.js +1589 -147
- package/dist/react-intl.d.ts +1 -1
- package/dist/react-intl.js +43 -6
- package/dist/react-island-sources.d.ts +53 -0
- package/dist/react-island-sources.js +979 -0
- package/dist/react-renderer.d.ts +2 -0
- package/dist/react-renderer.js +94 -0
- package/dist/scoped-store-contract.d.ts +9 -2
- package/dist/scoped-store-contract.js +209 -33
- package/dist/store-hook-uses.d.ts +7 -5
- package/dist/store-hook-uses.js +218 -62
- package/dist/store-inputs.d.ts +9 -2
- package/dist/store-inputs.js +213 -16
- package/dist/strong.d.ts +2 -2
- package/dist/strong.js +72 -18
- package/dist/transform.d.ts +14 -0
- package/dist/transform.js +2856 -292
- package/dist/types.d.ts +40 -10
- package/package.json +4 -3
- package/src/path-svg.ts +183 -62
- package/templates/octane-path-svg.ts +5 -1
- package/templates/react-intl.tsrx +64 -11
|
@@ -0,0 +1,979 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { parseSync } from '@tsrx/oxc/parser';
|
|
3
|
+
import { hasRuntimeDependency } from './scoped-store-contract.js';
|
|
4
|
+
import { literalCondition, skipWrappers } from './expression-wrappers.js';
|
|
5
|
+
import { atInitializedModuleHandle, initializedBefore } from './effect-receivers.js';
|
|
6
|
+
import { hasEval, 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';
|
|
10
|
+
export function importsReactBridge(source) {
|
|
11
|
+
return (((source.includes('ReactCompat') && source.includes('octane/react')) ||
|
|
12
|
+
source.includes('\\')) &&
|
|
13
|
+
ts
|
|
14
|
+
.preProcessFile(source)
|
|
15
|
+
.importedFiles.some(({ fileName }) => fileName === 'octane/react' || fileName === 'octane/react/server'));
|
|
16
|
+
}
|
|
17
|
+
async function bindIsland(file, source) {
|
|
18
|
+
let code = source;
|
|
19
|
+
if (file.endsWith('.tsrx')) {
|
|
20
|
+
const { compileToVolarMappings } = await import('octane/compiler/volar');
|
|
21
|
+
try {
|
|
22
|
+
code = compileToVolarMappings(source, file, { loose: true }).code;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
// Project-wide inspection also encounters unfinished files outside the selected inputs.
|
|
26
|
+
if (error instanceof SyntaxError)
|
|
27
|
+
return;
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return bind(file.endsWith('.tsrx') ? file + '.tsx' : file, code);
|
|
32
|
+
}
|
|
33
|
+
function imported(checker, node, defaultMember = false) {
|
|
34
|
+
const ref = ts.isPropertyAccessExpression(node) ? node.expression : node;
|
|
35
|
+
if (!ts.isIdentifier(ref))
|
|
36
|
+
return;
|
|
37
|
+
const decl = checker.getSymbolAtLocation(ref)?.declarations?.[0];
|
|
38
|
+
if (!decl)
|
|
39
|
+
return;
|
|
40
|
+
const name = ts.isPropertyAccessExpression(node)
|
|
41
|
+
? (ts.isNamespaceImport(decl) || (defaultMember && ts.isImportClause(decl))) && node.name.text
|
|
42
|
+
: ts.isImportSpecifier(decl) && !decl.isTypeOnly
|
|
43
|
+
? (decl.propertyName ?? decl.name).text
|
|
44
|
+
: ts.isImportClause(decl) && 'default';
|
|
45
|
+
const imp = name && ts.findAncestor(decl, ts.isImportDeclaration);
|
|
46
|
+
if (imp &&
|
|
47
|
+
!imp.attributes &&
|
|
48
|
+
!imp.importClause?.isTypeOnly &&
|
|
49
|
+
ts.isStringLiteral(imp.moduleSpecifier))
|
|
50
|
+
return { name, module: imp.moduleSpecifier.text };
|
|
51
|
+
}
|
|
52
|
+
function closedReactReceiver(receiver, uses) {
|
|
53
|
+
return uses(receiver).every((ref) => {
|
|
54
|
+
if (ts.isImportClause(ref.parent) || ts.isNamespaceImport(ref.parent) || inTypeNode(ref))
|
|
55
|
+
return true;
|
|
56
|
+
const member = ref.parent;
|
|
57
|
+
if (!ts.isPropertyAccessExpression(member) || member.expression !== ref)
|
|
58
|
+
return false;
|
|
59
|
+
const parent = member.parent;
|
|
60
|
+
return (ts.isExpressionWithTypeArguments(parent) ||
|
|
61
|
+
((ts.isJsxOpeningElement(parent) ||
|
|
62
|
+
ts.isJsxClosingElement(parent) ||
|
|
63
|
+
ts.isJsxSelfClosingElement(parent)) &&
|
|
64
|
+
parent.tagName === member &&
|
|
65
|
+
(member.name.text === 'Fragment' ||
|
|
66
|
+
member.name.text === 'Suspense' ||
|
|
67
|
+
member.name.text === 'StrictMode')) ||
|
|
68
|
+
(ts.isCallExpression(parent) &&
|
|
69
|
+
parent.expression === member &&
|
|
70
|
+
member.name.text !== 'default'));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function soleReactReceiver(checker, receiver) {
|
|
74
|
+
const declaration = checker.getSymbolAtLocation(receiver)?.declarations?.[0];
|
|
75
|
+
return (declaration &&
|
|
76
|
+
(ts.isNamespaceImport(declaration)
|
|
77
|
+
? !declaration.parent.name
|
|
78
|
+
: ts.isImportClause(declaration) && !declaration.namedBindings));
|
|
79
|
+
}
|
|
80
|
+
/** A direct assignment of inert metadata does not change the exported constructor. */
|
|
81
|
+
function classMetadataAssignment(ref, decl) {
|
|
82
|
+
const access = ref.parent;
|
|
83
|
+
if (!ts.isPropertyAccessExpression(access) || access.expression !== ref)
|
|
84
|
+
return false;
|
|
85
|
+
const name = access.name.text;
|
|
86
|
+
if (name !== 'displayName' && name !== 'defaultProps')
|
|
87
|
+
return false;
|
|
88
|
+
const assignment = access.parent;
|
|
89
|
+
if (!ts.isBinaryExpression(assignment) ||
|
|
90
|
+
assignment.left !== access ||
|
|
91
|
+
assignment.operatorToken.kind !== ts.SyntaxKind.EqualsToken ||
|
|
92
|
+
!ts.isExpressionStatement(assignment.parent) ||
|
|
93
|
+
assignment.parent.parent !== decl.parent ||
|
|
94
|
+
assignment.getStart() < decl.end ||
|
|
95
|
+
decl.members.some((member) => ts.isClassStaticBlockDeclaration(member) ||
|
|
96
|
+
(member.name && ts.isComputedPropertyName(member.name)) ||
|
|
97
|
+
(ts.canHaveDecorators(member) && ts.getDecorators(member)?.length) ||
|
|
98
|
+
(ts.getCombinedModifierFlags(member) & ts.ModifierFlags.Static &&
|
|
99
|
+
(ts.isPropertyDeclaration(member) ||
|
|
100
|
+
ts.isSetAccessorDeclaration(member) ||
|
|
101
|
+
ts.isGetAccessorDeclaration(member)))))
|
|
102
|
+
return false;
|
|
103
|
+
const value = skipWrappers(assignment.right);
|
|
104
|
+
if (name === 'displayName')
|
|
105
|
+
return ts.isStringLiteralLike(value);
|
|
106
|
+
return (ts.isObjectLiteralExpression(value) &&
|
|
107
|
+
value.properties.every((field) => {
|
|
108
|
+
if (!ts.isPropertyAssignment(field) ||
|
|
109
|
+
!(ts.isIdentifier(field.name) ||
|
|
110
|
+
ts.isStringLiteral(field.name) ||
|
|
111
|
+
ts.isNumericLiteral(field.name)) ||
|
|
112
|
+
field.name.text === '__proto__')
|
|
113
|
+
return false;
|
|
114
|
+
const value = skipWrappers(field.initializer);
|
|
115
|
+
return (ts.isStringLiteralLike(value) ||
|
|
116
|
+
ts.isNumericLiteral(value) ||
|
|
117
|
+
value.kind === ts.SyntaxKind.TrueKeyword ||
|
|
118
|
+
value.kind === ts.SyntaxKind.FalseKeyword ||
|
|
119
|
+
value.kind === ts.SyntaxKind.NullKeyword);
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
122
|
+
function localValueExport(symbol, checker) {
|
|
123
|
+
const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
124
|
+
if (!declaration || !ts.isExportSpecifier(declaration))
|
|
125
|
+
return declaration;
|
|
126
|
+
const exported = declaration.parent.parent;
|
|
127
|
+
if (declaration.isTypeOnly || exported.isTypeOnly || exported.moduleSpecifier)
|
|
128
|
+
return;
|
|
129
|
+
return checker.getExportSpecifierLocalTargetSymbol(declaration)?.valueDeclaration;
|
|
130
|
+
}
|
|
131
|
+
/** Direct React class exports, including an alias in a local export list. */
|
|
132
|
+
export function reactClassExports(file, source) {
|
|
133
|
+
const { sf, checker } = bind(file, source);
|
|
134
|
+
const names = new Set();
|
|
135
|
+
const module = checker.getSymbolAtLocation(sf);
|
|
136
|
+
if (!module || hasEval(sf))
|
|
137
|
+
return names;
|
|
138
|
+
const uses = localUses(sf, checker);
|
|
139
|
+
let decorators;
|
|
140
|
+
const hasDecorator = (node) => ts.isDecorator(node) ||
|
|
141
|
+
(ts.forEachChild(node, (child) => hasDecorator(child) || undefined) ?? false);
|
|
142
|
+
const erasedClassUse = (ref) => inTypeNode(ref) &&
|
|
143
|
+
(ts.isTypeQueryNode(ref.parent) || !(decorators ??= source.includes('@') && hasDecorator(sf)));
|
|
144
|
+
for (const symbol of checker.getExportsOfModule(module)) {
|
|
145
|
+
let decl = localValueExport(symbol, checker);
|
|
146
|
+
if (decl && ts.isExportAssignment(decl)) {
|
|
147
|
+
const value = decl.expression;
|
|
148
|
+
if (decl.isExportEquals || !ts.isIdentifier(value))
|
|
149
|
+
continue;
|
|
150
|
+
const local = checker.getSymbolAtLocation(value)?.valueDeclaration;
|
|
151
|
+
if (!local || local.parent !== sf || local.end > decl.getStart(sf))
|
|
152
|
+
continue;
|
|
153
|
+
decl = local;
|
|
154
|
+
}
|
|
155
|
+
if (!decl ||
|
|
156
|
+
!ts.isClassDeclaration(decl) ||
|
|
157
|
+
ts.getCombinedModifierFlags(decl) & ts.ModifierFlags.Ambient ||
|
|
158
|
+
ts.getDecorators(decl)?.length)
|
|
159
|
+
continue;
|
|
160
|
+
const base = decl.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword)?.types[0]?.expression;
|
|
161
|
+
if (!base)
|
|
162
|
+
continue;
|
|
163
|
+
let entry = imported(checker, base);
|
|
164
|
+
if (!entry && ts.isPropertyAccessExpression(base) && ts.isIdentifier(base.expression)) {
|
|
165
|
+
const candidate = imported(checker, base, true);
|
|
166
|
+
if (candidate?.module === 'react' && closedReactReceiver(base.expression, uses))
|
|
167
|
+
entry = candidate;
|
|
168
|
+
}
|
|
169
|
+
if (entry?.module !== 'react' || (entry.name !== 'Component' && entry.name !== 'PureComponent'))
|
|
170
|
+
continue;
|
|
171
|
+
if (!decl.name ||
|
|
172
|
+
uses(decl.name).every((ref) => erasedClassUse(ref) || componentTagOrExport(ref) || classMetadataAssignment(ref, decl)))
|
|
173
|
+
names.add(symbol.name);
|
|
174
|
+
}
|
|
175
|
+
return names;
|
|
176
|
+
}
|
|
177
|
+
function hasSingleReactImport(sf, source) {
|
|
178
|
+
const erased = new Set();
|
|
179
|
+
for (const statement of sf.statements) {
|
|
180
|
+
if ((ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) &&
|
|
181
|
+
statement.moduleSpecifier &&
|
|
182
|
+
!hasRuntimeDependency(statement, true))
|
|
183
|
+
erased.add(statement.moduleSpecifier.getStart(sf));
|
|
184
|
+
}
|
|
185
|
+
// Keep dynamic imports and require calls in the scan. Only declaration-level
|
|
186
|
+
// type imports and exports are erased even with verbatim module syntax.
|
|
187
|
+
return (ts
|
|
188
|
+
.preProcessFile(source, true, true)
|
|
189
|
+
.importedFiles.filter(({ fileName, pos }) => fileName === 'react' && !erased.has(pos))
|
|
190
|
+
.length === 1);
|
|
191
|
+
}
|
|
192
|
+
/** Direct module-level const exports initialized by the real React context factory. */
|
|
193
|
+
export function retainedReactContextExports(file, source) {
|
|
194
|
+
const { sf, checker } = bind(file, source);
|
|
195
|
+
const module = checker.getSymbolAtLocation(sf);
|
|
196
|
+
const names = new Map();
|
|
197
|
+
if (!module)
|
|
198
|
+
return names;
|
|
199
|
+
let uses;
|
|
200
|
+
let singleReactImport;
|
|
201
|
+
let dynamicScope;
|
|
202
|
+
const readContext = (callee) => {
|
|
203
|
+
const hook = ts.isIdentifier(callee) && imported(checker, callee);
|
|
204
|
+
return !!hook && hook.module === 'react' && hook.name === 'useContext';
|
|
205
|
+
};
|
|
206
|
+
for (const symbol of checker.getExportsOfModule(module)) {
|
|
207
|
+
const declaration = localValueExport(symbol, checker);
|
|
208
|
+
if (!declaration || !ts.isVariableDeclaration(declaration) || !declaration.initializer)
|
|
209
|
+
continue;
|
|
210
|
+
const list = declaration.parent;
|
|
211
|
+
if ((list.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
212
|
+
!ts.isVariableStatement(list.parent) ||
|
|
213
|
+
list.parent.parent !== sf ||
|
|
214
|
+
ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Ambient)
|
|
215
|
+
continue;
|
|
216
|
+
const init = skipWrappers(declaration.initializer);
|
|
217
|
+
if (!ts.isCallExpression(init) || ts.isOptionalChain(init))
|
|
218
|
+
continue;
|
|
219
|
+
const factory = init.expression;
|
|
220
|
+
const entry = imported(checker, factory, true);
|
|
221
|
+
if (entry?.module !== 'react' || entry.name !== 'createContext')
|
|
222
|
+
continue;
|
|
223
|
+
if (ts.isPropertyAccessExpression(factory)) {
|
|
224
|
+
// With interop the default and namespace can share the same mutable React object.
|
|
225
|
+
singleReactImport ??= !(dynamicScope ??= hasEval(sf)) && hasSingleReactImport(sf, source);
|
|
226
|
+
if (!singleReactImport ||
|
|
227
|
+
!ts.isIdentifier(factory.expression) ||
|
|
228
|
+
!soleReactReceiver(checker, factory.expression) ||
|
|
229
|
+
!closedReactReceiver(factory.expression, (uses ??= localUses(sf, checker))))
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
// The context itself stays a React value even if its Provider member is reassigned.
|
|
233
|
+
// For a retained member read, require the producer to keep that object local.
|
|
234
|
+
const unchangedProvider = ts.isIdentifier(declaration.name) &&
|
|
235
|
+
!(dynamicScope ??= hasEval(sf)) &&
|
|
236
|
+
(uses ??= localUses(sf, checker))(declaration.name).every((ref) => unchangedContextProviderUse(ref, readContext));
|
|
237
|
+
names.set(symbol.name, unchangedProvider);
|
|
238
|
+
}
|
|
239
|
+
return names;
|
|
240
|
+
}
|
|
241
|
+
/** Original imported components handed to an authored React island. */
|
|
242
|
+
export async function islandComponents(file, source, original) {
|
|
243
|
+
const bound = await bindIsland(file, source);
|
|
244
|
+
if (!bound)
|
|
245
|
+
return [];
|
|
246
|
+
const { sf, checker } = bound;
|
|
247
|
+
const components = new Set();
|
|
248
|
+
function component(expr) {
|
|
249
|
+
const node = skipWrappers(expr);
|
|
250
|
+
if (ts.isConditionalExpression(node)) {
|
|
251
|
+
component(node.whenTrue);
|
|
252
|
+
component(node.whenFalse);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if (ts.isBinaryExpression(node)) {
|
|
256
|
+
const op = node.operatorToken.kind;
|
|
257
|
+
if (op === ts.SyntaxKind.AmpersandAmpersandToken)
|
|
258
|
+
component(node.right);
|
|
259
|
+
else if (op === ts.SyntaxKind.BarBarToken || op === ts.SyntaxKind.QuestionQuestionToken) {
|
|
260
|
+
component(node.left);
|
|
261
|
+
component(node.right);
|
|
262
|
+
}
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const ref = ts.isJsxElement(node)
|
|
266
|
+
? node.openingElement.tagName
|
|
267
|
+
: ts.isJsxSelfClosingElement(node)
|
|
268
|
+
? node.tagName
|
|
269
|
+
: node;
|
|
270
|
+
if (ts.isIdentifier(ref) && ref !== node && /^[a-z]|-/.test(ref.text))
|
|
271
|
+
return;
|
|
272
|
+
let value = ref;
|
|
273
|
+
const seen = new Set();
|
|
274
|
+
for (;;) {
|
|
275
|
+
const entry = imported(checker, value);
|
|
276
|
+
if (entry) {
|
|
277
|
+
components.add(entry.module);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (!ts.isIdentifier(value))
|
|
281
|
+
return;
|
|
282
|
+
const symbol = checker.getSymbolAtLocation(value);
|
|
283
|
+
const alias = symbol?.declarations?.length === 1 && symbol.valueDeclaration;
|
|
284
|
+
if (!alias ||
|
|
285
|
+
!ts.isVariableDeclaration(alias) ||
|
|
286
|
+
!ts.isVariableDeclarationList(alias.parent) ||
|
|
287
|
+
(alias.parent.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
288
|
+
!ts.isIdentifier(alias.name) ||
|
|
289
|
+
!alias.initializer ||
|
|
290
|
+
seen.has(alias))
|
|
291
|
+
return;
|
|
292
|
+
seen.add(alias);
|
|
293
|
+
value = skipWrappers(alias.initializer);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function visit(n) {
|
|
297
|
+
if (ts.isJsxSelfClosingElement(n) || ts.isJsxOpeningElement(n)) {
|
|
298
|
+
const tag = imported(checker, n.tagName);
|
|
299
|
+
if (tag && nativeReactCompat(tag.module, tag.name) && original(tag.module)) {
|
|
300
|
+
const prop = n.attributes.properties.find((a) => ts.isJsxAttribute(a) && ts.isIdentifier(a.name) && a.name.text === 'component');
|
|
301
|
+
if (prop &&
|
|
302
|
+
ts.isJsxAttribute(prop) &&
|
|
303
|
+
prop.initializer &&
|
|
304
|
+
ts.isJsxExpression(prop.initializer) &&
|
|
305
|
+
prop.initializer.expression)
|
|
306
|
+
component(prop.initializer.expression);
|
|
307
|
+
if (ts.isJsxOpeningElement(n)) {
|
|
308
|
+
for (const child of n.parent.children) {
|
|
309
|
+
if (ts.isJsxElement(child) || ts.isJsxSelfClosingElement(child))
|
|
310
|
+
component(child);
|
|
311
|
+
else if (ts.isJsxExpression(child) && child.expression)
|
|
312
|
+
component(child.expression);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
ts.forEachChild(n, visit);
|
|
318
|
+
}
|
|
319
|
+
visit(sf);
|
|
320
|
+
return [...components];
|
|
321
|
+
}
|
|
322
|
+
function* exportReturns({ sf, checker }, allowSetup = false) {
|
|
323
|
+
const module = checker.getSymbolAtLocation(sf);
|
|
324
|
+
if (!module || hasEval(sf))
|
|
325
|
+
return;
|
|
326
|
+
const uses = localUses(sf, checker);
|
|
327
|
+
for (const symbol of checker.getExportsOfModule(module)) {
|
|
328
|
+
let decl = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
329
|
+
if (!decl)
|
|
330
|
+
continue;
|
|
331
|
+
let splitExport = false;
|
|
332
|
+
if (ts.isExportSpecifier(decl)) {
|
|
333
|
+
if (!allowSetup ||
|
|
334
|
+
decl.isTypeOnly ||
|
|
335
|
+
decl.parent.parent.isTypeOnly ||
|
|
336
|
+
decl.parent.parent.moduleSpecifier)
|
|
337
|
+
continue;
|
|
338
|
+
const local = checker.getExportSpecifierLocalTargetSymbol(decl)?.valueDeclaration;
|
|
339
|
+
if (!local || !ts.isFunctionDeclaration(local))
|
|
340
|
+
continue;
|
|
341
|
+
splitExport = true;
|
|
342
|
+
decl = local;
|
|
343
|
+
}
|
|
344
|
+
let value = decl;
|
|
345
|
+
let defaultRef;
|
|
346
|
+
if (ts.isExportAssignment(decl)) {
|
|
347
|
+
if (decl.isExportEquals)
|
|
348
|
+
continue;
|
|
349
|
+
value = skipWrappers(decl.expression);
|
|
350
|
+
if (ts.isIdentifier(value)) {
|
|
351
|
+
defaultRef = value;
|
|
352
|
+
decl = checker.getSymbolAtLocation(value)?.valueDeclaration;
|
|
353
|
+
if (!decl || (ts.isVariableDeclaration(decl) && decl.end > value.pos))
|
|
354
|
+
continue;
|
|
355
|
+
value = decl;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const name = ts.isVariableDeclaration(value) || ts.isFunctionDeclaration(value) ? value.name : undefined;
|
|
359
|
+
if (name &&
|
|
360
|
+
(!ts.isIdentifier(name) ||
|
|
361
|
+
!uses(name).every((ref) => ref === defaultRef || componentTagOrExport(ref))))
|
|
362
|
+
continue;
|
|
363
|
+
if (ts.isVariableDeclaration(value)) {
|
|
364
|
+
if (!(ts.getCombinedNodeFlags(value) & ts.NodeFlags.Const))
|
|
365
|
+
continue;
|
|
366
|
+
if (!value.initializer)
|
|
367
|
+
continue;
|
|
368
|
+
value = skipWrappers(value.initializer);
|
|
369
|
+
}
|
|
370
|
+
const returned = singleReturn(value, allowSetup);
|
|
371
|
+
if (returned)
|
|
372
|
+
yield {
|
|
373
|
+
name: symbol.name,
|
|
374
|
+
output: skipWrappers(returned),
|
|
375
|
+
localName: name?.text,
|
|
376
|
+
decl,
|
|
377
|
+
symbol,
|
|
378
|
+
splitExport,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function* directExportReturns(bound, allowSetup = false) {
|
|
383
|
+
for (const entry of exportReturns(bound, allowSetup)) {
|
|
384
|
+
const { output } = entry;
|
|
385
|
+
if (ts.isJsxElement(output) || ts.isJsxSelfClosingElement(output))
|
|
386
|
+
yield { ...entry, output };
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
function singleReturn(fn, allowSetup = false) {
|
|
390
|
+
if (!(ts.isFunctionDeclaration(fn) || ts.isFunctionExpression(fn) || ts.isArrowFunction(fn)) ||
|
|
391
|
+
fn.asteriskToken ||
|
|
392
|
+
fn.modifiers?.some((item) => item.kind === ts.SyntaxKind.AsyncKeyword))
|
|
393
|
+
return;
|
|
394
|
+
if (fn.body && ts.isBlock(fn.body)) {
|
|
395
|
+
const statements = fn.body.statements;
|
|
396
|
+
const last = statements.at(-1);
|
|
397
|
+
if (!last || !ts.isReturnStatement(last) || !last.expression)
|
|
398
|
+
return;
|
|
399
|
+
if (statements.length === 1)
|
|
400
|
+
return last.expression;
|
|
401
|
+
// The codemod moves this setup and its terminal JSX into one template body.
|
|
402
|
+
if (!allowSetup ||
|
|
403
|
+
(ts.getCombinedModifierFlags(fn) & ts.ModifierFlags.Ambient) !== 0 ||
|
|
404
|
+
(ts.isVariableDeclaration(fn.parent) &&
|
|
405
|
+
(ts.getCombinedModifierFlags(fn.parent) & ts.ModifierFlags.Ambient) !== 0) ||
|
|
406
|
+
(!ts.isFunctionDeclaration(fn) &&
|
|
407
|
+
!ts.isVariableDeclaration(fn.parent) &&
|
|
408
|
+
!ts.isExportAssignment(fn.parent)) ||
|
|
409
|
+
!statements
|
|
410
|
+
.slice(0, -1)
|
|
411
|
+
.every((statement) => (typeof allowSetup === 'function' && allowSetup(statement)) ||
|
|
412
|
+
(ts.isVariableStatement(statement) &&
|
|
413
|
+
!statement.modifiers?.some((item) => item.kind === ts.SyntaxKind.DeclareKeyword) &&
|
|
414
|
+
(statement.declarationList.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const &&
|
|
415
|
+
statement.declarationList.declarations.every((decl) => decl.initializer !== undefined))))
|
|
416
|
+
return;
|
|
417
|
+
if (typeof allowSetup === 'function')
|
|
418
|
+
return last.expression;
|
|
419
|
+
const output = skipParens(last.expression);
|
|
420
|
+
if (ts.isJsxElement(output) || ts.isJsxSelfClosingElement(output))
|
|
421
|
+
return output;
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
return fn.body;
|
|
425
|
+
}
|
|
426
|
+
function conditionalFallback(statement, checker) {
|
|
427
|
+
if (!ts.isIfStatement(statement) || statement.elseStatement)
|
|
428
|
+
return false;
|
|
429
|
+
const test = skipWrappers(statement.expression);
|
|
430
|
+
const negated = ts.isPrefixUnaryExpression(test) && test.operator === ts.SyntaxKind.ExclamationToken;
|
|
431
|
+
const identifier = negated ? skipWrappers(test.operand) : test;
|
|
432
|
+
if (!ts.isIdentifier(identifier))
|
|
433
|
+
return false;
|
|
434
|
+
const declaration = checker.getSymbolAtLocation(identifier)?.valueDeclaration;
|
|
435
|
+
if (declaration &&
|
|
436
|
+
ts.isVariableDeclaration(declaration) &&
|
|
437
|
+
ts.getCombinedNodeFlags(declaration) & ts.NodeFlags.Const &&
|
|
438
|
+
declaration.initializer) {
|
|
439
|
+
const value = literalCondition(declaration.initializer);
|
|
440
|
+
if (value !== undefined && negated !== (value === 'truthy'))
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
const branch = statement.thenStatement;
|
|
444
|
+
return (ts.isReturnStatement(branch) ||
|
|
445
|
+
(ts.isBlock(branch) &&
|
|
446
|
+
branch.statements.length === 1 &&
|
|
447
|
+
ts.isReturnStatement(branch.statements[0])));
|
|
448
|
+
}
|
|
449
|
+
function openingTag(element) {
|
|
450
|
+
return ts.isJsxElement(element) ? element.openingElement : element;
|
|
451
|
+
}
|
|
452
|
+
function hostedLeaf(output) {
|
|
453
|
+
const attrs = openingTag(output).attributes.properties;
|
|
454
|
+
const seen = new Set();
|
|
455
|
+
let component;
|
|
456
|
+
for (const attr of attrs) {
|
|
457
|
+
if (!ts.isJsxAttribute(attr) || !ts.isIdentifier(attr.name))
|
|
458
|
+
return;
|
|
459
|
+
const name = attr.name.text;
|
|
460
|
+
if (seen.has(name) ||
|
|
461
|
+
![
|
|
462
|
+
'component',
|
|
463
|
+
'props',
|
|
464
|
+
'key',
|
|
465
|
+
'hostClassName',
|
|
466
|
+
'hostStyle',
|
|
467
|
+
'hostRole',
|
|
468
|
+
'hostAriaLabel',
|
|
469
|
+
].includes(name))
|
|
470
|
+
return;
|
|
471
|
+
seen.add(name);
|
|
472
|
+
if (name === 'component') {
|
|
473
|
+
const value = attr.initializer;
|
|
474
|
+
if (!value || !ts.isJsxExpression(value) || !value.expression || value.dotDotDotToken)
|
|
475
|
+
return;
|
|
476
|
+
component = skipWrappers(value.expression);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
const children = ts.isJsxElement(output)
|
|
480
|
+
? withoutJsxIndentation(output.children).filter((child) => !ts.isJsxExpression(child) || child.expression)
|
|
481
|
+
: [];
|
|
482
|
+
if (component)
|
|
483
|
+
return children.length === 0 ? component : undefined;
|
|
484
|
+
if (seen.has('props'))
|
|
485
|
+
return;
|
|
486
|
+
const [child, extra] = children;
|
|
487
|
+
if (!extra && child && (ts.isJsxElement(child) || ts.isJsxSelfClosingElement(child)))
|
|
488
|
+
return openingTag(child).tagName;
|
|
489
|
+
}
|
|
490
|
+
function ordinaryReactJsx(source) {
|
|
491
|
+
return !(/@jsx(?:Frag)?\b/i.test(source) ||
|
|
492
|
+
[...source.matchAll(/@jsxRuntime\s+(\S+)/gi)].some(([, mode]) => mode !== 'automatic') ||
|
|
493
|
+
[...source.matchAll(/@jsxImportSource\s+(\S+)/gi)].some(([, name]) => name !== 'react'));
|
|
494
|
+
}
|
|
495
|
+
/** Runtime imports that an explicit React host returns directly. */
|
|
496
|
+
export function hostedComponents(file, source, bridge, selected = false) {
|
|
497
|
+
if (!ordinaryReactJsx(source))
|
|
498
|
+
return [];
|
|
499
|
+
const bound = bind(file, source);
|
|
500
|
+
const { sf, checker } = bound;
|
|
501
|
+
const edges = [];
|
|
502
|
+
function visit(output) {
|
|
503
|
+
if (selected && ts.isJsxFragment(output)) {
|
|
504
|
+
output.children.forEach(visit);
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
if (!ts.isJsxElement(output) && !ts.isJsxSelfClosingElement(output))
|
|
508
|
+
return;
|
|
509
|
+
const opening = openingTag(output);
|
|
510
|
+
const tag = opening.tagName;
|
|
511
|
+
if (ts.isIdentifier(tag) && (/^[a-z]/.test(tag.text) || tag.text.includes('-'))) {
|
|
512
|
+
// Intrinsic children reach the returned output; custom slots can capture JSX.
|
|
513
|
+
if (selected && ts.isJsxElement(output) && rendersElementChildren(opening))
|
|
514
|
+
output.children.forEach(visit);
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
const host = imported(checker, tag);
|
|
518
|
+
if (host?.module !== bridge ||
|
|
519
|
+
(host.name !== 'OctaneCompat' && host.name !== 'OctaneCompatInline'))
|
|
520
|
+
return;
|
|
521
|
+
const ref = hostedLeaf(output);
|
|
522
|
+
const leaf = ref && imported(checker, ref);
|
|
523
|
+
if (leaf)
|
|
524
|
+
edges.push({ ...leaf, line: sf.getLineAndCharacterOfPosition(output.getStart(sf)).line + 1 });
|
|
525
|
+
}
|
|
526
|
+
const allowSetup = selected && ((statement) => conditionalFallback(statement, checker));
|
|
527
|
+
for (const { output } of exportReturns(bound, allowSetup))
|
|
528
|
+
visit(output);
|
|
529
|
+
return edges;
|
|
530
|
+
}
|
|
531
|
+
function literalImport(expr) {
|
|
532
|
+
const call = skipWrappers(expr);
|
|
533
|
+
if (!ts.isCallExpression(call) || call.expression.kind !== ts.SyntaxKind.ImportKeyword)
|
|
534
|
+
return;
|
|
535
|
+
const [specifier, extra] = call.arguments;
|
|
536
|
+
if (specifier &&
|
|
537
|
+
!extra &&
|
|
538
|
+
ts.isStringLiteralLike(specifier) &&
|
|
539
|
+
/^\.{1,2}\//.test(specifier.text) &&
|
|
540
|
+
!/[!%?#\\]/.test(specifier.text))
|
|
541
|
+
return specifier.text;
|
|
542
|
+
}
|
|
543
|
+
function lazyComponentImport(checker, tag) {
|
|
544
|
+
if (!ts.isIdentifier(tag))
|
|
545
|
+
return;
|
|
546
|
+
const symbol = checker.getSymbolAtLocation(tag);
|
|
547
|
+
const decl = symbol?.valueDeclaration;
|
|
548
|
+
if (symbol?.declarations?.length !== 1 ||
|
|
549
|
+
!decl ||
|
|
550
|
+
!ts.isVariableDeclaration(decl) ||
|
|
551
|
+
ts.getCombinedModifierFlags(decl) & ts.ModifierFlags.Ambient ||
|
|
552
|
+
!decl.initializer ||
|
|
553
|
+
!ts.isVariableDeclarationList(decl.parent) ||
|
|
554
|
+
(decl.parent.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
555
|
+
!ts.isVariableStatement(decl.parent.parent) ||
|
|
556
|
+
!ts.isSourceFile(decl.parent.parent.parent))
|
|
557
|
+
return;
|
|
558
|
+
const call = skipWrappers(decl.initializer);
|
|
559
|
+
if (!ts.isCallExpression(call) || ts.isOptionalChain(call) || call.arguments.length !== 1)
|
|
560
|
+
return;
|
|
561
|
+
const callee = skipWrappers(call.expression);
|
|
562
|
+
if (ts.isOptionalChain(callee))
|
|
563
|
+
return;
|
|
564
|
+
const factory = imported(checker, callee);
|
|
565
|
+
if (factory?.module !== 'react' || factory.name !== 'lazy')
|
|
566
|
+
return;
|
|
567
|
+
const loader = skipWrappers(call.arguments[0]);
|
|
568
|
+
if (!ts.isArrowFunction(loader) || loader.parameters.length)
|
|
569
|
+
return;
|
|
570
|
+
const result = singleReturn(loader);
|
|
571
|
+
if (!result)
|
|
572
|
+
return;
|
|
573
|
+
const direct = literalImport(result);
|
|
574
|
+
if (direct)
|
|
575
|
+
return { module: direct, name: 'default', lazy: true };
|
|
576
|
+
const map = skipWrappers(result);
|
|
577
|
+
if (!ts.isCallExpression(map) ||
|
|
578
|
+
ts.isOptionalChain(map) ||
|
|
579
|
+
map.arguments.length !== 1 ||
|
|
580
|
+
!ts.isPropertyAccessExpression(map.expression) ||
|
|
581
|
+
map.expression.name.text !== 'then')
|
|
582
|
+
return;
|
|
583
|
+
const module = literalImport(map.expression.expression);
|
|
584
|
+
if (!module)
|
|
585
|
+
return;
|
|
586
|
+
const mapper = skipWrappers(map.arguments[0]);
|
|
587
|
+
if (!ts.isArrowFunction(mapper) || mapper.parameters.length !== 1)
|
|
588
|
+
return;
|
|
589
|
+
const parameter = mapper.parameters[0];
|
|
590
|
+
if (!ts.isIdentifier(parameter.name) || parameter.initializer || parameter.dotDotDotToken)
|
|
591
|
+
return;
|
|
592
|
+
const mapped = singleReturn(mapper);
|
|
593
|
+
const object = mapped && skipWrappers(mapped);
|
|
594
|
+
if (!object || !ts.isObjectLiteralExpression(object) || object.properties.length !== 1)
|
|
595
|
+
return;
|
|
596
|
+
const field = object.properties[0];
|
|
597
|
+
if (!ts.isPropertyAssignment(field) ||
|
|
598
|
+
!ts.isIdentifier(field.name) ||
|
|
599
|
+
field.name.text !== 'default')
|
|
600
|
+
return;
|
|
601
|
+
const value = skipWrappers(field.initializer);
|
|
602
|
+
if (ts.isPropertyAccessExpression(value) &&
|
|
603
|
+
!ts.isOptionalChain(value) &&
|
|
604
|
+
checker.getSymbolAtLocation(value.expression) === checker.getSymbolAtLocation(parameter.name))
|
|
605
|
+
return { module, name: value.name.text, lazy: true };
|
|
606
|
+
}
|
|
607
|
+
/** Imports rendered directly by a retained component. */
|
|
608
|
+
export function retainedReactComponentReturns(file, source) {
|
|
609
|
+
if (!ordinaryReactJsx(source))
|
|
610
|
+
return [];
|
|
611
|
+
const bound = bind(file, source);
|
|
612
|
+
const { sf, checker } = bound;
|
|
613
|
+
const edges = [];
|
|
614
|
+
const seen = new Set();
|
|
615
|
+
const mayBeLazy = source.includes('import') && (source.includes('lazy') || source.includes('\\'));
|
|
616
|
+
for (const { output, localName, decl } of directExportReturns(bound)) {
|
|
617
|
+
if (seen.has(output))
|
|
618
|
+
continue;
|
|
619
|
+
const tag = openingTag(output).tagName;
|
|
620
|
+
if (localName && !/^[A-Z]/.test(localName))
|
|
621
|
+
continue;
|
|
622
|
+
seen.add(output);
|
|
623
|
+
if (ts.isIdentifier(tag) && /^[a-z]/.test(tag.text)) {
|
|
624
|
+
if (!ts.isJsxElement(output) ||
|
|
625
|
+
(ts.getCombinedModifierFlags(decl) & ts.ModifierFlags.Ambient) !== 0 ||
|
|
626
|
+
!rendersElementChildren(output.openingElement) ||
|
|
627
|
+
output.openingElement.attributes.properties.some(ts.isJsxSpreadAttribute))
|
|
628
|
+
continue;
|
|
629
|
+
for (const child of output.children) {
|
|
630
|
+
if (!ts.isJsxExpression(child) || !child.expression || child.dotDotDotToken)
|
|
631
|
+
continue;
|
|
632
|
+
const call = skipWrappers(child.expression);
|
|
633
|
+
if (!ts.isCallExpression(call) || ts.isOptionalChain(call) || call.arguments.length)
|
|
634
|
+
continue;
|
|
635
|
+
const callee = skipWrappers(call.expression);
|
|
636
|
+
const leaf = ts.isIdentifier(callee) && imported(checker, callee);
|
|
637
|
+
if (leaf && /^\.{1,2}\/[^!%?#\\]*\.tsx$/.test(leaf.module))
|
|
638
|
+
edges.push({
|
|
639
|
+
...leaf,
|
|
640
|
+
factory: true,
|
|
641
|
+
line: sf.getLineAndCharacterOfPosition(call.getStart(sf)).line + 1,
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
continue;
|
|
645
|
+
}
|
|
646
|
+
if (ts.isIdentifier(tag) && !/^[A-Z]/.test(tag.text))
|
|
647
|
+
continue;
|
|
648
|
+
const leaf = imported(checker, tag) ?? (mayBeLazy ? lazyComponentImport(checker, tag) : undefined);
|
|
649
|
+
if (leaf?.module.startsWith('.'))
|
|
650
|
+
edges.push({ ...leaf, line: sf.getLineAndCharacterOfPosition(output.getStart(sf)).line + 1 });
|
|
651
|
+
}
|
|
652
|
+
return edges;
|
|
653
|
+
}
|
|
654
|
+
/** Named React factories returned by an otherwise retained function. */
|
|
655
|
+
export function retainedReactElementReturns(file, source) {
|
|
656
|
+
const bound = bind(file, source);
|
|
657
|
+
const { sf, checker } = bound;
|
|
658
|
+
const edges = [];
|
|
659
|
+
const seen = new Set();
|
|
660
|
+
for (const { output } of exportReturns(bound)) {
|
|
661
|
+
if (seen.has(output) ||
|
|
662
|
+
!ts.isCallExpression(output) ||
|
|
663
|
+
ts.isOptionalChain(output) ||
|
|
664
|
+
!ts.isIdentifier(output.expression) ||
|
|
665
|
+
!output.arguments.length ||
|
|
666
|
+
output.arguments.some(ts.isSpreadElement))
|
|
667
|
+
continue;
|
|
668
|
+
const factory = imported(checker, output.expression);
|
|
669
|
+
if (factory?.module !== 'react' || factory.name !== 'createElement')
|
|
670
|
+
continue;
|
|
671
|
+
const component = skipWrappers(output.arguments[0]);
|
|
672
|
+
if (!ts.isIdentifier(component))
|
|
673
|
+
continue;
|
|
674
|
+
const leaf = imported(checker, component);
|
|
675
|
+
if (leaf?.module.startsWith('.')) {
|
|
676
|
+
seen.add(output);
|
|
677
|
+
edges.push({
|
|
678
|
+
...leaf,
|
|
679
|
+
line: sf.getLineAndCharacterOfPosition(output.getStart(sf)).line + 1,
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
return edges;
|
|
684
|
+
}
|
|
685
|
+
/** Existing Octane roots that render a directly imported component body. */
|
|
686
|
+
export function octaneRootComponents(file, source) {
|
|
687
|
+
const { sf, checker } = bind(file, source);
|
|
688
|
+
const edges = [];
|
|
689
|
+
let uses;
|
|
690
|
+
function clientComponent(call, callee, hydrate) {
|
|
691
|
+
if (hydrate && call.arguments.length <= 4)
|
|
692
|
+
return call.arguments[1];
|
|
693
|
+
if (!ts.isPropertyAccessExpression(callee) ||
|
|
694
|
+
ts.isOptionalChain(callee) ||
|
|
695
|
+
callee.name.text !== 'render' ||
|
|
696
|
+
call.arguments.length > 2)
|
|
697
|
+
return;
|
|
698
|
+
const receiver = skipWrappers(callee.expression);
|
|
699
|
+
let root = receiver;
|
|
700
|
+
let handle;
|
|
701
|
+
if (ts.isIdentifier(receiver)) {
|
|
702
|
+
const declaration = checker.getSymbolAtLocation(receiver)?.valueDeclaration;
|
|
703
|
+
if (!declaration ||
|
|
704
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
705
|
+
!ts.isIdentifier(declaration.name) ||
|
|
706
|
+
!declaration.initializer ||
|
|
707
|
+
!(declaration.parent.flags & ts.NodeFlags.Const) ||
|
|
708
|
+
ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export ||
|
|
709
|
+
(!atInitializedModuleHandle(declaration, call) &&
|
|
710
|
+
!initializedBefore(checker, receiver, call)))
|
|
711
|
+
return;
|
|
712
|
+
handle = declaration.name;
|
|
713
|
+
root = skipWrappers(declaration.initializer);
|
|
714
|
+
}
|
|
715
|
+
if (!ts.isCallExpression(root) ||
|
|
716
|
+
ts.isOptionalChain(root) ||
|
|
717
|
+
root.arguments.length < 1 ||
|
|
718
|
+
root.arguments.length > 2 ||
|
|
719
|
+
root.arguments.some(ts.isSpreadElement))
|
|
720
|
+
return;
|
|
721
|
+
const factory = imported(checker, skipWrappers(root.expression));
|
|
722
|
+
if (factory?.module !== 'octane' || factory.name !== 'createRoot')
|
|
723
|
+
return;
|
|
724
|
+
if (handle) {
|
|
725
|
+
if ((source.includes('eval') || source.includes('\\u')) && hasEval(sf))
|
|
726
|
+
return;
|
|
727
|
+
const rootScope = (node) => ts.isFunctionLike(node) || ts.isClassLike(node);
|
|
728
|
+
const owner = ts.findAncestor(handle, rootScope);
|
|
729
|
+
const refs = (uses ??= localUses(sf, checker))(handle);
|
|
730
|
+
if (rootHandleEscapes(refs, (ref) => ts.findAncestor(ref, rootScope) === owner))
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
return call.arguments[0];
|
|
734
|
+
}
|
|
735
|
+
function visit(node) {
|
|
736
|
+
if (ts.isCallExpression(node) &&
|
|
737
|
+
!ts.isOptionalChain(node) &&
|
|
738
|
+
!node.arguments.some(ts.isSpreadElement)) {
|
|
739
|
+
const callee = skipWrappers(node.expression);
|
|
740
|
+
const entry = imported(checker, callee);
|
|
741
|
+
const server = entry?.module === 'octane/server';
|
|
742
|
+
const value = server
|
|
743
|
+
? node.arguments.length <= 3 &&
|
|
744
|
+
(entry.name === 'renderToString' || entry.name === 'renderToStaticMarkup') &&
|
|
745
|
+
node.arguments[0]
|
|
746
|
+
: clientComponent(node, callee, entry?.module === 'octane' && entry.name === 'hydrateRoot');
|
|
747
|
+
const leaf = value ? imported(checker, skipWrappers(value)) : undefined;
|
|
748
|
+
if (leaf?.module.startsWith('.') && /\.[jt]sx$/.test(leaf.module))
|
|
749
|
+
edges.push({
|
|
750
|
+
...leaf,
|
|
751
|
+
runtime: server ? 'octane/server' : 'octane',
|
|
752
|
+
line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1,
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
ts.forEachChild(node, visit);
|
|
756
|
+
}
|
|
757
|
+
visit(sf);
|
|
758
|
+
return edges;
|
|
759
|
+
}
|
|
760
|
+
/** React server calls that render directly imported components. */
|
|
761
|
+
export function reactServerComponents(file, source, original, selected = false) {
|
|
762
|
+
const { sf, checker } = bind(file, source);
|
|
763
|
+
const edges = [];
|
|
764
|
+
function visit(node) {
|
|
765
|
+
if (ts.isCallExpression(node) && !ts.isOptionalChain(node) && node.arguments.length === 1) {
|
|
766
|
+
const callee = imported(checker, node.expression);
|
|
767
|
+
if (callee?.module === 'react-dom/server' &&
|
|
768
|
+
(callee.name === 'renderToString' || callee.name === 'renderToStaticMarkup')) {
|
|
769
|
+
const value = skipWrappers(node.arguments[0]);
|
|
770
|
+
let ref;
|
|
771
|
+
const jsx = ts.isJsxSelfClosingElement(value) ||
|
|
772
|
+
(ts.isJsxElement(value) &&
|
|
773
|
+
withoutJsxIndentation(value.children).every((child) => ts.isJsxExpression(child) && !child.expression))
|
|
774
|
+
? openingTag(value)
|
|
775
|
+
: undefined;
|
|
776
|
+
if (jsx &&
|
|
777
|
+
(!ts.isIdentifier(jsx.tagName) || /^[A-Z][\w$]*$/.test(jsx.tagName.text)) &&
|
|
778
|
+
(jsx.attributes.properties.length === 0 ||
|
|
779
|
+
(selected &&
|
|
780
|
+
ts.isJsxSelfClosingElement(jsx) &&
|
|
781
|
+
staticReactProps(jsx) !== null &&
|
|
782
|
+
!jsx.attributes.properties.some((attr) => ts.isJsxAttribute(attr) &&
|
|
783
|
+
ts.isIdentifier(attr.name) &&
|
|
784
|
+
attr.name.text === 'children'))) &&
|
|
785
|
+
ordinaryReactJsx(source) &&
|
|
786
|
+
original('react/jsx-runtime'))
|
|
787
|
+
ref = jsx.tagName;
|
|
788
|
+
if (ts.isCallExpression(value) &&
|
|
789
|
+
!ts.isOptionalChain(value) &&
|
|
790
|
+
(value.arguments.length === 1 ||
|
|
791
|
+
(value.arguments.length === 2 &&
|
|
792
|
+
(selected || skipWrappers(value.arguments[1]).kind === ts.SyntaxKind.NullKeyword)))) {
|
|
793
|
+
const factory = imported(checker, value.expression);
|
|
794
|
+
if (factory?.module === 'react' && factory.name === 'createElement' && original('react'))
|
|
795
|
+
ref = skipWrappers(value.arguments[0]);
|
|
796
|
+
}
|
|
797
|
+
const leaf = ref && imported(checker, ref);
|
|
798
|
+
if (leaf?.module.startsWith('.') && (!selected || /\.[jt]sx$/.test(leaf.module)))
|
|
799
|
+
edges.push({
|
|
800
|
+
...leaf,
|
|
801
|
+
line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1,
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
if (!selected)
|
|
806
|
+
ts.forEachChild(node, visit);
|
|
807
|
+
}
|
|
808
|
+
if (selected) {
|
|
809
|
+
function visitDirectRoot(expression) {
|
|
810
|
+
const call = expression && skipParens(expression);
|
|
811
|
+
if (!call || !ts.isCallExpression(call))
|
|
812
|
+
return;
|
|
813
|
+
const argument = call.arguments[0] && skipParens(call.arguments[0]);
|
|
814
|
+
if (!argument)
|
|
815
|
+
return;
|
|
816
|
+
if (ts.isJsxSelfClosingElement(argument) && ts.isIdentifier(argument.tagName)) {
|
|
817
|
+
visit(call);
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
const target = ts.isJsxSelfClosingElement(argument)
|
|
821
|
+
? argument.tagName
|
|
822
|
+
: ts.isCallExpression(argument) && argument.arguments[0];
|
|
823
|
+
if (target &&
|
|
824
|
+
(ts.isIdentifier(target) || ts.isPropertyAccessExpression(target)) &&
|
|
825
|
+
/^[A-Z]/.test(target.getText(sf).split('.').pop() ?? ''))
|
|
826
|
+
visit(call);
|
|
827
|
+
}
|
|
828
|
+
function visitEntryStatement(statement) {
|
|
829
|
+
if (ts.isVariableStatement(statement)) {
|
|
830
|
+
for (const declaration of statement.declarationList.declarations)
|
|
831
|
+
visitDirectRoot(declaration.initializer);
|
|
832
|
+
}
|
|
833
|
+
else if (ts.isExpressionStatement(statement) ||
|
|
834
|
+
(ts.isExportAssignment(statement) && !statement.isExportEquals)) {
|
|
835
|
+
visitDirectRoot(statement.expression);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
for (const statement of sf.statements) {
|
|
839
|
+
if (ts.isIfStatement(statement)) {
|
|
840
|
+
const condition = skipParens(statement.expression);
|
|
841
|
+
if (ts.isPropertyAccessExpression(condition) &&
|
|
842
|
+
!ts.isOptionalChain(condition) &&
|
|
843
|
+
condition.name.text === 'main' &&
|
|
844
|
+
ts.isMetaProperty(condition.expression) &&
|
|
845
|
+
condition.expression.keywordToken === ts.SyntaxKind.ImportKeyword &&
|
|
846
|
+
condition.expression.name.text === 'meta') {
|
|
847
|
+
const body = statement.thenStatement;
|
|
848
|
+
if (ts.isBlock(body))
|
|
849
|
+
body.statements.forEach(visitEntryStatement);
|
|
850
|
+
else
|
|
851
|
+
visitEntryStatement(body);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
else if (ts.isFunctionDeclaration(statement) &&
|
|
855
|
+
statement.body &&
|
|
856
|
+
!statement.asteriskToken &&
|
|
857
|
+
!statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword)) {
|
|
858
|
+
for (const bodyStatement of statement.body.statements)
|
|
859
|
+
if (ts.isReturnStatement(bodyStatement))
|
|
860
|
+
visitDirectRoot(bodyStatement.expression);
|
|
861
|
+
}
|
|
862
|
+
else
|
|
863
|
+
visitEntryStatement(statement);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
else
|
|
867
|
+
visit(sf);
|
|
868
|
+
return edges;
|
|
869
|
+
}
|
|
870
|
+
/** Local exports whose original body returns React JSX directly. */
|
|
871
|
+
export function directReactExports(file, source) {
|
|
872
|
+
const intrinsics = new Set();
|
|
873
|
+
const wrappers = new Set();
|
|
874
|
+
let splitIntrinsicDefault = false;
|
|
875
|
+
let mayExportThen = false;
|
|
876
|
+
if (ordinaryReactJsx(source)) {
|
|
877
|
+
const bound = bind(file, source);
|
|
878
|
+
const { sf, checker } = bound;
|
|
879
|
+
const module = checker.getSymbolAtLocation(sf);
|
|
880
|
+
const then = module && checker.getExportsOfModule(module).find((symbol) => symbol.name === 'then');
|
|
881
|
+
// import() assimilates a callable namespace then. This local checker cannot resolve wildcard re-exports.
|
|
882
|
+
mayExportThen =
|
|
883
|
+
!!then &&
|
|
884
|
+
(!!(then.flags & ts.SymbolFlags.Value) ||
|
|
885
|
+
!!then.declarations?.some((decl) => (ts.isExportSpecifier(decl) && !decl.isTypeOnly && !decl.parent.parent.isTypeOnly) ||
|
|
886
|
+
(ts.isImportEqualsDeclaration(decl) && !decl.isTypeOnly)));
|
|
887
|
+
mayExportThen ||= sf.statements.some((stmt) => ts.isExportDeclaration(stmt) && !stmt.isTypeOnly && !stmt.exportClause);
|
|
888
|
+
for (const { name, output, localName, decl, splitExport } of directExportReturns(bound, true)) {
|
|
889
|
+
const tag = openingTag(output).tagName;
|
|
890
|
+
if (ts.isIdentifier(tag) &&
|
|
891
|
+
/^[a-z]/.test(tag.text) &&
|
|
892
|
+
(!localName || /^[A-Z]/.test(localName))) {
|
|
893
|
+
if (!splitExport)
|
|
894
|
+
intrinsics.add(name);
|
|
895
|
+
else if (name === 'default')
|
|
896
|
+
splitIntrinsicDefault = true;
|
|
897
|
+
}
|
|
898
|
+
else if (ts.isFunctionDeclaration(decl) &&
|
|
899
|
+
(name === 'default'
|
|
900
|
+
? !localName || /^[A-Z]/.test(localName)
|
|
901
|
+
: localName === name && /^[A-Z]/.test(name)) &&
|
|
902
|
+
ts.isIdentifier(tag) &&
|
|
903
|
+
/^[A-Z]/.test(tag.text))
|
|
904
|
+
wrappers.add(name);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
return { intrinsics, wrappers, splitIntrinsicDefault, mayExportThen };
|
|
908
|
+
}
|
|
909
|
+
/** Factories whose intrinsic JSX return survives as Octane JSX in the emitted file. */
|
|
910
|
+
export function migratedJsxFactories(file, source, emitted) {
|
|
911
|
+
if (!ordinaryReactJsx(source))
|
|
912
|
+
return new Set();
|
|
913
|
+
function factories(code) {
|
|
914
|
+
const names = new Set();
|
|
915
|
+
for (const { name, output, decl, symbol } of directExportReturns(bind(file, code))) {
|
|
916
|
+
const tag = openingTag(output).tagName;
|
|
917
|
+
if (ts.isFunctionDeclaration(decl) &&
|
|
918
|
+
symbol.declarations?.length === 1 &&
|
|
919
|
+
decl.name?.text === name &&
|
|
920
|
+
/^[a-z]/.test(name) &&
|
|
921
|
+
decl.parameters.length === 0 &&
|
|
922
|
+
(ts.getCombinedModifierFlags(decl) &
|
|
923
|
+
(ts.ModifierFlags.Export | ts.ModifierFlags.Ambient)) ===
|
|
924
|
+
ts.ModifierFlags.Export &&
|
|
925
|
+
ts.isIdentifier(tag) &&
|
|
926
|
+
/^[a-z]/.test(tag.text))
|
|
927
|
+
names.add(name);
|
|
928
|
+
}
|
|
929
|
+
return names;
|
|
930
|
+
}
|
|
931
|
+
const original = factories(source);
|
|
932
|
+
// TSX parsing proves the emitted return directly. Volar can hoist it out of the function.
|
|
933
|
+
if (!original.size || parseSync(file, emitted).errors.length)
|
|
934
|
+
return new Set();
|
|
935
|
+
return original.intersection(factories(emitted));
|
|
936
|
+
}
|
|
937
|
+
/** Inspect the emitted local export before assigning a reverse island to a server host. */
|
|
938
|
+
export async function reverseIslandExports(file, source, original) {
|
|
939
|
+
const names = new Set();
|
|
940
|
+
if (!importsReactBridge(source))
|
|
941
|
+
return names;
|
|
942
|
+
const bound = await bindIsland(file, source);
|
|
943
|
+
if (!bound)
|
|
944
|
+
return names;
|
|
945
|
+
for (const { name, output } of directExportReturns(bound)) {
|
|
946
|
+
const island = imported(bound.checker, openingTag(output).tagName);
|
|
947
|
+
if (island && nativeReactCompat(island.module, island.name) && original(island.module))
|
|
948
|
+
names.add(name);
|
|
949
|
+
}
|
|
950
|
+
return names;
|
|
951
|
+
}
|
|
952
|
+
/** Declaration-level type imports disappear even with verbatimModuleSyntax. */
|
|
953
|
+
export function executableImports(file, source, verbatim) {
|
|
954
|
+
const sf = ts.createSourceFile(file, source, ts.ScriptTarget.Latest);
|
|
955
|
+
const imports = [];
|
|
956
|
+
function visit(node) {
|
|
957
|
+
if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
|
|
958
|
+
if (hasRuntimeDependency(node, verbatim) &&
|
|
959
|
+
node.moduleSpecifier &&
|
|
960
|
+
ts.isStringLiteral(node.moduleSpecifier))
|
|
961
|
+
imports.push(node.moduleSpecifier.text);
|
|
962
|
+
return;
|
|
963
|
+
}
|
|
964
|
+
if (ts.isImportEqualsDeclaration(node) && !node.isTypeOnly) {
|
|
965
|
+
const reference = node.moduleReference;
|
|
966
|
+
if (ts.isExternalModuleReference(reference) && ts.isStringLiteral(reference.expression)) {
|
|
967
|
+
imports.push(reference.expression.text);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
if (ts.isCallExpression(node) &&
|
|
971
|
+
node.expression.kind === ts.SyntaxKind.ImportKeyword &&
|
|
972
|
+
node.arguments[0] &&
|
|
973
|
+
ts.isStringLiteralLike(node.arguments[0]))
|
|
974
|
+
imports.push(node.arguments[0].text);
|
|
975
|
+
ts.forEachChild(node, visit);
|
|
976
|
+
}
|
|
977
|
+
visit(sf);
|
|
978
|
+
return imports;
|
|
979
|
+
}
|