@farm.js/react 0.1.0-beta.2 → 0.1.0-beta.3

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
@@ -851,11 +851,11 @@ function analyzeKeyedRowsContainer(container, statesByValue, safeGlobals, listNa
851
851
  if (children.length !== 1) return void 0;
852
852
  return analyzeKeyedRowChild(children[0], statesByValue, safeGlobals, listNames);
853
853
  }
854
- function isStaticKeyedRangeSibling(element, statesByValue, safeGlobals) {
854
+ function isStaticRangeSibling(element, statesByValue, safeGlobals) {
855
855
  for (const child of meaningfulJsxChildren(element)) {
856
856
  if (types.isJSXFragment(child)) return false;
857
857
  if (types.isJSXElement(child)) {
858
- if (!isHostElement(child) || !isStaticKeyedRangeSibling(child, statesByValue, safeGlobals)) {
858
+ if (!isHostElement(child) || !isStaticRangeSibling(child, statesByValue, safeGlobals)) {
859
859
  return false;
860
860
  }
861
861
  continue;
@@ -896,7 +896,7 @@ function analyzeKeyedRangesContainer(container, statesByValue, safeGlobals, list
896
896
  continue;
897
897
  }
898
898
  if (!types.isJSXElement(child) || !isHostElement(child)) return void 0;
899
- if (!isStaticKeyedRangeSibling(child, statesByValue, safeGlobals)) return void 0;
899
+ if (!isStaticRangeSibling(child, statesByValue, safeGlobals)) return void 0;
900
900
  staticBefore += 1;
901
901
  }
902
902
  if (ranges.length === 0) return void 0;
@@ -1048,6 +1048,60 @@ function analyzeHostConditionalContainer(container, statesByValue, safeGlobals)
1048
1048
  falsyBindings: falsyAnalysis.bindings || []
1049
1049
  };
1050
1050
  }
1051
+ function analyzeConditionalRangesContainer(container, statesByValue, safeGlobals, allowSingleRange = false) {
1052
+ const containerName = container.openingElement.name;
1053
+ if (!types.isJSXIdentifier(containerName) || !/^[a-z]/.test(containerName.name) || containerName.name === "svg" || container.openingElement.attributes.some(
1054
+ (attribute) => types.isJSXSpreadAttribute(attribute) || types.isJSXAttribute(attribute) && types.isJSXIdentifier(attribute.name) && (attribute.name.name === "ref" || attribute.name.name === "dangerouslySetInnerHTML")
1055
+ )) {
1056
+ return void 0;
1057
+ }
1058
+ const children = meaningfulJsxChildren(container);
1059
+ if (children.length < (allowSingleRange ? 1 : 2)) return void 0;
1060
+ const ranges = [];
1061
+ const dependencies = /* @__PURE__ */ new Set();
1062
+ let staticBefore = 0;
1063
+ for (const child of children) {
1064
+ if (types.isJSXExpressionContainer(child) && !types.isJSXEmptyExpression(child.expression)) {
1065
+ const shape = conditionalBlockShape(child.expression);
1066
+ if (shape && !validateDerivedExpression(shape.test, safeGlobals)) {
1067
+ const truthyAnalysis = shape.truthy ? analyzeHostConditionalTree(shape.truthy, safeGlobals) : { bindings: [] };
1068
+ const falsyAnalysis = shape.falsy ? analyzeHostConditionalTree(shape.falsy, safeGlobals) : { bindings: [] };
1069
+ if (truthyAnalysis.reason || falsyAnalysis.reason) return void 0;
1070
+ const rangeDependencies = new Set(collectStateDependencies(shape.test, statesByValue));
1071
+ for (const binding of [
1072
+ ...truthyAnalysis.bindings || [],
1073
+ ...falsyAnalysis.bindings || []
1074
+ ]) {
1075
+ for (const dependency of collectStateDependencies(binding.value, statesByValue)) {
1076
+ rangeDependencies.add(dependency);
1077
+ }
1078
+ }
1079
+ for (const dependency of rangeDependencies) dependencies.add(dependency);
1080
+ ranges.push({
1081
+ before: staticBefore,
1082
+ source: child.expression,
1083
+ test: cloneExpression(shape.test),
1084
+ logical: shape.logical,
1085
+ truthy: shape.truthy,
1086
+ falsy: shape.falsy,
1087
+ truthyBindings: truthyAnalysis.bindings || [],
1088
+ falsyBindings: falsyAnalysis.bindings || []
1089
+ });
1090
+ staticBefore = 0;
1091
+ continue;
1092
+ }
1093
+ }
1094
+ if (!types.isJSXElement(child) || !isHostElement(child)) return void 0;
1095
+ if (!isStaticRangeSibling(child, statesByValue, safeGlobals)) return void 0;
1096
+ staticBefore += 1;
1097
+ }
1098
+ if (ranges.length === 0) return void 0;
1099
+ return {
1100
+ ranges,
1101
+ trailing: staticBefore,
1102
+ dependencies: [...dependencies].sort((left, right) => left - right)
1103
+ };
1104
+ }
1051
1105
  function keyedListBoundary(blockRuntime, plan, source) {
1052
1106
  const name = types.jsxMemberExpression(
1053
1107
  types.jsxIdentifier(blockRuntime.name),
@@ -1510,6 +1564,67 @@ function hostConditionalBoundary(blockRuntime, plan) {
1510
1564
  }
1511
1565
  return types.jsxElement(types.jsxOpeningElement(name, attributes, true), null, [], true);
1512
1566
  }
1567
+ function conditionalRangeObject(range) {
1568
+ const properties = [
1569
+ types.objectProperty(types.identifier("before"), types.numericLiteral(range.before)),
1570
+ types.objectProperty(
1571
+ types.identifier("test"),
1572
+ types.arrowFunctionExpression([], cloneExpression(range.test))
1573
+ )
1574
+ ];
1575
+ if (range.logical) {
1576
+ properties.push(types.objectProperty(types.identifier("logical"), types.booleanLiteral(true)));
1577
+ }
1578
+ if (range.truthy) {
1579
+ properties.push(
1580
+ types.objectProperty(
1581
+ types.identifier("truthy"),
1582
+ hostConditionalBranchObject(range.truthy, range.truthyBindings)
1583
+ )
1584
+ );
1585
+ }
1586
+ if (range.falsy) {
1587
+ properties.push(
1588
+ types.objectProperty(
1589
+ types.identifier("falsy"),
1590
+ hostConditionalBranchObject(range.falsy, range.falsyBindings)
1591
+ )
1592
+ );
1593
+ }
1594
+ return types.objectExpression(properties);
1595
+ }
1596
+ function conditionalRangesBoundary(blockRuntime, plan) {
1597
+ const name = types.jsxMemberExpression(
1598
+ types.jsxIdentifier(blockRuntime.name),
1599
+ types.jsxIdentifier("ConditionalRanges")
1600
+ );
1601
+ const source = types.cloneNode(plan.source, true);
1602
+ const rootRefIndex = source.openingElement.attributes.findIndex(
1603
+ (attribute) => types.isJSXAttribute(attribute) && types.isJSXIdentifier(attribute.name) && attribute.name.name === "ref"
1604
+ );
1605
+ const rootRef = rootRefIndex >= 0 ? source.openingElement.attributes.splice(rootRefIndex, 1)[0] : void 0;
1606
+ const attributes = [
1607
+ types.jsxAttribute(types.jsxIdentifier("id"), types.jsxExpressionContainer(types.numericLiteral(plan.id))),
1608
+ types.jsxAttribute(
1609
+ types.jsxIdentifier("render"),
1610
+ types.jsxExpressionContainer(types.arrowFunctionExpression([], source))
1611
+ ),
1612
+ types.jsxAttribute(
1613
+ types.jsxIdentifier("ranges"),
1614
+ types.jsxExpressionContainer(
1615
+ types.arrayExpression(plan.ranges.map((range) => conditionalRangeObject(range)))
1616
+ )
1617
+ ),
1618
+ types.jsxAttribute(
1619
+ types.jsxIdentifier("trailing"),
1620
+ types.jsxExpressionContainer(types.numericLiteral(plan.trailing))
1621
+ )
1622
+ ];
1623
+ if (rootRef?.value) {
1624
+ attributes.push(types.jsxAttribute(types.jsxIdentifier("rootRef"), types.cloneNode(rootRef.value, true)));
1625
+ }
1626
+ return types.jsxElement(types.jsxOpeningElement(name, attributes, true), null, [], true);
1627
+ }
1513
1628
  function analyzeComponentIslandElement(element, statesByValue, safeGlobals, allowedComponentNames) {
1514
1629
  const name = element.openingElement.name;
1515
1630
  if (!types.isJSXIdentifier(name) || !isComponentName(name.name)) {
@@ -1692,6 +1807,22 @@ function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, al
1692
1807
  }
1693
1808
  continue;
1694
1809
  }
1810
+ const conditionalRanges = insideConditional ? void 0 : analyzeConditionalRangesContainer(child, statesByValue, safeGlobals);
1811
+ if (conditionalRanges) {
1812
+ plans.push({
1813
+ kind: "conditional-ranges",
1814
+ id: nextId++,
1815
+ parent,
1816
+ dependencies: conditionalRanges.dependencies,
1817
+ source: child,
1818
+ ranges: conditionalRanges.ranges,
1819
+ trailing: conditionalRanges.trailing
1820
+ });
1821
+ for (const range of conditionalRanges.ranges) {
1822
+ conditionalExpressions.add(range.source);
1823
+ }
1824
+ continue;
1825
+ }
1695
1826
  const hostConditional = analyzeHostConditionalContainer(
1696
1827
  child,
1697
1828
  statesByValue,
@@ -1846,6 +1977,32 @@ function analyzeComposableBlocks(root, statesByValue, safeGlobals, listNames, al
1846
1977
  keyedExpressions
1847
1978
  };
1848
1979
  }
1980
+ const rootConditionalRanges = analyzeConditionalRangesContainer(
1981
+ root,
1982
+ statesByValue,
1983
+ safeGlobals,
1984
+ true
1985
+ );
1986
+ if (rootConditionalRanges) {
1987
+ plans.push({
1988
+ kind: "conditional-ranges",
1989
+ id: nextId++,
1990
+ dependencies: rootConditionalRanges.dependencies,
1991
+ source: root,
1992
+ ranges: rootConditionalRanges.ranges,
1993
+ trailing: rootConditionalRanges.trailing
1994
+ });
1995
+ for (const range of rootConditionalRanges.ranges) {
1996
+ conditionalExpressions.add(range.source);
1997
+ }
1998
+ return {
1999
+ plans,
2000
+ componentElements,
2001
+ conditionalExpressions,
2002
+ ownedElements,
2003
+ keyedExpressions
2004
+ };
2005
+ }
1849
2006
  const result = visitHost(root, void 0, false);
1850
2007
  if (result.reason) return { reason: result.reason };
1851
2008
  return {
@@ -1886,6 +2043,9 @@ function lowerComposableBlocks(root, plans, blockRuntime, listNames) {
1886
2043
  if (rootPlan?.kind === "keyed-ranges") {
1887
2044
  return keyedRangesBoundary(blockRuntime, rootPlan);
1888
2045
  }
2046
+ if (rootPlan?.kind === "conditional-ranges") {
2047
+ return conditionalRangesBoundary(blockRuntime, rootPlan);
2048
+ }
1889
2049
  const visit = (element) => {
1890
2050
  element.children = element.children.map((child) => {
1891
2051
  if (types.isJSXElement(child)) {
@@ -1899,6 +2059,9 @@ function lowerComposableBlocks(root, plans, blockRuntime, listNames) {
1899
2059
  if (plan2?.kind === "keyed-ranges") {
1900
2060
  return keyedRangesBoundary(blockRuntime, plan2);
1901
2061
  }
2062
+ if (plan2?.kind === "conditional-ranges") {
2063
+ return conditionalRangesBoundary(blockRuntime, plan2);
2064
+ }
1902
2065
  if (plan2?.kind === "component") {
1903
2066
  return componentIslandBoundary(blockRuntime, plan2, child);
1904
2067
  }