babel-plugin-react-compiler 0.0.0-experimental-27e0f40-20241002 → 0.0.0-experimental-7779988-20241007

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/index.js CHANGED
@@ -134442,7 +134442,7 @@ function installTypeConfig(globals, shapes, typeConfig, moduleName, loc) {
134442
134442
  }
134443
134443
  }
134444
134444
  }
134445
- function installReAnimatedTypes(globals, registry) {
134445
+ function getReanimatedModuleType(registry) {
134446
134446
  const frozenHooks = [
134447
134447
  'useFrameCallback',
134448
134448
  'useAnimatedStyle',
@@ -134451,8 +134451,9 @@ function installReAnimatedTypes(globals, registry) {
134451
134451
  'useAnimatedReaction',
134452
134452
  'useWorkletCallback',
134453
134453
  ];
134454
+ const reanimatedType = [];
134454
134455
  for (const hook of frozenHooks) {
134455
- globals.set(
134456
+ reanimatedType.push([
134456
134457
  hook,
134457
134458
  addHook(registry, {
134458
134459
  positionalParams: [],
@@ -134462,12 +134463,12 @@ function installReAnimatedTypes(globals, registry) {
134462
134463
  noAlias: true,
134463
134464
  calleeEffect: exports.Effect.Read,
134464
134465
  hookKind: 'Custom',
134465
- })
134466
- );
134466
+ }),
134467
+ ]);
134467
134468
  }
134468
134469
  const mutableHooks = ['useSharedValue', 'useDerivedValue'];
134469
134470
  for (const hook of mutableHooks) {
134470
- globals.set(
134471
+ reanimatedType.push([
134471
134472
  hook,
134472
134473
  addHook(registry, {
134473
134474
  positionalParams: [],
@@ -134477,8 +134478,8 @@ function installReAnimatedTypes(globals, registry) {
134477
134478
  noAlias: true,
134478
134479
  calleeEffect: exports.Effect.Read,
134479
134480
  hookKind: 'Custom',
134480
- })
134481
- );
134481
+ }),
134482
+ ]);
134482
134483
  }
134483
134484
  const funcs = [
134484
134485
  'withTiming',
@@ -134490,7 +134491,7 @@ function installReAnimatedTypes(globals, registry) {
134490
134491
  'executeOnUIRuntimeSync',
134491
134492
  ];
134492
134493
  for (const fn of funcs) {
134493
- globals.set(
134494
+ reanimatedType.push([
134494
134495
  fn,
134495
134496
  addFunction(registry, [], {
134496
134497
  positionalParams: [],
@@ -134499,9 +134500,10 @@ function installReAnimatedTypes(globals, registry) {
134499
134500
  calleeEffect: exports.Effect.Read,
134500
134501
  returnValueKind: exports.ValueKind.Mutable,
134501
134502
  noAlias: true,
134502
- })
134503
- );
134503
+ }),
134504
+ ]);
134504
134505
  }
134506
+ return addObject(registry, null, reanimatedType);
134505
134507
  }
134506
134508
  const ObjectPropertiesSchema = z
134507
134509
  .record(
@@ -134790,10 +134792,13 @@ class Environment {
134790
134792
  );
134791
134793
  }
134792
134794
  if (config.enableCustomTypeDefinitionForReanimated) {
134793
- installReAnimatedTypes(
134794
- __classPrivateFieldGet(this, _Environment_globals, 'f'),
134795
+ const reanimatedModuleType = getReanimatedModuleType(
134795
134796
  __classPrivateFieldGet(this, _Environment_shapes, 'f')
134796
134797
  );
134798
+ __classPrivateFieldGet(this, _Environment_moduleTypes, 'f').set(
134799
+ REANIMATED_MODULE_NAME,
134800
+ reanimatedModuleType
134801
+ );
134797
134802
  }
134798
134803
  __classPrivateFieldSet(
134799
134804
  this,
@@ -135141,15 +135146,15 @@ class Environment {
135141
135146
  moduleName,
135142
135147
  loc
135143
135148
  ) {
135144
- if (this.config.moduleTypeProvider == null) {
135145
- return null;
135146
- }
135147
135149
  let moduleType = __classPrivateFieldGet(
135148
135150
  this,
135149
135151
  _Environment_moduleTypes,
135150
135152
  'f'
135151
135153
  ).get(moduleName);
135152
135154
  if (moduleType === undefined) {
135155
+ if (this.config.moduleTypeProvider == null) {
135156
+ return null;
135157
+ }
135153
135158
  const unparsedModuleConfig = this.config.moduleTypeProvider(moduleName);
135154
135159
  if (unparsedModuleConfig != null) {
135155
135160
  const parsedModuleConfig = TypeSchema.safeParse(unparsedModuleConfig);
@@ -135193,6 +135198,7 @@ class Environment {
135193
135198
  return DefaultMutatingHook;
135194
135199
  }
135195
135200
  });
135201
+ const REANIMATED_MODULE_NAME = 'react-native-reanimated';
135196
135202
  function isHookName$1(name) {
135197
135203
  return /^use[A-Z0-9]/.test(name);
135198
135204
  }
@@ -137676,6 +137682,7 @@ function addMemoCacheFunctionImportDeclaration(program, moduleName, localName) {
137676
137682
  );
137677
137683
  }
137678
137684
  z.enum(['all_errors', 'critical_errors', 'none']);
137685
+ const CompilerReactTargetSchema = z.enum(['17', '18', '19']);
137679
137686
  z.enum(['infer', 'syntax', 'annotation', 'all']);
137680
137687
  const defaultOptions = {
137681
137688
  compilationMode: 'infer',
@@ -137684,12 +137691,12 @@ const defaultOptions = {
137684
137691
  logger: null,
137685
137692
  gating: null,
137686
137693
  noEmit: false,
137687
- runtimeModule: null,
137688
137694
  eslintSuppressionRules: null,
137689
137695
  flowSuppressions: true,
137690
137696
  ignoreUseNoForget: false,
137691
137697
  sources: filename => filename.indexOf('node_modules') === -1,
137692
137698
  enableReanimatedCheck: true,
137699
+ target: '19',
137693
137700
  };
137694
137701
  function parsePluginOptions(obj) {
137695
137702
  if (obj == null || typeof obj !== 'object') {
@@ -137700,24 +137707,47 @@ function parsePluginOptions(obj) {
137700
137707
  if (typeof value === 'string') {
137701
137708
  value = value.toLowerCase();
137702
137709
  }
137703
- if (key === 'environment') {
137704
- const environmentResult = parseEnvironmentConfig(value);
137705
- if (environmentResult.isErr()) {
137706
- CompilerError.throwInvalidConfig({
137707
- reason:
137708
- 'Error in validating environment config. This is an advanced setting and not meant to be used directly',
137709
- description: environmentResult.unwrapErr().toString(),
137710
- suggestions: null,
137711
- loc: null,
137712
- });
137710
+ if (isCompilerFlag(key)) {
137711
+ switch (key) {
137712
+ case 'environment': {
137713
+ const environmentResult = parseEnvironmentConfig(value);
137714
+ if (environmentResult.isErr()) {
137715
+ CompilerError.throwInvalidConfig({
137716
+ reason:
137717
+ 'Error in validating environment config. This is an advanced setting and not meant to be used directly',
137718
+ description: environmentResult.unwrapErr().toString(),
137719
+ suggestions: null,
137720
+ loc: null,
137721
+ });
137722
+ }
137723
+ parsedOptions[key] = environmentResult.unwrap();
137724
+ break;
137725
+ }
137726
+ case 'target': {
137727
+ parsedOptions[key] = parseTargetConfig(value);
137728
+ break;
137729
+ }
137730
+ default: {
137731
+ parsedOptions[key] = value;
137732
+ }
137713
137733
  }
137714
- parsedOptions[key] = environmentResult.unwrap();
137715
- } else if (isCompilerFlag(key)) {
137716
- parsedOptions[key] = value;
137717
137734
  }
137718
137735
  }
137719
137736
  return Object.assign(Object.assign({}, defaultOptions), parsedOptions);
137720
137737
  }
137738
+ function parseTargetConfig(value) {
137739
+ const parsed = CompilerReactTargetSchema.safeParse(value);
137740
+ if (parsed.success) {
137741
+ return parsed.data;
137742
+ } else {
137743
+ CompilerError.throwInvalidConfig({
137744
+ reason: 'Not a valid target',
137745
+ description: `${fromZodError(parsed.error)}`,
137746
+ suggestions: null,
137747
+ loc: null,
137748
+ });
137749
+ }
137750
+ }
137721
137751
  function isCompilerFlag(s) {
137722
137752
  return hasOwnProperty$1(defaultOptions, s);
137723
137753
  }
@@ -154239,29 +154269,232 @@ function validateNoJSXInTryStatement(fn) {
154239
154269
  throw errors;
154240
154270
  }
154241
154271
  }
154272
+ function collectOptionalChainSidemap(fn) {
154273
+ const context = {
154274
+ blocks: fn.body.blocks,
154275
+ seenOptionals: new Set(),
154276
+ processedInstrsInOptional: new Set(),
154277
+ temporariesReadInOptional: new Map(),
154278
+ hoistableObjects: new Map(),
154279
+ };
154280
+ for (const [_, block] of fn.body.blocks) {
154281
+ if (
154282
+ block.terminal.kind === 'optional' &&
154283
+ !context.seenOptionals.has(block.id)
154284
+ ) {
154285
+ traverseOptionalBlock(block, context, null);
154286
+ }
154287
+ }
154288
+ return {
154289
+ temporariesReadInOptional: context.temporariesReadInOptional,
154290
+ processedInstrsInOptional: context.processedInstrsInOptional,
154291
+ hoistableObjects: context.hoistableObjects,
154292
+ };
154293
+ }
154294
+ function matchOptionalTestBlock(terminal, blocks) {
154295
+ const consequentBlock = assertNonNull(blocks.get(terminal.consequent));
154296
+ if (
154297
+ consequentBlock.instructions.length === 2 &&
154298
+ consequentBlock.instructions[0].value.kind === 'PropertyLoad' &&
154299
+ consequentBlock.instructions[1].value.kind === 'StoreLocal'
154300
+ ) {
154301
+ const propertyLoad = consequentBlock.instructions[0];
154302
+ const storeLocal = consequentBlock.instructions[1].value;
154303
+ const storeLocalInstrId = consequentBlock.instructions[1].id;
154304
+ CompilerError.invariant(
154305
+ propertyLoad.value.object.identifier.id === terminal.test.identifier.id,
154306
+ {
154307
+ reason:
154308
+ '[OptionalChainDeps] Inconsistent optional chaining property load',
154309
+ description: `Test=${printIdentifier(terminal.test.identifier)} PropertyLoad base=${printIdentifier(propertyLoad.value.object.identifier)}`,
154310
+ loc: propertyLoad.loc,
154311
+ }
154312
+ );
154313
+ CompilerError.invariant(
154314
+ storeLocal.value.identifier.id === propertyLoad.lvalue.identifier.id,
154315
+ {
154316
+ reason: '[OptionalChainDeps] Unexpected storeLocal',
154317
+ loc: propertyLoad.loc,
154318
+ }
154319
+ );
154320
+ if (
154321
+ consequentBlock.terminal.kind !== 'goto' ||
154322
+ consequentBlock.terminal.variant !== GotoVariant.Break
154323
+ ) {
154324
+ return null;
154325
+ }
154326
+ const alternate = assertNonNull(blocks.get(terminal.alternate));
154327
+ CompilerError.invariant(
154328
+ alternate.instructions.length === 2 &&
154329
+ alternate.instructions[0].value.kind === 'Primitive' &&
154330
+ alternate.instructions[1].value.kind === 'StoreLocal',
154331
+ {reason: 'Unexpected alternate structure', loc: terminal.loc}
154332
+ );
154333
+ return {
154334
+ consequentId: storeLocal.lvalue.place.identifier.id,
154335
+ property: propertyLoad.value.property,
154336
+ propertyId: propertyLoad.lvalue.identifier.id,
154337
+ storeLocalInstrId: storeLocalInstrId,
154338
+ consequentGoto: consequentBlock.terminal.block,
154339
+ };
154340
+ }
154341
+ return null;
154342
+ }
154343
+ function traverseOptionalBlock(optional, context, outerAlternate) {
154344
+ context.seenOptionals.add(optional.id);
154345
+ const maybeTest = context.blocks.get(optional.terminal.test);
154346
+ let test;
154347
+ let baseObject;
154348
+ if (maybeTest.terminal.kind === 'branch') {
154349
+ CompilerError.invariant(optional.terminal.optional, {
154350
+ reason: '[OptionalChainDeps] Expect base case to be always optional',
154351
+ loc: optional.terminal.loc,
154352
+ });
154353
+ if (
154354
+ maybeTest.instructions.length === 0 ||
154355
+ maybeTest.instructions[0].value.kind !== 'LoadLocal'
154356
+ ) {
154357
+ return null;
154358
+ }
154359
+ const path = [];
154360
+ for (let i = 1; i < maybeTest.instructions.length; i++) {
154361
+ const instrVal = maybeTest.instructions[i].value;
154362
+ const prevInstr = maybeTest.instructions[i - 1];
154363
+ if (
154364
+ instrVal.kind === 'PropertyLoad' &&
154365
+ instrVal.object.identifier.id === prevInstr.lvalue.identifier.id
154366
+ ) {
154367
+ path.push({property: instrVal.property, optional: false});
154368
+ } else {
154369
+ return null;
154370
+ }
154371
+ }
154372
+ CompilerError.invariant(
154373
+ maybeTest.terminal.test.identifier.id ===
154374
+ maybeTest.instructions.at(-1).lvalue.identifier.id,
154375
+ {
154376
+ reason: '[OptionalChainDeps] Unexpected test expression',
154377
+ loc: maybeTest.terminal.loc,
154378
+ }
154379
+ );
154380
+ baseObject = {
154381
+ identifier: maybeTest.instructions[0].value.place.identifier,
154382
+ path: path,
154383
+ };
154384
+ test = maybeTest.terminal;
154385
+ } else if (maybeTest.terminal.kind === 'optional') {
154386
+ const testBlock = context.blocks.get(maybeTest.terminal.fallthrough);
154387
+ if (testBlock.terminal.kind !== 'branch') {
154388
+ CompilerError.throwTodo({
154389
+ reason: `Unexpected terminal kind \`${testBlock.terminal.kind}\` for optional fallthrough block`,
154390
+ loc: maybeTest.terminal.loc,
154391
+ });
154392
+ }
154393
+ const innerOptional = traverseOptionalBlock(
154394
+ maybeTest,
154395
+ context,
154396
+ testBlock.terminal.alternate
154397
+ );
154398
+ if (innerOptional == null) {
154399
+ return null;
154400
+ }
154401
+ if (testBlock.terminal.test.identifier.id !== innerOptional) {
154402
+ return null;
154403
+ }
154404
+ if (!optional.terminal.optional) {
154405
+ context.hoistableObjects.set(
154406
+ optional.id,
154407
+ assertNonNull(context.temporariesReadInOptional.get(innerOptional))
154408
+ );
154409
+ }
154410
+ baseObject = assertNonNull(
154411
+ context.temporariesReadInOptional.get(innerOptional)
154412
+ );
154413
+ test = testBlock.terminal;
154414
+ } else {
154415
+ return null;
154416
+ }
154417
+ if (test.alternate === outerAlternate) {
154418
+ CompilerError.invariant(optional.instructions.length === 0, {
154419
+ reason:
154420
+ '[OptionalChainDeps] Unexpected instructions an inner optional block. ' +
154421
+ 'This indicates that the compiler may be incorrectly concatenating two unrelated optional chains',
154422
+ loc: optional.terminal.loc,
154423
+ });
154424
+ }
154425
+ const matchConsequentResult = matchOptionalTestBlock(test, context.blocks);
154426
+ if (!matchConsequentResult) {
154427
+ return null;
154428
+ }
154429
+ CompilerError.invariant(
154430
+ matchConsequentResult.consequentGoto === optional.terminal.fallthrough,
154431
+ {
154432
+ reason: '[OptionalChainDeps] Unexpected optional goto-fallthrough',
154433
+ description: `${matchConsequentResult.consequentGoto} != ${optional.terminal.fallthrough}`,
154434
+ loc: optional.terminal.loc,
154435
+ }
154436
+ );
154437
+ const load = {
154438
+ identifier: baseObject.identifier,
154439
+ path: [
154440
+ ...baseObject.path,
154441
+ {
154442
+ property: matchConsequentResult.property,
154443
+ optional: optional.terminal.optional,
154444
+ },
154445
+ ],
154446
+ };
154447
+ context.processedInstrsInOptional.add(
154448
+ matchConsequentResult.storeLocalInstrId
154449
+ );
154450
+ context.processedInstrsInOptional.add(test.id);
154451
+ context.temporariesReadInOptional.set(
154452
+ matchConsequentResult.consequentId,
154453
+ load
154454
+ );
154455
+ context.temporariesReadInOptional.set(matchConsequentResult.propertyId, load);
154456
+ return matchConsequentResult.consequentId;
154457
+ }
154242
154458
  function collectHoistablePropertyLoads(
154243
154459
  fn,
154244
154460
  temporaries,
154245
- hoistableFromOptionals
154461
+ hoistableFromOptionals,
154462
+ nestedFnImmutableContext
154246
154463
  ) {
154247
154464
  const registry = new PropertyPathRegistry();
154248
- const nodes = collectNonNullsInBlocks(
154249
- fn,
154250
- temporaries,
154251
- hoistableFromOptionals,
154252
- registry
154465
+ const functionExpressionLoads = collectFunctionExpressionFakeLoads(fn);
154466
+ const actuallyEvaluatedTemporaries = new Map(
154467
+ [...temporaries].filter(([id]) => !functionExpressionLoads.has(id))
154253
154468
  );
154469
+ const knownImmutableIdentifiers = new Set();
154470
+ if (fn.fnType === 'Component' || fn.fnType === 'Hook') {
154471
+ for (const p of fn.params) {
154472
+ if (p.kind === 'Identifier') {
154473
+ knownImmutableIdentifiers.add(p.identifier.id);
154474
+ }
154475
+ }
154476
+ }
154477
+ const nodes = collectNonNullsInBlocks(fn, {
154478
+ temporaries: actuallyEvaluatedTemporaries,
154479
+ knownImmutableIdentifiers: knownImmutableIdentifiers,
154480
+ hoistableFromOptionals: hoistableFromOptionals,
154481
+ registry: registry,
154482
+ nestedFnImmutableContext: nestedFnImmutableContext,
154483
+ });
154254
154484
  propagateNonNull(fn, nodes, registry);
154255
- const nodesKeyedByScopeId = new Map();
154485
+ return nodes;
154486
+ }
154487
+ function keyByScopeId(fn, source) {
154488
+ const keyedByScopeId = new Map();
154256
154489
  for (const [_, block] of fn.body.blocks) {
154257
154490
  if (block.terminal.kind === 'scope') {
154258
- nodesKeyedByScopeId.set(
154491
+ keyedByScopeId.set(
154259
154492
  block.terminal.scope.id,
154260
- nodes.get(block.terminal.block)
154493
+ source.get(block.terminal.block)
154261
154494
  );
154262
154495
  }
154263
154496
  }
154264
- return nodesKeyedByScopeId;
154497
+ return keyedByScopeId;
154265
154498
  }
154266
154499
  class PropertyPathRegistry {
154267
154500
  constructor() {
@@ -154317,39 +154550,45 @@ class PropertyPathRegistry {
154317
154550
  );
154318
154551
  }
154319
154552
  }
154320
- function addNonNullPropertyPath(
154321
- source,
154322
- sourceNode,
154323
- instrId,
154324
- knownImmutableIdentifiers,
154325
- result
154326
- ) {
154327
- const isMutableAtInstr =
154328
- source.mutableRange.end > source.mutableRange.start + 1 &&
154329
- source.scope != null &&
154330
- inRange({id: instrId}, source.scope.range);
154331
- if (
154332
- !isMutableAtInstr ||
154333
- knownImmutableIdentifiers.has(sourceNode.fullPath.identifier.id)
154334
- ) {
154335
- result.add(sourceNode);
154553
+ function getMaybeNonNullInInstruction(instr, context) {
154554
+ var _a, _b, _c;
154555
+ let path = null;
154556
+ if (instr.kind === 'PropertyLoad') {
154557
+ path =
154558
+ (_a = context.temporaries.get(instr.object.identifier.id)) !== null &&
154559
+ _a !== void 0
154560
+ ? _a
154561
+ : {identifier: instr.object.identifier, path: []};
154562
+ } else if (instr.kind === 'Destructure') {
154563
+ path =
154564
+ (_b = context.temporaries.get(instr.value.identifier.id)) !== null &&
154565
+ _b !== void 0
154566
+ ? _b
154567
+ : null;
154568
+ } else if (instr.kind === 'ComputedLoad') {
154569
+ path =
154570
+ (_c = context.temporaries.get(instr.object.identifier.id)) !== null &&
154571
+ _c !== void 0
154572
+ ? _c
154573
+ : null;
154336
154574
  }
154575
+ return path != null ? context.registry.getOrCreateProperty(path) : null;
154337
154576
  }
154338
- function collectNonNullsInBlocks(
154339
- fn,
154340
- temporaries,
154341
- hoistableFromOptionals,
154342
- registry
154343
- ) {
154344
- var _a;
154345
- const knownImmutableIdentifiers = new Set();
154346
- if (fn.fnType === 'Component' || fn.fnType === 'Hook') {
154347
- for (const p of fn.params) {
154348
- if (p.kind === 'Identifier') {
154349
- knownImmutableIdentifiers.add(p.identifier.id);
154350
- }
154351
- }
154577
+ function isImmutableAtInstr(identifier, instr, context) {
154578
+ if (context.nestedFnImmutableContext != null) {
154579
+ return context.nestedFnImmutableContext.has(identifier.id);
154580
+ } else {
154581
+ const mutableAtInstr =
154582
+ identifier.mutableRange.end > identifier.mutableRange.start + 1 &&
154583
+ identifier.scope != null &&
154584
+ inRange({id: instr}, identifier.scope.range);
154585
+ return (
154586
+ !mutableAtInstr || context.knownImmutableIdentifiers.has(identifier.id)
154587
+ );
154352
154588
  }
154589
+ }
154590
+ function collectNonNullsInBlocks(fn, context) {
154591
+ var _a;
154353
154592
  const knownNonNullIdentifiers = new Set();
154354
154593
  if (
154355
154594
  fn.fnType === 'Component' &&
@@ -154357,54 +154596,56 @@ function collectNonNullsInBlocks(
154357
154596
  fn.params[0].kind === 'Identifier'
154358
154597
  ) {
154359
154598
  const identifier = fn.params[0].identifier;
154360
- knownNonNullIdentifiers.add(registry.getOrCreateIdentifier(identifier));
154599
+ knownNonNullIdentifiers.add(
154600
+ context.registry.getOrCreateIdentifier(identifier)
154601
+ );
154361
154602
  }
154362
154603
  const nodes = new Map();
154363
154604
  for (const [_, block] of fn.body.blocks) {
154364
154605
  const assumedNonNullObjects = new Set(knownNonNullIdentifiers);
154365
- const maybeOptionalChain = hoistableFromOptionals.get(block.id);
154606
+ const maybeOptionalChain = context.hoistableFromOptionals.get(block.id);
154366
154607
  if (maybeOptionalChain != null) {
154367
154608
  assumedNonNullObjects.add(
154368
- registry.getOrCreateProperty(maybeOptionalChain)
154609
+ context.registry.getOrCreateProperty(maybeOptionalChain)
154369
154610
  );
154370
154611
  }
154371
154612
  for (const instr of block.instructions) {
154372
- if (instr.value.kind === 'PropertyLoad') {
154373
- const source =
154374
- (_a = temporaries.get(instr.value.object.identifier.id)) !== null &&
154375
- _a !== void 0
154613
+ const maybeNonNull = getMaybeNonNullInInstruction(instr.value, context);
154614
+ if (
154615
+ maybeNonNull != null &&
154616
+ isImmutableAtInstr(maybeNonNull.fullPath.identifier, instr.id, context)
154617
+ ) {
154618
+ assumedNonNullObjects.add(maybeNonNull);
154619
+ }
154620
+ if (
154621
+ instr.value.kind === 'FunctionExpression' &&
154622
+ !fn.env.config.enableTreatFunctionDepsAsConditional
154623
+ ) {
154624
+ const innerFn = instr.value.loweredFunc;
154625
+ const innerTemporaries = collectTemporariesSidemap(
154626
+ innerFn.func,
154627
+ new Set()
154628
+ );
154629
+ const innerOptionals = collectOptionalChainSidemap(innerFn.func);
154630
+ const innerHoistableMap = collectHoistablePropertyLoads(
154631
+ innerFn.func,
154632
+ innerTemporaries,
154633
+ innerOptionals.hoistableObjects,
154634
+ (_a = context.nestedFnImmutableContext) !== null && _a !== void 0
154376
154635
  ? _a
154377
- : {identifier: instr.value.object.identifier, path: []};
154378
- addNonNullPropertyPath(
154379
- instr.value.object.identifier,
154380
- registry.getOrCreateProperty(source),
154381
- instr.id,
154382
- knownImmutableIdentifiers,
154383
- assumedNonNullObjects
154636
+ : new Set(
154637
+ innerFn.func.context
154638
+ .filter(place =>
154639
+ isImmutableAtInstr(place.identifier, instr.id, context)
154640
+ )
154641
+ .map(place => place.identifier.id)
154642
+ )
154384
154643
  );
154385
- } else if (instr.value.kind === 'Destructure') {
154386
- const source = instr.value.value.identifier.id;
154387
- const sourceNode = temporaries.get(source);
154388
- if (sourceNode != null) {
154389
- addNonNullPropertyPath(
154390
- instr.value.value.identifier,
154391
- registry.getOrCreateProperty(sourceNode),
154392
- instr.id,
154393
- knownImmutableIdentifiers,
154394
- assumedNonNullObjects
154395
- );
154396
- }
154397
- } else if (instr.value.kind === 'ComputedLoad') {
154398
- const source = instr.value.object.identifier.id;
154399
- const sourceNode = temporaries.get(source);
154400
- if (sourceNode != null) {
154401
- addNonNullPropertyPath(
154402
- instr.value.object.identifier,
154403
- registry.getOrCreateProperty(sourceNode),
154404
- instr.id,
154405
- knownImmutableIdentifiers,
154406
- assumedNonNullObjects
154407
- );
154644
+ const innerHoistables = assertNonNull(
154645
+ innerHoistableMap.get(innerFn.func.body.entry)
154646
+ );
154647
+ for (const entry of innerHoistables.assumedNonNullObjects) {
154648
+ assumedNonNullObjects.add(entry);
154408
154649
  }
154409
154650
  }
154410
154651
  }
@@ -154542,6 +154783,26 @@ function reduceMaybeOptionalChains(nodes, registry) {
154542
154783
  }
154543
154784
  } while (changed);
154544
154785
  }
154786
+ function collectFunctionExpressionFakeLoads(fn) {
154787
+ const sources = new Map();
154788
+ const functionExpressionReferences = new Set();
154789
+ for (const [_, block] of fn.body.blocks) {
154790
+ for (const {lvalue: lvalue, value: value} of block.instructions) {
154791
+ if (value.kind === 'FunctionExpression') {
154792
+ for (const reference of value.loweredFunc.dependencies) {
154793
+ let curr = reference.identifier.id;
154794
+ while (curr != null) {
154795
+ functionExpressionReferences.add(curr);
154796
+ curr = sources.get(curr);
154797
+ }
154798
+ }
154799
+ } else if (value.kind === 'PropertyLoad') {
154800
+ sources.set(lvalue.identifier.id, value.object.identifier.id);
154801
+ }
154802
+ }
154803
+ }
154804
+ return functionExpressionReferences;
154805
+ }
154545
154806
  var _a,
154546
154807
  _ReactiveScopeDependencyTreeHIR_hoistableObjects,
154547
154808
  _ReactiveScopeDependencyTreeHIR_deps,
@@ -154801,192 +155062,6 @@ function makeOrMergeProperty(node, property, accessType) {
154801
155062
  }
154802
155063
  return child;
154803
155064
  }
154804
- function collectOptionalChainSidemap(fn) {
154805
- const context = {
154806
- blocks: fn.body.blocks,
154807
- seenOptionals: new Set(),
154808
- processedInstrsInOptional: new Set(),
154809
- temporariesReadInOptional: new Map(),
154810
- hoistableObjects: new Map(),
154811
- };
154812
- for (const [_, block] of fn.body.blocks) {
154813
- if (
154814
- block.terminal.kind === 'optional' &&
154815
- !context.seenOptionals.has(block.id)
154816
- ) {
154817
- traverseOptionalBlock(block, context, null);
154818
- }
154819
- }
154820
- return {
154821
- temporariesReadInOptional: context.temporariesReadInOptional,
154822
- processedInstrsInOptional: context.processedInstrsInOptional,
154823
- hoistableObjects: context.hoistableObjects,
154824
- };
154825
- }
154826
- function matchOptionalTestBlock(terminal, blocks) {
154827
- const consequentBlock = assertNonNull(blocks.get(terminal.consequent));
154828
- if (
154829
- consequentBlock.instructions.length === 2 &&
154830
- consequentBlock.instructions[0].value.kind === 'PropertyLoad' &&
154831
- consequentBlock.instructions[1].value.kind === 'StoreLocal'
154832
- ) {
154833
- const propertyLoad = consequentBlock.instructions[0];
154834
- const storeLocal = consequentBlock.instructions[1].value;
154835
- const storeLocalInstrId = consequentBlock.instructions[1].id;
154836
- CompilerError.invariant(
154837
- propertyLoad.value.object.identifier.id === terminal.test.identifier.id,
154838
- {
154839
- reason:
154840
- '[OptionalChainDeps] Inconsistent optional chaining property load',
154841
- description: `Test=${printIdentifier(terminal.test.identifier)} PropertyLoad base=${printIdentifier(propertyLoad.value.object.identifier)}`,
154842
- loc: propertyLoad.loc,
154843
- }
154844
- );
154845
- CompilerError.invariant(
154846
- storeLocal.value.identifier.id === propertyLoad.lvalue.identifier.id,
154847
- {
154848
- reason: '[OptionalChainDeps] Unexpected storeLocal',
154849
- loc: propertyLoad.loc,
154850
- }
154851
- );
154852
- if (
154853
- consequentBlock.terminal.kind !== 'goto' ||
154854
- consequentBlock.terminal.variant !== GotoVariant.Break
154855
- ) {
154856
- return null;
154857
- }
154858
- const alternate = assertNonNull(blocks.get(terminal.alternate));
154859
- CompilerError.invariant(
154860
- alternate.instructions.length === 2 &&
154861
- alternate.instructions[0].value.kind === 'Primitive' &&
154862
- alternate.instructions[1].value.kind === 'StoreLocal',
154863
- {reason: 'Unexpected alternate structure', loc: terminal.loc}
154864
- );
154865
- return {
154866
- consequentId: storeLocal.lvalue.place.identifier.id,
154867
- property: propertyLoad.value.property,
154868
- propertyId: propertyLoad.lvalue.identifier.id,
154869
- storeLocalInstrId: storeLocalInstrId,
154870
- consequentGoto: consequentBlock.terminal.block,
154871
- };
154872
- }
154873
- return null;
154874
- }
154875
- function traverseOptionalBlock(optional, context, outerAlternate) {
154876
- context.seenOptionals.add(optional.id);
154877
- const maybeTest = context.blocks.get(optional.terminal.test);
154878
- let test;
154879
- let baseObject;
154880
- if (maybeTest.terminal.kind === 'branch') {
154881
- CompilerError.invariant(optional.terminal.optional, {
154882
- reason: '[OptionalChainDeps] Expect base case to be always optional',
154883
- loc: optional.terminal.loc,
154884
- });
154885
- if (
154886
- maybeTest.instructions.length === 0 ||
154887
- maybeTest.instructions[0].value.kind !== 'LoadLocal'
154888
- ) {
154889
- return null;
154890
- }
154891
- const path = [];
154892
- for (let i = 1; i < maybeTest.instructions.length; i++) {
154893
- const instrVal = maybeTest.instructions[i].value;
154894
- const prevInstr = maybeTest.instructions[i - 1];
154895
- if (
154896
- instrVal.kind === 'PropertyLoad' &&
154897
- instrVal.object.identifier.id === prevInstr.lvalue.identifier.id
154898
- ) {
154899
- path.push({property: instrVal.property, optional: false});
154900
- } else {
154901
- return null;
154902
- }
154903
- }
154904
- CompilerError.invariant(
154905
- maybeTest.terminal.test.identifier.id ===
154906
- maybeTest.instructions.at(-1).lvalue.identifier.id,
154907
- {
154908
- reason: '[OptionalChainDeps] Unexpected test expression',
154909
- loc: maybeTest.terminal.loc,
154910
- }
154911
- );
154912
- baseObject = {
154913
- identifier: maybeTest.instructions[0].value.place.identifier,
154914
- path: path,
154915
- };
154916
- test = maybeTest.terminal;
154917
- } else if (maybeTest.terminal.kind === 'optional') {
154918
- const testBlock = context.blocks.get(maybeTest.terminal.fallthrough);
154919
- if (testBlock.terminal.kind !== 'branch') {
154920
- CompilerError.throwTodo({
154921
- reason: `Unexpected terminal kind \`${testBlock.terminal.kind}\` for optional fallthrough block`,
154922
- loc: maybeTest.terminal.loc,
154923
- });
154924
- }
154925
- const innerOptional = traverseOptionalBlock(
154926
- maybeTest,
154927
- context,
154928
- testBlock.terminal.alternate
154929
- );
154930
- if (innerOptional == null) {
154931
- return null;
154932
- }
154933
- if (testBlock.terminal.test.identifier.id !== innerOptional) {
154934
- return null;
154935
- }
154936
- if (!optional.terminal.optional) {
154937
- context.hoistableObjects.set(
154938
- optional.id,
154939
- assertNonNull(context.temporariesReadInOptional.get(innerOptional))
154940
- );
154941
- }
154942
- baseObject = assertNonNull(
154943
- context.temporariesReadInOptional.get(innerOptional)
154944
- );
154945
- test = testBlock.terminal;
154946
- } else {
154947
- return null;
154948
- }
154949
- if (test.alternate === outerAlternate) {
154950
- CompilerError.invariant(optional.instructions.length === 0, {
154951
- reason:
154952
- '[OptionalChainDeps] Unexpected instructions an inner optional block. ' +
154953
- 'This indicates that the compiler may be incorrectly concatenating two unrelated optional chains',
154954
- loc: optional.terminal.loc,
154955
- });
154956
- }
154957
- const matchConsequentResult = matchOptionalTestBlock(test, context.blocks);
154958
- if (!matchConsequentResult) {
154959
- return null;
154960
- }
154961
- CompilerError.invariant(
154962
- matchConsequentResult.consequentGoto === optional.terminal.fallthrough,
154963
- {
154964
- reason: '[OptionalChainDeps] Unexpected optional goto-fallthrough',
154965
- description: `${matchConsequentResult.consequentGoto} != ${optional.terminal.fallthrough}`,
154966
- loc: optional.terminal.loc,
154967
- }
154968
- );
154969
- const load = {
154970
- identifier: baseObject.identifier,
154971
- path: [
154972
- ...baseObject.path,
154973
- {
154974
- property: matchConsequentResult.property,
154975
- optional: optional.terminal.optional,
154976
- },
154977
- ],
154978
- };
154979
- context.processedInstrsInOptional.add(
154980
- matchConsequentResult.storeLocalInstrId
154981
- );
154982
- context.processedInstrsInOptional.add(test.id);
154983
- context.temporariesReadInOptional.set(
154984
- matchConsequentResult.consequentId,
154985
- load
154986
- );
154987
- context.temporariesReadInOptional.set(matchConsequentResult.propertyId, load);
154988
- return matchConsequentResult.consequentId;
154989
- }
154990
155065
  var _Context_instances,
154991
155066
  _Context_declarations,
154992
155067
  _Context_reassignments,
@@ -155005,10 +155080,9 @@ function propagateScopeDependenciesHIR(fn) {
155005
155080
  processedInstrsInOptional: processedInstrsInOptional,
155006
155081
  hoistableObjects: hoistableObjects,
155007
155082
  } = collectOptionalChainSidemap(fn);
155008
- const hoistablePropertyLoads = collectHoistablePropertyLoads(
155083
+ const hoistablePropertyLoads = keyByScopeId(
155009
155084
  fn,
155010
- temporaries,
155011
- hoistableObjects
155085
+ collectHoistablePropertyLoads(fn, temporaries, hoistableObjects, null)
155012
155086
  );
155013
155087
  const scopeDeps = collectDependencies(
155014
155088
  fn,
@@ -156148,7 +156222,7 @@ function isFilePartOfSources(sources, filename) {
156148
156222
  return false;
156149
156223
  }
156150
156224
  function compileProgram(program, pass) {
156151
- var _a, _b, _c;
156225
+ var _a, _b;
156152
156226
  if (shouldSkipCompilation(program, pass)) {
156153
156227
  return;
156154
156228
  }
@@ -156159,14 +156233,10 @@ function compileProgram(program, pass) {
156159
156233
  return;
156160
156234
  }
156161
156235
  const useMemoCacheIdentifier = program.scope.generateUidIdentifier('c');
156162
- const moduleName =
156163
- (_a = pass.opts.runtimeModule) !== null && _a !== void 0
156164
- ? _a
156165
- : 'react/compiler-runtime';
156166
156236
  const suppressions = findProgramSuppressions(
156167
156237
  pass.comments,
156168
- (_b = pass.opts.eslintSuppressionRules) !== null && _b !== void 0
156169
- ? _b
156238
+ (_a = pass.opts.eslintSuppressionRules) !== null && _a !== void 0
156239
+ ? _a
156170
156240
  : DEFAULT_ESLINT_SUPPRESSIONS,
156171
156241
  pass.opts.flowSuppressions
156172
156242
  );
@@ -156197,7 +156267,7 @@ function compileProgram(program, pass) {
156197
156267
  },
156198
156268
  Object.assign(Object.assign({}, pass), {
156199
156269
  opts: Object.assign(Object.assign({}, pass.opts), pass.opts),
156200
- filename: (_c = pass.filename) !== null && _c !== void 0 ? _c : null,
156270
+ filename: (_b = pass.filename) !== null && _b !== void 0 ? _b : null,
156201
156271
  })
156202
156272
  );
156203
156273
  const processFn = (fn, fnType) => {
@@ -156405,7 +156475,7 @@ function compileProgram(program, pass) {
156405
156475
  if (needsMemoCacheFunctionImport) {
156406
156476
  updateMemoCacheFunctionImport(
156407
156477
  program,
156408
- moduleName,
156478
+ getReactCompilerRuntimeModule(pass.opts),
156409
156479
  useMemoCacheIdentifier.name
156410
156480
  );
156411
156481
  }
@@ -156413,7 +156483,6 @@ function compileProgram(program, pass) {
156413
156483
  }
156414
156484
  }
156415
156485
  function shouldSkipCompilation(program, pass) {
156416
- var _a;
156417
156486
  if (pass.opts.sources) {
156418
156487
  if (pass.filename === null) {
156419
156488
  const error = new CompilerError();
@@ -156433,11 +156502,12 @@ function shouldSkipCompilation(program, pass) {
156433
156502
  return true;
156434
156503
  }
156435
156504
  }
156436
- const moduleName =
156437
- (_a = pass.opts.runtimeModule) !== null && _a !== void 0
156438
- ? _a
156439
- : 'react/compiler-runtime';
156440
- if (hasMemoCacheFunctionImport(program, moduleName)) {
156505
+ if (
156506
+ hasMemoCacheFunctionImport(
156507
+ program,
156508
+ getReactCompilerRuntimeModule(pass.opts)
156509
+ )
156510
+ ) {
156441
156511
  return true;
156442
156512
  }
156443
156513
  return false;
@@ -156778,6 +156848,28 @@ function checkFunctionReferencedBeforeDeclarationAtTopLevel(program, fns) {
156778
156848
  });
156779
156849
  return errors.details.length > 0 ? errors : null;
156780
156850
  }
156851
+ function getReactCompilerRuntimeModule(opts) {
156852
+ let moduleName = null;
156853
+ switch (opts.target) {
156854
+ case '17':
156855
+ case '18': {
156856
+ moduleName = 'react-compiler-runtime';
156857
+ break;
156858
+ }
156859
+ case '19': {
156860
+ moduleName = 'react/compiler-runtime';
156861
+ break;
156862
+ }
156863
+ default:
156864
+ CompilerError.invariant(moduleName != null, {
156865
+ reason: 'Expected target to already be validated',
156866
+ description: null,
156867
+ loc: null,
156868
+ suggestions: null,
156869
+ });
156870
+ }
156871
+ return moduleName;
156872
+ }
156781
156873
  function hasModule(name) {
156782
156874
  try {
156783
156875
  return !!require.resolve(name);