@octanejs/codemod 0.2.3 → 0.2.4

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/dist/project.js CHANGED
@@ -19,13 +19,52 @@ import { contains, forwardedProps } from './forwarded-props.js';
19
19
  import { importsReactBridge, islandComponents, executableImports, hostedComponents, directReactExports, migratedJsxFactories, octaneRootComponents, reactClientComponents, reactClassExports, reactServerComponents, retainedReactComponentReturns, retainedReactContextExports, retainedReactElementReturns, reverseIslandExports, } from './react-island-sources.js';
20
20
  const SOURCE = /\.(?:[cm]?[jt]sx?)$/;
21
21
  const ADAPTER_OCTANE_VERSION = /^0\.(?:16\.(?:[7-9]|[1-9]\d+)|1[789]\.(?:0|[1-9]\d*))$/;
22
- // React Intl 7.1.14. Both entry points re-export a hook that calls React.useContext.
23
- const INTL_HOOK_FILES = {
22
+ const INTL_ENTRIES = {
24
23
  'index.js': '8f5854dadcd055f066234e13a344bd0b102aea2d50ddfd2f0cf12e9f48116059',
25
24
  'lib/index.js': '208130dc122cbb84cbc0d4cce54fd7f458954f45d2cd5dc505d7263686ca6093',
25
+ };
26
+ // Both React Intl 7.1.14 entries re-export a hook that calls React.useContext.
27
+ const INTL_HOOK_FILES = {
28
+ ...INTL_ENTRIES,
26
29
  'src/components/useIntl.js': '7f26c3c5aebaf874aa2ee7f56952358530c246382f7ca9e729a3ee33d196d6c8',
27
30
  'lib/src/components/useIntl.js': '36bb0433939586c08e79a211372987b4173c73552f75d2ae66b6250bdf1f3b8c',
28
31
  };
32
+ // These are the React wrappers and FormatJS paths that dispatch the two onError calls.
33
+ const INTL_FACTORY_FILES = {
34
+ ...INTL_ENTRIES,
35
+ 'src/components/createIntl.js': 'a94ae5971e93412d91f03d44fdb0412a0593f444d64c373b26bd9b76120e6448',
36
+ 'lib/src/components/createIntl.js': '61ade432d00522e58379cc55fb8d2a7b5aa5e8a2a869638a010f0bb53e679d56',
37
+ };
38
+ const FORMATJS_FACTORY_FILES = {
39
+ 'index.js': '5d533bc4ecb92df151a5778494dc55c529fc433f48c0e3fe0562af5847f27391',
40
+ 'lib/index.js': 'c7a6b9d2c163232345a6b65918385c04ae8dd1a54bc8ab07d77308861c128ec3',
41
+ 'src/create-intl.js': 'e50ab7b2f4adbcfc2639d9f8e7f3a80f365b343650c89f0a9a2572449d8850a7',
42
+ 'lib/src/create-intl.js': 'b198b844cb3aa00893841f401dd50f9797444db417933b4b77049961a350ed3c',
43
+ 'src/message.js': 'bffb1daf92059cfb21f8de7aa19b4e739a94854024c689ae9af00caa4af484f4',
44
+ 'lib/src/message.js': '6a052dd6e84bb6d5dfc15537fb2145e18f4831c240204ddfdd018e72d1bc0840',
45
+ };
46
+ function stockIntlLayout(root, name, version) {
47
+ const installed = readInstalled(root, name);
48
+ if (installed?.manifest.name === name &&
49
+ installed.version === version &&
50
+ !installed.manifest.browser &&
51
+ !installed.manifest.exports &&
52
+ installed.manifest.main === 'index.js' &&
53
+ installed.manifest.module === 'lib/index.js')
54
+ return fs.realpathSync(installed.dir);
55
+ }
56
+ function matchIntlFiles(root, files) {
57
+ return Object.entries(files).every(([name, hash]) => {
58
+ const file = path.join(root, name);
59
+ const stat = fs.lstatSync(file, { throwIfNoEntry: false });
60
+ const stem = file.slice(0, -3);
61
+ return (stat?.isFile() &&
62
+ stat.size < 16_384 &&
63
+ fs.realpathSync(file) === file &&
64
+ !['', '.mjs', '.mts', '.ts', '.jsx', '.tsx', '.tsrx', '.json'].some((extension) => fs.lstatSync(stem + extension, { throwIfNoEntry: false })) &&
65
+ createHash('sha256').update(fs.readFileSync(file)).digest('hex') === hash);
66
+ });
67
+ }
29
68
  /** React packages and the Octane entry point that replaces each. */
30
69
  const CATALOG = { ...DATA.reactPackages, ...PORTED_PACKAGES };
31
70
  const MOTION_BINDING = '@octanejs/motion';
@@ -657,6 +696,7 @@ export async function runCodemod(paths, options = {}) {
657
696
  function initializerResourceHint(from, module, name, retainedOnly = false) {
658
697
  const file = localFile(from, module);
659
698
  if (!file ||
699
+ (retainedOnly && !module.endsWith('.ts') && fs.existsSync(file.slice(0, -3) + '.tsrx')) ||
660
700
  ts.findConfigFile(path.dirname(file), ts.sys.fileExists) !== rootProject ||
661
701
  !installedReact(from))
662
702
  return;
@@ -677,28 +717,52 @@ export async function runCodemod(paths, options = {}) {
677
717
  }
678
718
  const localHooks = new Map();
679
719
  const intlHookPackages = new Map();
680
- function originalIntlHook(file) {
720
+ const intlFactoryPackages = new Map();
721
+ function originalIntlPackage(file) {
681
722
  const types = originalModule(file, 'react-intl');
682
723
  if (types?.packageId?.name !== 'react-intl')
724
+ return;
725
+ try {
726
+ const dir = stockIntlLayout(path.dirname(file), 'react-intl', '7.1.14');
727
+ if (dir?.split(path.sep).includes('node_modules') &&
728
+ fs.realpathSync(types.resolvedFileName) === path.join(dir, 'index.d.ts') &&
729
+ createRequire(file).resolve('react-intl') === path.join(dir, 'index.js'))
730
+ return dir;
731
+ }
732
+ catch {
733
+ return;
734
+ }
735
+ }
736
+ function originalIntlFactory(file) {
737
+ const dir = originalIntlPackage(file);
738
+ if (!dir)
683
739
  return false;
684
- const installed = readInstalled(path.dirname(file), 'react-intl');
685
- if (installed?.manifest.name !== 'react-intl' ||
686
- installed.version !== '7.1.14' ||
687
- installed.manifest.browser ||
688
- installed.manifest.exports ||
689
- installed.manifest.main !== 'index.js' ||
690
- installed.manifest.module !== 'lib/index.js')
740
+ const cached = intlFactoryPackages.get(dir);
741
+ if (cached !== undefined)
742
+ return cached;
743
+ let verified = false;
744
+ try {
745
+ const core = '@formatjs/intl';
746
+ const wrappers = ['src', 'lib/src'].map((prefix) => path.join(dir, prefix, 'components/createIntl.js'));
747
+ const dependency = stockIntlLayout(path.dirname(wrappers[0]), core, '3.1.8');
748
+ verified =
749
+ !!dependency &&
750
+ wrappers.every((wrapper) => createRequire(wrapper).resolve(core) === path.join(dependency, 'index.js')) &&
751
+ matchIntlFiles(dir, INTL_FACTORY_FILES) &&
752
+ matchIntlFiles(dependency, FORMATJS_FACTORY_FILES);
753
+ }
754
+ catch {
755
+ // An unreadable or incomplete package does not establish the callback behavior.
756
+ }
757
+ intlFactoryPackages.set(dir, verified);
758
+ return verified;
759
+ }
760
+ function originalIntlHook(file) {
761
+ const dir = originalIntlPackage(file);
762
+ if (!dir || readInstalled(path.dirname(file), 'react')?.manifest.name !== 'react')
691
763
  return false;
692
764
  try {
693
- const dir = fs.realpathSync(installed.dir);
694
- const entry = path.join(dir, 'index.js');
695
- const issuer = createRequire(file);
696
- if (!dir.split(path.sep).includes('node_modules') ||
697
- fs.realpathSync(types.resolvedFileName) !== path.join(dir, 'index.d.ts') ||
698
- issuer.resolve('react-intl') !== entry ||
699
- readInstalled(path.dirname(file), 'react')?.manifest.name !== 'react')
700
- return false;
701
- const react = issuer.resolve('react');
765
+ const react = createRequire(file).resolve('react');
702
766
  if (!['src', 'lib/src'].every((prefix) => createRequire(path.join(dir, prefix, 'components/useIntl.js')).resolve('react') ===
703
767
  react))
704
768
  return false;
@@ -1171,6 +1235,7 @@ export async function runCodemod(paths, options = {}) {
1171
1235
  initializerResourceHint: renames.has(file)
1172
1236
  ? (module, name, retainedOnly) => initializerResourceHint(file, module, name, retainedOnly)
1173
1237
  : undefined,
1238
+ originalIntlFactory: () => originalIntlFactory(file),
1174
1239
  localReactHook: renames.has(file)
1175
1240
  ? (module, name, nativeOnly) => localReactHook(file, module, name, nativeOnly)
1176
1241
  : undefined,
package/dist/transform.js CHANGED
@@ -4453,8 +4453,7 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
4453
4453
  return null;
4454
4454
  }
4455
4455
  checkIntlProviderResources(tag) {
4456
- const resourceHint = this.options.initializerResourceHint;
4457
- if (!resourceHint)
4456
+ if (!this.options.initializerResourceHint)
4458
4457
  return;
4459
4458
  // Provider admission already excludes spreads. JSX takes the last duplicate prop.
4460
4459
  const attribute = (name) => tag.attributes.properties.findLast((attr) => ts.isJsxAttribute(attr) && ts.isIdentifier(attr.name) && attr.name.text === name);
@@ -4467,20 +4466,106 @@ function ${this.nativeRefStoreFactory}<T>(initial: T) {
4467
4466
  const handler = callback?.initializer;
4468
4467
  if (!callback || !handler || !ts.isJsxExpression(handler) || !handler.expression)
4469
4468
  return;
4470
- const callee = skipWrappers(handler.expression);
4471
- if (!ts.isIdentifier(callee))
4472
- return;
4473
- const declaration = this.declarationOf(callee);
4474
- const imported = declaration && this.imports.get(declaration);
4475
- if (!declaration ||
4476
- !imported ||
4477
- imported.typeOnly ||
4478
- imported.namespace ||
4479
- ts.findAncestor(declaration, ts.isImportDeclaration)?.attributes)
4469
+ const resource = this.intlCallbackResource(handler.expression);
4470
+ if (resource)
4471
+ this.note(RULE.bindings, 'error', callback, `IntlProvider can call \`${resource.name}\` for an empty locale. The retained callback uses React DOM ${resource.hint} outside the original React server request. Move it into Octane-compiled code or retain the React provider.`);
4472
+ }
4473
+ intlCallbackResource(handler) {
4474
+ const imported = this.directImportedValue(skipWrappers(handler))?.entry;
4475
+ if (!imported)
4480
4476
  return;
4481
- const hint = resourceHint(imported.module, imported.imported, true);
4477
+ const hint = this.options.initializerResourceHint?.(imported.module, imported.imported, true);
4482
4478
  if (hint)
4483
- this.note(RULE.bindings, 'error', callback, `IntlProvider can call \`${imported.imported}\` for an empty locale. The retained callback uses React DOM ${hint} outside the original React server request. Move it into Octane-compiled code or retain the React provider.`);
4479
+ return { name: imported.imported, hint };
4480
+ }
4481
+ checkIntlFactoryResources(setup) {
4482
+ if (!this.options.initializerResourceHint ||
4483
+ !this.options.reactIntlAdapter ||
4484
+ !this.options.automaticReactJsx)
4485
+ return;
4486
+ const firstConst = (statement) => {
4487
+ if (!statement || !ts.isVariableStatement(statement))
4488
+ return;
4489
+ const list = statement.declarationList;
4490
+ if (list.flags & ts.NodeFlags.Const && list.declarations.length === 1)
4491
+ return list.declarations[0];
4492
+ };
4493
+ const formatter = firstConst(setup[0]);
4494
+ if (!formatter || !ts.isIdentifier(formatter.name))
4495
+ return;
4496
+ const factory = formatter.initializer && skipWrappers(formatter.initializer);
4497
+ if (!factory ||
4498
+ !ts.isCallExpression(factory) ||
4499
+ ts.isOptionalChain(factory) ||
4500
+ factory.arguments.length !== 1)
4501
+ return;
4502
+ const binding = this.directImportedValue(skipWrappers(factory.expression))?.entry;
4503
+ if (binding?.module !== 'react-intl' || binding.imported !== 'createIntl')
4504
+ return;
4505
+ const options = skipWrappers(factory.arguments[0]);
4506
+ if (!options || !ts.isObjectLiteralExpression(options))
4507
+ return;
4508
+ const fields = new Map();
4509
+ for (const field of options.properties) {
4510
+ if (!field.name || !(ts.isIdentifier(field.name) || ts.isStringLiteral(field.name)))
4511
+ return;
4512
+ const key = field.name.text;
4513
+ if (key !== 'locale' && key !== 'messages' && key !== 'onError')
4514
+ return;
4515
+ if (fields.has(key))
4516
+ return;
4517
+ if (ts.isPropertyAssignment(field))
4518
+ fields.set(key, skipWrappers(field.initializer));
4519
+ else if (ts.isShorthandPropertyAssignment(field) && !field.objectAssignmentInitializer)
4520
+ fields.set(key, field.name);
4521
+ else
4522
+ return;
4523
+ }
4524
+ const locale = fields.get('locale');
4525
+ const handler = fields.get('onError');
4526
+ if (!locale || !ts.isStringLiteralLike(locale) || !handler)
4527
+ return;
4528
+ const messages = fields.get('messages');
4529
+ if (messages && (!ts.isObjectLiteralExpression(messages) || messages.properties.length))
4530
+ return;
4531
+ if (!this.options.originalIntlFactory?.())
4532
+ return;
4533
+ const resource = this.intlCallbackResource(handler);
4534
+ if (!resource)
4535
+ return;
4536
+ const report = (site, operation) => this.note(RULE.bindings, 'error', site, `${operation} can call \`${resource.name}\`. The retained callback uses React DOM ${resource.hint} outside the original React server request in standalone Octane SSR. Move it into Octane-compiled code or keep the formatting under React.`);
4537
+ if (locale.text === '') {
4538
+ report(factory, 'createIntl with an empty locale');
4539
+ return;
4540
+ }
4541
+ if (!messages)
4542
+ return;
4543
+ const read = firstConst(setup[1]);
4544
+ const format = read?.initializer;
4545
+ const call = format && skipWrappers(format);
4546
+ if (!call ||
4547
+ !ts.isCallExpression(call) ||
4548
+ ts.isOptionalChain(call) ||
4549
+ call.arguments.length !== 1)
4550
+ return;
4551
+ const method = call.expression;
4552
+ if (!ts.isPropertyAccessExpression(method) ||
4553
+ method.name.text !== 'formatMessage' ||
4554
+ !ts.isIdentifier(method.expression) ||
4555
+ this.declarationOf(method.expression) !== formatter)
4556
+ return;
4557
+ const value = skipWrappers(call.arguments[0]);
4558
+ if (!ts.isObjectLiteralExpression(value))
4559
+ return;
4560
+ const [id, extra] = value.properties;
4561
+ if (id &&
4562
+ !extra &&
4563
+ ts.isPropertyAssignment(id) &&
4564
+ (ts.isIdentifier(id.name) || ts.isStringLiteral(id.name)) &&
4565
+ id.name.text === 'id' &&
4566
+ ts.isStringLiteralLike(id.initializer) &&
4567
+ id.initializer.text)
4568
+ report(call, 'formatMessage with a missing translation');
4484
4569
  }
4485
4570
  checkInitializerResourceHints(initializer) {
4486
4571
  const resourceHint = this.options.initializerResourceHint;
@@ -7302,6 +7387,7 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
7302
7387
  let output = this.output(inner);
7303
7388
  if (output === null)
7304
7389
  return null;
7390
+ this.checkIntlFactoryResources(setupStatements);
7305
7391
  const head = this.head(node, body.getStart(this.sf));
7306
7392
  this.markTemplate();
7307
7393
  if (this.startsLine(inner)) {
@@ -7347,6 +7433,7 @@ type ${handler}<T = unknown> = { listener(event: ${event}<T>): void }['listener'
7347
7433
  const output = this.arm(last.expression, checkRoot && terminal === -1);
7348
7434
  if (output === null)
7349
7435
  return null;
7436
+ this.checkIntlFactoryResources(statements.slice(0, first));
7350
7437
  const chain = (from) => {
7351
7438
  let at = from;
7352
7439
  while (at < statements.length - 1 && !guardReturn(statements[at]))
package/dist/types.d.ts CHANGED
@@ -68,6 +68,8 @@ export interface TransformOptions {
68
68
  } | null;
69
69
  /** Relative path to the project-local React Intl adapter, when its installed peers support it. */
70
70
  reactIntlAdapter?: string;
71
+ /** Whether the installed React Intl factory and FormatJS error paths match the supported release. */
72
+ originalIntlFactory?(): boolean;
71
73
  /** Whether a directly imported, selected component forwards this prop only to text attributes. */
72
74
  forwardedText?(module: string, exported: string, prop: string, ordinaryCaller?: boolean, through?: boolean): boolean;
73
75
  /** Whether this selected component accepts one explicit element, with separate review for path aliases. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octanejs/codemod",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=22.22.2"
@@ -47,7 +47,7 @@
47
47
  "dependencies": {
48
48
  "@octanejs/cli": "0.3.6",
49
49
  "@tsrx/oxc": "0.23.0",
50
- "octane": "0.19.1",
50
+ "octane": "0.19.2",
51
51
  "parse5": "8.0.1",
52
52
  "picocolors": "^1.1.1",
53
53
  "typescript": "^5.9.3"