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