@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,4 +1,4 @@
1
1
  import type { TransformOptions } from './types.js';
2
2
  import ts from 'typescript';
3
3
  /** Select whole named imports that the shared adapter can replace without changing renderer-owned values. */
4
- export declare function intlImports(file: ts.SourceFile, importedText: TransformOptions['forwardedText'], checker: ts.TypeChecker, typeOf: (expression: ts.Expression) => ts.Type, isComponent: (functionNode: ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression) => boolean, inReact: (element: ts.JsxElement | ts.JsxSelfClosingElement) => boolean, canPassToReact: (element: ts.JsxElement | ts.JsxSelfClosingElement) => boolean, returnedReceiver: (tag: ts.JsxOpeningLikeElement) => boolean, originalReact: () => boolean): Set<ts.ImportSpecifier>;
4
+ export declare function intlImports(file: ts.SourceFile, importedText: TransformOptions['forwardedText'], checker: ts.TypeChecker, typeOf: (expression: ts.Expression) => ts.Type, isComponent: (functionNode: ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression) => boolean, inReact: (element: ts.JsxElement | ts.JsxSelfClosingElement) => boolean, canPassToReact: (element: ts.JsxElement | ts.JsxSelfClosingElement) => boolean, returnedReceiver: (tag: ts.JsxOpeningLikeElement) => boolean, originalReact: () => boolean, reactHook: (callee: ts.Expression) => string | null, unknownFormatter: (call: ts.CallExpression, method: string) => void): Set<ts.ImportSpecifier>;
@@ -1,4 +1,5 @@
1
1
  import { closedForwardedAttribute, forwardedProps, hasEval, hasPrototypeAccess, renderUses, } from './forwarded-props.js';
2
+ import { isWrapper } from './expression-wrappers.js';
2
3
  import { localUses, portableArguments, scalar } from './local-uses.js';
3
4
  import ts from 'typescript';
4
5
  const scalarMethods = new Set([
@@ -11,8 +12,27 @@ const scalarMethods = new Set([
11
12
  'formatDisplayName',
12
13
  ]);
13
14
  const descriptor = new Set(['id', 'description', 'defaultMessage', 'ignoreTag']);
15
+ function isEagerFunction(node, reactHook) {
16
+ if (!(ts.isArrowFunction(node) || ts.isFunctionExpression(node)) ||
17
+ node.asteriskToken ||
18
+ node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword))
19
+ return false;
20
+ let expression = node;
21
+ while (isWrapper(expression.parent))
22
+ expression = expression.parent;
23
+ const call = expression.parent;
24
+ if (!ts.isCallExpression(call))
25
+ return false;
26
+ if (call.expression === expression)
27
+ return true;
28
+ if (call.arguments[0] !== expression || ts.isOptionalChain(call))
29
+ return false;
30
+ const hook = reactHook(call.expression);
31
+ return ((hook === 'useState' && call.arguments.length === 1) ||
32
+ (hook === 'useMemo' && call.arguments.length === 2 && !ts.isSpreadElement(call.arguments[1])));
33
+ }
14
34
  /** Select whole named imports that the shared adapter can replace without changing renderer-owned values. */
15
- export function intlImports(file, importedText, checker, typeOf, isComponent, inReact, canPassToReact, returnedReceiver, originalReact) {
35
+ export function intlImports(file, importedText, checker, typeOf, isComponent, inReact, canPassToReact, returnedReceiver, originalReact, reactHook, unknownFormatter) {
16
36
  const candidates = file.statements.filter(ts.isImportDeclaration).flatMap((decl) => {
17
37
  const named = decl.importClause?.namedBindings;
18
38
  if (!ts.isStringLiteral(decl.moduleSpecifier) ||
@@ -165,7 +185,8 @@ export function intlImports(file, importedText, checker, typeOf, isComponent, in
165
185
  return scalar(typeOf(source));
166
186
  }));
167
187
  }
168
- function callsScalarMethod(receiver) {
188
+ const nativeFormatters = new Map();
189
+ function callsScalarMethod(receiver, owner) {
169
190
  const access = receiver.parent;
170
191
  if (!ts.isPropertyAccessExpression(access) ||
171
192
  access.expression !== receiver ||
@@ -174,8 +195,18 @@ export function intlImports(file, importedText, checker, typeOf, isComponent, in
174
195
  const call = access.parent;
175
196
  if (!ts.isCallExpression(call) || call.expression !== access)
176
197
  return false;
177
- if (scalarMethods.has(access.name.text))
198
+ if (scalarMethods.has(access.name.text)) {
199
+ // React runs inline calls, memo calculators and lazy state initializers during render.
200
+ if (!owner.asteriskToken &&
201
+ !owner.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword) &&
202
+ ts.findAncestor(call, (node) => node === owner.body ||
203
+ (ts.isFunctionLike(node) && !isEagerFunction(node, reactHook)) ||
204
+ ts.isClassLike(node) ||
205
+ ts.isInterfaceDeclaration(node) ||
206
+ ts.isTypeNode(node)) === owner.body)
207
+ nativeFormatters.set(call, access.name.text);
178
208
  return true;
209
+ }
179
210
  if (access.name.text !== 'formatMessage' ||
180
211
  call.arguments.length < 1 ||
181
212
  call.arguments.length > 3 ||
@@ -188,7 +219,10 @@ export function intlImports(file, importedText, checker, typeOf, isComponent, in
188
219
  ts.isStringLiteralLike(field.initializer))))
189
220
  return false;
190
221
  const values = call.arguments[1];
191
- return !values || scalarValues(values);
222
+ if (values && !scalarValues(values))
223
+ return false;
224
+ nativeFormatters.set(call, access.name.text);
225
+ return true;
192
226
  }
193
227
  function localeView(owner, decl, name) {
194
228
  const body = owner.body?.statements;
@@ -298,7 +332,7 @@ export function intlImports(file, importedText, checker, typeOf, isComponent, in
298
332
  ts.isArrowFunction(owner)) ||
299
333
  !isComponent(owner))
300
334
  return false;
301
- if (callsScalarMethod(call))
335
+ if (callsScalarMethod(call, owner))
302
336
  return true;
303
337
  const access = call.parent;
304
338
  const directLocale = ts.isPropertyAccessExpression(access) &&
@@ -314,7 +348,7 @@ export function intlImports(file, importedText, checker, typeOf, isComponent, in
314
348
  let name;
315
349
  if (ts.isIdentifier(decl.name)) {
316
350
  if (!directLocale)
317
- return uses(decl.name).every(callsScalarMethod);
351
+ return uses(decl.name).every((ref) => callsScalarMethod(ref, owner));
318
352
  name = decl.name;
319
353
  }
320
354
  else {
@@ -345,6 +379,7 @@ export function intlImports(file, importedText, checker, typeOf, isComponent, in
345
379
  // Direct eval can read the imported hook or its formatter through an untracked name.
346
380
  if (hasEval(file))
347
381
  continue;
382
+ nativeFormatters.clear();
348
383
  const proof = locations.length === 1
349
384
  ? readsIntl(locations[0], true)
350
385
  : locations.every((id) => readsIntl(id));
@@ -352,6 +387,8 @@ export function intlImports(file, importedText, checker, typeOf, isComponent, in
352
387
  selected.add(item);
353
388
  if (proof !== true)
354
389
  selected.add(proof);
390
+ for (const [call, method] of nativeFormatters)
391
+ unknownFormatter(call, method);
355
392
  }
356
393
  continue;
357
394
  }
@@ -0,0 +1,53 @@
1
+ export declare function importsReactBridge(source: string): boolean;
2
+ /** Direct React class exports, including an alias in a local export list. */
3
+ export declare function reactClassExports(file: string, source: string): Set<string>;
4
+ /** Direct module-level const exports initialized by the real React context factory. */
5
+ export declare function retainedReactContextExports(file: string, source: string): Map<string, boolean>;
6
+ /** Original imported components handed to an authored React island. */
7
+ export declare function islandComponents(file: string, source: string, original: (module: string) => boolean): Promise<string[]>;
8
+ /** Runtime imports that an explicit React host returns directly. */
9
+ export declare function hostedComponents(file: string, source: string, bridge: 'octane/react' | 'octane/react/server', selected?: boolean): {
10
+ name: string;
11
+ module: string;
12
+ line: number;
13
+ }[];
14
+ /** Imports rendered directly by a retained component. */
15
+ export declare function retainedReactComponentReturns(file: string, source: string): {
16
+ name: string;
17
+ module: string;
18
+ factory?: true;
19
+ lazy?: true;
20
+ line: number;
21
+ }[];
22
+ /** Named React factories returned by an otherwise retained function. */
23
+ export declare function retainedReactElementReturns(file: string, source: string): {
24
+ name: string;
25
+ module: string;
26
+ line: number;
27
+ }[];
28
+ /** Existing Octane roots that render a directly imported component body. */
29
+ export declare function octaneRootComponents(file: string, source: string): {
30
+ runtime: "octane" | "octane/server";
31
+ module: string;
32
+ name: string;
33
+ line: number;
34
+ }[];
35
+ /** React server calls that render directly imported components. */
36
+ export declare function reactServerComponents(file: string, source: string, original: (module: 'react' | 'react/jsx-runtime') => boolean, selected?: boolean): {
37
+ module: string;
38
+ name: string;
39
+ line: number;
40
+ }[];
41
+ /** Local exports whose original body returns React JSX directly. */
42
+ export declare function directReactExports(file: string, source: string): {
43
+ intrinsics: Set<string>;
44
+ wrappers: Set<string>;
45
+ splitIntrinsicDefault: boolean;
46
+ mayExportThen: boolean;
47
+ };
48
+ /** Factories whose intrinsic JSX return survives as Octane JSX in the emitted file. */
49
+ export declare function migratedJsxFactories(file: string, source: string, emitted: string): Set<string>;
50
+ /** Inspect the emitted local export before assigning a reverse island to a server host. */
51
+ export declare function reverseIslandExports(file: string, source: string, original: (module: string) => boolean): Promise<Set<string>>;
52
+ /** Declaration-level type imports disappear even with verbatimModuleSyntax. */
53
+ export declare function executableImports(file: string, source: string, verbatim: boolean): string[];