@octanejs/codemod 0.1.3 → 0.2.0

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.
Files changed (58) hide show
  1. package/README.md +305 -5
  2. package/dist/bin.d.ts +2 -0
  3. package/dist/bin.js +162 -0
  4. package/dist/compiler.d.ts +11 -0
  5. package/dist/compiler.js +48 -0
  6. package/dist/effect-receivers.d.ts +11 -0
  7. package/dist/effect-receivers.js +315 -0
  8. package/dist/expression-wrappers.d.ts +6 -0
  9. package/dist/expression-wrappers.js +16 -0
  10. package/dist/forwarded-props.d.ts +27 -0
  11. package/dist/forwarded-props.js +489 -0
  12. package/dist/index.d.ts +6 -0
  13. package/{src → dist}/index.js +1 -0
  14. package/dist/linked-state.d.ts +17 -0
  15. package/dist/linked-state.js +126 -0
  16. package/dist/local-calls.d.ts +20 -0
  17. package/dist/local-calls.js +143 -0
  18. package/dist/local-react-hook.d.ts +15 -0
  19. package/dist/local-react-hook.js +353 -0
  20. package/dist/local-uses.d.ts +13 -0
  21. package/dist/local-uses.js +987 -0
  22. package/dist/mappings.d.ts +57 -0
  23. package/dist/mappings.js +343 -0
  24. package/dist/path-svg.d.ts +60 -0
  25. package/dist/path-svg.js +207 -0
  26. package/dist/project.d.ts +9 -0
  27. package/dist/project.js +1568 -0
  28. package/dist/react-intl.d.ts +4 -0
  29. package/dist/react-intl.js +389 -0
  30. package/dist/react-renderer.d.ts +9 -0
  31. package/dist/react-renderer.js +106 -0
  32. package/dist/scoped-signal.d.ts +4 -0
  33. package/dist/scoped-signal.js +480 -0
  34. package/dist/scoped-store-contract.d.ts +13 -0
  35. package/dist/scoped-store-contract.js +741 -0
  36. package/dist/store-hook-uses.d.ts +18 -0
  37. package/dist/store-hook-uses.js +403 -0
  38. package/dist/store-inputs.d.ts +16 -0
  39. package/dist/store-inputs.js +391 -0
  40. package/dist/strong.d.ts +24 -0
  41. package/dist/strong.js +142 -0
  42. package/dist/transform.d.ts +27 -0
  43. package/dist/transform.js +4890 -0
  44. package/dist/types.d.ts +203 -0
  45. package/dist/types.js +1 -0
  46. package/package.json +19 -6
  47. package/src/path-svg.ts +218 -0
  48. package/templates/octane-path-svg.ts +56 -0
  49. package/templates/octane-store-context.tsrx +49 -0
  50. package/templates/octane-store-hooks.ts +61 -0
  51. package/templates/react-intl.tsrx +116 -0
  52. package/src/bin.js +0 -151
  53. package/src/compiler.js +0 -61
  54. package/src/index.d.ts +0 -95
  55. package/src/mappings.js +0 -429
  56. package/src/project.js +0 -670
  57. package/src/strong.js +0 -149
  58. package/src/transform.js +0 -3425
@@ -0,0 +1,489 @@
1
+ import ts from 'typescript';
2
+ import { isWrapper, skipWrappers } from './expression-wrappers.js';
3
+ import { localUses } from './local-uses.js';
4
+ export function contains(node, matches) {
5
+ return (matches(node) ||
6
+ ts.forEachChild(node, (child) => contains(child, matches) || undefined) === true);
7
+ }
8
+ export function hasEval(file) {
9
+ return contains(file, (node) => ts.isIdentifier(node) && node.text === 'eval');
10
+ }
11
+ export function hasPrototypeAccess(file) {
12
+ return contains(file, (node) => (ts.isIdentifier(node) || ts.isStringLiteralLike(node)) &&
13
+ ['prototype', '__proto__', 'getPrototypeOf'].includes(node.text));
14
+ }
15
+ /** The DOM attributes whose renderer stringifies the supplied value. */
16
+ function hostText(tag, prop) {
17
+ return (ts.isIdentifier(tag) &&
18
+ /^[a-z][a-z0-9]*$/.test(tag.text) &&
19
+ (prop === 'title' ||
20
+ prop.startsWith('aria-') ||
21
+ prop.startsWith('data-') ||
22
+ (prop === 'placeholder' && ['input', 'textarea'].includes(tag.text)) ||
23
+ (prop === 'alt' && ['img', 'input', 'area'].includes(tag.text)) ||
24
+ (prop === 'label' && ['option', 'optgroup', 'track'].includes(tag.text))));
25
+ }
26
+ function hostAttribute(attr, prop) {
27
+ return (hostText(attr.parent.parent.tagName, prop) &&
28
+ attr.parent.properties.every((a) => a === attr ||
29
+ (ts.isJsxAttribute(a) && a.name.getText() !== prop && a.name.getText() !== 'is')));
30
+ }
31
+ function directAttribute(value) {
32
+ const expr = value.parent;
33
+ if (!ts.isJsxExpression(expr) || !ts.isJsxAttribute(expr.parent))
34
+ return false;
35
+ const attr = expr.parent;
36
+ return ts.isIdentifier(attr.name) && hostAttribute(attr, attr.name.text);
37
+ }
38
+ function memberKey(ref) {
39
+ const use = ref.parent;
40
+ if (ts.isPropertyAccessExpression(use) && use.expression === ref)
41
+ return use.name.text;
42
+ if (ts.isElementAccessExpression(use) &&
43
+ use.expression === ref &&
44
+ ts.isStringLiteral(use.argumentExpression))
45
+ return use.argumentExpression.text;
46
+ }
47
+ /** Find the single binding that receives this property, or the rest bag containing it. */
48
+ function incoming(param, prop) {
49
+ if (param.dotDotDotToken || param.initializer)
50
+ return null;
51
+ if (ts.isIdentifier(param.name))
52
+ return { name: param.name, bag: true };
53
+ if (!ts.isObjectBindingPattern(param.name))
54
+ return null;
55
+ let direct, rest;
56
+ for (const field of param.name.elements) {
57
+ const key = field.propertyName ?? field.name;
58
+ if (!ts.isIdentifier(key) && !ts.isStringLiteral(key))
59
+ return null;
60
+ if (field.dotDotDotToken)
61
+ rest = field;
62
+ else if (key.text === prop) {
63
+ if (direct)
64
+ return null;
65
+ direct = field;
66
+ }
67
+ }
68
+ const field = direct ?? rest;
69
+ return field && !field.initializer && ts.isIdentifier(field.name)
70
+ ? { name: field.name, bag: !!field.dotDotDotToken }
71
+ : null;
72
+ }
73
+ // Raw-text and void HTML elements do not render ordinary child elements.
74
+ const nonElementHosts = /^(?:textarea|title|script|style|option|noscript|iframe|noembed|noframes|xmp|plaintext|area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
75
+ const hookName = /^use(?:[A-Z0-9]|$)/;
76
+ // A scalar assertion can turn a child into a text binding. Require a member
77
+ // that the compiler cannot classify as primitive text, even through aliases.
78
+ function renderType(type) {
79
+ const members = type.isUnion() ? type.types : [type];
80
+ return members.some((t) => !!(t.flags & (ts.TypeFlags.Object | ts.TypeFlags.BooleanLike)));
81
+ }
82
+ // Type queries and type-only exports cannot hand a component or its children to a renderer.
83
+ export function renderUses(refs) {
84
+ return refs.filter((ref) => {
85
+ let name = ref;
86
+ while (ts.isQualifiedName(name.parent))
87
+ name = name.parent;
88
+ return !ts.isTypeQueryNode(name.parent) && !ts.isTypeOnlyExportDeclaration(ref.parent);
89
+ });
90
+ }
91
+ /** Follow the value being rendered all the way to its containing component's return. */
92
+ export function nativeOutputOwner(value, slot, { typeOf, text }) {
93
+ for (;;) {
94
+ const parent = value.parent;
95
+ if (ts.isReturnStatement(parent)) {
96
+ const fn = ts.findAncestor(parent, ts.isFunctionLike);
97
+ return fn &&
98
+ (ts.isFunctionDeclaration(fn) || ts.isArrowFunction(fn) || ts.isFunctionExpression(fn))
99
+ ? fn
100
+ : undefined;
101
+ }
102
+ if (ts.isArrowFunction(parent) && parent.body === value)
103
+ return parent;
104
+ if (ts.isJsxElement(parent) || ts.isJsxAttribute(parent)) {
105
+ const attr = ts.isJsxAttribute(parent) ? parent : undefined;
106
+ const element = ts.isJsxElement(parent) ? parent.openingElement : parent.parent.parent;
107
+ if (!(ts.isJsxOpeningElement(element) || ts.isJsxSelfClosingElement(element)))
108
+ return;
109
+ const tag = element.tagName;
110
+ if (!ts.isIdentifier(tag))
111
+ return;
112
+ if (attr
113
+ ? attr.initializer !== value || !ts.isIdentifier(attr.name)
114
+ : ts.isJsxElement(parent) && !parent.children.some((child) => child === value))
115
+ return;
116
+ if (/^[a-z]/.test(tag.text)) {
117
+ if (attr ||
118
+ nonElementHosts.test(tag.text) ||
119
+ element.attributes.properties.some((a) => ts.isJsxAttribute(a) && a.name.getText() === 'dangerouslySetInnerHTML'))
120
+ return;
121
+ }
122
+ else {
123
+ const key = attr ? attr.name.getText() : 'children';
124
+ if ((attr &&
125
+ key === 'children' &&
126
+ ts.isJsxOpeningElement(element) &&
127
+ element.parent.children.length) ||
128
+ element.attributes.properties.some((a) => !ts.isJsxAttribute(a) ||
129
+ !ts.isIdentifier(a.name) ||
130
+ a.name.text === '__proto__' ||
131
+ (a !== attr && a.name.text === key)) ||
132
+ !slot(tag, key))
133
+ return;
134
+ }
135
+ value = ts.isJsxOpeningElement(element) ? element.parent : element;
136
+ continue;
137
+ }
138
+ if (isWrapper(parent) && !ts.isParenthesizedExpression(parent)) {
139
+ if (parent.expression !== value ||
140
+ !renderType(typeOf(parent)) ||
141
+ (ts.isSatisfiesExpression(parent) && !renderType(typeOf(parent.type))))
142
+ return;
143
+ value = parent;
144
+ continue;
145
+ }
146
+ if (!(ts.isJsxFragment(parent) ||
147
+ (ts.isJsxExpression(parent) &&
148
+ !parent.dotDotDotToken &&
149
+ (ts.isJsxAttribute(parent.parent) || !text(value))) ||
150
+ ts.isParenthesizedExpression(parent) ||
151
+ (ts.isConditionalExpression(parent) && parent.condition !== value) ||
152
+ (ts.isBinaryExpression(parent) &&
153
+ parent.right === value &&
154
+ (parent.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken ||
155
+ parent.operatorToken.kind === ts.SyntaxKind.QuestionQuestionToken))))
156
+ return;
157
+ value = parent;
158
+ }
159
+ }
160
+ // Logical operators can return the original value. Follow them to the test
161
+ // that consumes it, or to a rendered && whose left operand only gates its right.
162
+ function testsPresence(value, rendered) {
163
+ for (;;) {
164
+ const parent = value.parent;
165
+ if (ts.isBinaryExpression(parent)) {
166
+ const op = parent.operatorToken.kind;
167
+ if (op === ts.SyntaxKind.AmpersandAmpersandToken || op === ts.SyntaxKind.BarBarToken) {
168
+ if (op === ts.SyntaxKind.AmpersandAmpersandToken &&
169
+ parent.left === value &&
170
+ rendered(parent))
171
+ return true;
172
+ }
173
+ else {
174
+ const other = parent.left === value ? parent.right : parent.left;
175
+ return (other.kind === ts.SyntaxKind.NullKeyword &&
176
+ (op === ts.SyntaxKind.EqualsEqualsToken ||
177
+ op === ts.SyntaxKind.ExclamationEqualsToken ||
178
+ op === ts.SyntaxKind.EqualsEqualsEqualsToken ||
179
+ op === ts.SyntaxKind.ExclamationEqualsEqualsToken));
180
+ }
181
+ }
182
+ else if (!ts.isParenthesizedExpression(parent)) {
183
+ return ((ts.isPrefixUnaryExpression(parent) &&
184
+ parent.operator === ts.SyntaxKind.ExclamationToken) ||
185
+ (ts.isConditionalExpression(parent) && parent.condition === value) ||
186
+ (ts.isIfStatement(parent) && parent.expression === value));
187
+ }
188
+ value = parent;
189
+ }
190
+ }
191
+ // A hook can require a different renderer before the receiver reaches its template.
192
+ function callsHook(node, checker) {
193
+ if (!ts.isCallExpression(node))
194
+ return false;
195
+ let callee = skipWrappers(node.expression);
196
+ const seen = new Set();
197
+ while (ts.isIdentifier(callee)) {
198
+ if (hookName.test(callee.text))
199
+ return true;
200
+ const decl = checker.getSymbolAtLocation(callee)?.declarations?.[0];
201
+ if (!decl || seen.has(decl))
202
+ return false;
203
+ if (ts.isImportSpecifier(decl))
204
+ return hookName.test((decl.propertyName ?? decl.name).text);
205
+ if (!ts.isVariableDeclaration(decl) ||
206
+ !(ts.getCombinedNodeFlags(decl) & ts.NodeFlags.Const) ||
207
+ !decl.initializer)
208
+ return false;
209
+ seen.add(decl);
210
+ callee = skipWrappers(decl.initializer);
211
+ }
212
+ if (ts.isPropertyAccessExpression(callee))
213
+ return hookName.test(callee.name.text);
214
+ return (ts.isElementAccessExpression(callee) &&
215
+ ts.isStringLiteralLike(callee.argumentExpression) &&
216
+ hookName.test(callee.argumentExpression.text));
217
+ }
218
+ export function literalJsxAttribute(attr) {
219
+ if (!ts.isJsxAttribute(attr))
220
+ return false;
221
+ const value = attr.initializer;
222
+ return (!value ||
223
+ ts.isStringLiteral(value) ||
224
+ (ts.isJsxExpression(value) &&
225
+ (value.expression?.kind === ts.SyntaxKind.TrueKeyword ||
226
+ value.expression?.kind === ts.SyntaxKind.FalseKeyword)));
227
+ }
228
+ function standaloneWrite(node) {
229
+ return (ts.isBinaryExpression(node) &&
230
+ node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
231
+ ts.isExpressionStatement(node.parent));
232
+ }
233
+ /** Follow one stored SVG value to its only runtime read, inside the same component body. */
234
+ export function svgLocalReads(checker, uses, typeOf, reactNode) {
235
+ const reads = new Map();
236
+ const eligibleBodies = new Map();
237
+ function outlet(decl, name) {
238
+ const list = decl.parent;
239
+ if (!ts.isVariableDeclarationList(list) ||
240
+ !(list.flags & (ts.NodeFlags.Let | ts.NodeFlags.Const)) ||
241
+ list.flags & ts.NodeFlags.Using ||
242
+ !ts.isVariableStatement(list.parent))
243
+ return;
244
+ const body = list.parent.parent;
245
+ if (!ts.isBlock(body) || !ts.isFunctionLike(body.parent))
246
+ return;
247
+ const fn = body.parent;
248
+ const annotation = decl.type ?? ts.getJSDocType(decl);
249
+ if (annotation && !renderType(typeOf(annotation)) && !reactNode(annotation))
250
+ return;
251
+ let read;
252
+ for (const ref of renderUses(uses(name))) {
253
+ if (ts.findAncestor(ref, ts.isFunctionLike) !== fn)
254
+ return;
255
+ if (standaloneWrite(ref.parent) && ref.parent.left === ref)
256
+ continue;
257
+ if (read)
258
+ return;
259
+ read = ref;
260
+ }
261
+ if (!read)
262
+ return;
263
+ if (!eligibleBodies.has(body)) {
264
+ const last = body.statements.at(-1);
265
+ // Early returns can split mutable locals across compiler-generated functions.
266
+ eligibleBodies.set(body, !!last &&
267
+ ts.isReturnStatement(last) &&
268
+ !contains(fn, (node) => callsHook(node, checker) ||
269
+ (ts.isReturnStatement(node) &&
270
+ node !== last &&
271
+ ts.findAncestor(node, ts.isFunctionLike) === fn)));
272
+ }
273
+ return eligibleBodies.get(body) ? read : undefined;
274
+ }
275
+ return (element) => {
276
+ let value = element;
277
+ while (ts.isParenthesizedExpression(value.parent))
278
+ value = value.parent;
279
+ const parent = value.parent;
280
+ // A chained assignment or call can receive the element independently of the local.
281
+ const name = ts.isVariableDeclaration(parent) && parent.initializer === value
282
+ ? parent.name
283
+ : standaloneWrite(parent) && parent.right === value
284
+ ? parent.left
285
+ : undefined;
286
+ if (!name || !ts.isIdentifier(name))
287
+ return;
288
+ // Literal props have the same value at assignment and at the render outlet.
289
+ if (!element.attributes.properties.every(literalJsxAttribute))
290
+ return;
291
+ const symbol = checker.getSymbolAtLocation(name);
292
+ const decl = symbol?.valueDeclaration;
293
+ if (!decl ||
294
+ symbol.declarations?.length !== 1 ||
295
+ !ts.isVariableDeclaration(decl) ||
296
+ !ts.isIdentifier(decl.name))
297
+ return;
298
+ if (!reads.has(decl))
299
+ reads.set(decl, outlet(decl, decl.name));
300
+ return reads.get(decl);
301
+ };
302
+ }
303
+ /** The returned native tree must own the element before its props can reach another renderer. */
304
+ export function returnedTextReceiver(tag, types, uses) {
305
+ const element = ts.isJsxOpeningElement(tag) ? tag.parent : tag;
306
+ const owner = nativeOutputOwner(element, () => false, types);
307
+ return owner &&
308
+ ts.isFunctionDeclaration(owner) &&
309
+ !owner.asteriskToken &&
310
+ !owner.modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword) &&
311
+ ts.isSourceFile(owner.parent) &&
312
+ owner.name &&
313
+ ts.findAncestor(element, ts.isFunctionLike) === owner &&
314
+ uses(owner.name).every((ref) => (ts.isJsxOpeningElement(ref.parent) ||
315
+ ts.isJsxClosingElement(ref.parent) ||
316
+ ts.isJsxSelfClosingElement(ref.parent)) &&
317
+ ref.parent.tagName === ref)
318
+ ? owner
319
+ : undefined;
320
+ }
321
+ /** A competing prop, custom element, or spread can change which receiver observes the value. */
322
+ export function closedForwardedAttribute(attr) {
323
+ return attr.parent.properties.every((a) => ts.isJsxAttribute(a) &&
324
+ ts.isIdentifier(a.name) &&
325
+ !['key', 'ref', 'children', '__proto__', 'is'].includes(a.name.text) &&
326
+ (a === attr || a.name.text !== attr.name.getText()));
327
+ }
328
+ /** Prove where a selected component delivers every reference to an incoming prop. */
329
+ export function forwardedProps(file, checker, destination, uses = localUses(file, checker), ordinaryProps = !hasPrototypeAccess(file), textPath) {
330
+ // Direct eval may read any local or reassign a component declaration by spelling alone.
331
+ if (((destination !== 'text' || textPath) &&
332
+ (/@jsx(?:ImportSource|Runtime|Frag)?\b/i.test(file.text) || !ordinaryProps)) ||
333
+ hasEval(file))
334
+ return () => false;
335
+ const references = destination === 'text' ? uses : (id) => renderUses(uses(id));
336
+ const directOnly = new Map();
337
+ const withSiblings = new Map();
338
+ function check(symbol, prop, allowSiblings, depth) {
339
+ const decl = symbol.valueDeclaration;
340
+ if (destination !== 'text' && destination.imported && symbol.declarations?.length === 1) {
341
+ const item = symbol.declarations[0];
342
+ const clause = ts.isImportClause(item)
343
+ ? item
344
+ : ts.isImportSpecifier(item) && !item.isTypeOnly
345
+ ? item.parent.parent
346
+ : null;
347
+ if (clause) {
348
+ if (clause.isTypeOnly ||
349
+ clause.parent.attributes ||
350
+ !ts.isStringLiteral(clause.parent.moduleSpecifier))
351
+ return false;
352
+ const name = ts.isImportSpecifier(item) ? (item.propertyName ?? item.name).text : 'default';
353
+ return destination.imported(clause.parent.moduleSpecifier.text, name, prop, depth);
354
+ }
355
+ }
356
+ if (!decl || decl.getSourceFile() !== file || symbol.declarations?.length !== 1)
357
+ return false;
358
+ const fn = ts.isFunctionDeclaration(decl)
359
+ ? decl
360
+ : ts.isVariableDeclaration(decl) &&
361
+ ts.getCombinedNodeFlags(decl) & ts.NodeFlags.Const &&
362
+ decl.initializer &&
363
+ (ts.isArrowFunction(decl.initializer) || ts.isFunctionExpression(decl.initializer))
364
+ ? decl.initializer
365
+ : null;
366
+ if (!fn?.body || fn.parameters.length !== 1)
367
+ return false;
368
+ // A function declaration can be reassigned. Be conservative about uses of its binding.
369
+ if ((ts.isFunctionDeclaration(decl) ||
370
+ (destination !== 'text' && ts.isVariableDeclaration(decl))) &&
371
+ decl.name &&
372
+ ts.isIdentifier(decl.name) &&
373
+ !references(decl.name).every((ref) => {
374
+ const p = ref.parent;
375
+ if (destination !== 'text' && (ts.isExportSpecifier(p) || ts.isExportAssignment(p)))
376
+ return true;
377
+ return ((ts.isJsxOpeningElement(p) ||
378
+ ts.isJsxClosingElement(p) ||
379
+ ts.isJsxSelfClosingElement(p)) &&
380
+ p.tagName === ref);
381
+ }))
382
+ return false;
383
+ const binding = incoming(fn.parameters[0], prop);
384
+ if (!binding)
385
+ return false;
386
+ // The arguments object is another way to read the original props.
387
+ if (contains(fn, (node) => (ts.isIdentifier(node) && node.text === 'arguments') ||
388
+ ((destination !== 'text' || !!textPath) && callsHook(node, checker))))
389
+ return false;
390
+ const refs = references(binding.name);
391
+ if (textPath) {
392
+ if (binding.bag || !refs.length)
393
+ return false;
394
+ return refs.every((ref) => {
395
+ const expr = ref.parent;
396
+ if (!ts.isJsxExpression(expr) || !ts.isJsxAttribute(expr.parent))
397
+ return false;
398
+ const attr = expr.parent;
399
+ const tag = attr.parent.parent;
400
+ if (!ts.isIdentifier(attr.name) || returnedTextReceiver(tag, textPath, uses) !== fn)
401
+ return false;
402
+ if (hostAttribute(attr, attr.name.text))
403
+ return true;
404
+ if (!textPath.next ||
405
+ !ts.isIdentifier(tag.tagName) ||
406
+ !/^[A-Z]/.test(tag.tagName.text) ||
407
+ !closedForwardedAttribute(attr))
408
+ return false;
409
+ const declarations = checker.getSymbolAtLocation(tag.tagName)?.declarations;
410
+ const spec = declarations?.length === 1 ? declarations[0] : undefined;
411
+ if (!spec || !ts.isImportSpecifier(spec) || spec.isTypeOnly)
412
+ return false;
413
+ const clause = spec.parent.parent;
414
+ const imp = clause.parent;
415
+ return (!clause.isTypeOnly &&
416
+ !imp.attributes &&
417
+ ts.isStringLiteral(imp.moduleSpecifier) &&
418
+ textPath.next(imp.moduleSpecifier.text, (spec.propertyName ?? spec.name).text, attr.name.text));
419
+ });
420
+ }
421
+ if (destination !== 'text') {
422
+ // Direct destructuring prevents another call from observing this slot through the props object.
423
+ if (binding.bag ||
424
+ fn.modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword) ||
425
+ fn.asteriskToken)
426
+ return false;
427
+ const rendered = (node) => nativeOutputOwner(node, (tag, slot) => accepts(checker.getSymbolAtLocation(tag), slot, true, depth + 1), destination) === fn;
428
+ let hasRender = false;
429
+ for (const ref of refs) {
430
+ if (!destination.text(ref) && rendered(ref)) {
431
+ hasRender = true;
432
+ continue;
433
+ }
434
+ // Implicit children can be a truthy render body even when empty.
435
+ // A forwarded slot may have started as children in another component.
436
+ if (depth !== 0 ||
437
+ prop === 'children' ||
438
+ !testsPresence(ref, rendered) ||
439
+ ts.findAncestor(ref, ts.isFunctionLike) !== fn)
440
+ return false;
441
+ }
442
+ return hasRender;
443
+ }
444
+ return (refs.length > 0 &&
445
+ refs.every((ref) => {
446
+ if (!binding.bag)
447
+ return directAttribute(ref);
448
+ const p = ref.parent;
449
+ const key = memberKey(ref);
450
+ if (key !== undefined) {
451
+ if (key === prop)
452
+ return directAttribute(p);
453
+ if (!allowSiblings)
454
+ return false; // An inherited getter can observe the entire receiver.
455
+ if (directAttribute(p))
456
+ return true;
457
+ // A test of a sibling does not expose the formatted prop or pass the bag as `this`.
458
+ const test = p.parent;
459
+ return ((ts.isIfStatement(test) && test.expression === p) ||
460
+ (ts.isConditionalExpression(test) && test.condition === p) ||
461
+ (ts.isBinaryExpression(test) &&
462
+ test.left === p &&
463
+ test.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken));
464
+ }
465
+ return ts.isJsxSpreadAttribute(p) && p.expression === ref && hostAttribute(p, prop);
466
+ }));
467
+ }
468
+ function accepts(symbol, prop, ordinaryCaller = true, depth = 0) {
469
+ // An arbitrarily deep or cyclic chain cannot be proved within this analysis budget.
470
+ if (depth >= 32)
471
+ return false;
472
+ if (!symbol ||
473
+ ['ref', 'key', 'is', '__proto__'].includes(prop) ||
474
+ (destination === 'text' && prop === 'children'))
475
+ return false;
476
+ const allowSiblings = ordinaryCaller && ordinaryProps;
477
+ const proven = allowSiblings ? withSiblings : directOnly;
478
+ let props = proven.get(symbol);
479
+ if (!props)
480
+ proven.set(symbol, (props = new Map()));
481
+ const key = depth ? depth + ':' + prop : prop;
482
+ if (!props.has(key)) {
483
+ props.set(key, false);
484
+ props.set(key, check(symbol, prop, allowSiblings, depth));
485
+ }
486
+ return props.get(key);
487
+ }
488
+ return accepts;
489
+ }
@@ -0,0 +1,6 @@
1
+ export { transformModule } from './transform.js';
2
+ export { runCodemod } from './project.js';
3
+ export { strengthen } from './strong.js';
4
+ export { loadCompiler } from './compiler.js';
5
+ export { createScopedSignalRecipe } from './scoped-signal.js';
6
+ export type * from './types.js';
@@ -2,3 +2,4 @@ export { transformModule } from './transform.js';
2
2
  export { runCodemod } from './project.js';
3
3
  export { strengthen } from './strong.js';
4
4
  export { loadCompiler } from './compiler.js';
5
+ export { createScopedSignalRecipe } from './scoped-signal.js';
@@ -0,0 +1,17 @@
1
+ import ts from 'typescript';
2
+ /** A false-to-true update that returns null on the old pass before retrying this exact prelude. */
3
+ export declare function linkedRenderState(owner: ts.FunctionDeclaration, checker: ts.TypeChecker, uses: (name: ts.Identifier) => ts.Identifier[], isReactState: (expr: ts.Expression) => boolean): {
4
+ state: ts.Statement;
5
+ guard: ts.IfStatement;
6
+ name: string;
7
+ flag: ts.PropertyAccessExpression;
8
+ eager: ts.Expression;
9
+ replay: ts.PropertyAccessExpression[];
10
+ } | {
11
+ state: ts.Statement;
12
+ guard: ts.IfStatement;
13
+ name: string;
14
+ flag: ts.Expression;
15
+ eager: undefined;
16
+ replay: ts.BinaryExpression[];
17
+ } | undefined;
@@ -0,0 +1,126 @@
1
+ import ts from 'typescript';
2
+ import { componentTagOrExport } from './local-uses.js';
3
+ function declaration(statement) {
4
+ if (!ts.isVariableStatement(statement))
5
+ return;
6
+ const list = statement.declarationList;
7
+ if (list.flags & ts.NodeFlags.Const && list.declarations.length === 1)
8
+ return list.declarations[0];
9
+ }
10
+ function negated(expr) {
11
+ return ts.isPrefixUnaryExpression(expr) && expr.operator === ts.SyntaxKind.ExclamationToken
12
+ ? expr.operand
13
+ : undefined;
14
+ }
15
+ function ungroup(expr) {
16
+ return ts.isParenthesizedExpression(expr) ? ungroup(expr.expression) : expr;
17
+ }
18
+ /** A false-to-true update that returns null on the old pass before retrying this exact prelude. */
19
+ export function linkedRenderState(owner, checker, uses, isReactState) {
20
+ if (!owner.body ||
21
+ owner.asteriskToken ||
22
+ owner.modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword) ||
23
+ owner.parameters.length !== 1 ||
24
+ owner.parameters[0].initializer ||
25
+ owner.parameters[0].dotDotDotToken ||
26
+ !ts.isIdentifier(owner.parameters[0].name))
27
+ return;
28
+ const body = owner.body.statements;
29
+ if (body.length !== 3 && body.length !== 4)
30
+ return;
31
+ const [state, guard, output] = body.slice(-3);
32
+ const decl = declaration(state);
33
+ const hook = decl?.initializer;
34
+ if (!decl ||
35
+ decl.type ||
36
+ !hook ||
37
+ !ts.isCallExpression(hook) ||
38
+ hook.questionDotToken ||
39
+ hook.arguments.length !== 1 ||
40
+ hook.typeArguments ||
41
+ !isReactState(hook.expression) ||
42
+ !ts.isArrayBindingPattern(decl.name) ||
43
+ decl.name.elements.length !== 2)
44
+ return;
45
+ const [value, setter] = decl.name.elements;
46
+ if (!ts.isBindingElement(value) ||
47
+ !ts.isIdentifier(value.name) ||
48
+ value.initializer ||
49
+ value.dotDotDotToken ||
50
+ !ts.isBindingElement(setter) ||
51
+ !ts.isIdentifier(setter.name) ||
52
+ setter.initializer ||
53
+ setter.dotDotDotToken)
54
+ return;
55
+ const same = (expr, name) => !!expr &&
56
+ ts.isIdentifier(expr) &&
57
+ checker.getSymbolAtLocation(expr) === checker.getSymbolAtLocation(name);
58
+ if (!ts.isIfStatement(guard) ||
59
+ guard.elseStatement ||
60
+ !ts.isBinaryExpression(guard.expression) ||
61
+ guard.expression.operatorToken.kind !== ts.SyntaxKind.AmpersandAmpersandToken ||
62
+ !same(negated(guard.expression.right), value.name))
63
+ return;
64
+ const branch = guard.thenStatement;
65
+ const update = ts.isBlock(branch) && branch.statements.length === 1 ? branch.statements[0] : branch;
66
+ if (!ts.isExpressionStatement(update) || !ts.isCallExpression(update.expression))
67
+ return;
68
+ const call = update.expression;
69
+ if (!same(call.expression, setter.name) ||
70
+ call.questionDotToken ||
71
+ call.arguments.length !== 1 ||
72
+ call.arguments[0].kind !== ts.SyntaxKind.TrueKeyword ||
73
+ uses(setter.name).length !== 1)
74
+ return;
75
+ const ret = ts.isReturnStatement(output) && output.expression ? ungroup(output.expression) : undefined;
76
+ if (!ret ||
77
+ !ts.isConditionalExpression(ret) ||
78
+ !same(ret.condition, value.name) ||
79
+ ungroup(ret.whenFalse).kind !== ts.SyntaxKind.NullKeyword ||
80
+ uses(value.name).length !== 2)
81
+ return;
82
+ if (owner.name && !uses(owner.name).every(componentTagOrExport))
83
+ return;
84
+ const positive = ungroup(ret.whenTrue);
85
+ if (!ts.isJsxElement(positive) && !ts.isJsxSelfClosingElement(positive))
86
+ return;
87
+ const source = owner.getSourceFile();
88
+ if (/\/[*/]/.test(source.text.slice(state.getStart(source), guard.end)))
89
+ return;
90
+ const parameter = owner.parameters[0].name;
91
+ const prop = (expr) => ts.isPropertyAccessExpression(expr) &&
92
+ !expr.questionDotToken &&
93
+ same(expr.expression, parameter) &&
94
+ ts.isIdentifier(expr.name);
95
+ const flag = ungroup(guard.expression.left);
96
+ const initial = ungroup(hook.arguments[0]);
97
+ if (body.length === 3) {
98
+ const once = negated(initial);
99
+ const read = once && negated(once);
100
+ if (read && prop(read) && prop(flag) && read.name.text === flag.name.text) {
101
+ return { state, guard, name: value.name.text, flag, eager: initial, replay: [read, flag] };
102
+ }
103
+ }
104
+ else {
105
+ const local = declaration(body[0]);
106
+ const prelude = local?.initializer;
107
+ const nonNull = (expr) => ts.isBinaryExpression(expr) &&
108
+ expr.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsToken &&
109
+ prop(expr.left) &&
110
+ expr.right.kind === ts.SyntaxKind.NullKeyword;
111
+ if (local &&
112
+ !local.type &&
113
+ ts.isIdentifier(local.name) &&
114
+ prelude &&
115
+ ts.isBinaryExpression(prelude) &&
116
+ prelude.operatorToken.kind === ts.SyntaxKind.BarBarToken &&
117
+ nonNull(prelude.left) &&
118
+ nonNull(prelude.right) &&
119
+ same(initial, local.name) &&
120
+ same(flag, local.name) &&
121
+ uses(local.name).length === 2 &&
122
+ !/\/[*/]/.test(prelude.getText())) {
123
+ return { state, guard, name: value.name.text, flag, eager: undefined, replay: [prelude] };
124
+ }
125
+ }
126
+ }
@@ -0,0 +1,20 @@
1
+ import ts from 'typescript';
2
+ type Helper = ts.FunctionDeclaration | ts.FunctionExpression | ts.ArrowFunction;
3
+ export type Invocation = {
4
+ call: ts.CallExpression;
5
+ helper: Helper;
6
+ outer?: Invocation;
7
+ };
8
+ export type CallContext = Invocation | null | undefined;
9
+ export declare function localCalls(file: ts.SourceFile, checker: ts.TypeChecker, reassigned: (symbol: ts.Symbol) => boolean): {
10
+ scan: (call: ts.CallExpression) => void;
11
+ targets: Map<ts.CallExpression, Helper>;
12
+ sites: (fn: ts.Node) => ts.CallExpression[];
13
+ returns: (fn: Helper) => ts.Expression[];
14
+ forwardedArgument: (call: ts.CallExpression) => ts.Expression | undefined;
15
+ owner: (node: ts.Node) => Helper | undefined;
16
+ frame: (call: ts.CallExpression, outer?: Invocation) => Invocation;
17
+ enclosing: (context: CallContext, fn: ts.Node | undefined) => Invocation | undefined;
18
+ atRead: (source: CallContext, written: CallContext, read: CallContext) => CallContext | false;
19
+ };
20
+ export {};