@farm.js/react 0.1.0-beta.6 → 0.1.0-beta.7

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/vite.cjs CHANGED
@@ -845,6 +845,13 @@ function staticSliceIndex(expression) {
845
845
  }
846
846
  return void 0;
847
847
  }
848
+ function validateKeyedArrayPositionExpression(expression, safeGlobals) {
849
+ const isNumericLiteral = core.types.isNumericLiteral(expression) || core.types.isUnaryExpression(expression) && (expression.operator === "+" || expression.operator === "-") && core.types.isNumericLiteral(expression.argument);
850
+ if (isNumericLiteral && staticSliceIndex(expression) === void 0) {
851
+ return "non-safe-integer position literals";
852
+ }
853
+ return validateDerivedExpression(expression, safeGlobals);
854
+ }
848
855
  function rewriteKeyedArrayPositionHints(root, hintedStateIndices, statesBySetter, helperIdentifier, safeGlobals) {
849
856
  if (hintedStateIndices.size === 0) {
850
857
  return { root: core.types.cloneNode(root, true), count: 0, stateIndices: /* @__PURE__ */ new Set() };
@@ -866,11 +873,11 @@ function rewriteKeyedArrayPositionHints(root, hintedStateIndices, statesBySetter
866
873
  }
867
874
  const methodName = updater.body.callee.property.name;
868
875
  const args = updater.body.arguments;
869
- const kind = methodName === "with" && args.length === 2 ? "replace" : methodName === "toSpliced" && args.length === 3 && core.types.isNumericLiteral(args[1], { value: 0 }) ? "insert" : void 0;
870
- if (!kind || !core.types.isExpression(args[0]) || !core.types.isExpression(args.at(-1))) return;
871
- const position = staticSliceIndex(args[0]);
872
- const item = args.at(-1);
873
- if (position === void 0 || !core.types.isExpression(item) || validateDerivedExpression(item, safeGlobals) !== void 0) {
876
+ const kind = methodName === "with" && args.length === 2 ? "replace" : methodName === "toSpliced" && args.length === 3 && core.types.isNumericLiteral(args[1], { value: 0 }) ? "insert" : methodName === "toSpliced" && args.length === 2 && core.types.isNumericLiteral(args[1], { value: 1 }) ? "remove" : void 0;
877
+ if (!kind || !core.types.isExpression(args[0])) return;
878
+ const position = args[0];
879
+ const item = kind === "remove" ? void 0 : args.at(-1);
880
+ if (validateKeyedArrayPositionExpression(position, safeGlobals) !== void 0 || item !== void 0 && (!core.types.isExpression(item) || validateDerivedExpression(item, safeGlobals) !== void 0)) {
874
881
  return;
875
882
  }
876
883
  const previous = core.types.cloneNode(updater.params[0]);
@@ -956,6 +963,61 @@ function rewriteKeyedArrayReorderHints(root, hintedStateIndices, statesBySetter,
956
963
  stateIndices
957
964
  };
958
965
  }
966
+ function rewriteKeyedArraySortHints(root, hintedStateIndices, statesBySetter, helperIdentifier, safeGlobals) {
967
+ if (hintedStateIndices.size === 0) {
968
+ return { root: core.types.cloneNode(root, true), count: 0, stateIndices: /* @__PURE__ */ new Set() };
969
+ }
970
+ const file = expressionFile(core.types.cloneNode(root, true));
971
+ const stateIndices = /* @__PURE__ */ new Set();
972
+ let count = 0;
973
+ core.traverse(file, {
974
+ CallExpression(path) {
975
+ const callee = path.get("callee");
976
+ if (!callee.isIdentifier() || callee.scope.hasBinding(callee.node.name)) return;
977
+ const state = statesBySetter.get(callee.node.name);
978
+ if (!state || !hintedStateIndices.has(state.index) || path.node.arguments.length !== 1) {
979
+ return;
980
+ }
981
+ const updater = path.node.arguments[0];
982
+ if (!core.types.isArrowFunctionExpression(updater) || updater.async || updater.generator || updater.params.length !== 1 || !core.types.isIdentifier(updater.params[0]) || !core.types.isCallExpression(updater.body) || updater.body.arguments.length > 1 || !core.types.isMemberExpression(updater.body.callee) || updater.body.callee.computed || !core.types.isIdentifier(updater.body.callee.object, { name: updater.params[0].name }) || !core.types.isIdentifier(updater.body.callee.property, { name: "toSorted" })) {
983
+ return;
984
+ }
985
+ const comparator = updater.body.arguments[0];
986
+ if (comparator && !core.types.isArrowFunctionExpression(comparator) && !core.types.isFunctionExpression(comparator)) {
987
+ return;
988
+ }
989
+ if (comparator && validateCollectionCallback(comparator, "toSorted", safeGlobals)) return;
990
+ const previous = core.types.cloneNode(updater.params[0]);
991
+ const method = path.scope.generateUidIdentifier("farmToSorted");
992
+ path.node.arguments[0] = core.types.arrowFunctionExpression(
993
+ [core.types.cloneNode(previous)],
994
+ core.types.blockStatement([
995
+ core.types.variableDeclaration("const", [
996
+ core.types.variableDeclarator(
997
+ core.types.cloneNode(method),
998
+ core.types.memberExpression(core.types.cloneNode(previous), core.types.identifier("toSorted"))
999
+ )
1000
+ ]),
1001
+ core.types.returnStatement(
1002
+ core.types.callExpression(core.types.cloneNode(helperIdentifier), [
1003
+ core.types.cloneNode(previous),
1004
+ core.types.cloneNode(method),
1005
+ ...comparator ? [core.types.cloneNode(comparator, true)] : []
1006
+ ])
1007
+ )
1008
+ ])
1009
+ );
1010
+ count += 1;
1011
+ stateIndices.add(state.index);
1012
+ path.skip();
1013
+ }
1014
+ });
1015
+ return {
1016
+ root: file.program.body[0].expression,
1017
+ count,
1018
+ stateIndices
1019
+ };
1020
+ }
959
1021
  function rewriteKeyedArraySliceHints(root, hintedStateIndices, statesBySetter, helperIdentifier) {
960
1022
  if (hintedStateIndices.size === 0) {
961
1023
  return { root: core.types.cloneNode(root, true), count: 0, stateIndices: /* @__PURE__ */ new Set() };
@@ -4757,7 +4819,7 @@ function wrapperElement(definitionIdentifier, props) {
4757
4819
  const attributes = props ? [core.types.jsxSpreadAttribute(core.types.cloneNode(props))] : [];
4758
4820
  return core.types.jsxElement(core.types.jsxOpeningElement(name, attributes, true), null, [], true);
4759
4821
  }
4760
- function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateIdentifier, keyedArrayAppendIdentifier, keyedArrayFilterIdentifier, keyedArrayPrependIdentifier, keyedArrayPositionIdentifier, keyedArrayReorderIdentifier, keyedArrayRollingWindowIdentifier, keyedArraySliceIdentifier, keyedCollectionUpdateIdentifier, keyedCollectionMutationIdentifier, runtimeFeatureIdentifiers, usedRuntimeFeatures, optimizationCounts, useStateNames, reactNames, listNames, moduleId, reactivity) {
4822
+ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateIdentifier, keyedArrayAppendIdentifier, keyedArrayFilterIdentifier, keyedArrayPrependIdentifier, keyedArrayPositionIdentifier, keyedArrayReorderIdentifier, keyedArraySortIdentifier, keyedArrayRollingWindowIdentifier, keyedArraySliceIdentifier, keyedCollectionUpdateIdentifier, keyedCollectionMutationIdentifier, runtimeFeatureIdentifiers, usedRuntimeFeatures, optimizationCounts, useStateNames, reactNames, listNames, moduleId, reactivity) {
4761
4823
  const { path, name, statementPath } = candidate;
4762
4824
  if (path.node.async || path.node.generator)
4763
4825
  return "async and generator components are not supported";
@@ -5193,6 +5255,41 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
5193
5255
  optimizationCounts.keyedArrayReorderHints += reorderHintedRoot.count;
5194
5256
  }
5195
5257
  }
5258
+ const sortHintedRoot = rewriteKeyedArraySortHints(
5259
+ expandedReactiveRoot,
5260
+ shiftedIndexIndependentStateIndices,
5261
+ statesBySetter,
5262
+ keyedArraySortIdentifier,
5263
+ safeGlobals
5264
+ );
5265
+ let appliedKeyedArraySortHints = 0;
5266
+ let appliedSortHintedStateIndices = /* @__PURE__ */ new Set();
5267
+ if (sortHintedRoot.count > 0) {
5268
+ const hintedBlockAnalysis = analyzeComposableBlocks(
5269
+ sortHintedRoot.root,
5270
+ reactiveByValue,
5271
+ safeGlobals,
5272
+ listNames,
5273
+ allowedComponentNames
5274
+ );
5275
+ const hintedAnalysis = analyzeHostTree(
5276
+ sortHintedRoot.root,
5277
+ reactiveByValue,
5278
+ safeGlobals,
5279
+ hintedBlockAnalysis.conditionalExpressions || /* @__PURE__ */ new Set(),
5280
+ hintedBlockAnalysis.keyedExpressions || /* @__PURE__ */ new Set(),
5281
+ hintedBlockAnalysis.ownedElements || /* @__PURE__ */ new Set(),
5282
+ hintedBlockAnalysis.componentElements || /* @__PURE__ */ new Set()
5283
+ );
5284
+ if (!hintedBlockAnalysis.reason && !hintedAnalysis.reason) {
5285
+ expandedReactiveRoot = sortHintedRoot.root;
5286
+ blockAnalysis = hintedBlockAnalysis;
5287
+ analysis = hintedAnalysis;
5288
+ appliedKeyedArraySortHints = sortHintedRoot.count;
5289
+ appliedSortHintedStateIndices = sortHintedRoot.stateIndices;
5290
+ optimizationCounts.keyedArraySortHints += sortHintedRoot.count;
5291
+ }
5292
+ }
5196
5293
  const sliceHintedRoot = rewriteKeyedArraySliceHints(
5197
5294
  expandedReactiveRoot,
5198
5295
  shiftedIndexIndependentStateIndices,
@@ -5327,7 +5424,7 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
5327
5424
  (count, plan) => count + (plan.kind === "keyed-rows" ? plan.bindings.filter((binding) => binding.mapLookupTarget !== void 0).length : 0),
5328
5425
  0
5329
5426
  );
5330
- const hasKeyedUpdateHints = appliedKeyedMapUpdateHints > 0 || appliedKeyedArrayAppendHints > 0 || appliedKeyedArrayFilterHints > 0 || appliedKeyedArrayPrependHints > 0 || appliedKeyedArrayPositionHints > 0 || appliedKeyedArrayReorderHints > 0 || appliedKeyedArrayRollingWindowHints > 0 || appliedKeyedArraySliceHints > 0;
5427
+ const hasKeyedUpdateHints = appliedKeyedMapUpdateHints > 0 || appliedKeyedArrayAppendHints > 0 || appliedKeyedArrayFilterHints > 0 || appliedKeyedArrayPrependHints > 0 || appliedKeyedArrayPositionHints > 0 || appliedKeyedArrayReorderHints > 0 || appliedKeyedArraySortHints > 0 || appliedKeyedArrayRollingWindowHints > 0 || appliedKeyedArraySliceHints > 0;
5331
5428
  const hasKeyedArrayRemovalHints = appliedKeyedArrayFilterHints > 0 || appliedKeyedArrayRollingWindowHints > 0 || appliedKeyedArraySliceHints > 0;
5332
5429
  const runtimeFeatures = runtimeFeaturesForPlans(
5333
5430
  blockPlans,
@@ -5335,7 +5432,7 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
5335
5432
  hasKeyedArrayRemovalHints,
5336
5433
  appliedKeyedArrayPrependHints > 0,
5337
5434
  appliedKeyedArrayPositionHints > 0,
5338
- appliedKeyedArrayReorderHints > 0,
5435
+ appliedKeyedArrayReorderHints > 0 || appliedKeyedArraySortHints > 0,
5339
5436
  appliedKeyedArrayRollingWindowHints > 0
5340
5437
  );
5341
5438
  markShortCircuitBindings(analysis.bindings || []);
@@ -5364,7 +5461,7 @@ function compileCandidate(candidate, createComponentIdentifier, keyedMapUpdateId
5364
5461
  appliedRemovalHintedStateIndices,
5365
5462
  appliedPrependHintedStateIndices,
5366
5463
  appliedPositionHintedStateIndices,
5367
- appliedReorderHintedStateIndices
5464
+ /* @__PURE__ */ new Set([...appliedReorderHintedStateIndices, ...appliedSortHintedStateIndices])
5368
5465
  );
5369
5466
  const rewrittenRoot = rewriteStateAccess(
5370
5467
  rootWithBlocks,
@@ -5508,6 +5605,7 @@ async function compileReactModule(code, id, options) {
5508
5605
  keyedArrayPrependHints: 0,
5509
5606
  keyedArrayPositionHints: 0,
5510
5607
  keyedArrayReorderHints: 0,
5608
+ keyedArraySortHints: 0,
5511
5609
  keyedArrayRollingWindowHints: 0,
5512
5610
  keyedArraySliceHints: 0,
5513
5611
  keyedCollectionUpdateHints: 0,
@@ -5563,6 +5661,9 @@ async function compileReactModule(code, id, options) {
5563
5661
  const keyedArrayReorderIdentifier = programPath.scope.generateUidIdentifier(
5564
5662
  "createCompilerKeyedArrayReorder"
5565
5663
  );
5664
+ const keyedArraySortIdentifier = programPath.scope.generateUidIdentifier(
5665
+ "createCompilerKeyedArraySort"
5666
+ );
5566
5667
  const keyedArrayRollingWindowIdentifier = programPath.scope.generateUidIdentifier(
5567
5668
  "createCompilerKeyedArrayRollingWindow"
5568
5669
  );
@@ -5598,6 +5699,7 @@ async function compileReactModule(code, id, options) {
5598
5699
  keyedArrayPrependIdentifier,
5599
5700
  keyedArrayPositionIdentifier,
5600
5701
  keyedArrayReorderIdentifier,
5702
+ keyedArraySortIdentifier,
5601
5703
  keyedArrayRollingWindowIdentifier,
5602
5704
  keyedArraySliceIdentifier,
5603
5705
  keyedCollectionUpdateIdentifier,
@@ -5666,6 +5768,12 @@ async function compileReactModule(code, id, options) {
5666
5768
  core.types.identifier("createCompilerKeyedArrayReorder")
5667
5769
  )
5668
5770
  ] : [],
5771
+ ...optimizationCounts.keyedArraySortHints > 0 ? [
5772
+ core.types.importSpecifier(
5773
+ keyedArraySortIdentifier,
5774
+ core.types.identifier("createCompilerKeyedArraySort")
5775
+ )
5776
+ ] : [],
5669
5777
  ...optimizationCounts.keyedArrayRollingWindowHints > 0 ? [
5670
5778
  core.types.importSpecifier(
5671
5779
  keyedArrayRollingWindowIdentifier,
@@ -5741,6 +5849,7 @@ function createReactCompilerReport(projectRoot, observations) {
5741
5849
  let keyedArrayPrependHints = 0;
5742
5850
  let keyedArrayPositionHints = 0;
5743
5851
  let keyedArrayReorderHints = 0;
5852
+ let keyedArraySortHints = 0;
5744
5853
  let keyedArrayRollingWindowHints = 0;
5745
5854
  let keyedArraySliceHints = 0;
5746
5855
  let keyedCollectionUpdateHints = 0;
@@ -5757,6 +5866,7 @@ function createReactCompilerReport(projectRoot, observations) {
5757
5866
  keyedArrayPrependHints += result.optimizations.keyedArrayPrependHints || 0;
5758
5867
  keyedArrayPositionHints += result.optimizations.keyedArrayPositionHints || 0;
5759
5868
  keyedArrayReorderHints += result.optimizations.keyedArrayReorderHints || 0;
5869
+ keyedArraySortHints += result.optimizations.keyedArraySortHints || 0;
5760
5870
  keyedArrayRollingWindowHints += result.optimizations.keyedArrayRollingWindowHints || 0;
5761
5871
  keyedArraySliceHints += result.optimizations.keyedArraySliceHints || 0;
5762
5872
  keyedCollectionUpdateHints += result.optimizations.keyedCollectionUpdateHints || 0;
@@ -5789,6 +5899,7 @@ function createReactCompilerReport(projectRoot, observations) {
5789
5899
  keyedArrayPrependHints,
5790
5900
  keyedArrayPositionHints,
5791
5901
  keyedArrayReorderHints,
5902
+ keyedArraySortHints,
5792
5903
  keyedArrayRollingWindowHints,
5793
5904
  keyedArraySliceHints,
5794
5905
  keyedCollectionUpdateHints,