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