@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.
Files changed (40) hide show
  1. package/README.md +140 -12
  2. package/dist/bin.js +2 -2
  3. package/dist/effect-receivers.d.ts +14 -2
  4. package/dist/effect-receivers.js +171 -22
  5. package/dist/expression-wrappers.d.ts +2 -0
  6. package/dist/expression-wrappers.js +19 -0
  7. package/dist/forwarded-props.d.ts +4 -1
  8. package/dist/forwarded-props.js +18 -3
  9. package/dist/linked-state.d.ts +1 -1
  10. package/dist/linked-state.js +7 -3
  11. package/dist/local-react-hook.d.ts +13 -6
  12. package/dist/local-react-hook.js +316 -53
  13. package/dist/local-uses.d.ts +10 -1
  14. package/dist/local-uses.js +87 -5
  15. package/dist/mappings.d.ts +7 -1
  16. package/dist/mappings.js +21 -3
  17. package/dist/path-svg.d.ts +68 -4
  18. package/dist/path-svg.js +162 -56
  19. package/dist/project.js +1589 -147
  20. package/dist/react-intl.d.ts +1 -1
  21. package/dist/react-intl.js +43 -6
  22. package/dist/react-island-sources.d.ts +53 -0
  23. package/dist/react-island-sources.js +979 -0
  24. package/dist/react-renderer.d.ts +2 -0
  25. package/dist/react-renderer.js +94 -0
  26. package/dist/scoped-store-contract.d.ts +9 -2
  27. package/dist/scoped-store-contract.js +209 -33
  28. package/dist/store-hook-uses.d.ts +7 -5
  29. package/dist/store-hook-uses.js +218 -62
  30. package/dist/store-inputs.d.ts +9 -2
  31. package/dist/store-inputs.js +213 -16
  32. package/dist/strong.d.ts +2 -2
  33. package/dist/strong.js +72 -18
  34. package/dist/transform.d.ts +14 -0
  35. package/dist/transform.js +2856 -292
  36. package/dist/types.d.ts +40 -10
  37. package/package.json +4 -3
  38. package/src/path-svg.ts +183 -62
  39. package/templates/octane-path-svg.ts +5 -1
  40. package/templates/react-intl.tsrx +64 -11
@@ -1,6 +1,6 @@
1
- import { nativeOutputOwner, renderUses } from './forwarded-props.js';
2
- import { componentTagOrExport, localUses, portableArguments, scalar } from './local-uses.js';
3
- import { skipWrappers } from './expression-wrappers.js';
1
+ import { hasEval, nativeOutputOwner, renderUses } from './forwarded-props.js';
2
+ import { componentTagOrExport, localSymbol, localUses, portableArguments, scalar, valueReads, } from './local-uses.js';
3
+ import { literalCondition, skipWrappers } from './expression-wrappers.js';
4
4
  import ts from 'typescript';
5
5
  function literal(value) {
6
6
  return (ts.isStringLiteralLike(value) ||
@@ -20,24 +20,22 @@ function literalOptions(value) {
20
20
  literalOptions(p.initializer));
21
21
  return literal(value);
22
22
  }
23
- /** The short-circuit behavior of a literal operand, independent of project types. */
24
- function literalCondition(expr) {
25
- const value = skipWrappers(expr);
26
- if (value.kind === ts.SyntaxKind.NullKeyword || ts.isVoidExpression(value))
27
- return 'nullish';
28
- if (ts.isStringLiteralLike(value))
29
- return value.text.length ? 'truthy' : 'falsy';
30
- if (ts.isNumericLiteral(value))
31
- return Number(value.text) ? 'truthy' : 'falsy';
32
- if (value.kind === ts.SyntaxKind.FalseKeyword)
33
- return 'falsy';
34
- if (value.kind === ts.SyntaxKind.TrueKeyword ||
35
- ts.isArrowFunction(value) ||
36
- ts.isFunctionExpression(value) ||
37
- ts.isClassExpression(value) ||
38
- ts.isArrayLiteralExpression(value) ||
39
- ts.isObjectLiteralExpression(value))
40
- return 'truthy';
23
+ /** A later spread or unknown key may replace the selected own data property. */
24
+ function finalOwnField(source, key) {
25
+ if (key === '__proto__')
26
+ return;
27
+ for (let i = source.properties.length - 1; i >= 0; i--) {
28
+ const field = source.properties[i];
29
+ if (!field.name || !(ts.isIdentifier(field.name) || ts.isStringLiteralLike(field.name)))
30
+ return;
31
+ if (field.name.text !== key)
32
+ continue;
33
+ if (ts.isPropertyAssignment(field))
34
+ return field.initializer;
35
+ if (ts.isShorthandPropertyAssignment(field) && !field.objectAssignmentInitializer)
36
+ return field.name;
37
+ return;
38
+ }
41
39
  }
42
40
  /** A final, defined own property in an inline object prevents its binding default from running. */
43
41
  function suppliesBinding(binding) {
@@ -52,32 +50,83 @@ function suppliesBinding(binding) {
52
50
  const source = skipWrappers(decl.initializer);
53
51
  const key = binding.propertyName ?? binding.name;
54
52
  if (!ts.isObjectLiteralExpression(source) ||
55
- !(ts.isIdentifier(key) || ts.isStringLiteralLike(key)) ||
56
- key.text === '__proto__')
53
+ !(ts.isIdentifier(key) || ts.isStringLiteralLike(key)))
57
54
  return false;
58
- // A later spread or a key we cannot compare may overwrite the value.
59
- for (let i = source.properties.length - 1; i >= 0; i--) {
60
- const field = source.properties[i];
61
- if (!field.name || !(ts.isIdentifier(field.name) || ts.isStringLiteralLike(field.name)))
62
- return false;
63
- if (field.name.text !== key.text)
64
- continue;
65
- if (!ts.isPropertyAssignment(field))
66
- return false;
67
- const value = skipWrappers(field.initializer);
68
- return literal(value) || literalCondition(value) === 'truthy';
55
+ const value = finalOwnField(source, key.text);
56
+ return !!value && (literal(value) || literalCondition(value) === 'truthy');
57
+ }
58
+ /** Only follow one ordinary const record which cannot be changed through another reader. */
59
+ function selectedRecordField(d, checker, uses) {
60
+ const pattern = d.parent;
61
+ const key = d.propertyName ?? d.name;
62
+ if (d.dotDotDotToken ||
63
+ !ts.isObjectBindingPattern(pattern) ||
64
+ !(ts.isIdentifier(key) || ts.isStringLiteralLike(key)))
65
+ return;
66
+ const decl = pattern.parent;
67
+ function constant(node) {
68
+ return (!!node &&
69
+ ts.isVariableDeclaration(node) &&
70
+ !!node.initializer &&
71
+ ts.isVariableDeclarationList(node.parent) &&
72
+ (node.parent.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const);
73
+ }
74
+ if (!constant(decl))
75
+ return;
76
+ let record = skipWrappers(decl.initializer);
77
+ if (ts.isIdentifier(record)) {
78
+ const source = checker.getSymbolAtLocation(record)?.valueDeclaration;
79
+ if (!constant(source) ||
80
+ !ts.isIdentifier(source.name) ||
81
+ source.end > decl.pos ||
82
+ !ts.isVariableStatement(source.parent.parent) ||
83
+ source.parent.parent.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) ||
84
+ !renderUses(uses(source.name)).every((ref) => ref === record))
85
+ return;
86
+ record = skipWrappers(source.initializer);
87
+ }
88
+ if (!ts.isObjectLiteralExpression(record))
89
+ return;
90
+ const file = d.getSourceFile();
91
+ if ((file.text.includes('eval') || file.text.includes('\\u')) && hasEval(file))
92
+ return;
93
+ // A getter or default in an earlier binding could change the selected field.
94
+ for (const entry of pattern.elements) {
95
+ if (entry === d)
96
+ return finalOwnField(record, key.text);
97
+ const earlier = entry.propertyName ?? entry.name;
98
+ if (!ts.isIdentifier(entry.name) ||
99
+ entry.initializer ||
100
+ entry.dotDotDotToken ||
101
+ !(ts.isIdentifier(earlier) || ts.isStringLiteralLike(earlier)) ||
102
+ !finalOwnField(record, earlier.text))
103
+ return;
69
104
  }
70
- return false;
71
105
  }
72
- /** Bare SVG default imports can switch from a component to an asset URL for a .tsrx issuer. */
73
- export function isBareSvgTag(tag, checker, imports) {
74
- if (!ts.isIdentifier(tag) || /^[a-z]|-/.test(tag.text))
106
+ /** A bare SVG's default export can switch from a component to an asset URL for a .tsrx issuer. */
107
+ export function isBareSvgTag(tag, checker, imports, uses) {
108
+ if (!ts.isPropertyAccessExpression(tag) && (!ts.isIdentifier(tag) || /^[a-z]|-/.test(tag.text)))
75
109
  return false;
110
+ function isSvgNamespace(value) {
111
+ value = skipWrappers(value);
112
+ const declaration = ts.isIdentifier(value) && localSymbol(checker, value)?.declarations?.[0];
113
+ return !!declaration && ts.isNamespaceImport(declaration) && imports.has(declaration);
114
+ }
76
115
  const seen = new Set();
77
116
  function fromSvg(value) {
78
117
  value = skipWrappers(value);
79
- if (ts.isConditionalExpression(value))
118
+ if (ts.isPropertyAccessExpression(value))
119
+ return value.name.text === 'default' && isSvgNamespace(value.expression);
120
+ if (ts.isElementAccessExpression(value)) {
121
+ const key = skipWrappers(value.argumentExpression);
122
+ return (ts.isStringLiteralLike(key) && key.text === 'default' && isSvgNamespace(value.expression));
123
+ }
124
+ if (ts.isConditionalExpression(value)) {
125
+ const condition = literalCondition(value.condition);
126
+ if (condition !== undefined)
127
+ return fromSvg(condition === 'truthy' ? value.whenTrue : value.whenFalse);
80
128
  return fromSvg(value.whenTrue) || fromSvg(value.whenFalse);
129
+ }
81
130
  if (ts.isBinaryExpression(value)) {
82
131
  const op = value.operatorToken.kind;
83
132
  const left = literalCondition(value.left);
@@ -92,21 +141,40 @@ export function isBareSvgTag(tag, checker, imports) {
92
141
  }
93
142
  if (!ts.isIdentifier(value))
94
143
  return false;
95
- const symbol = checker.getSymbolAtLocation(value);
144
+ const symbol = localSymbol(checker, value);
96
145
  if (!symbol || seen.has(symbol))
97
146
  return false;
98
147
  seen.add(symbol);
99
148
  const d = symbol.declarations?.[0];
100
149
  if (!d)
101
150
  return false;
102
- if ((ts.isParameter(d) || ts.isBindingElement(d)) && ts.isIdentifier(d.name) && d.initializer)
103
- return !(ts.isBindingElement(d) && suppliesBinding(d)) && fromSvg(d.initializer);
151
+ if (ts.isBindingElement(d) && ts.isIdentifier(d.name)) {
152
+ const key = d.propertyName ?? d.name;
153
+ const pattern = d.parent;
154
+ const declaration = pattern.parent;
155
+ if (!d.dotDotDotToken &&
156
+ (ts.isIdentifier(key) || ts.isStringLiteralLike(key)) &&
157
+ key.text === 'default' &&
158
+ ts.isObjectBindingPattern(pattern) &&
159
+ ts.isVariableDeclaration(declaration) &&
160
+ declaration.initializer &&
161
+ ts.isVariableDeclarationList(declaration.parent) &&
162
+ declaration.parent.flags & ts.NodeFlags.Const &&
163
+ isSvgNamespace(declaration.initializer))
164
+ return true;
165
+ if (d.initializer)
166
+ return !suppliesBinding(d) && fromSvg(d.initializer);
167
+ const field = selectedRecordField(d, checker, uses);
168
+ return !!field && fromSvg(field);
169
+ }
170
+ if (ts.isParameter(d) && ts.isIdentifier(d.name) && d.initializer)
171
+ return fromSvg(d.initializer);
104
172
  if (ts.isVariableDeclaration(d) &&
105
173
  d.initializer &&
106
174
  ts.isVariableDeclarationList(d.parent) &&
107
175
  d.parent.flags & ts.NodeFlags.Const)
108
176
  return fromSvg(d.initializer);
109
- return imports.has(d);
177
+ return !ts.isNamespaceImport(d) && imports.has(d);
110
178
  }
111
179
  return fromSvg(tag);
112
180
  }
@@ -142,6 +210,13 @@ export function storeHookUses(file, checker, isComponent, typeOf) {
142
210
  call.arguments[0] === fn &&
143
211
  reactHook(call.expression, ['useEffect', 'useLayoutEffect']));
144
212
  }
213
+ function sameEffectSetup(call, hook) {
214
+ const effect = ts.findAncestor(call, effectBoundary);
215
+ return (!!effect &&
216
+ ts.isFunctionLike(effect) &&
217
+ effectCallback(effect) &&
218
+ ts.findAncestor(effect.parent, effectBoundary) === ts.findAncestor(hook, effectBoundary));
219
+ }
145
220
  function clickHandler(value, hook) {
146
221
  let outer = value;
147
222
  while (ts.isParenthesizedExpression(outer.parent))
@@ -160,17 +235,64 @@ export function storeHookUses(file, checker, isComponent, typeOf) {
160
235
  text: (node) => scalar(typeOf(node)),
161
236
  }) === ts.findAncestor(hook, ts.isFunctionLike));
162
237
  }
163
- function runsAfterRender(call, hook) {
164
- const fn = ts.findAncestor(call, ts.isFunctionLike);
165
- if (!fn)
238
+ function effectSetup(fn) {
239
+ if (effectCallback(fn))
240
+ return fn;
241
+ let cleanup = fn;
242
+ while (ts.isParenthesizedExpression(cleanup.parent))
243
+ cleanup = cleanup.parent;
244
+ const parent = cleanup.parent;
245
+ const setup = ts.isArrowFunction(parent) && parent.body === cleanup
246
+ ? parent
247
+ : ts.isReturnStatement(parent) && parent.expression === cleanup
248
+ ? ts.findAncestor(parent, ts.isFunctionLike)
249
+ : undefined;
250
+ if (setup && effectCallback(setup))
251
+ return setup;
252
+ }
253
+ function effectBoundary(node) {
254
+ return ts.isFunctionLike(node) || ts.isClassLike(node);
255
+ }
256
+ function effectEventCallback(fn, hook, use) {
257
+ const event = fn.parent;
258
+ if (!ts.isCallExpression(event) ||
259
+ event.questionDotToken ||
260
+ event.arguments[0] !== fn ||
261
+ !reactHook(event.expression, ['useEffectEvent']))
166
262
  return false;
167
- if (effectCallback(fn) || clickHandler(fn, hook))
168
- return true;
169
- const parent = fn.parent;
170
- if (!ts.isReturnStatement(parent) || parent.expression !== fn)
263
+ const declaration = event.parent;
264
+ const owner = ts.findAncestor(hook, ts.isFunctionLike);
265
+ if (!owner ||
266
+ ts.findAncestor(event, effectBoundary) !== owner ||
267
+ ts.findAncestor(use, effectBoundary) !== fn ||
268
+ !ts.isVariableDeclaration(declaration) ||
269
+ declaration.initializer !== event ||
270
+ !ts.isIdentifier(declaration.name) ||
271
+ !(ts.getCombinedNodeFlags(declaration) & ts.NodeFlags.Const))
171
272
  return false;
172
- const setup = ts.findAncestor(parent, ts.isFunctionLike);
173
- return !!setup && effectCallback(setup);
273
+ // Every reference must be a direct call in this component's effects.
274
+ const refs = uses(declaration.name);
275
+ return (refs.length > 0 &&
276
+ refs.every((ref) => {
277
+ const call = ref.parent;
278
+ if (!ts.isCallExpression(call) || call.expression !== ref || call.questionDotToken)
279
+ return false;
280
+ const caller = ts.findAncestor(call, effectBoundary);
281
+ const setup = caller && ts.isFunctionLike(caller) && effectSetup(caller);
282
+ return !!setup && ts.findAncestor(setup.parent, effectBoundary) === owner;
283
+ }));
284
+ }
285
+ function runsAfterRender(use, hook) {
286
+ let owner = use.parent;
287
+ for (let child = use; owner && !ts.isFunctionLike(owner); child = owner, owner = owner.parent) {
288
+ // An instance initializer can run in another renderer if the class escapes.
289
+ if (ts.isPropertyDeclaration(owner) &&
290
+ owner.initializer === child &&
291
+ !(ts.getCombinedModifierFlags(owner) & ts.ModifierFlags.Static))
292
+ return false;
293
+ }
294
+ return (!!owner &&
295
+ (!!effectSetup(owner) || clickHandler(owner, hook) || effectEventCallback(owner, hook, use)));
174
296
  }
175
297
  function componentUses(fn) {
176
298
  if (!isComponent(fn))
@@ -192,8 +314,8 @@ export function storeHookUses(file, checker, isComponent, typeOf) {
192
314
  }
193
315
  const portable = portableArguments(checker, typeOf);
194
316
  function argument(expr, check, allowShared = false) {
195
- return portable(expr, (module, name) => {
196
- const input = check.imported(module, name);
317
+ return portable(expr, (module, name, selection) => {
318
+ const input = check.imported(module, name, selection);
197
319
  // A scalar key can still dispatch to renderer-owned code through the store.
198
320
  return input === 'opaque' ? 'reject' : allowShared && input;
199
321
  });
@@ -293,6 +415,13 @@ export function storeHookUses(file, checker, isComponent, typeOf) {
293
415
  call.arguments.every((arg) => arg === value || plainData(arg) || literalOptions(arg)) &&
294
416
  check.unchangedHelper(statement.moduleSpecifier.text, (item.propertyName ?? item.name).text));
295
417
  }
418
+ function readsStoreDuringRender(value, use, check) {
419
+ return valueReads(value, checker, [], () => false, (node) => ts.isCallExpression(node) &&
420
+ !node.questionDotToken &&
421
+ ts.isIdentifier(node.expression) &&
422
+ checker.getSymbolAtLocation(node.expression)?.declarations?.[0] === check.item &&
423
+ !runsAfterRender(use, node));
424
+ }
296
425
  function bound(name, check) {
297
426
  if (ts.isIdentifier(name))
298
427
  return uses(name).every((use) => safe(use, check));
@@ -314,8 +443,11 @@ export function storeHookUses(file, checker, isComponent, typeOf) {
314
443
  if (ts.isVariableDeclaration(p) &&
315
444
  p.initializer === value &&
316
445
  ts.isVariableDeclarationList(p.parent) &&
317
- p.parent.flags & ts.NodeFlags.Const)
318
- return !p.type && bound(p.name, check);
446
+ p.parent.flags & ts.NodeFlags.Const) {
447
+ return (!p.type &&
448
+ (ts.isIdentifier(p.name) || !readsStoreDuringRender(value, p, check)) &&
449
+ bound(p.name, check));
450
+ }
319
451
  if (ts.isExpressionStatement(p) || ts.isVoidExpression(p) || ts.isTypeOfExpression(p))
320
452
  return true;
321
453
  if (ts.isIfStatement(p) || ts.isWhileStatement(p) || ts.isDoStatement(p))
@@ -342,10 +474,14 @@ export function storeHookUses(file, checker, isComponent, typeOf) {
342
474
  op === ts.SyntaxKind.InKeyword ||
343
475
  op === ts.SyntaxKind.InstanceOfKeyword);
344
476
  }
345
- if (ts.isPropertyAccessExpression(p))
346
- return p.expression === value && safe(p, check);
347
- if (ts.isElementAccessExpression(p))
348
- return p.expression === value && argument(p.argumentExpression, check) && safe(p, check);
477
+ if (ts.isPropertyAccessExpression(p) || ts.isElementAccessExpression(p)) {
478
+ if (p.expression !== value)
479
+ return false;
480
+ // The provider owns the member. Even retrieving it can execute a getter.
481
+ return (!readsStoreDuringRender(value, p, check) &&
482
+ (!ts.isElementAccessExpression(p) || argument(p.argumentExpression, check)) &&
483
+ safe(p, check));
484
+ }
349
485
  if (ts.isArrayLiteralExpression(p) &&
350
486
  ts.isCallExpression(p.parent) &&
351
487
  p.parent.arguments[1] === p &&
@@ -363,6 +499,19 @@ export function storeHookUses(file, checker, isComponent, typeOf) {
363
499
  safe(p, check))
364
500
  return check.descriptors(p.arguments.filter(ts.isJsxSelfClosingElement));
365
501
  const handle = p.arguments[0];
502
+ if (check.retainedArgument &&
503
+ handle &&
504
+ !p.questionDotToken &&
505
+ ts.isPropertyAccessExpression(value) &&
506
+ !value.questionDotToken &&
507
+ (ts.isExpressionStatement(p.parent) || ts.isVoidExpression(p.parent))) {
508
+ const receiver = originalReceiver(value.expression, check, true);
509
+ if (receiver &&
510
+ sameEffectSetup(p, receiver) &&
511
+ p.arguments.slice(1).every((arg) => argument(arg, check)) &&
512
+ sharedHandle(handle, check.retainedArgument) === true)
513
+ return true;
514
+ }
366
515
  return (ts.isPropertyAccessExpression(value) &&
367
516
  !!originalReceiver(value.expression, check) &&
368
517
  !!handle &&
@@ -382,8 +531,15 @@ export function storeHookUses(file, checker, isComponent, typeOf) {
382
531
  }
383
532
  return false;
384
533
  }
385
- return (item, imported, descriptors, unchangedHelper) => {
386
- const check = { item, imported, known: new Map(), descriptors, unchangedHelper };
534
+ return (item, imported, descriptors, unchangedHelper, retainedArgument) => {
535
+ const check = {
536
+ item,
537
+ imported,
538
+ known: new Map(),
539
+ descriptors,
540
+ unchangedHelper,
541
+ retainedArgument,
542
+ };
387
543
  const references = uses(item.name);
388
544
  return (references.length > 0 &&
389
545
  references.every((ref) => {
@@ -1,4 +1,6 @@
1
1
  import ts from 'typescript';
2
+ type Selection = (string | undefined)[];
3
+ export type StoreInputCheck = (module: string, name: string, selection?: Selection) => boolean | 'opaque';
2
4
  type Factory = {
3
5
  module: string;
4
6
  name: string;
@@ -7,10 +9,15 @@ type Factory = {
7
9
  type Opaque = {
8
10
  opaque: true;
9
11
  };
12
+ export declare const resourceHints: Set<string>;
13
+ /** Read const data exported by an original module without executable dependencies. */
14
+ export declare function retainedLiteralExports(file: string): Set<string> | null;
10
15
  /** Resolve application source used by a workspace store consumer. */
11
- export declare function storeSources(root: string, config: ts.CompilerOptions | null, unchanged: (file: string, source: string) => boolean): {
16
+ export declare function storeSources(root: string, config: ts.CompilerOptions | null, unchanged: (file: string, source: string) => boolean, changesRenderer?: (file: string) => boolean): {
12
17
  inputFactory: (from: string, specifier: string, name: string, shared?: (module: string, name: string, from: string) => boolean) => Factory | Opaque | null;
18
+ changedJsxExport: (from: string, module: string, name: string, selection: Selection) => boolean;
13
19
  helperFunction: (from: string, specifier: string, name: string) => boolean;
14
- localFile: (from: string, specifier: string) => string | null;
20
+ localFile: (from: string, specifier: string, allowTsx?: boolean) => string | null;
21
+ exportsConstObject: (file: string, name: string) => boolean;
15
22
  };
16
23
  export {};