babel-plugin-react-compiler 0.0.0-experimental-b719367-20241104 → 0.0.0-experimental-fcabbc1-20241106

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
@@ -136633,8 +136633,8 @@ const ExternalFunctionSchema = z.object({
136633
136633
  const InstrumentationSchema = z
136634
136634
  .object({
136635
136635
  fn: ExternalFunctionSchema,
136636
- gating: ExternalFunctionSchema.nullish(),
136637
- globalGating: z.string().nullish(),
136636
+ gating: ExternalFunctionSchema.nullable(),
136637
+ globalGating: z.string().nullable(),
136638
136638
  })
136639
136639
  .refine(
136640
136640
  opts => opts.gating != null || opts.globalGating != null,
@@ -136655,7 +136655,7 @@ const HookSchema = z.object({
136655
136655
  transitiveMixedData: z.boolean().default(false),
136656
136656
  });
136657
136657
  const EnvironmentConfigSchema = z.object({
136658
- customHooks: z.map(z.string(), HookSchema).optional().default(new Map()),
136658
+ customHooks: z.map(z.string(), HookSchema).default(new Map()),
136659
136659
  moduleTypeProvider: z.nullable(z.function().args(z.string())).default(null),
136660
136660
  customMacros: z.nullable(z.array(MacroSchema)).default(null),
136661
136661
  enableResetCacheOnSourceFileChanges: z.boolean().default(false),
@@ -136664,9 +136664,7 @@ const EnvironmentConfigSchema = z.object({
136664
136664
  enablePreserveExistingManualUseMemo: z.boolean().default(false),
136665
136665
  enableForest: z.boolean().default(false),
136666
136666
  enableUseTypeAnnotations: z.boolean().default(false),
136667
- enablePropagateDepsInHIR: z.boolean().default(false),
136668
- enableOptionalDependencies: z.boolean().default(true),
136669
- inlineJsxTransform: ReactElementSymbolSchema.nullish(),
136667
+ inlineJsxTransform: ReactElementSymbolSchema.nullable().default(null),
136670
136668
  validateHooksUsage: z.boolean().default(true),
136671
136669
  validateRefAccessDuringRender: z.boolean().default(true),
136672
136670
  validateNoSetStateInRender: z.boolean().default(true),
@@ -136677,26 +136675,60 @@ const EnvironmentConfigSchema = z.object({
136677
136675
  validateBlocklistedImports: z.nullable(z.array(z.string())).default(null),
136678
136676
  enableAssumeHooksFollowRulesOfReact: z.boolean().default(true),
136679
136677
  enableTransitivelyFreezeFunctionExpressions: z.boolean().default(true),
136680
- enableEmitFreeze: ExternalFunctionSchema.nullish(),
136681
- enableEmitHookGuards: ExternalFunctionSchema.nullish(),
136678
+ enableEmitFreeze: ExternalFunctionSchema.nullable().default(null),
136679
+ enableEmitHookGuards: ExternalFunctionSchema.nullable().default(null),
136682
136680
  enableInstructionReordering: z.boolean().default(false),
136683
136681
  enableFunctionOutlining: z.boolean().default(true),
136684
136682
  enableJsxOutlining: z.boolean().default(false),
136685
- enableEmitInstrumentForget: InstrumentationSchema.nullish(),
136683
+ enableEmitInstrumentForget: InstrumentationSchema.nullable().default(null),
136686
136684
  assertValidMutableRanges: z.boolean().default(false),
136687
136685
  enableChangeVariableCodegen: z.boolean().default(false),
136688
136686
  enableMemoizationComments: z.boolean().default(false),
136689
136687
  throwUnknownException__testonly: z.boolean().default(false),
136690
- enableSharedRuntime__testonly: z.boolean().default(false),
136691
136688
  enableTreatFunctionDepsAsConditional: z.boolean().default(false),
136692
136689
  disableMemoizationForDebugging: z.boolean().default(false),
136693
- enableChangeDetectionForDebugging: ExternalFunctionSchema.nullish(),
136690
+ enableChangeDetectionForDebugging:
136691
+ ExternalFunctionSchema.nullable().default(null),
136694
136692
  enableCustomTypeDefinitionForReanimated: z.boolean().default(false),
136695
136693
  hookPattern: z.string().nullable().default(null),
136696
- enableTreatRefLikeIdentifiersAsRefs: z.boolean().nullable().default(false),
136697
- lowerContextAccess: ExternalFunctionSchema.nullish(),
136694
+ enableTreatRefLikeIdentifiersAsRefs: z.boolean().default(false),
136695
+ lowerContextAccess: ExternalFunctionSchema.nullable().default(null),
136698
136696
  });
136699
- function parseConfigPragma(pragma) {
136697
+ const testComplexConfigDefaults = {
136698
+ validateNoCapitalizedCalls: [],
136699
+ enableChangeDetectionForDebugging: {
136700
+ source: 'react-compiler-runtime',
136701
+ importSpecifierName: '$structuralCheck',
136702
+ },
136703
+ enableEmitFreeze: {
136704
+ source: 'react-compiler-runtime',
136705
+ importSpecifierName: 'makeReadOnly',
136706
+ },
136707
+ enableEmitInstrumentForget: {
136708
+ fn: {
136709
+ source: 'react-compiler-runtime',
136710
+ importSpecifierName: 'useRenderCounter',
136711
+ },
136712
+ gating: {
136713
+ source: 'react-compiler-runtime',
136714
+ importSpecifierName: 'shouldInstrument',
136715
+ },
136716
+ globalGating: '__DEV__',
136717
+ },
136718
+ enableEmitHookGuards: {
136719
+ source: 'react-compiler-runtime',
136720
+ importSpecifierName: '$dispatcherGuard',
136721
+ },
136722
+ inlineJsxTransform: {
136723
+ elementSymbol: 'react.transitional.element',
136724
+ globalDevVar: 'DEV',
136725
+ },
136726
+ lowerContextAccess: {
136727
+ source: 'react-compiler-runtime',
136728
+ importSpecifierName: 'useContext_withSelector',
136729
+ },
136730
+ };
136731
+ function parseConfigPragmaForTests(pragma) {
136700
136732
  const maybeConfig = {};
136701
136733
  const defaultConfig = EnvironmentConfigSchema.parse({});
136702
136734
  for (const token of pragma.split(' ')) {
@@ -136704,19 +136736,10 @@ function parseConfigPragma(pragma) {
136704
136736
  continue;
136705
136737
  }
136706
136738
  const keyVal = token.slice(1);
136707
- let [key, val] = keyVal.split(':');
136708
- if (key === 'validateNoCapitalizedCalls') {
136709
- maybeConfig[key] = [];
136710
- continue;
136711
- }
136712
- if (
136713
- key === 'enableChangeDetectionForDebugging' &&
136714
- (val === undefined || val === 'true')
136715
- ) {
136716
- maybeConfig[key] = {
136717
- source: 'react-compiler-runtime',
136718
- importSpecifierName: '$structuralCheck',
136719
- };
136739
+ let [key, val = undefined] = keyVal.split(':');
136740
+ const isSet = val === undefined || val === 'true';
136741
+ if (isSet && key in testComplexConfigDefaults) {
136742
+ maybeConfig[key] = testComplexConfigDefaults[key];
136720
136743
  continue;
136721
136744
  }
136722
136745
  if (key === 'customMacros' && val) {
@@ -136730,15 +136753,6 @@ function parseConfigPragma(pragma) {
136730
136753
  props.push({type: 'name', name: elt});
136731
136754
  }
136732
136755
  }
136733
- console.log([
136734
- valSplit[0],
136735
- props
136736
- .map(x => {
136737
- var _a;
136738
- return (_a = x.name) !== null && _a !== void 0 ? _a : '*';
136739
- })
136740
- .join('.'),
136741
- ]);
136742
136756
  maybeConfig[key] = [[valSplit[0], props]];
136743
136757
  }
136744
136758
  continue;
@@ -136747,11 +136761,10 @@ function parseConfigPragma(pragma) {
136747
136761
  continue;
136748
136762
  }
136749
136763
  if (val === undefined || val === 'true') {
136750
- val = true;
136764
+ maybeConfig[key] = true;
136751
136765
  } else {
136752
- val = false;
136766
+ maybeConfig[key] = false;
136753
136767
  }
136754
- maybeConfig[key] = val;
136755
136768
  }
136756
136769
  const config = EnvironmentConfigSchema.safeParse(maybeConfig);
136757
136770
  if (config.success) {
@@ -139841,7 +139854,7 @@ function findContextIdentifiers(func) {
139841
139854
  const currentFn =
139842
139855
  (_a = state.currentFn.at(-1)) !== null && _a !== void 0 ? _a : null;
139843
139856
  if (path.isReferencedIdentifier()) {
139844
- handleIdentifier(currentFn, state.identifiers, path);
139857
+ handleIdentifier$1(currentFn, state.identifiers, path);
139845
139858
  }
139846
139859
  },
139847
139860
  },
@@ -139857,7 +139870,7 @@ function findContextIdentifiers(func) {
139857
139870
  }
139858
139871
  return result;
139859
139872
  }
139860
- function handleIdentifier(currentFn, identifiers, path) {
139873
+ function handleIdentifier$1(currentFn, identifiers, path) {
139861
139874
  const name = path.node.name;
139862
139875
  const binding = path.scope.getBinding(name);
139863
139876
  if (binding == null) {
@@ -141467,6 +141480,28 @@ function inlineJsxTransform(fn, inlineJsxTransformConfig) {
141467
141480
  mapTerminalOperands(block.terminal, place =>
141468
141481
  handlePlace(place, blockId, inlinedJsxDeclarations)
141469
141482
  );
141483
+ if (block.terminal.kind === 'scope') {
141484
+ const scope = block.terminal.scope;
141485
+ for (const dep of scope.dependencies) {
141486
+ dep.identifier = handleIdentifier(
141487
+ dep.identifier,
141488
+ inlinedJsxDeclarations
141489
+ );
141490
+ }
141491
+ for (const [origId, decl] of [...scope.declarations]) {
141492
+ const newDecl = handleIdentifier(
141493
+ decl.identifier,
141494
+ inlinedJsxDeclarations
141495
+ );
141496
+ if (newDecl.id !== origId) {
141497
+ scope.declarations.delete(origId);
141498
+ scope.declarations.set(decl.identifier.id, {
141499
+ identifier: newDecl,
141500
+ scope: decl.scope,
141501
+ });
141502
+ }
141503
+ }
141504
+ }
141470
141505
  }
141471
141506
  reversePostorderBlocks(fn.body);
141472
141507
  markPredecessors(fn.body);
@@ -141773,10 +141808,10 @@ function handlePlace(place, blockId, inlinedJsxDeclarations) {
141773
141808
  inlinedJsxDeclaration == null ||
141774
141809
  inlinedJsxDeclaration.blockIdsToIgnore.has(blockId)
141775
141810
  ) {
141776
- return Object.assign({}, place);
141811
+ return place;
141777
141812
  }
141778
141813
  return Object.assign(Object.assign({}, place), {
141779
- identifier: Object.assign({}, inlinedJsxDeclaration.identifier),
141814
+ identifier: inlinedJsxDeclaration.identifier,
141780
141815
  });
141781
141816
  }
141782
141817
  function handlelValue(lvalue, blockId, inlinedJsxDeclarations) {
@@ -141787,12 +141822,20 @@ function handlelValue(lvalue, blockId, inlinedJsxDeclarations) {
141787
141822
  inlinedJsxDeclaration == null ||
141788
141823
  inlinedJsxDeclaration.blockIdsToIgnore.has(blockId)
141789
141824
  ) {
141790
- return Object.assign({}, lvalue);
141825
+ return lvalue;
141791
141826
  }
141792
141827
  return Object.assign(Object.assign({}, lvalue), {
141793
- identifier: Object.assign({}, inlinedJsxDeclaration.identifier),
141828
+ identifier: inlinedJsxDeclaration.identifier,
141794
141829
  });
141795
141830
  }
141831
+ function handleIdentifier(identifier, inlinedJsxDeclarations) {
141832
+ const inlinedJsxDeclaration = inlinedJsxDeclarations.get(
141833
+ identifier.declarationId
141834
+ );
141835
+ return inlinedJsxDeclaration == null
141836
+ ? identifier
141837
+ : inlinedJsxDeclaration.identifier;
141838
+ }
141796
141839
  function findScopesToMerge(fn) {
141797
141840
  const objectMethodDecls = new Set();
141798
141841
  const mergeScopesBuilder = new DisjointSet();
@@ -142451,7 +142494,7 @@ var _Context_nextScheduleId,
142451
142494
  _Context_catchHandlers,
142452
142495
  _Context_controlFlowStack;
142453
142496
  function buildReactiveFunction(fn) {
142454
- const cx = new Context$3(fn.body);
142497
+ const cx = new Context$2(fn.body);
142455
142498
  const driver = new Driver(cx);
142456
142499
  const body = driver.traverseBlock(cx.block(fn.body.entry));
142457
142500
  return {
@@ -143499,7 +143542,7 @@ class Driver {
143499
143542
  };
143500
143543
  }
143501
143544
  }
143502
- let Context$3 = class Context {
143545
+ let Context$2 = class Context {
143503
143546
  constructor(ir) {
143504
143547
  _Context_nextScheduleId.set(this, 0);
143505
143548
  this.emitted = new Set();
@@ -143879,7 +143922,7 @@ function expandFbtScopeRange(fbtRange, extendWith) {
143879
143922
  );
143880
143923
  }
143881
143924
  }
143882
- var _Context_nextCacheIndex, _Context_declarations$2;
143925
+ var _Context_nextCacheIndex, _Context_declarations$1;
143883
143926
  const MEMO_CACHE_SENTINEL = 'react.memo_cache_sentinel';
143884
143927
  const EARLY_RETURN_SENTINEL = 'react.early_return_sentinel';
143885
143928
  function codegenFunction(
@@ -143887,7 +143930,7 @@ function codegenFunction(
143887
143930
  {uniqueIdentifiers: uniqueIdentifiers, fbtOperands: fbtOperands}
143888
143931
  ) {
143889
143932
  var _a, _b, _c;
143890
- const cx = new Context$2(
143933
+ const cx = new Context$1(
143891
143934
  fn.env,
143892
143935
  (_a = fn.id) !== null && _a !== void 0 ? _a : '[[ anonymous ]]',
143893
143936
  uniqueIdentifiers,
@@ -144052,7 +144095,7 @@ function codegenFunction(
144052
144095
  pruneHoistedContexts(reactiveFunction);
144053
144096
  const identifiers = renameVariables(reactiveFunction);
144054
144097
  const codegen = codegenReactiveFunction(
144055
- new Context$2(
144098
+ new Context$1(
144056
144099
  cx.env,
144057
144100
  (_c = reactiveFunction.id) !== null && _c !== void 0
144058
144101
  ? _c
@@ -144139,10 +144182,10 @@ function convertParameter(param) {
144139
144182
  return t__namespace.restElement(convertIdentifier(param.place.identifier));
144140
144183
  }
144141
144184
  }
144142
- let Context$2 = class Context {
144185
+ let Context$1 = class Context {
144143
144186
  constructor(env, fnName, uniqueIdentifiers, fbtOperands, temporaries = null) {
144144
144187
  _Context_nextCacheIndex.set(this, 0);
144145
- _Context_declarations$2.set(this, new Set());
144188
+ _Context_declarations$1.set(this, new Set());
144146
144189
  this.errors = new CompilerError();
144147
144190
  this.objectMethods = new Map();
144148
144191
  this.synthesizedNames = new Map();
@@ -144167,12 +144210,12 @@ let Context$2 = class Context {
144167
144210
  );
144168
144211
  }
144169
144212
  declare(identifier) {
144170
- __classPrivateFieldGet(this, _Context_declarations$2, 'f').add(
144213
+ __classPrivateFieldGet(this, _Context_declarations$1, 'f').add(
144171
144214
  identifier.declarationId
144172
144215
  );
144173
144216
  }
144174
144217
  hasDeclared(identifier) {
144175
- return __classPrivateFieldGet(this, _Context_declarations$2, 'f').has(
144218
+ return __classPrivateFieldGet(this, _Context_declarations$1, 'f').has(
144176
144219
  identifier.declarationId
144177
144220
  );
144178
144221
  }
@@ -144192,7 +144235,7 @@ let Context$2 = class Context {
144192
144235
  }
144193
144236
  };
144194
144237
  (_Context_nextCacheIndex = new WeakMap()),
144195
- (_Context_declarations$2 = new WeakMap());
144238
+ (_Context_declarations$1 = new WeakMap());
144196
144239
  function codegenBlock(cx, block) {
144197
144240
  const temp = new Map(cx.temp);
144198
144241
  const result = codegenBlockNoReset(cx, block);
@@ -144285,7 +144328,7 @@ function codegenReactiveScope(cx, statements, scope, block) {
144285
144328
  const changeExpressions = [];
144286
144329
  const changeExpressionComments = [];
144287
144330
  const outputComments = [];
144288
- for (const dep of scope.dependencies) {
144331
+ for (const dep of [...scope.dependencies].sort(compareScopeDependency)) {
144289
144332
  const index = cx.nextCacheIndex;
144290
144333
  changeExpressionComments.push(printDependencyComment(dep));
144291
144334
  const comparison = t__namespace.binaryExpression(
@@ -144325,7 +144368,9 @@ function codegenReactiveScope(cx, statements, scope, block) {
144325
144368
  );
144326
144369
  }
144327
144370
  let firstOutputIndex = null;
144328
- for (const [, {identifier: identifier}] of scope.declarations) {
144371
+ for (const [, {identifier: identifier}] of [...scope.declarations].sort(
144372
+ ([, a], [, b]) => compareScopeDeclaration(a, b)
144373
+ )) {
144329
144374
  const index = cx.nextCacheIndex;
144330
144375
  if (firstOutputIndex === null) {
144331
144376
  firstOutputIndex = index;
@@ -145468,7 +145513,7 @@ function codegenInstructionValue(cx, instrValue) {
145468
145513
  pruneUnusedLabels(reactiveFunction);
145469
145514
  pruneUnusedLValues(reactiveFunction);
145470
145515
  const fn = codegenReactiveFunction(
145471
- new Context$2(
145516
+ new Context$1(
145472
145517
  cx.env,
145473
145518
  (_e = reactiveFunction.id) !== null && _e !== void 0
145474
145519
  ? _e
@@ -145667,7 +145712,7 @@ function codegenInstructionValue(cx, instrValue) {
145667
145712
  pruneUnusedLValues(reactiveFunction);
145668
145713
  pruneHoistedContexts(reactiveFunction);
145669
145714
  const fn = codegenReactiveFunction(
145670
- new Context$2(
145715
+ new Context$1(
145671
145716
  cx.env,
145672
145717
  (_g = reactiveFunction.id) !== null && _g !== void 0
145673
145718
  ? _g
@@ -146136,6 +146181,50 @@ function convertIdentifier(identifier) {
146136
146181
  );
146137
146182
  return t__namespace.identifier(identifier.name.value);
146138
146183
  }
146184
+ function compareScopeDependency(a, b) {
146185
+ var _a, _b;
146186
+ CompilerError.invariant(
146187
+ ((_a = a.identifier.name) === null || _a === void 0 ? void 0 : _a.kind) ===
146188
+ 'named' &&
146189
+ ((_b = b.identifier.name) === null || _b === void 0
146190
+ ? void 0
146191
+ : _b.kind) === 'named',
146192
+ {
146193
+ reason: '[Codegen] Expected named identifier for dependency',
146194
+ loc: a.identifier.loc,
146195
+ }
146196
+ );
146197
+ const aName = [
146198
+ a.identifier.name.value,
146199
+ ...a.path.map(entry => `${entry.optional ? '?' : ''}${entry.property}`),
146200
+ ].join('.');
146201
+ const bName = [
146202
+ b.identifier.name.value,
146203
+ ...b.path.map(entry => `${entry.optional ? '?' : ''}${entry.property}`),
146204
+ ].join('.');
146205
+ if (aName < bName) return -1;
146206
+ else if (aName > bName) return 1;
146207
+ else return 0;
146208
+ }
146209
+ function compareScopeDeclaration(a, b) {
146210
+ var _a, _b;
146211
+ CompilerError.invariant(
146212
+ ((_a = a.identifier.name) === null || _a === void 0 ? void 0 : _a.kind) ===
146213
+ 'named' &&
146214
+ ((_b = b.identifier.name) === null || _b === void 0
146215
+ ? void 0
146216
+ : _b.kind) === 'named',
146217
+ {
146218
+ reason: '[Codegen] Expected named identifier for declaration',
146219
+ loc: a.identifier.loc,
146220
+ }
146221
+ );
146222
+ const aName = a.identifier.name.value;
146223
+ const bName = b.identifier.name.value;
146224
+ if (aName < bName) return -1;
146225
+ else if (aName > bName) return 1;
146226
+ else return 0;
146227
+ }
146139
146228
  function extractScopeDeclarationsFromDestructuring(fn) {
146140
146229
  const state = new State$1(fn.env);
146141
146230
  visitReactiveFunction(fn, new Visitor$9(), state);
@@ -147132,1677 +147221,68 @@ let Transform$3 = class Transform extends ReactiveFunctionTransform {
147132
147221
  return {kind: 'keep'};
147133
147222
  }
147134
147223
  };
147135
- var _Node_value, _Node_next;
147136
- function empty() {
147137
- return EMPTY;
147138
- }
147139
- class Node {
147140
- constructor(value, next = EMPTY) {
147141
- _Node_value.set(this, void 0);
147142
- _Node_next.set(this, void 0);
147143
- __classPrivateFieldSet(this, _Node_value, value, 'f');
147144
- __classPrivateFieldSet(this, _Node_next, next, 'f');
147145
- }
147146
- push(value) {
147147
- return new Node(value, this);
147148
- }
147149
- pop() {
147150
- return __classPrivateFieldGet(this, _Node_next, 'f');
147151
- }
147152
- find(fn) {
147153
- return fn(__classPrivateFieldGet(this, _Node_value, 'f'))
147154
- ? true
147155
- : __classPrivateFieldGet(this, _Node_next, 'f').find(fn);
147156
- }
147157
- contains(value) {
147158
- return (
147159
- value === __classPrivateFieldGet(this, _Node_value, 'f') ||
147160
- (__classPrivateFieldGet(this, _Node_next, 'f') !== null &&
147161
- __classPrivateFieldGet(this, _Node_next, 'f').contains(value))
147162
- );
147163
- }
147164
- each(fn) {
147165
- fn(__classPrivateFieldGet(this, _Node_value, 'f'));
147166
- __classPrivateFieldGet(this, _Node_next, 'f').each(fn);
147167
- }
147168
- get value() {
147169
- return __classPrivateFieldGet(this, _Node_value, 'f');
147170
- }
147171
- print(fn) {
147172
- return (
147173
- fn(__classPrivateFieldGet(this, _Node_value, 'f')) +
147174
- __classPrivateFieldGet(this, _Node_next, 'f').print(fn)
147175
- );
147176
- }
147177
- }
147178
- (_Node_value = new WeakMap()), (_Node_next = new WeakMap());
147179
- class Empty {
147180
- push(value) {
147181
- return new Node(value, this);
147182
- }
147183
- pop() {
147184
- return this;
147185
- }
147186
- find(_fn) {
147187
- return false;
147188
- }
147189
- contains(_value) {
147190
- return false;
147191
- }
147192
- each(_fn) {
147193
- return;
147194
- }
147195
- get value() {
147196
- return null;
147197
- }
147198
- print(_) {
147199
- return '';
147200
- }
147224
+ function pruneHoistedContexts(fn) {
147225
+ const hoistedIdentifiers = new Map();
147226
+ visitReactiveFunction(fn, new Visitor$8(), hoistedIdentifiers);
147201
147227
  }
147202
- const EMPTY = new Empty();
147203
- var _ReactiveScopeDependencyTree_instances,
147204
- _ReactiveScopeDependencyTree_roots,
147205
- _ReactiveScopeDependencyTree_getOrCreateRoot,
147206
- _ReactiveScopeDependencyTree_debugImpl;
147207
- class ReactiveScopeDependencyTree {
147208
- constructor() {
147209
- _ReactiveScopeDependencyTree_instances.add(this);
147210
- _ReactiveScopeDependencyTree_roots.set(this, new Map());
147211
- }
147212
- add(dep, inConditional) {
147213
- const {path: path} = dep;
147214
- let currNode = __classPrivateFieldGet(
147215
- this,
147216
- _ReactiveScopeDependencyTree_instances,
147217
- 'm',
147218
- _ReactiveScopeDependencyTree_getOrCreateRoot
147219
- ).call(this, dep.identifier);
147220
- for (const item of path) {
147221
- let currChild = getOrMakeProperty(currNode, item.property);
147222
- const accessType = inConditional
147223
- ? PropertyAccessType$1.ConditionalAccess
147224
- : item.optional
147225
- ? PropertyAccessType$1.OptionalAccess
147226
- : PropertyAccessType$1.UnconditionalAccess;
147227
- currChild.accessType = merge$1(currChild.accessType, accessType);
147228
- currNode = currChild;
147229
- }
147230
- const depType = inConditional
147231
- ? PropertyAccessType$1.ConditionalDependency
147232
- : isOptional$1(currNode.accessType)
147233
- ? PropertyAccessType$1.OptionalDependency
147234
- : PropertyAccessType$1.UnconditionalDependency;
147235
- currNode.accessType = merge$1(currNode.accessType, depType);
147236
- }
147237
- deriveMinimalDependencies() {
147238
- const results = new Set();
147239
- for (const [rootId, rootNode] of __classPrivateFieldGet(
147240
- this,
147241
- _ReactiveScopeDependencyTree_roots,
147242
- 'f'
147243
- ).entries()) {
147244
- const deps = deriveMinimalDependenciesInSubtree(rootNode, null);
147245
- CompilerError.invariant(
147246
- deps.every(
147247
- dep =>
147248
- dep.accessType === PropertyAccessType$1.UnconditionalDependency ||
147249
- dep.accessType == PropertyAccessType$1.OptionalDependency
147250
- ),
147251
- {
147252
- reason:
147253
- '[PropagateScopeDependencies] All dependencies must be reduced to unconditional dependencies.',
147254
- description: null,
147255
- loc: null,
147256
- suggestions: null,
147257
- }
147228
+ let Visitor$8 = class Visitor extends ReactiveFunctionTransform {
147229
+ transformInstruction(instruction, state) {
147230
+ this.visitInstruction(instruction, state);
147231
+ if (
147232
+ instruction.value.kind === 'DeclareContext' &&
147233
+ instruction.value.lvalue.kind === 'HoistedConst'
147234
+ ) {
147235
+ state.set(
147236
+ instruction.value.lvalue.place.identifier.declarationId,
147237
+ InstructionKind.Const
147258
147238
  );
147259
- for (const dep of deps) {
147260
- results.add({identifier: rootId, path: dep.relativePath});
147261
- }
147262
- }
147263
- return results;
147264
- }
147265
- addDepsFromInnerScope(
147266
- depsFromInnerScope,
147267
- innerScopeInConditionalWithinParent,
147268
- checkValidDepIdFn
147269
- ) {
147270
- for (const [id, otherRoot] of __classPrivateFieldGet(
147271
- depsFromInnerScope,
147272
- _ReactiveScopeDependencyTree_roots,
147273
- 'f'
147274
- )) {
147275
- if (!checkValidDepIdFn({identifier: id, path: []})) {
147276
- continue;
147277
- }
147278
- let currRoot = __classPrivateFieldGet(
147279
- this,
147280
- _ReactiveScopeDependencyTree_instances,
147281
- 'm',
147282
- _ReactiveScopeDependencyTree_getOrCreateRoot
147283
- ).call(this, id);
147284
- addSubtree(currRoot, otherRoot, innerScopeInConditionalWithinParent);
147285
- if (!isUnconditional(currRoot.accessType)) {
147286
- currRoot.accessType = isDependency$1(currRoot.accessType)
147287
- ? PropertyAccessType$1.UnconditionalDependency
147288
- : PropertyAccessType$1.UnconditionalAccess;
147289
- }
147239
+ return {kind: 'remove'};
147290
147240
  }
147291
- }
147292
- promoteDepsFromExhaustiveConditionals(trees) {
147293
- CompilerError.invariant(trees.length > 1, {
147294
- reason: 'Expected trees to be at least 2 elements long.',
147295
- description: null,
147296
- loc: null,
147297
- suggestions: null,
147298
- });
147299
- for (const [id, root] of __classPrivateFieldGet(
147300
- this,
147301
- _ReactiveScopeDependencyTree_roots,
147302
- 'f'
147303
- )) {
147304
- const nodesForRootId = mapNonNull(trees, tree => {
147305
- const node = __classPrivateFieldGet(
147306
- tree,
147307
- _ReactiveScopeDependencyTree_roots,
147308
- 'f'
147309
- ).get(id);
147310
- if (node != null && isUnconditional(node.accessType)) {
147311
- return node;
147312
- } else {
147313
- return null;
147314
- }
147315
- });
147316
- if (nodesForRootId) {
147317
- addSubtreeIntersection(
147318
- root.properties,
147319
- nodesForRootId.map(root => root.properties)
147320
- );
147321
- }
147241
+ if (
147242
+ instruction.value.kind === 'DeclareContext' &&
147243
+ instruction.value.lvalue.kind === 'HoistedLet'
147244
+ ) {
147245
+ state.set(
147246
+ instruction.value.lvalue.place.identifier.declarationId,
147247
+ InstructionKind.Let
147248
+ );
147249
+ return {kind: 'remove'};
147322
147250
  }
147323
- }
147324
- printDeps(includeAccesses) {
147325
- let res = [];
147326
- for (const [rootId, rootNode] of __classPrivateFieldGet(
147327
- this,
147328
- _ReactiveScopeDependencyTree_roots,
147329
- 'f'
147330
- ).entries()) {
147331
- const rootResults = printSubtree$1(rootNode, includeAccesses).map(
147332
- result => `${printIdentifier(rootId)}.${result}`
147251
+ if (
147252
+ instruction.value.kind === 'DeclareContext' &&
147253
+ instruction.value.lvalue.kind === 'HoistedFunction'
147254
+ ) {
147255
+ state.set(
147256
+ instruction.value.lvalue.place.identifier.declarationId,
147257
+ InstructionKind.Function
147333
147258
  );
147334
- res.push(rootResults);
147259
+ return {kind: 'remove'};
147335
147260
  }
147336
- return res.flat().join('\n');
147337
- }
147338
- debug() {
147339
- const buf = [`tree() [`];
147340
- for (const [rootId, rootNode] of __classPrivateFieldGet(
147341
- this,
147342
- _ReactiveScopeDependencyTree_roots,
147343
- 'f'
147344
- )) {
147345
- buf.push(`${printIdentifier(rootId)} (${rootNode.accessType}):`);
147346
- __classPrivateFieldGet(
147347
- this,
147348
- _ReactiveScopeDependencyTree_instances,
147349
- 'm',
147350
- _ReactiveScopeDependencyTree_debugImpl
147351
- ).call(this, buf, rootNode, 1);
147261
+ if (
147262
+ instruction.value.kind === 'StoreContext' &&
147263
+ state.has(instruction.value.lvalue.place.identifier.declarationId)
147264
+ ) {
147265
+ const kind = state.get(
147266
+ instruction.value.lvalue.place.identifier.declarationId
147267
+ );
147268
+ return {
147269
+ kind: 'replace',
147270
+ value: {
147271
+ kind: 'instruction',
147272
+ instruction: Object.assign(Object.assign({}, instruction), {
147273
+ value: Object.assign(Object.assign({}, instruction.value), {
147274
+ lvalue: Object.assign(
147275
+ Object.assign({}, instruction.value.lvalue),
147276
+ {kind: kind}
147277
+ ),
147278
+ type: null,
147279
+ kind: 'StoreLocal',
147280
+ }),
147281
+ }),
147282
+ },
147283
+ };
147352
147284
  }
147353
- buf.push(']');
147354
- return buf.length > 2 ? buf.join('\n') : buf.join('');
147355
- }
147356
- }
147357
- (_ReactiveScopeDependencyTree_roots = new WeakMap()),
147358
- (_ReactiveScopeDependencyTree_instances = new WeakSet()),
147359
- (_ReactiveScopeDependencyTree_getOrCreateRoot =
147360
- function _ReactiveScopeDependencyTree_getOrCreateRoot(identifier) {
147361
- let rootNode = __classPrivateFieldGet(
147362
- this,
147363
- _ReactiveScopeDependencyTree_roots,
147364
- 'f'
147365
- ).get(identifier);
147366
- if (rootNode === undefined) {
147367
- rootNode = {
147368
- properties: new Map(),
147369
- accessType: PropertyAccessType$1.UnconditionalAccess,
147370
- };
147371
- __classPrivateFieldGet(
147372
- this,
147373
- _ReactiveScopeDependencyTree_roots,
147374
- 'f'
147375
- ).set(identifier, rootNode);
147376
- }
147377
- return rootNode;
147378
- }),
147379
- (_ReactiveScopeDependencyTree_debugImpl =
147380
- function _ReactiveScopeDependencyTree_debugImpl(buf, node, depth = 0) {
147381
- for (const [property, childNode] of node.properties) {
147382
- buf.push(
147383
- `${' '.repeat(depth)}.${property} (${childNode.accessType}):`
147384
- );
147385
- __classPrivateFieldGet(
147386
- this,
147387
- _ReactiveScopeDependencyTree_instances,
147388
- 'm',
147389
- _ReactiveScopeDependencyTree_debugImpl
147390
- ).call(this, buf, childNode, depth + 1);
147391
- }
147392
- });
147393
- var PropertyAccessType$1;
147394
- (function (PropertyAccessType) {
147395
- PropertyAccessType['ConditionalAccess'] = 'ConditionalAccess';
147396
- PropertyAccessType['OptionalAccess'] = 'OptionalAccess';
147397
- PropertyAccessType['UnconditionalAccess'] = 'UnconditionalAccess';
147398
- PropertyAccessType['ConditionalDependency'] = 'ConditionalDependency';
147399
- PropertyAccessType['OptionalDependency'] = 'OptionalDependency';
147400
- PropertyAccessType['UnconditionalDependency'] = 'UnconditionalDependency';
147401
- })(PropertyAccessType$1 || (PropertyAccessType$1 = {}));
147402
- const MIN_ACCESS_TYPE = PropertyAccessType$1.ConditionalAccess;
147403
- function isUnconditional(access) {
147404
- return (
147405
- access === PropertyAccessType$1.UnconditionalAccess ||
147406
- access === PropertyAccessType$1.UnconditionalDependency
147407
- );
147408
- }
147409
- function isDependency$1(access) {
147410
- return (
147411
- access === PropertyAccessType$1.ConditionalDependency ||
147412
- access === PropertyAccessType$1.OptionalDependency ||
147413
- access === PropertyAccessType$1.UnconditionalDependency
147414
- );
147415
- }
147416
- function isOptional$1(access) {
147417
- return (
147418
- access === PropertyAccessType$1.OptionalAccess ||
147419
- access === PropertyAccessType$1.OptionalDependency
147420
- );
147421
- }
147422
- function merge$1(access1, access2) {
147423
- const resultIsUnconditional =
147424
- isUnconditional(access1) || isUnconditional(access2);
147425
- const resultIsDependency = isDependency$1(access1) || isDependency$1(access2);
147426
- const resultIsOptional = isOptional$1(access1) || isOptional$1(access2);
147427
- if (resultIsUnconditional) {
147428
- if (resultIsDependency) {
147429
- return PropertyAccessType$1.UnconditionalDependency;
147430
- } else {
147431
- return PropertyAccessType$1.UnconditionalAccess;
147432
- }
147433
- } else if (resultIsOptional) {
147434
- if (resultIsDependency) {
147435
- return PropertyAccessType$1.OptionalDependency;
147436
- } else {
147437
- return PropertyAccessType$1.OptionalAccess;
147438
- }
147439
- } else {
147440
- if (resultIsDependency) {
147441
- return PropertyAccessType$1.ConditionalDependency;
147442
- } else {
147443
- return PropertyAccessType$1.ConditionalAccess;
147444
- }
147445
- }
147446
- }
147447
- function promoteResult(accessType, path) {
147448
- const result = {relativePath: [], accessType: accessType};
147449
- if (path !== null) {
147450
- result.relativePath.push(path);
147451
- }
147452
- return [result];
147453
- }
147454
- function prependPath(results, path) {
147455
- if (path === null) {
147456
- return results;
147457
- }
147458
- return results.map(result => ({
147459
- accessType: result.accessType,
147460
- relativePath: [path, ...result.relativePath],
147461
- }));
147462
- }
147463
- function deriveMinimalDependenciesInSubtree(dep, property) {
147464
- const results = [];
147465
- for (const [childName, childNode] of dep.properties) {
147466
- const childResult = deriveMinimalDependenciesInSubtree(
147467
- childNode,
147468
- childName
147469
- );
147470
- results.push(...childResult);
147471
- }
147472
- switch (dep.accessType) {
147473
- case PropertyAccessType$1.UnconditionalDependency: {
147474
- return promoteResult(
147475
- PropertyAccessType$1.UnconditionalDependency,
147476
- property !== null ? {property: property, optional: false} : null
147477
- );
147478
- }
147479
- case PropertyAccessType$1.UnconditionalAccess: {
147480
- if (
147481
- results.every(
147482
- ({accessType: accessType}) =>
147483
- accessType === PropertyAccessType$1.UnconditionalDependency ||
147484
- accessType === PropertyAccessType$1.OptionalDependency
147485
- )
147486
- ) {
147487
- return prependPath(
147488
- results,
147489
- property !== null ? {property: property, optional: false} : null
147490
- );
147491
- } else {
147492
- return promoteResult(
147493
- PropertyAccessType$1.UnconditionalDependency,
147494
- property !== null ? {property: property, optional: false} : null
147495
- );
147496
- }
147497
- }
147498
- case PropertyAccessType$1.OptionalDependency: {
147499
- return promoteResult(
147500
- PropertyAccessType$1.OptionalDependency,
147501
- property !== null ? {property: property, optional: true} : null
147502
- );
147503
- }
147504
- case PropertyAccessType$1.OptionalAccess: {
147505
- if (
147506
- results.every(
147507
- ({accessType: accessType}) =>
147508
- accessType === PropertyAccessType$1.UnconditionalDependency ||
147509
- accessType === PropertyAccessType$1.OptionalDependency
147510
- )
147511
- ) {
147512
- return prependPath(
147513
- results,
147514
- property !== null ? {property: property, optional: true} : null
147515
- );
147516
- } else {
147517
- return promoteResult(
147518
- PropertyAccessType$1.OptionalDependency,
147519
- property !== null ? {property: property, optional: true} : null
147520
- );
147521
- }
147522
- }
147523
- case PropertyAccessType$1.ConditionalAccess:
147524
- case PropertyAccessType$1.ConditionalDependency: {
147525
- if (
147526
- results.every(
147527
- ({accessType: accessType}) =>
147528
- accessType === PropertyAccessType$1.ConditionalDependency
147529
- )
147530
- ) {
147531
- return promoteResult(
147532
- PropertyAccessType$1.ConditionalDependency,
147533
- property !== null ? {property: property, optional: true} : null
147534
- );
147535
- } else {
147536
- return promoteResult(
147537
- PropertyAccessType$1.UnconditionalDependency,
147538
- property !== null ? {property: property, optional: true} : null
147539
- );
147540
- }
147541
- }
147542
- default: {
147543
- assertExhaustive(
147544
- dep.accessType,
147545
- '[PropgateScopeDependencies] Unhandled access type!'
147546
- );
147547
- }
147548
- }
147549
- }
147550
- function demoteSubtreeToConditional(subtree) {
147551
- const stack = [subtree];
147552
- let node;
147553
- while ((node = stack.pop()) !== undefined) {
147554
- const {accessType: accessType, properties: properties} = node;
147555
- if (!isUnconditional(accessType)) {
147556
- continue;
147557
- }
147558
- node.accessType = isDependency$1(accessType)
147559
- ? PropertyAccessType$1.ConditionalDependency
147560
- : PropertyAccessType$1.ConditionalAccess;
147561
- for (const childNode of properties.values()) {
147562
- if (isUnconditional(accessType)) {
147563
- stack.push(childNode);
147564
- }
147565
- }
147566
- }
147567
- }
147568
- function addSubtree(currNode, otherNode, demoteOtherNode) {
147569
- let otherType = otherNode.accessType;
147570
- if (demoteOtherNode) {
147571
- otherType = isDependency$1(otherType)
147572
- ? PropertyAccessType$1.ConditionalDependency
147573
- : PropertyAccessType$1.ConditionalAccess;
147574
- }
147575
- currNode.accessType = merge$1(currNode.accessType, otherType);
147576
- for (const [propertyName, otherChild] of otherNode.properties) {
147577
- const currChild = currNode.properties.get(propertyName);
147578
- if (currChild) {
147579
- addSubtree(currChild, otherChild, demoteOtherNode);
147580
- } else {
147581
- if (demoteOtherNode) {
147582
- demoteSubtreeToConditional(otherChild);
147583
- }
147584
- currNode.properties.set(propertyName, otherChild);
147585
- }
147586
- }
147587
- }
147588
- function addSubtreeIntersection(currProperties, otherProperties) {
147589
- CompilerError.invariant(otherProperties.length > 1, {
147590
- reason:
147591
- '[DeriveMinimalDependencies] Expected otherProperties to be at least 2 elements long.',
147592
- description: null,
147593
- loc: null,
147594
- suggestions: null,
147595
- });
147596
- for (const [propertyName, currNode] of currProperties) {
147597
- const otherNodes = mapNonNull(otherProperties, properties => {
147598
- const node = properties.get(propertyName);
147599
- if (node != null && isUnconditional(node.accessType)) {
147600
- return node;
147601
- } else {
147602
- return null;
147603
- }
147604
- });
147605
- if (otherNodes) {
147606
- addSubtreeIntersection(
147607
- currNode.properties,
147608
- otherNodes.map(node => node.properties)
147609
- );
147610
- const isDep = otherNodes.some(tree => isDependency$1(tree.accessType));
147611
- const externalAccessType = isDep
147612
- ? PropertyAccessType$1.UnconditionalDependency
147613
- : PropertyAccessType$1.UnconditionalAccess;
147614
- currNode.accessType = merge$1(externalAccessType, currNode.accessType);
147615
- }
147616
- }
147617
- }
147618
- function printSubtree$1(node, includeAccesses) {
147619
- const results = [];
147620
- for (const [propertyName, propertyNode] of node.properties) {
147621
- if (includeAccesses || isDependency$1(propertyNode.accessType)) {
147622
- results.push(`${propertyName} (${propertyNode.accessType})`);
147623
- }
147624
- const propertyResults = printSubtree$1(propertyNode, includeAccesses);
147625
- results.push(...propertyResults.map(result => `${propertyName}.${result}`));
147626
- }
147627
- return results;
147628
- }
147629
- function getOrMakeProperty(node, property) {
147630
- let child = node.properties.get(property);
147631
- if (child == null) {
147632
- child = {properties: new Map(), accessType: MIN_ACCESS_TYPE};
147633
- node.properties.set(property, child);
147634
- }
147635
- return child;
147636
- }
147637
- function mapNonNull(arr, fn) {
147638
- const result = [];
147639
- for (let i = 0; i < arr.length; i++) {
147640
- const element = fn(arr[i]);
147641
- if (element) {
147642
- result.push(element);
147643
- } else {
147644
- return null;
147645
- }
147646
- }
147647
- return result;
147648
- }
147649
- var _PoisonState_instances,
147650
- _PoisonState_invalidate,
147651
- _Context_instances$1,
147652
- _Context_temporariesUsedOutsideScope$1,
147653
- _Context_declarations$1,
147654
- _Context_reassignments$1,
147655
- _Context_dependencies$1,
147656
- _Context_properties,
147657
- _Context_temporaries$1,
147658
- _Context_inConditionalWithinScope,
147659
- _Context_depsInCurrentConditional,
147660
- _Context_scopes$1,
147661
- _Context_getProperty,
147662
- _Context_checkValidDependency$1,
147663
- _Context_isScopeActive$1;
147664
- function propagateScopeDependencies(fn) {
147665
- const escapingTemporaries = {
147666
- declarations: new Map(),
147667
- usedOutsideDeclaringScope: new Set(),
147668
- };
147669
- visitReactiveFunction(fn, new FindPromotedTemporaries(), escapingTemporaries);
147670
- const context = new Context$1(escapingTemporaries.usedOutsideDeclaringScope);
147671
- for (const param of fn.params) {
147672
- if (param.kind === 'Identifier') {
147673
- context.declare(param.identifier, {
147674
- id: makeInstructionId(0),
147675
- scope: empty(),
147676
- });
147677
- } else {
147678
- context.declare(param.place.identifier, {
147679
- id: makeInstructionId(0),
147680
- scope: empty(),
147681
- });
147682
- }
147683
- }
147684
- visitReactiveFunction(fn, new PropagationVisitor(fn.env), context);
147685
- }
147686
- class FindPromotedTemporaries extends ReactiveFunctionVisitor {
147687
- constructor() {
147688
- super(...arguments);
147689
- this.scopes = [];
147690
- }
147691
- visitScope(scope, state) {
147692
- this.scopes.push(scope.scope.id);
147693
- this.traverseScope(scope, state);
147694
- this.scopes.pop();
147695
- }
147696
- visitInstruction(instruction, state) {
147697
- this.traverseInstruction(instruction, state);
147698
- const scope = this.scopes.at(-1);
147699
- if (instruction.lvalue === null || scope === undefined) {
147700
- return;
147701
- }
147702
- switch (instruction.value.kind) {
147703
- case 'LoadLocal':
147704
- case 'LoadContext':
147705
- case 'PropertyLoad': {
147706
- state.declarations.set(
147707
- instruction.lvalue.identifier.declarationId,
147708
- scope
147709
- );
147710
- break;
147711
- }
147712
- }
147713
- }
147714
- visitPlace(_id, place, state) {
147715
- const declaringScope = state.declarations.get(
147716
- place.identifier.declarationId
147717
- );
147718
- if (declaringScope === undefined) {
147719
- return;
147720
- }
147721
- if (this.scopes.indexOf(declaringScope) === -1) {
147722
- state.usedOutsideDeclaringScope.add(place.identifier.declarationId);
147723
- }
147724
- }
147725
- }
147726
- class PoisonState {
147727
- constructor(poisonedBlocks, poisonedScopes, isPoisoned) {
147728
- _PoisonState_instances.add(this);
147729
- this.poisonedBlocks = new Set();
147730
- this.poisonedScopes = new Set();
147731
- this.isPoisoned = false;
147732
- this.poisonedBlocks = poisonedBlocks;
147733
- this.poisonedScopes = poisonedScopes;
147734
- this.isPoisoned = isPoisoned;
147735
- }
147736
- clone() {
147737
- return new PoisonState(
147738
- new Set(this.poisonedBlocks),
147739
- new Set(this.poisonedScopes),
147740
- this.isPoisoned
147741
- );
147742
- }
147743
- take(other) {
147744
- const copy = new PoisonState(
147745
- this.poisonedBlocks,
147746
- this.poisonedScopes,
147747
- this.isPoisoned
147748
- );
147749
- this.poisonedBlocks = other.poisonedBlocks;
147750
- this.poisonedScopes = other.poisonedScopes;
147751
- this.isPoisoned = other.isPoisoned;
147752
- return copy;
147753
- }
147754
- merge(others, currentScope) {
147755
- for (const other of others) {
147756
- for (const id of other.poisonedBlocks) {
147757
- this.poisonedBlocks.add(id);
147758
- }
147759
- for (const id of other.poisonedScopes) {
147760
- this.poisonedScopes.add(id);
147761
- }
147762
- }
147763
- __classPrivateFieldGet(
147764
- this,
147765
- _PoisonState_instances,
147766
- 'm',
147767
- _PoisonState_invalidate
147768
- ).call(this, currentScope);
147769
- }
147770
- addPoisonTarget(target, activeScopes) {
147771
- const currentScope = activeScopes.value;
147772
- if (target == null && currentScope != null) {
147773
- let cursor = activeScopes;
147774
- while (true) {
147775
- const next = cursor.pop();
147776
- if (next.value == null) {
147777
- const poisonedScope = cursor.value.value.id;
147778
- this.poisonedScopes.add(poisonedScope);
147779
- if (
147780
- poisonedScope ===
147781
- (currentScope === null || currentScope === void 0
147782
- ? void 0
147783
- : currentScope.value.id)
147784
- ) {
147785
- this.isPoisoned = true;
147786
- }
147787
- break;
147788
- } else {
147789
- cursor = next;
147790
- }
147791
- }
147792
- } else if (target != null) {
147793
- this.poisonedBlocks.add(target);
147794
- if (
147795
- !this.isPoisoned &&
147796
- (currentScope === null || currentScope === void 0
147797
- ? void 0
147798
- : currentScope.ownBlocks.find(blockId => blockId === target))
147799
- ) {
147800
- this.isPoisoned = true;
147801
- }
147802
- }
147803
- }
147804
- removeMaybePoisonedScope(id, currentScope) {
147805
- this.poisonedScopes.delete(id);
147806
- __classPrivateFieldGet(
147807
- this,
147808
- _PoisonState_instances,
147809
- 'm',
147810
- _PoisonState_invalidate
147811
- ).call(this, currentScope);
147812
- }
147813
- removeMaybePoisonedBlock(id, currentScope) {
147814
- this.poisonedBlocks.delete(id);
147815
- __classPrivateFieldGet(
147816
- this,
147817
- _PoisonState_instances,
147818
- 'm',
147819
- _PoisonState_invalidate
147820
- ).call(this, currentScope);
147821
- }
147822
- }
147823
- (_PoisonState_instances = new WeakSet()),
147824
- (_PoisonState_invalidate = function _PoisonState_invalidate(currentScope) {
147825
- if (currentScope != null) {
147826
- if (this.poisonedScopes.has(currentScope.value.id)) {
147827
- this.isPoisoned = true;
147828
- return;
147829
- } else if (
147830
- currentScope.ownBlocks.find(blockId => this.poisonedBlocks.has(blockId))
147831
- ) {
147832
- this.isPoisoned = true;
147833
- return;
147834
- }
147835
- }
147836
- this.isPoisoned = false;
147837
- });
147838
- let Context$1 = class Context {
147839
- constructor(temporariesUsedOutsideScope) {
147840
- _Context_instances$1.add(this);
147841
- _Context_temporariesUsedOutsideScope$1.set(this, void 0);
147842
- _Context_declarations$1.set(this, new Map());
147843
- _Context_reassignments$1.set(this, new Map());
147844
- _Context_dependencies$1.set(this, new ReactiveScopeDependencyTree());
147845
- _Context_properties.set(this, new Map());
147846
- _Context_temporaries$1.set(this, new Map());
147847
- _Context_inConditionalWithinScope.set(this, false);
147848
- _Context_depsInCurrentConditional.set(
147849
- this,
147850
- new ReactiveScopeDependencyTree()
147851
- );
147852
- _Context_scopes$1.set(this, empty());
147853
- this.poisonState = new PoisonState(new Set(), new Set(), false);
147854
- __classPrivateFieldSet(
147855
- this,
147856
- _Context_temporariesUsedOutsideScope$1,
147857
- temporariesUsedOutsideScope,
147858
- 'f'
147859
- );
147860
- }
147861
- enter(scope, fn) {
147862
- const prevInConditional = __classPrivateFieldGet(
147863
- this,
147864
- _Context_inConditionalWithinScope,
147865
- 'f'
147866
- );
147867
- const previousDependencies = __classPrivateFieldGet(
147868
- this,
147869
- _Context_dependencies$1,
147870
- 'f'
147871
- );
147872
- const prevDepsInConditional = this.isPoisoned
147873
- ? __classPrivateFieldGet(this, _Context_depsInCurrentConditional, 'f')
147874
- : null;
147875
- if (prevDepsInConditional != null) {
147876
- __classPrivateFieldSet(
147877
- this,
147878
- _Context_depsInCurrentConditional,
147879
- new ReactiveScopeDependencyTree(),
147880
- 'f'
147881
- );
147882
- }
147883
- const scopedDependencies = new ReactiveScopeDependencyTree();
147884
- __classPrivateFieldSet(this, _Context_inConditionalWithinScope, false, 'f');
147885
- __classPrivateFieldSet(
147886
- this,
147887
- _Context_dependencies$1,
147888
- scopedDependencies,
147889
- 'f'
147890
- );
147891
- __classPrivateFieldSet(
147892
- this,
147893
- _Context_scopes$1,
147894
- __classPrivateFieldGet(this, _Context_scopes$1, 'f').push({
147895
- value: scope,
147896
- ownBlocks: empty(),
147897
- }),
147898
- 'f'
147899
- );
147900
- this.poisonState.isPoisoned = false;
147901
- fn();
147902
- __classPrivateFieldSet(
147903
- this,
147904
- _Context_scopes$1,
147905
- __classPrivateFieldGet(this, _Context_scopes$1, 'f').pop(),
147906
- 'f'
147907
- );
147908
- this.poisonState.removeMaybePoisonedScope(
147909
- scope.id,
147910
- __classPrivateFieldGet(this, _Context_scopes$1, 'f').value
147911
- );
147912
- __classPrivateFieldSet(
147913
- this,
147914
- _Context_dependencies$1,
147915
- previousDependencies,
147916
- 'f'
147917
- );
147918
- __classPrivateFieldSet(
147919
- this,
147920
- _Context_inConditionalWithinScope,
147921
- prevInConditional,
147922
- 'f'
147923
- );
147924
- const minInnerScopeDependencies =
147925
- scopedDependencies.deriveMinimalDependencies();
147926
- __classPrivateFieldGet(
147927
- this,
147928
- _Context_dependencies$1,
147929
- 'f'
147930
- ).addDepsFromInnerScope(
147931
- scopedDependencies,
147932
- __classPrivateFieldGet(this, _Context_inConditionalWithinScope, 'f') ||
147933
- this.isPoisoned,
147934
- __classPrivateFieldGet(
147935
- this,
147936
- _Context_instances$1,
147937
- 'm',
147938
- _Context_checkValidDependency$1
147939
- ).bind(this)
147940
- );
147941
- if (prevDepsInConditional != null) {
147942
- prevDepsInConditional.addDepsFromInnerScope(
147943
- __classPrivateFieldGet(this, _Context_depsInCurrentConditional, 'f'),
147944
- true,
147945
- __classPrivateFieldGet(
147946
- this,
147947
- _Context_instances$1,
147948
- 'm',
147949
- _Context_checkValidDependency$1
147950
- ).bind(this)
147951
- );
147952
- __classPrivateFieldSet(
147953
- this,
147954
- _Context_depsInCurrentConditional,
147955
- prevDepsInConditional,
147956
- 'f'
147957
- );
147958
- }
147959
- return minInnerScopeDependencies;
147960
- }
147961
- isUsedOutsideDeclaringScope(place) {
147962
- return __classPrivateFieldGet(
147963
- this,
147964
- _Context_temporariesUsedOutsideScope$1,
147965
- 'f'
147966
- ).has(place.identifier.declarationId);
147967
- }
147968
- printDeps(includeAccesses = false) {
147969
- return __classPrivateFieldGet(this, _Context_dependencies$1, 'f').printDeps(
147970
- includeAccesses
147971
- );
147972
- }
147973
- enterConditional(fn) {
147974
- const prevInConditional = __classPrivateFieldGet(
147975
- this,
147976
- _Context_inConditionalWithinScope,
147977
- 'f'
147978
- );
147979
- const prevUncondAccessed = __classPrivateFieldGet(
147980
- this,
147981
- _Context_depsInCurrentConditional,
147982
- 'f'
147983
- );
147984
- __classPrivateFieldSet(this, _Context_inConditionalWithinScope, true, 'f');
147985
- __classPrivateFieldSet(
147986
- this,
147987
- _Context_depsInCurrentConditional,
147988
- new ReactiveScopeDependencyTree(),
147989
- 'f'
147990
- );
147991
- fn();
147992
- const result = __classPrivateFieldGet(
147993
- this,
147994
- _Context_depsInCurrentConditional,
147995
- 'f'
147996
- );
147997
- __classPrivateFieldSet(
147998
- this,
147999
- _Context_inConditionalWithinScope,
148000
- prevInConditional,
148001
- 'f'
148002
- );
148003
- __classPrivateFieldSet(
148004
- this,
148005
- _Context_depsInCurrentConditional,
148006
- prevUncondAccessed,
148007
- 'f'
148008
- );
148009
- return result;
148010
- }
148011
- promoteDepsFromExhaustiveConditionals(depsInConditionals) {
148012
- __classPrivateFieldGet(
148013
- this,
148014
- _Context_dependencies$1,
148015
- 'f'
148016
- ).promoteDepsFromExhaustiveConditionals(depsInConditionals);
148017
- __classPrivateFieldGet(
148018
- this,
148019
- _Context_depsInCurrentConditional,
148020
- 'f'
148021
- ).promoteDepsFromExhaustiveConditionals(depsInConditionals);
148022
- }
148023
- declare(identifier, decl) {
148024
- if (
148025
- !__classPrivateFieldGet(this, _Context_declarations$1, 'f').has(
148026
- identifier.declarationId
148027
- )
148028
- ) {
148029
- __classPrivateFieldGet(this, _Context_declarations$1, 'f').set(
148030
- identifier.declarationId,
148031
- decl
148032
- );
148033
- }
148034
- __classPrivateFieldGet(this, _Context_reassignments$1, 'f').set(
148035
- identifier,
148036
- decl
148037
- );
148038
- }
148039
- declareTemporary(lvalue, place) {
148040
- __classPrivateFieldGet(this, _Context_temporaries$1, 'f').set(
148041
- lvalue.identifier,
148042
- place
148043
- );
148044
- }
148045
- resolveTemporary(place) {
148046
- var _a;
148047
- return (_a = __classPrivateFieldGet(this, _Context_temporaries$1, 'f').get(
148048
- place.identifier
148049
- )) !== null && _a !== void 0
148050
- ? _a
148051
- : place;
148052
- }
148053
- declareProperty(lvalue, object, property, optional) {
148054
- const nextDependency = __classPrivateFieldGet(
148055
- this,
148056
- _Context_instances$1,
148057
- 'm',
148058
- _Context_getProperty
148059
- ).call(this, object, property, optional);
148060
- __classPrivateFieldGet(this, _Context_properties, 'f').set(
148061
- lvalue.identifier,
148062
- nextDependency
148063
- );
148064
- }
148065
- get currentScope() {
148066
- return __classPrivateFieldGet(this, _Context_scopes$1, 'f');
148067
- }
148068
- get isPoisoned() {
148069
- return this.poisonState.isPoisoned;
148070
- }
148071
- visitOperand(place) {
148072
- const resolved = this.resolveTemporary(place);
148073
- let dependency = {identifier: resolved.identifier, path: []};
148074
- if (resolved.identifier.name === null) {
148075
- const propertyDependency = __classPrivateFieldGet(
148076
- this,
148077
- _Context_properties,
148078
- 'f'
148079
- ).get(resolved.identifier);
148080
- if (propertyDependency !== undefined) {
148081
- dependency = Object.assign({}, propertyDependency);
148082
- }
148083
- }
148084
- this.visitDependency(dependency);
148085
- }
148086
- visitProperty(object, property, optional) {
148087
- const nextDependency = __classPrivateFieldGet(
148088
- this,
148089
- _Context_instances$1,
148090
- 'm',
148091
- _Context_getProperty
148092
- ).call(this, object, property, optional);
148093
- this.visitDependency(nextDependency);
148094
- }
148095
- visitDependency(maybeDependency) {
148096
- const originalDeclaration = __classPrivateFieldGet(
148097
- this,
148098
- _Context_declarations$1,
148099
- 'f'
148100
- ).get(maybeDependency.identifier.declarationId);
148101
- if (
148102
- originalDeclaration !== undefined &&
148103
- originalDeclaration.scope.value !== null
148104
- ) {
148105
- originalDeclaration.scope.each(scope => {
148106
- if (
148107
- !__classPrivateFieldGet(
148108
- this,
148109
- _Context_instances$1,
148110
- 'm',
148111
- _Context_isScopeActive$1
148112
- ).call(this, scope.value) &&
148113
- !Iterable_some(
148114
- scope.value.declarations.values(),
148115
- decl =>
148116
- decl.identifier.declarationId ===
148117
- maybeDependency.identifier.declarationId
148118
- )
148119
- ) {
148120
- scope.value.declarations.set(maybeDependency.identifier.id, {
148121
- identifier: maybeDependency.identifier,
148122
- scope: originalDeclaration.scope.value.value,
148123
- });
148124
- }
148125
- });
148126
- }
148127
- if (
148128
- __classPrivateFieldGet(
148129
- this,
148130
- _Context_instances$1,
148131
- 'm',
148132
- _Context_checkValidDependency$1
148133
- ).call(this, maybeDependency)
148134
- ) {
148135
- const isPoisoned = this.isPoisoned;
148136
- __classPrivateFieldGet(this, _Context_depsInCurrentConditional, 'f').add(
148137
- maybeDependency,
148138
- isPoisoned
148139
- );
148140
- __classPrivateFieldGet(this, _Context_dependencies$1, 'f').add(
148141
- maybeDependency,
148142
- __classPrivateFieldGet(this, _Context_inConditionalWithinScope, 'f') ||
148143
- isPoisoned
148144
- );
148145
- }
148146
- }
148147
- visitReassignment(place) {
148148
- var _a;
148149
- const currentScope =
148150
- (_a = this.currentScope.value) === null || _a === void 0
148151
- ? void 0
148152
- : _a.value;
148153
- if (
148154
- currentScope != null &&
148155
- !Iterable_some(
148156
- currentScope.reassignments,
148157
- identifier =>
148158
- identifier.declarationId === place.identifier.declarationId
148159
- ) &&
148160
- __classPrivateFieldGet(
148161
- this,
148162
- _Context_instances$1,
148163
- 'm',
148164
- _Context_checkValidDependency$1
148165
- ).call(this, {identifier: place.identifier, path: []})
148166
- ) {
148167
- currentScope.reassignments.add(place.identifier);
148168
- }
148169
- }
148170
- pushLabeledBlock(id) {
148171
- const currentScope = __classPrivateFieldGet(
148172
- this,
148173
- _Context_scopes$1,
148174
- 'f'
148175
- ).value;
148176
- if (currentScope != null) {
148177
- currentScope.ownBlocks = currentScope.ownBlocks.push(id);
148178
- }
148179
- }
148180
- popLabeledBlock(id) {
148181
- const currentScope = __classPrivateFieldGet(
148182
- this,
148183
- _Context_scopes$1,
148184
- 'f'
148185
- ).value;
148186
- if (currentScope != null) {
148187
- const last = currentScope.ownBlocks.value;
148188
- currentScope.ownBlocks = currentScope.ownBlocks.pop();
148189
- CompilerError.invariant(last != null && last === id, {
148190
- reason: '[PropagateScopeDependencies] Misformed block stack',
148191
- loc: GeneratedSource,
148192
- });
148193
- }
148194
- this.poisonState.removeMaybePoisonedBlock(id, currentScope);
148195
- }
148196
- };
148197
- (_Context_temporariesUsedOutsideScope$1 = new WeakMap()),
148198
- (_Context_declarations$1 = new WeakMap()),
148199
- (_Context_reassignments$1 = new WeakMap()),
148200
- (_Context_dependencies$1 = new WeakMap()),
148201
- (_Context_properties = new WeakMap()),
148202
- (_Context_temporaries$1 = new WeakMap()),
148203
- (_Context_inConditionalWithinScope = new WeakMap()),
148204
- (_Context_depsInCurrentConditional = new WeakMap()),
148205
- (_Context_scopes$1 = new WeakMap()),
148206
- (_Context_instances$1 = new WeakSet()),
148207
- (_Context_getProperty = function _Context_getProperty(
148208
- object,
148209
- property,
148210
- optional
148211
- ) {
148212
- const resolvedObject = this.resolveTemporary(object);
148213
- const resolvedDependency = __classPrivateFieldGet(
148214
- this,
148215
- _Context_properties,
148216
- 'f'
148217
- ).get(resolvedObject.identifier);
148218
- let objectDependency;
148219
- if (resolvedDependency === undefined) {
148220
- objectDependency = {identifier: resolvedObject.identifier, path: []};
148221
- } else {
148222
- objectDependency = {
148223
- identifier: resolvedDependency.identifier,
148224
- path: [...resolvedDependency.path],
148225
- };
148226
- }
148227
- objectDependency.path.push({property: property, optional: optional});
148228
- return objectDependency;
148229
- }),
148230
- (_Context_checkValidDependency$1 = function _Context_checkValidDependency(
148231
- maybeDependency
148232
- ) {
148233
- var _a, _b, _c, _d;
148234
- if (
148235
- isUseRefType(maybeDependency.identifier) &&
148236
- ((_a = maybeDependency.path.at(0)) === null || _a === void 0
148237
- ? void 0
148238
- : _a.property) === 'current'
148239
- ) {
148240
- return false;
148241
- }
148242
- if (isRefValueType(maybeDependency.identifier)) {
148243
- return false;
148244
- }
148245
- if (isObjectMethodType(maybeDependency.identifier)) {
148246
- return false;
148247
- }
148248
- const identifier = maybeDependency.identifier;
148249
- const currentDeclaration =
148250
- (_b = __classPrivateFieldGet(this, _Context_reassignments$1, 'f').get(
148251
- identifier
148252
- )) !== null && _b !== void 0
148253
- ? _b
148254
- : __classPrivateFieldGet(this, _Context_declarations$1, 'f').get(
148255
- identifier.declarationId
148256
- );
148257
- const currentScope =
148258
- (_c = this.currentScope.value) === null || _c === void 0
148259
- ? void 0
148260
- : _c.value;
148261
- return (
148262
- currentScope != null &&
148263
- currentDeclaration !== undefined &&
148264
- currentDeclaration.id < currentScope.range.start &&
148265
- (currentDeclaration.scope == null ||
148266
- ((_d = currentDeclaration.scope.value) === null || _d === void 0
148267
- ? void 0
148268
- : _d.value) !== currentScope)
148269
- );
148270
- }),
148271
- (_Context_isScopeActive$1 = function _Context_isScopeActive(scope) {
148272
- if (__classPrivateFieldGet(this, _Context_scopes$1, 'f') === null) {
148273
- return false;
148274
- }
148275
- return __classPrivateFieldGet(this, _Context_scopes$1, 'f').find(
148276
- state => state.value === scope
148277
- );
148278
- });
148279
- class PropagationVisitor extends ReactiveFunctionVisitor {
148280
- constructor(env) {
148281
- super();
148282
- this.env = env;
148283
- }
148284
- visitScope(scope, context) {
148285
- const scopeDependencies = context.enter(scope.scope, () => {
148286
- this.visitBlock(scope.instructions, context);
148287
- });
148288
- for (const candidateDep of scopeDependencies) {
148289
- if (
148290
- !Iterable_some(
148291
- scope.scope.dependencies,
148292
- existingDep =>
148293
- existingDep.identifier.declarationId ===
148294
- candidateDep.identifier.declarationId &&
148295
- areEqualPaths(existingDep.path, candidateDep.path)
148296
- )
148297
- ) {
148298
- scope.scope.dependencies.add(candidateDep);
148299
- }
148300
- }
148301
- }
148302
- visitPrunedScope(scopeBlock, context) {
148303
- context.enter(scopeBlock.scope, () => {
148304
- this.visitBlock(scopeBlock.instructions, context);
148305
- });
148306
- }
148307
- visitInstruction(instruction, context) {
148308
- const {id: id, value: value, lvalue: lvalue} = instruction;
148309
- this.visitInstructionValue(context, id, value, lvalue);
148310
- if (lvalue == null) {
148311
- return;
148312
- }
148313
- context.declare(lvalue.identifier, {id: id, scope: context.currentScope});
148314
- }
148315
- extractOptionalProperty(context, optionalValue, lvalue) {
148316
- const sequence = optionalValue.value;
148317
- CompilerError.invariant(sequence.kind === 'SequenceExpression', {
148318
- reason: 'Expected OptionalExpression value to be a SequenceExpression',
148319
- description: `Found a \`${sequence.kind}\``,
148320
- loc: sequence.loc,
148321
- });
148322
- if (
148323
- sequence.instructions.length === 1 &&
148324
- sequence.instructions[0].lvalue !== null &&
148325
- sequence.instructions[0].value.kind === 'LoadLocal' &&
148326
- sequence.instructions[0].value.place.identifier.name !== null &&
148327
- !context.isUsedOutsideDeclaringScope(sequence.instructions[0].lvalue) &&
148328
- sequence.value.kind === 'SequenceExpression' &&
148329
- sequence.value.instructions.length === 1 &&
148330
- sequence.value.instructions[0].value.kind === 'PropertyLoad' &&
148331
- sequence.value.instructions[0].value.object.identifier.id ===
148332
- sequence.instructions[0].lvalue.identifier.id &&
148333
- sequence.value.instructions[0].lvalue !== null &&
148334
- sequence.value.value.kind === 'LoadLocal' &&
148335
- sequence.value.value.place.identifier.id ===
148336
- sequence.value.instructions[0].lvalue.identifier.id
148337
- ) {
148338
- context.declareTemporary(
148339
- sequence.instructions[0].lvalue,
148340
- sequence.instructions[0].value.place
148341
- );
148342
- const propertyLoad = sequence.value.instructions[0].value;
148343
- return {
148344
- lvalue: lvalue,
148345
- object: propertyLoad.object,
148346
- property: propertyLoad.property,
148347
- optional: optionalValue.optional,
148348
- };
148349
- }
148350
- if (
148351
- sequence.instructions.length === 1 &&
148352
- sequence.instructions[0].lvalue !== null &&
148353
- sequence.instructions[0].value.kind === 'SequenceExpression' &&
148354
- sequence.instructions[0].value.instructions.length === 1 &&
148355
- sequence.instructions[0].value.instructions[0].lvalue !== null &&
148356
- sequence.instructions[0].value.instructions[0].value.kind ===
148357
- 'LoadLocal' &&
148358
- sequence.instructions[0].value.instructions[0].value.place.identifier
148359
- .name !== null &&
148360
- !context.isUsedOutsideDeclaringScope(
148361
- sequence.instructions[0].value.instructions[0].lvalue
148362
- ) &&
148363
- sequence.instructions[0].value.value.kind === 'PropertyLoad' &&
148364
- sequence.instructions[0].value.value.object.identifier.id ===
148365
- sequence.instructions[0].value.instructions[0].lvalue.identifier.id &&
148366
- sequence.value.kind === 'SequenceExpression' &&
148367
- sequence.value.instructions.length === 1 &&
148368
- sequence.value.instructions[0].lvalue !== null &&
148369
- sequence.value.instructions[0].value.kind === 'PropertyLoad' &&
148370
- sequence.value.instructions[0].value.object.identifier.id ===
148371
- sequence.instructions[0].lvalue.identifier.id &&
148372
- sequence.value.value.kind === 'LoadLocal' &&
148373
- sequence.value.value.place.identifier.id ===
148374
- sequence.value.instructions[0].lvalue.identifier.id
148375
- ) {
148376
- context.declareTemporary(
148377
- sequence.instructions[0].value.instructions[0].lvalue,
148378
- sequence.instructions[0].value.instructions[0].value.place
148379
- );
148380
- context.declareProperty(
148381
- sequence.instructions[0].lvalue,
148382
- sequence.instructions[0].value.value.object,
148383
- sequence.instructions[0].value.value.property,
148384
- false
148385
- );
148386
- const propertyLoad = sequence.value.instructions[0].value;
148387
- return {
148388
- lvalue: lvalue,
148389
- object: propertyLoad.object,
148390
- property: propertyLoad.property,
148391
- optional: optionalValue.optional,
148392
- };
148393
- }
148394
- if (
148395
- sequence.instructions.length === 1 &&
148396
- sequence.instructions[0].value.kind === 'SequenceExpression' &&
148397
- sequence.instructions[0].value.instructions.length === 1 &&
148398
- sequence.instructions[0].value.instructions[0].lvalue !== null &&
148399
- sequence.instructions[0].value.instructions[0].value.kind ===
148400
- 'OptionalExpression' &&
148401
- sequence.instructions[0].value.value.kind === 'LoadLocal' &&
148402
- sequence.instructions[0].value.value.place.identifier.id ===
148403
- sequence.instructions[0].value.instructions[0].lvalue.identifier.id &&
148404
- sequence.value.kind === 'SequenceExpression' &&
148405
- sequence.value.instructions.length === 1 &&
148406
- sequence.value.instructions[0].lvalue !== null &&
148407
- sequence.value.instructions[0].value.kind === 'PropertyLoad' &&
148408
- sequence.value.instructions[0].value.object.identifier.id ===
148409
- sequence.instructions[0].value.value.place.identifier.id &&
148410
- sequence.value.value.kind === 'LoadLocal' &&
148411
- sequence.value.value.place.identifier.id ===
148412
- sequence.value.instructions[0].lvalue.identifier.id
148413
- ) {
148414
- const {lvalue: innerLvalue, value: innerOptional} =
148415
- sequence.instructions[0].value.instructions[0];
148416
- const innerProperty = this.extractOptionalProperty(
148417
- context,
148418
- innerOptional,
148419
- innerLvalue
148420
- );
148421
- if (innerProperty === null) {
148422
- return null;
148423
- }
148424
- context.declareProperty(
148425
- innerProperty.lvalue,
148426
- innerProperty.object,
148427
- innerProperty.property,
148428
- innerProperty.optional
148429
- );
148430
- const propertyLoad = sequence.value.instructions[0].value;
148431
- return {
148432
- lvalue: lvalue,
148433
- object: propertyLoad.object,
148434
- property: propertyLoad.property,
148435
- optional: optionalValue.optional,
148436
- };
148437
- }
148438
- return null;
148439
- }
148440
- visitOptionalExpression(context, id, value, lvalue) {
148441
- if (
148442
- lvalue !== null &&
148443
- value.optional &&
148444
- this.env.config.enableOptionalDependencies
148445
- ) {
148446
- const inner = this.extractOptionalProperty(context, value, lvalue);
148447
- if (inner !== null) {
148448
- context.visitProperty(inner.object, inner.property, inner.optional);
148449
- return;
148450
- }
148451
- }
148452
- const inner = value.value;
148453
- CompilerError.invariant(inner.kind === 'SequenceExpression', {
148454
- reason: 'Expected OptionalExpression value to be a SequenceExpression',
148455
- description: `Found a \`${value.kind}\``,
148456
- loc: value.loc,
148457
- suggestions: null,
148458
- });
148459
- for (const instr of inner.instructions) {
148460
- this.visitInstruction(instr, context);
148461
- }
148462
- context.enterConditional(() => {
148463
- this.visitReactiveValue(context, id, inner.value, null);
148464
- });
148465
- }
148466
- visitReactiveValue(context, id, value, lvalue) {
148467
- switch (value.kind) {
148468
- case 'OptionalExpression': {
148469
- this.visitOptionalExpression(context, id, value, lvalue);
148470
- break;
148471
- }
148472
- case 'LogicalExpression': {
148473
- this.visitReactiveValue(context, id, value.left, null);
148474
- context.enterConditional(() => {
148475
- this.visitReactiveValue(context, id, value.right, null);
148476
- });
148477
- break;
148478
- }
148479
- case 'ConditionalExpression': {
148480
- this.visitReactiveValue(context, id, value.test, null);
148481
- const consequentDeps = context.enterConditional(() => {
148482
- this.visitReactiveValue(context, id, value.consequent, null);
148483
- });
148484
- const alternateDeps = context.enterConditional(() => {
148485
- this.visitReactiveValue(context, id, value.alternate, null);
148486
- });
148487
- context.promoteDepsFromExhaustiveConditionals([
148488
- consequentDeps,
148489
- alternateDeps,
148490
- ]);
148491
- break;
148492
- }
148493
- case 'SequenceExpression': {
148494
- for (const instr of value.instructions) {
148495
- this.visitInstruction(instr, context);
148496
- }
148497
- this.visitInstructionValue(context, id, value.value, null);
148498
- break;
148499
- }
148500
- case 'FunctionExpression': {
148501
- if (this.env.config.enableTreatFunctionDepsAsConditional) {
148502
- context.enterConditional(() => {
148503
- for (const operand of eachInstructionValueOperand(value)) {
148504
- context.visitOperand(operand);
148505
- }
148506
- });
148507
- } else {
148508
- for (const operand of eachInstructionValueOperand(value)) {
148509
- context.visitOperand(operand);
148510
- }
148511
- }
148512
- break;
148513
- }
148514
- case 'ReactiveFunctionValue': {
148515
- CompilerError.invariant(false, {
148516
- reason: `Unexpected ReactiveFunctionValue`,
148517
- loc: value.loc,
148518
- description: null,
148519
- suggestions: null,
148520
- });
148521
- }
148522
- default: {
148523
- for (const operand of eachInstructionValueOperand(value)) {
148524
- context.visitOperand(operand);
148525
- }
148526
- }
148527
- }
148528
- }
148529
- visitInstructionValue(context, id, value, lvalue) {
148530
- if (value.kind === 'LoadLocal' && lvalue !== null) {
148531
- if (
148532
- value.place.identifier.name !== null &&
148533
- lvalue.identifier.name === null &&
148534
- !context.isUsedOutsideDeclaringScope(lvalue)
148535
- ) {
148536
- context.declareTemporary(lvalue, value.place);
148537
- } else {
148538
- context.visitOperand(value.place);
148539
- }
148540
- } else if (value.kind === 'PropertyLoad') {
148541
- if (lvalue !== null && !context.isUsedOutsideDeclaringScope(lvalue)) {
148542
- context.declareProperty(lvalue, value.object, value.property, false);
148543
- } else {
148544
- context.visitProperty(value.object, value.property, false);
148545
- }
148546
- } else if (value.kind === 'StoreLocal') {
148547
- context.visitOperand(value.value);
148548
- if (value.lvalue.kind === InstructionKind.Reassign) {
148549
- context.visitReassignment(value.lvalue.place);
148550
- }
148551
- context.declare(value.lvalue.place.identifier, {
148552
- id: id,
148553
- scope: context.currentScope,
148554
- });
148555
- } else if (
148556
- value.kind === 'DeclareLocal' ||
148557
- value.kind === 'DeclareContext'
148558
- ) {
148559
- context.declare(value.lvalue.place.identifier, {
148560
- id: id,
148561
- scope: context.currentScope,
148562
- });
148563
- } else if (value.kind === 'Destructure') {
148564
- context.visitOperand(value.value);
148565
- for (const place of eachPatternOperand(value.lvalue.pattern)) {
148566
- if (value.lvalue.kind === InstructionKind.Reassign) {
148567
- context.visitReassignment(place);
148568
- }
148569
- context.declare(place.identifier, {
148570
- id: id,
148571
- scope: context.currentScope,
148572
- });
148573
- }
148574
- } else {
148575
- this.visitReactiveValue(context, id, value, lvalue);
148576
- }
148577
- }
148578
- enterTerminal(stmt, context) {
148579
- if (stmt.label != null) {
148580
- context.pushLabeledBlock(stmt.label.id);
148581
- }
148582
- const terminal = stmt.terminal;
148583
- switch (terminal.kind) {
148584
- case 'continue':
148585
- case 'break': {
148586
- context.poisonState.addPoisonTarget(
148587
- terminal.target,
148588
- context.currentScope
148589
- );
148590
- break;
148591
- }
148592
- case 'throw':
148593
- case 'return': {
148594
- context.poisonState.addPoisonTarget(null, context.currentScope);
148595
- break;
148596
- }
148597
- }
148598
- }
148599
- exitTerminal(stmt, context) {
148600
- if (stmt.label != null) {
148601
- context.popLabeledBlock(stmt.label.id);
148602
- }
148603
- }
148604
- visitTerminal(stmt, context) {
148605
- this.enterTerminal(stmt, context);
148606
- const terminal = stmt.terminal;
148607
- switch (terminal.kind) {
148608
- case 'break':
148609
- case 'continue': {
148610
- break;
148611
- }
148612
- case 'return': {
148613
- context.visitOperand(terminal.value);
148614
- break;
148615
- }
148616
- case 'throw': {
148617
- context.visitOperand(terminal.value);
148618
- break;
148619
- }
148620
- case 'for': {
148621
- this.visitReactiveValue(context, terminal.id, terminal.init, null);
148622
- this.visitReactiveValue(context, terminal.id, terminal.test, null);
148623
- context.enterConditional(() => {
148624
- this.visitBlock(terminal.loop, context);
148625
- if (terminal.update !== null) {
148626
- this.visitReactiveValue(
148627
- context,
148628
- terminal.id,
148629
- terminal.update,
148630
- null
148631
- );
148632
- }
148633
- });
148634
- break;
148635
- }
148636
- case 'for-of': {
148637
- this.visitReactiveValue(context, terminal.id, terminal.init, null);
148638
- context.enterConditional(() => {
148639
- this.visitBlock(terminal.loop, context);
148640
- });
148641
- break;
148642
- }
148643
- case 'for-in': {
148644
- this.visitReactiveValue(context, terminal.id, terminal.init, null);
148645
- context.enterConditional(() => {
148646
- this.visitBlock(terminal.loop, context);
148647
- });
148648
- break;
148649
- }
148650
- case 'do-while': {
148651
- this.visitBlock(terminal.loop, context);
148652
- context.enterConditional(() => {
148653
- this.visitReactiveValue(context, terminal.id, terminal.test, null);
148654
- });
148655
- break;
148656
- }
148657
- case 'while': {
148658
- this.visitReactiveValue(context, terminal.id, terminal.test, null);
148659
- context.enterConditional(() => {
148660
- this.visitBlock(terminal.loop, context);
148661
- });
148662
- break;
148663
- }
148664
- case 'if': {
148665
- context.visitOperand(terminal.test);
148666
- const {consequent: consequent, alternate: alternate} = terminal;
148667
- const prevPoisonState = context.poisonState.clone();
148668
- const depsInIf = context.enterConditional(() => {
148669
- this.visitBlock(consequent, context);
148670
- });
148671
- if (alternate !== null) {
148672
- const ifPoisonState = context.poisonState.take(prevPoisonState);
148673
- const depsInElse = context.enterConditional(() => {
148674
- this.visitBlock(alternate, context);
148675
- });
148676
- context.poisonState.merge(
148677
- [ifPoisonState],
148678
- context.currentScope.value
148679
- );
148680
- context.promoteDepsFromExhaustiveConditionals([depsInIf, depsInElse]);
148681
- }
148682
- break;
148683
- }
148684
- case 'switch': {
148685
- context.visitOperand(terminal.test);
148686
- const isDefaultOnly =
148687
- terminal.cases.length === 1 && terminal.cases[0].test == null;
148688
- if (isDefaultOnly) {
148689
- const case_ = terminal.cases[0];
148690
- if (case_.block != null) {
148691
- this.visitBlock(case_.block, context);
148692
- break;
148693
- }
148694
- }
148695
- const depsInCases = [];
148696
- let foundDefault = false;
148697
- const prevPoisonState = context.poisonState.clone();
148698
- const mutExPoisonStates = [];
148699
- for (const {test: test, block: block} of terminal.cases) {
148700
- if (test !== null) {
148701
- context.visitOperand(test);
148702
- } else {
148703
- foundDefault = true;
148704
- }
148705
- if (block !== undefined) {
148706
- mutExPoisonStates.push(
148707
- context.poisonState.take(prevPoisonState.clone())
148708
- );
148709
- depsInCases.push(
148710
- context.enterConditional(() => {
148711
- this.visitBlock(block, context);
148712
- })
148713
- );
148714
- }
148715
- }
148716
- if (foundDefault) {
148717
- context.promoteDepsFromExhaustiveConditionals(depsInCases);
148718
- }
148719
- context.poisonState.merge(
148720
- mutExPoisonStates,
148721
- context.currentScope.value
148722
- );
148723
- break;
148724
- }
148725
- case 'label': {
148726
- this.visitBlock(terminal.block, context);
148727
- break;
148728
- }
148729
- case 'try': {
148730
- this.visitBlock(terminal.block, context);
148731
- this.visitBlock(terminal.handler, context);
148732
- break;
148733
- }
148734
- default: {
148735
- assertExhaustive(
148736
- terminal,
148737
- `Unexpected terminal kind \`${terminal.kind}\``
148738
- );
148739
- }
148740
- }
148741
- this.exitTerminal(stmt, context);
148742
- }
148743
- }
148744
- function pruneHoistedContexts(fn) {
148745
- const hoistedIdentifiers = new Map();
148746
- visitReactiveFunction(fn, new Visitor$8(), hoistedIdentifiers);
148747
- }
148748
- let Visitor$8 = class Visitor extends ReactiveFunctionTransform {
148749
- transformInstruction(instruction, state) {
148750
- this.visitInstruction(instruction, state);
148751
- if (
148752
- instruction.value.kind === 'DeclareContext' &&
148753
- instruction.value.lvalue.kind === 'HoistedConst'
148754
- ) {
148755
- state.set(
148756
- instruction.value.lvalue.place.identifier.declarationId,
148757
- InstructionKind.Const
148758
- );
148759
- return {kind: 'remove'};
148760
- }
148761
- if (
148762
- instruction.value.kind === 'DeclareContext' &&
148763
- instruction.value.lvalue.kind === 'HoistedLet'
148764
- ) {
148765
- state.set(
148766
- instruction.value.lvalue.place.identifier.declarationId,
148767
- InstructionKind.Let
148768
- );
148769
- return {kind: 'remove'};
148770
- }
148771
- if (
148772
- instruction.value.kind === 'DeclareContext' &&
148773
- instruction.value.lvalue.kind === 'HoistedFunction'
148774
- ) {
148775
- state.set(
148776
- instruction.value.lvalue.place.identifier.declarationId,
148777
- InstructionKind.Function
148778
- );
148779
- return {kind: 'remove'};
148780
- }
148781
- if (
148782
- instruction.value.kind === 'StoreContext' &&
148783
- state.has(instruction.value.lvalue.place.identifier.declarationId)
148784
- ) {
148785
- const kind = state.get(
148786
- instruction.value.lvalue.place.identifier.declarationId
148787
- );
148788
- return {
148789
- kind: 'replace',
148790
- value: {
148791
- kind: 'instruction',
148792
- instruction: Object.assign(Object.assign({}, instruction), {
148793
- value: Object.assign(Object.assign({}, instruction.value), {
148794
- lvalue: Object.assign(
148795
- Object.assign({}, instruction.value.lvalue),
148796
- {kind: kind}
148797
- ),
148798
- type: null,
148799
- kind: 'StoreLocal',
148800
- }),
148801
- }),
148802
- },
148803
- };
148804
- }
148805
- return {kind: 'keep'};
147285
+ return {kind: 'keep'};
148806
147286
  }
148807
147287
  };
148808
147288
  function inferOperandEffect(state, place) {
@@ -156656,203 +155136,12 @@ function validateNoJSXInTryStatement(fn) {
156656
155136
  throw errors;
156657
155137
  }
156658
155138
  }
156659
- function collectOptionalChainSidemap(fn) {
156660
- const context = {
156661
- blocks: fn.body.blocks,
156662
- seenOptionals: new Set(),
156663
- processedInstrsInOptional: new Set(),
156664
- temporariesReadInOptional: new Map(),
156665
- hoistableObjects: new Map(),
156666
- };
156667
- for (const [_, block] of fn.body.blocks) {
156668
- if (
156669
- block.terminal.kind === 'optional' &&
156670
- !context.seenOptionals.has(block.id)
156671
- ) {
156672
- traverseOptionalBlock(block, context, null);
156673
- }
156674
- }
156675
- return {
156676
- temporariesReadInOptional: context.temporariesReadInOptional,
156677
- processedInstrsInOptional: context.processedInstrsInOptional,
156678
- hoistableObjects: context.hoistableObjects,
156679
- };
156680
- }
156681
- function matchOptionalTestBlock(terminal, blocks) {
156682
- const consequentBlock = assertNonNull(blocks.get(terminal.consequent));
156683
- if (
156684
- consequentBlock.instructions.length === 2 &&
156685
- consequentBlock.instructions[0].value.kind === 'PropertyLoad' &&
156686
- consequentBlock.instructions[1].value.kind === 'StoreLocal'
156687
- ) {
156688
- const propertyLoad = consequentBlock.instructions[0];
156689
- const storeLocal = consequentBlock.instructions[1].value;
156690
- const storeLocalInstrId = consequentBlock.instructions[1].id;
156691
- CompilerError.invariant(
156692
- propertyLoad.value.object.identifier.id === terminal.test.identifier.id,
156693
- {
156694
- reason:
156695
- '[OptionalChainDeps] Inconsistent optional chaining property load',
156696
- description: `Test=${printIdentifier(terminal.test.identifier)} PropertyLoad base=${printIdentifier(propertyLoad.value.object.identifier)}`,
156697
- loc: propertyLoad.loc,
156698
- }
156699
- );
156700
- CompilerError.invariant(
156701
- storeLocal.value.identifier.id === propertyLoad.lvalue.identifier.id,
156702
- {
156703
- reason: '[OptionalChainDeps] Unexpected storeLocal',
156704
- loc: propertyLoad.loc,
156705
- }
156706
- );
156707
- if (
156708
- consequentBlock.terminal.kind !== 'goto' ||
156709
- consequentBlock.terminal.variant !== GotoVariant.Break
156710
- ) {
156711
- return null;
156712
- }
156713
- const alternate = assertNonNull(blocks.get(terminal.alternate));
156714
- CompilerError.invariant(
156715
- alternate.instructions.length === 2 &&
156716
- alternate.instructions[0].value.kind === 'Primitive' &&
156717
- alternate.instructions[1].value.kind === 'StoreLocal',
156718
- {reason: 'Unexpected alternate structure', loc: terminal.loc}
156719
- );
156720
- return {
156721
- consequentId: storeLocal.lvalue.place.identifier.id,
156722
- property: propertyLoad.value.property,
156723
- propertyId: propertyLoad.lvalue.identifier.id,
156724
- storeLocalInstrId: storeLocalInstrId,
156725
- consequentGoto: consequentBlock.terminal.block,
156726
- };
156727
- }
156728
- return null;
156729
- }
156730
- function traverseOptionalBlock(optional, context, outerAlternate) {
156731
- context.seenOptionals.add(optional.id);
156732
- const maybeTest = context.blocks.get(optional.terminal.test);
156733
- let test;
156734
- let baseObject;
156735
- if (maybeTest.terminal.kind === 'branch') {
156736
- CompilerError.invariant(optional.terminal.optional, {
156737
- reason: '[OptionalChainDeps] Expect base case to be always optional',
156738
- loc: optional.terminal.loc,
156739
- });
156740
- if (
156741
- maybeTest.instructions.length === 0 ||
156742
- maybeTest.instructions[0].value.kind !== 'LoadLocal'
156743
- ) {
156744
- return null;
156745
- }
156746
- const path = [];
156747
- for (let i = 1; i < maybeTest.instructions.length; i++) {
156748
- const instrVal = maybeTest.instructions[i].value;
156749
- const prevInstr = maybeTest.instructions[i - 1];
156750
- if (
156751
- instrVal.kind === 'PropertyLoad' &&
156752
- instrVal.object.identifier.id === prevInstr.lvalue.identifier.id
156753
- ) {
156754
- path.push({property: instrVal.property, optional: false});
156755
- } else {
156756
- return null;
156757
- }
156758
- }
156759
- CompilerError.invariant(
156760
- maybeTest.terminal.test.identifier.id ===
156761
- maybeTest.instructions.at(-1).lvalue.identifier.id,
156762
- {
156763
- reason: '[OptionalChainDeps] Unexpected test expression',
156764
- loc: maybeTest.terminal.loc,
156765
- }
156766
- );
156767
- baseObject = {
156768
- identifier: maybeTest.instructions[0].value.place.identifier,
156769
- path: path,
156770
- };
156771
- test = maybeTest.terminal;
156772
- } else if (maybeTest.terminal.kind === 'optional') {
156773
- const testBlock = context.blocks.get(maybeTest.terminal.fallthrough);
156774
- if (testBlock.terminal.kind !== 'branch') {
156775
- CompilerError.throwTodo({
156776
- reason: `Unexpected terminal kind \`${testBlock.terminal.kind}\` for optional fallthrough block`,
156777
- loc: maybeTest.terminal.loc,
156778
- });
156779
- }
156780
- const innerOptional = traverseOptionalBlock(
156781
- maybeTest,
156782
- context,
156783
- testBlock.terminal.alternate
156784
- );
156785
- if (innerOptional == null) {
156786
- return null;
156787
- }
156788
- if (testBlock.terminal.test.identifier.id !== innerOptional) {
156789
- return null;
156790
- }
156791
- if (!optional.terminal.optional) {
156792
- context.hoistableObjects.set(
156793
- optional.id,
156794
- assertNonNull(context.temporariesReadInOptional.get(innerOptional))
156795
- );
156796
- }
156797
- baseObject = assertNonNull(
156798
- context.temporariesReadInOptional.get(innerOptional)
156799
- );
156800
- test = testBlock.terminal;
156801
- } else {
156802
- return null;
156803
- }
156804
- if (test.alternate === outerAlternate) {
156805
- CompilerError.invariant(optional.instructions.length === 0, {
156806
- reason:
156807
- '[OptionalChainDeps] Unexpected instructions an inner optional block. ' +
156808
- 'This indicates that the compiler may be incorrectly concatenating two unrelated optional chains',
156809
- loc: optional.terminal.loc,
156810
- });
156811
- }
156812
- const matchConsequentResult = matchOptionalTestBlock(test, context.blocks);
156813
- if (!matchConsequentResult) {
156814
- return null;
156815
- }
156816
- CompilerError.invariant(
156817
- matchConsequentResult.consequentGoto === optional.terminal.fallthrough,
156818
- {
156819
- reason: '[OptionalChainDeps] Unexpected optional goto-fallthrough',
156820
- description: `${matchConsequentResult.consequentGoto} != ${optional.terminal.fallthrough}`,
156821
- loc: optional.terminal.loc,
156822
- }
156823
- );
156824
- const load = {
156825
- identifier: baseObject.identifier,
156826
- path: [
156827
- ...baseObject.path,
156828
- {
156829
- property: matchConsequentResult.property,
156830
- optional: optional.terminal.optional,
156831
- },
156832
- ],
156833
- };
156834
- context.processedInstrsInOptional.add(
156835
- matchConsequentResult.storeLocalInstrId
156836
- );
156837
- context.processedInstrsInOptional.add(test.id);
156838
- context.temporariesReadInOptional.set(
156839
- matchConsequentResult.consequentId,
156840
- load
156841
- );
156842
- context.temporariesReadInOptional.set(matchConsequentResult.propertyId, load);
156843
- return matchConsequentResult.consequentId;
156844
- }
156845
155139
  function collectHoistablePropertyLoads(
156846
155140
  fn,
156847
155141
  temporaries,
156848
- hoistableFromOptionals,
156849
- nestedFnImmutableContext
155142
+ hoistableFromOptionals
156850
155143
  ) {
156851
155144
  const registry = new PropertyPathRegistry();
156852
- const functionExpressionLoads = collectFunctionExpressionFakeLoads(fn);
156853
- const actuallyEvaluatedTemporaries = new Map(
156854
- [...temporaries].filter(([id]) => !functionExpressionLoads.has(id))
156855
- );
156856
155145
  const knownImmutableIdentifiers = new Set();
156857
155146
  if (fn.fnType === 'Component' || fn.fnType === 'Hook') {
156858
155147
  for (const p of fn.params) {
@@ -156861,14 +155150,26 @@ function collectHoistablePropertyLoads(
156861
155150
  }
156862
155151
  }
156863
155152
  }
156864
- const nodes = collectNonNullsInBlocks(fn, {
156865
- temporaries: actuallyEvaluatedTemporaries,
155153
+ return collectHoistablePropertyLoadsImpl(fn, {
155154
+ temporaries: temporaries,
156866
155155
  knownImmutableIdentifiers: knownImmutableIdentifiers,
156867
155156
  hoistableFromOptionals: hoistableFromOptionals,
156868
155157
  registry: registry,
156869
- nestedFnImmutableContext: nestedFnImmutableContext,
155158
+ nestedFnImmutableContext: null,
156870
155159
  });
156871
- propagateNonNull(fn, nodes, registry);
155160
+ }
155161
+ function collectHoistablePropertyLoadsImpl(fn, context) {
155162
+ const functionExpressionLoads = collectFunctionExpressionFakeLoads(fn);
155163
+ const actuallyEvaluatedTemporaries = new Map(
155164
+ [...context.temporaries].filter(([id]) => !functionExpressionLoads.has(id))
155165
+ );
155166
+ const nodes = collectNonNullsInBlocks(
155167
+ fn,
155168
+ Object.assign(Object.assign({}, context), {
155169
+ temporaries: actuallyEvaluatedTemporaries,
155170
+ })
155171
+ );
155172
+ propagateNonNull(fn, nodes, context.registry);
156872
155173
  return nodes;
156873
155174
  }
156874
155175
  function keyByScopeId(fn, source) {
@@ -157005,28 +155306,25 @@ function collectNonNullsInBlocks(fn, context) {
157005
155306
  assumedNonNullObjects.add(maybeNonNull);
157006
155307
  }
157007
155308
  if (
157008
- instr.value.kind === 'FunctionExpression' &&
155309
+ (instr.value.kind === 'FunctionExpression' ||
155310
+ instr.value.kind === 'ObjectMethod') &&
157009
155311
  !fn.env.config.enableTreatFunctionDepsAsConditional
157010
155312
  ) {
157011
155313
  const innerFn = instr.value.loweredFunc;
157012
- const innerTemporaries = collectTemporariesSidemap(
157013
- innerFn.func,
157014
- new Set()
157015
- );
157016
- const innerOptionals = collectOptionalChainSidemap(innerFn.func);
157017
- const innerHoistableMap = collectHoistablePropertyLoads(
155314
+ const innerHoistableMap = collectHoistablePropertyLoadsImpl(
157018
155315
  innerFn.func,
157019
- innerTemporaries,
157020
- innerOptionals.hoistableObjects,
157021
- (_a = context.nestedFnImmutableContext) !== null && _a !== void 0
157022
- ? _a
157023
- : new Set(
157024
- innerFn.func.context
157025
- .filter(place =>
157026
- isImmutableAtInstr(place.identifier, instr.id, context)
157027
- )
157028
- .map(place => place.identifier.id)
157029
- )
155316
+ Object.assign(Object.assign({}, context), {
155317
+ nestedFnImmutableContext:
155318
+ (_a = context.nestedFnImmutableContext) !== null && _a !== void 0
155319
+ ? _a
155320
+ : new Set(
155321
+ innerFn.func.context
155322
+ .filter(place =>
155323
+ isImmutableAtInstr(place.identifier, instr.id, context)
155324
+ )
155325
+ .map(place => place.identifier.id)
155326
+ ),
155327
+ })
157030
155328
  );
157031
155329
  const innerHoistables = assertNonNull(
157032
155330
  innerHoistableMap.get(innerFn.func.body.entry)
@@ -157175,7 +155473,10 @@ function collectFunctionExpressionFakeLoads(fn) {
157175
155473
  const functionExpressionReferences = new Set();
157176
155474
  for (const [_, block] of fn.body.blocks) {
157177
155475
  for (const {lvalue: lvalue, value: value} of block.instructions) {
157178
- if (value.kind === 'FunctionExpression') {
155476
+ if (
155477
+ value.kind === 'FunctionExpression' ||
155478
+ value.kind === 'ObjectMethod'
155479
+ ) {
157179
155480
  for (const reference of value.loweredFunc.dependencies) {
157180
155481
  let curr = reference.identifier.id;
157181
155482
  while (curr != null) {
@@ -157188,8 +155489,76 @@ function collectFunctionExpressionFakeLoads(fn) {
157188
155489
  }
157189
155490
  }
157190
155491
  }
157191
- return functionExpressionReferences;
155492
+ return functionExpressionReferences;
155493
+ }
155494
+ var _Node_value, _Node_next;
155495
+ function empty() {
155496
+ return EMPTY;
155497
+ }
155498
+ class Node {
155499
+ constructor(value, next = EMPTY) {
155500
+ _Node_value.set(this, void 0);
155501
+ _Node_next.set(this, void 0);
155502
+ __classPrivateFieldSet(this, _Node_value, value, 'f');
155503
+ __classPrivateFieldSet(this, _Node_next, next, 'f');
155504
+ }
155505
+ push(value) {
155506
+ return new Node(value, this);
155507
+ }
155508
+ pop() {
155509
+ return __classPrivateFieldGet(this, _Node_next, 'f');
155510
+ }
155511
+ find(fn) {
155512
+ return fn(__classPrivateFieldGet(this, _Node_value, 'f'))
155513
+ ? true
155514
+ : __classPrivateFieldGet(this, _Node_next, 'f').find(fn);
155515
+ }
155516
+ contains(value) {
155517
+ return (
155518
+ value === __classPrivateFieldGet(this, _Node_value, 'f') ||
155519
+ (__classPrivateFieldGet(this, _Node_next, 'f') !== null &&
155520
+ __classPrivateFieldGet(this, _Node_next, 'f').contains(value))
155521
+ );
155522
+ }
155523
+ each(fn) {
155524
+ fn(__classPrivateFieldGet(this, _Node_value, 'f'));
155525
+ __classPrivateFieldGet(this, _Node_next, 'f').each(fn);
155526
+ }
155527
+ get value() {
155528
+ return __classPrivateFieldGet(this, _Node_value, 'f');
155529
+ }
155530
+ print(fn) {
155531
+ return (
155532
+ fn(__classPrivateFieldGet(this, _Node_value, 'f')) +
155533
+ __classPrivateFieldGet(this, _Node_next, 'f').print(fn)
155534
+ );
155535
+ }
155536
+ }
155537
+ (_Node_value = new WeakMap()), (_Node_next = new WeakMap());
155538
+ class Empty {
155539
+ push(value) {
155540
+ return new Node(value, this);
155541
+ }
155542
+ pop() {
155543
+ return this;
155544
+ }
155545
+ find(_fn) {
155546
+ return false;
155547
+ }
155548
+ contains(_value) {
155549
+ return false;
155550
+ }
155551
+ each(_fn) {
155552
+ return;
155553
+ }
155554
+ get value() {
155555
+ return null;
155556
+ }
155557
+ print(_) {
155558
+ return '';
155559
+ }
157192
155560
  }
155561
+ const EMPTY = new Empty();
157193
155562
  var _a,
157194
155563
  _ReactiveScopeDependencyTreeHIR_hoistableObjects,
157195
155564
  _ReactiveScopeDependencyTreeHIR_deps,
@@ -157449,6 +155818,208 @@ function makeOrMergeProperty(node, property, accessType) {
157449
155818
  }
157450
155819
  return child;
157451
155820
  }
155821
+ function collectOptionalChainSidemap(fn) {
155822
+ const context = {
155823
+ currFn: fn,
155824
+ blocks: fn.body.blocks,
155825
+ seenOptionals: new Set(),
155826
+ processedInstrsInOptional: new Set(),
155827
+ temporariesReadInOptional: new Map(),
155828
+ hoistableObjects: new Map(),
155829
+ };
155830
+ traverseFunction(fn, context);
155831
+ return {
155832
+ temporariesReadInOptional: context.temporariesReadInOptional,
155833
+ processedInstrsInOptional: context.processedInstrsInOptional,
155834
+ hoistableObjects: context.hoistableObjects,
155835
+ };
155836
+ }
155837
+ function traverseFunction(fn, context) {
155838
+ for (const [_, block] of fn.body.blocks) {
155839
+ for (const instr of block.instructions) {
155840
+ if (
155841
+ instr.value.kind === 'FunctionExpression' ||
155842
+ instr.value.kind === 'ObjectMethod'
155843
+ ) {
155844
+ traverseFunction(
155845
+ instr.value.loweredFunc.func,
155846
+ Object.assign(Object.assign({}, context), {
155847
+ currFn: instr.value.loweredFunc.func,
155848
+ blocks: instr.value.loweredFunc.func.body.blocks,
155849
+ })
155850
+ );
155851
+ }
155852
+ }
155853
+ if (
155854
+ block.terminal.kind === 'optional' &&
155855
+ !context.seenOptionals.has(block.id)
155856
+ ) {
155857
+ traverseOptionalBlock(block, context, null);
155858
+ }
155859
+ }
155860
+ }
155861
+ function matchOptionalTestBlock(terminal, blocks) {
155862
+ const consequentBlock = assertNonNull(blocks.get(terminal.consequent));
155863
+ if (
155864
+ consequentBlock.instructions.length === 2 &&
155865
+ consequentBlock.instructions[0].value.kind === 'PropertyLoad' &&
155866
+ consequentBlock.instructions[1].value.kind === 'StoreLocal'
155867
+ ) {
155868
+ const propertyLoad = consequentBlock.instructions[0];
155869
+ const storeLocal = consequentBlock.instructions[1].value;
155870
+ const storeLocalInstr = consequentBlock.instructions[1];
155871
+ CompilerError.invariant(
155872
+ propertyLoad.value.object.identifier.id === terminal.test.identifier.id,
155873
+ {
155874
+ reason:
155875
+ '[OptionalChainDeps] Inconsistent optional chaining property load',
155876
+ description: `Test=${printIdentifier(terminal.test.identifier)} PropertyLoad base=${printIdentifier(propertyLoad.value.object.identifier)}`,
155877
+ loc: propertyLoad.loc,
155878
+ }
155879
+ );
155880
+ CompilerError.invariant(
155881
+ storeLocal.value.identifier.id === propertyLoad.lvalue.identifier.id,
155882
+ {
155883
+ reason: '[OptionalChainDeps] Unexpected storeLocal',
155884
+ loc: propertyLoad.loc,
155885
+ }
155886
+ );
155887
+ if (
155888
+ consequentBlock.terminal.kind !== 'goto' ||
155889
+ consequentBlock.terminal.variant !== GotoVariant.Break
155890
+ ) {
155891
+ return null;
155892
+ }
155893
+ const alternate = assertNonNull(blocks.get(terminal.alternate));
155894
+ CompilerError.invariant(
155895
+ alternate.instructions.length === 2 &&
155896
+ alternate.instructions[0].value.kind === 'Primitive' &&
155897
+ alternate.instructions[1].value.kind === 'StoreLocal',
155898
+ {reason: 'Unexpected alternate structure', loc: terminal.loc}
155899
+ );
155900
+ return {
155901
+ consequentId: storeLocal.lvalue.place.identifier.id,
155902
+ property: propertyLoad.value.property,
155903
+ propertyId: propertyLoad.lvalue.identifier.id,
155904
+ storeLocalInstr: storeLocalInstr,
155905
+ consequentGoto: consequentBlock.terminal.block,
155906
+ };
155907
+ }
155908
+ return null;
155909
+ }
155910
+ function traverseOptionalBlock(optional, context, outerAlternate) {
155911
+ context.seenOptionals.add(optional.id);
155912
+ const maybeTest = context.blocks.get(optional.terminal.test);
155913
+ let test;
155914
+ let baseObject;
155915
+ if (maybeTest.terminal.kind === 'branch') {
155916
+ CompilerError.invariant(optional.terminal.optional, {
155917
+ reason: '[OptionalChainDeps] Expect base case to be always optional',
155918
+ loc: optional.terminal.loc,
155919
+ });
155920
+ if (
155921
+ maybeTest.instructions.length === 0 ||
155922
+ maybeTest.instructions[0].value.kind !== 'LoadLocal'
155923
+ ) {
155924
+ return null;
155925
+ }
155926
+ const path = [];
155927
+ for (let i = 1; i < maybeTest.instructions.length; i++) {
155928
+ const instrVal = maybeTest.instructions[i].value;
155929
+ const prevInstr = maybeTest.instructions[i - 1];
155930
+ if (
155931
+ instrVal.kind === 'PropertyLoad' &&
155932
+ instrVal.object.identifier.id === prevInstr.lvalue.identifier.id
155933
+ ) {
155934
+ path.push({property: instrVal.property, optional: false});
155935
+ } else {
155936
+ return null;
155937
+ }
155938
+ }
155939
+ CompilerError.invariant(
155940
+ maybeTest.terminal.test.identifier.id ===
155941
+ maybeTest.instructions.at(-1).lvalue.identifier.id,
155942
+ {
155943
+ reason: '[OptionalChainDeps] Unexpected test expression',
155944
+ loc: maybeTest.terminal.loc,
155945
+ }
155946
+ );
155947
+ baseObject = {
155948
+ identifier: maybeTest.instructions[0].value.place.identifier,
155949
+ path: path,
155950
+ };
155951
+ test = maybeTest.terminal;
155952
+ } else if (maybeTest.terminal.kind === 'optional') {
155953
+ const testBlock = context.blocks.get(maybeTest.terminal.fallthrough);
155954
+ if (testBlock.terminal.kind !== 'branch') {
155955
+ CompilerError.throwTodo({
155956
+ reason: `Unexpected terminal kind \`${testBlock.terminal.kind}\` for optional fallthrough block`,
155957
+ loc: maybeTest.terminal.loc,
155958
+ });
155959
+ }
155960
+ const innerOptional = traverseOptionalBlock(
155961
+ maybeTest,
155962
+ context,
155963
+ testBlock.terminal.alternate
155964
+ );
155965
+ if (innerOptional == null) {
155966
+ return null;
155967
+ }
155968
+ if (testBlock.terminal.test.identifier.id !== innerOptional) {
155969
+ return null;
155970
+ }
155971
+ if (!optional.terminal.optional) {
155972
+ context.hoistableObjects.set(
155973
+ optional.id,
155974
+ assertNonNull(context.temporariesReadInOptional.get(innerOptional))
155975
+ );
155976
+ }
155977
+ baseObject = assertNonNull(
155978
+ context.temporariesReadInOptional.get(innerOptional)
155979
+ );
155980
+ test = testBlock.terminal;
155981
+ } else {
155982
+ return null;
155983
+ }
155984
+ if (test.alternate === outerAlternate) {
155985
+ CompilerError.invariant(optional.instructions.length === 0, {
155986
+ reason:
155987
+ '[OptionalChainDeps] Unexpected instructions an inner optional block. ' +
155988
+ 'This indicates that the compiler may be incorrectly concatenating two unrelated optional chains',
155989
+ loc: optional.terminal.loc,
155990
+ });
155991
+ }
155992
+ const matchConsequentResult = matchOptionalTestBlock(test, context.blocks);
155993
+ if (!matchConsequentResult) {
155994
+ return null;
155995
+ }
155996
+ CompilerError.invariant(
155997
+ matchConsequentResult.consequentGoto === optional.terminal.fallthrough,
155998
+ {
155999
+ reason: '[OptionalChainDeps] Unexpected optional goto-fallthrough',
156000
+ description: `${matchConsequentResult.consequentGoto} != ${optional.terminal.fallthrough}`,
156001
+ loc: optional.terminal.loc,
156002
+ }
156003
+ );
156004
+ const load = {
156005
+ identifier: baseObject.identifier,
156006
+ path: [
156007
+ ...baseObject.path,
156008
+ {
156009
+ property: matchConsequentResult.property,
156010
+ optional: optional.terminal.optional,
156011
+ },
156012
+ ],
156013
+ };
156014
+ context.processedInstrsInOptional.add(matchConsequentResult.storeLocalInstr);
156015
+ context.processedInstrsInOptional.add(test);
156016
+ context.temporariesReadInOptional.set(
156017
+ matchConsequentResult.consequentId,
156018
+ load
156019
+ );
156020
+ context.temporariesReadInOptional.set(matchConsequentResult.propertyId, load);
156021
+ return matchConsequentResult.consequentId;
156022
+ }
157452
156023
  var _Context_instances,
157453
156024
  _Context_declarations,
157454
156025
  _Context_reassignments,
@@ -157469,7 +156040,7 @@ function propagateScopeDependenciesHIR(fn) {
157469
156040
  } = collectOptionalChainSidemap(fn);
157470
156041
  const hoistablePropertyLoads = keyByScopeId(
157471
156042
  fn,
157472
- collectHoistablePropertyLoads(fn, temporaries, hoistableObjects, null)
156043
+ collectHoistablePropertyLoads(fn, temporaries, hoistableObjects)
157473
156044
  );
157474
156045
  const scopeDeps = collectDependencies(
157475
156046
  fn,
@@ -157561,6 +156132,20 @@ function findTemporariesUsedOutsideDeclaringScope(fn) {
157561
156132
  }
157562
156133
  function collectTemporariesSidemap(fn, usedOutsideDeclaringScope) {
157563
156134
  const temporaries = new Map();
156135
+ collectTemporariesSidemapImpl(
156136
+ fn,
156137
+ usedOutsideDeclaringScope,
156138
+ temporaries,
156139
+ false
156140
+ );
156141
+ return temporaries;
156142
+ }
156143
+ function collectTemporariesSidemapImpl(
156144
+ fn,
156145
+ usedOutsideDeclaringScope,
156146
+ temporaries,
156147
+ isInnerFn
156148
+ ) {
157564
156149
  for (const [_, block] of fn.body.blocks) {
157565
156150
  for (const instr of block.instructions) {
157566
156151
  const {value: value, lvalue: lvalue} = instr;
@@ -157568,27 +156153,45 @@ function collectTemporariesSidemap(fn, usedOutsideDeclaringScope) {
157568
156153
  lvalue.identifier.declarationId
157569
156154
  );
157570
156155
  if (value.kind === 'PropertyLoad' && !usedOutside) {
157571
- const property = getProperty(
157572
- value.object,
157573
- value.property,
157574
- false,
157575
- temporaries
157576
- );
157577
- temporaries.set(lvalue.identifier.id, property);
156156
+ if (!isInnerFn || temporaries.has(value.object.identifier.id)) {
156157
+ const property = getProperty(
156158
+ value.object,
156159
+ value.property,
156160
+ false,
156161
+ temporaries
156162
+ );
156163
+ temporaries.set(lvalue.identifier.id, property);
156164
+ }
157578
156165
  } else if (
157579
156166
  value.kind === 'LoadLocal' &&
157580
156167
  lvalue.identifier.name == null &&
157581
156168
  value.place.identifier.name !== null &&
157582
156169
  !usedOutside
157583
156170
  ) {
157584
- temporaries.set(lvalue.identifier.id, {
157585
- identifier: value.place.identifier,
157586
- path: [],
157587
- });
156171
+ if (
156172
+ !isInnerFn ||
156173
+ fn.context.some(
156174
+ context => context.identifier.id === value.place.identifier.id
156175
+ )
156176
+ ) {
156177
+ temporaries.set(lvalue.identifier.id, {
156178
+ identifier: value.place.identifier,
156179
+ path: [],
156180
+ });
156181
+ }
156182
+ } else if (
156183
+ value.kind === 'FunctionExpression' ||
156184
+ value.kind === 'ObjectMethod'
156185
+ ) {
156186
+ collectTemporariesSidemapImpl(
156187
+ value.loweredFunc.func,
156188
+ usedOutsideDeclaringScope,
156189
+ temporaries,
156190
+ true
156191
+ );
157588
156192
  }
157589
156193
  }
157590
156194
  }
157591
- return temporaries;
157592
156195
  }
157593
156196
  function getProperty(object, propertyName, optional, temporaries) {
157594
156197
  const resolvedDependency = temporaries.get(object.identifier.id);
@@ -157619,6 +156222,7 @@ class Context {
157619
156222
  this.deps = new Map();
157620
156223
  _Context_temporaries.set(this, void 0);
157621
156224
  _Context_temporariesUsedOutsideScope.set(this, void 0);
156225
+ this.inInnerFn = false;
157622
156226
  __classPrivateFieldSet(
157623
156227
  this,
157624
156228
  _Context_temporariesUsedOutsideScope,
@@ -157694,6 +156298,7 @@ class Context {
157694
156298
  ).has(place.identifier.declarationId);
157695
156299
  }
157696
156300
  declare(identifier, decl) {
156301
+ if (this.inInnerFn) return;
157697
156302
  if (
157698
156303
  !__classPrivateFieldGet(this, _Context_declarations, 'f').has(
157699
156304
  identifier.declarationId
@@ -157939,11 +156544,11 @@ function collectDependencies(
157939
156544
  }
157940
156545
  }
157941
156546
  for (const instr of block.instructions) {
157942
- if (!processedInstrsInOptional.has(instr.id)) {
156547
+ if (!processedInstrsInOptional.has(instr)) {
157943
156548
  handleInstruction(instr, context);
157944
156549
  }
157945
156550
  }
157946
- if (!processedInstrsInOptional.has(block.terminal.id)) {
156551
+ if (!processedInstrsInOptional.has(block.terminal)) {
157947
156552
  for (const place of eachTerminalOperand(block.terminal)) {
157948
156553
  context.visitOperand(place);
157949
156554
  }
@@ -158416,10 +157021,8 @@ function* runWithEnvironment(func, env) {
158416
157021
  yield log({kind: 'hir', name: 'FlattenScopesWithHooksOrUseHIR', value: hir});
158417
157022
  assertTerminalSuccessorsExist(hir);
158418
157023
  assertTerminalPredsExist(hir);
158419
- if (env.config.enablePropagateDepsInHIR) {
158420
- propagateScopeDependenciesHIR(hir);
158421
- yield log({kind: 'hir', name: 'PropagateScopeDependenciesHIR', value: hir});
158422
- }
157024
+ propagateScopeDependenciesHIR(hir);
157025
+ yield log({kind: 'hir', name: 'PropagateScopeDependenciesHIR', value: hir});
158423
157026
  if (env.config.inlineJsxTransform) {
158424
157027
  inlineJsxTransform(hir, env.config.inlineJsxTransform);
158425
157028
  yield log({kind: 'hir', name: 'inlineJsxTransform', value: hir});
@@ -158438,14 +157041,6 @@ function* runWithEnvironment(func, env) {
158438
157041
  value: reactiveFunction,
158439
157042
  });
158440
157043
  assertScopeInstructionsWithinScopes(reactiveFunction);
158441
- if (!env.config.enablePropagateDepsInHIR) {
158442
- propagateScopeDependencies(reactiveFunction);
158443
- yield log({
158444
- kind: 'reactive',
158445
- name: 'PropagateScopeDependencies',
158446
- value: reactiveFunction,
158447
- });
158448
- }
158449
157044
  pruneNonEscapingScopes(reactiveFunction);
158450
157045
  yield log({
158451
157046
  kind: 'reactive',
@@ -159676,7 +158271,7 @@ exports.OPT_OUT_DIRECTIVES = OPT_OUT_DIRECTIVES;
159676
158271
  exports.compile = compileFn;
159677
158272
  exports.compileProgram = compileProgram;
159678
158273
  exports.default = BabelPluginReactCompiler;
159679
- exports.parseConfigPragma = parseConfigPragma;
158274
+ exports.parseConfigPragmaForTests = parseConfigPragmaForTests;
159680
158275
  exports.parsePluginOptions = parsePluginOptions;
159681
158276
  exports.printHIR = printHIR;
159682
158277
  exports.printReactiveFunction = printReactiveFunction;