@homebound/truss 2.19.0 → 2.19.2

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.
@@ -1749,7 +1749,7 @@ function applyModifierNodeToConditionContext(context, node, mapping) {
1749
1749
  context.pseudoClass = pseudoSelector(node.name);
1750
1750
  }
1751
1751
  }
1752
- function resolveFullChain(chain, mapping, cssBindingName, initialContext = emptyConditionContext()) {
1752
+ function resolveFullChain(chain, mapping, cssBindingName, initialContext = emptyConditionContext(), resolveCssChainReference2) {
1753
1753
  const parts = [];
1754
1754
  const markers = [];
1755
1755
  const nestedErrors = [];
@@ -1779,7 +1779,7 @@ function resolveFullChain(chain, mapping, cssBindingName, initialContext = empty
1779
1779
  }
1780
1780
  parts.push({
1781
1781
  type: "unconditional",
1782
- segments: resolveChain(currentNodes, mapping, currentNodesStartContext, cssBindingName)
1782
+ segments: resolveChain(currentNodes, mapping, currentNodesStartContext, cssBindingName, resolveCssChainReference2)
1783
1783
  });
1784
1784
  currentNodes = [];
1785
1785
  currentNodesStartContext = cloneConditionContext(currentContext);
@@ -1801,8 +1801,8 @@ function resolveFullChain(chain, mapping, cssBindingName, initialContext = empty
1801
1801
  const branchContext = cloneConditionContext(currentContext);
1802
1802
  const thenNodes = mediaStart.thenNodes ? [...mediaStart.thenNodes, ...filteredChain.slice(i + 1, elseIndex)] : filteredChain.slice(i, elseIndex);
1803
1803
  const elseNodes = [makeMediaQueryNode(mediaStart.inverseMediaQuery), ...filteredChain.slice(elseIndex + 1)];
1804
- const thenSegs = resolveChain(thenNodes, mapping, branchContext, cssBindingName);
1805
- const elseSegs = resolveChain(elseNodes, mapping, branchContext, cssBindingName);
1804
+ const thenSegs = resolveChain(thenNodes, mapping, branchContext, cssBindingName, resolveCssChainReference2);
1805
+ const elseSegs = resolveChain(elseNodes, mapping, branchContext, cssBindingName, resolveCssChainReference2);
1806
1806
  parts.push({ type: "unconditional", segments: [...thenSegs, ...elseSegs] });
1807
1807
  i = filteredChain.length;
1808
1808
  break;
@@ -1810,7 +1810,13 @@ function resolveFullChain(chain, mapping, cssBindingName, initialContext = empty
1810
1810
  }
1811
1811
  if (isWhenObjectCall(node)) {
1812
1812
  flushCurrentNodes();
1813
- const resolved = resolveWhenObjectSelectors(node, mapping, cssBindingName, currentContext);
1813
+ const resolved = resolveWhenObjectSelectors(
1814
+ node,
1815
+ mapping,
1816
+ cssBindingName,
1817
+ currentContext,
1818
+ resolveCssChainReference2
1819
+ );
1814
1820
  parts.push(...resolved.parts);
1815
1821
  markers.push(...resolved.markers);
1816
1822
  nestedErrors.push(...resolved.errors);
@@ -1846,8 +1852,8 @@ function resolveFullChain(chain, mapping, cssBindingName, initialContext = empty
1846
1852
  }
1847
1853
  i++;
1848
1854
  }
1849
- const thenSegs = resolveChain(thenNodes, mapping, branchContext, cssBindingName);
1850
- const elseSegs = resolveChain(elseNodes, mapping, branchContext, cssBindingName);
1855
+ const thenSegs = resolveChain(thenNodes, mapping, branchContext, cssBindingName, resolveCssChainReference2);
1856
+ const elseSegs = resolveChain(elseNodes, mapping, branchContext, cssBindingName, resolveCssChainReference2);
1851
1857
  parts.push({
1852
1858
  type: "conditional",
1853
1859
  conditionNode: node.conditionNode,
@@ -1874,7 +1880,7 @@ function resolveFullChain(chain, mapping, cssBindingName, initialContext = empty
1874
1880
  function isWhenObjectCall(node) {
1875
1881
  return node.type === "call" && node.name === "when" && node.args.length === 1 && node.args[0].type === "ObjectExpression";
1876
1882
  }
1877
- function resolveWhenObjectSelectors(node, mapping, cssBindingName, initialContext) {
1883
+ function resolveWhenObjectSelectors(node, mapping, cssBindingName, initialContext, resolveCssChainReference2) {
1878
1884
  if (!cssBindingName) {
1879
1885
  return {
1880
1886
  parts: [],
@@ -1901,16 +1907,13 @@ function resolveWhenObjectSelectors(node, mapping, cssBindingName, initialContex
1901
1907
  throw new UnsupportedPatternError(`when({ ... }) selector keys must be string literals`);
1902
1908
  }
1903
1909
  const value = unwrapExpression(property.value);
1904
- if (value.type !== "MemberExpression" || value.computed || value.property.type !== "Identifier" || value.property.name !== "$") {
1905
- throw new UnsupportedPatternError(`when({ ... }) values must be Css.*.$ expressions`);
1906
- }
1907
- const innerChain = extractChain(value.object, cssBindingName);
1910
+ const innerChain = resolveWhenObjectValueChain(value, cssBindingName, resolveCssChainReference2);
1908
1911
  if (!innerChain) {
1909
1912
  throw new UnsupportedPatternError(`when({ ... }) values must be Css.*.$ expressions`);
1910
1913
  }
1911
1914
  const selectorContext = cloneConditionContext(initialContext);
1912
1915
  selectorContext.pseudoClass = property.key.value;
1913
- const resolved = resolveFullChain(innerChain, mapping, cssBindingName, selectorContext);
1916
+ const resolved = resolveFullChain(innerChain, mapping, cssBindingName, selectorContext, resolveCssChainReference2);
1914
1917
  parts.push(...resolved.parts);
1915
1918
  markers.push(...resolved.markers);
1916
1919
  errors.push(...resolved.errors);
@@ -1924,6 +1927,12 @@ function resolveWhenObjectSelectors(node, mapping, cssBindingName, initialContex
1924
1927
  }
1925
1928
  return { parts, markers, errors: [...new Set(errors)] };
1926
1929
  }
1930
+ function resolveWhenObjectValueChain(value, cssBindingName, resolveCssChainReference2) {
1931
+ if (cssBindingName && value.type === "MemberExpression" && !value.computed && value.property.type === "Identifier" && value.property.name === "$") {
1932
+ return extractChain(value.object, cssBindingName);
1933
+ }
1934
+ return resolveCssChainReference2?.(value) ?? null;
1935
+ }
1927
1936
  function flattenWhenObjectParts(resolved) {
1928
1937
  const segments = [];
1929
1938
  for (const part of resolved.parts) {
@@ -1984,7 +1993,7 @@ function invertMediaQuery(query) {
1984
1993
  }
1985
1994
  return query.replace("@media", "@media not");
1986
1995
  }
1987
- function resolveChain(chain, mapping, initialContext = emptyConditionContext(), cssBindingName) {
1996
+ function resolveChain(chain, mapping, initialContext = emptyConditionContext(), cssBindingName, resolveCssChainReference2) {
1988
1997
  const segments = [];
1989
1998
  const context = cloneConditionContext(initialContext);
1990
1999
  for (const node of chain) {
@@ -2084,7 +2093,13 @@ function resolveChain(chain, mapping, initialContext = emptyConditionContext(),
2084
2093
  }
2085
2094
  if (abbr === "when") {
2086
2095
  if (isWhenObjectCall(node)) {
2087
- const resolved2 = resolveWhenObjectSelectors(node, mapping, cssBindingName, context);
2096
+ const resolved2 = resolveWhenObjectSelectors(
2097
+ node,
2098
+ mapping,
2099
+ cssBindingName,
2100
+ context,
2101
+ resolveCssChainReference2
2102
+ );
2088
2103
  segments.push(...flattenWhenObjectParts(resolved2));
2089
2104
  continue;
2090
2105
  }
@@ -3098,7 +3113,8 @@ function transformTruss(code, filename, mapping, options = {}) {
3098
3113
  if (parentPath && parentPath.isMemberExpression() && t4.isIdentifier(parentPath.node.property, { name: "$" })) {
3099
3114
  return;
3100
3115
  }
3101
- const resolvedChain = resolveFullChain(chain, mapping, cssBindingName);
3116
+ const resolveCssChainReference2 = buildCssChainReferenceResolver(path, cssBindingName);
3117
+ const resolvedChain = resolveFullChain(chain, mapping, cssBindingName, void 0, resolveCssChainReference2);
3102
3118
  sites.push({ path, resolvedChain });
3103
3119
  const line = path.node.loc?.start.line ?? null;
3104
3120
  for (const err of resolvedChain.errors) {
@@ -3255,6 +3271,40 @@ function isInsideWhenObjectValue(path, cssBindingName) {
3255
3271
  }
3256
3272
  return false;
3257
3273
  }
3274
+ function buildCssChainReferenceResolver(path, cssBindingName) {
3275
+ return (node) => {
3276
+ return resolveCssChainReference(path, node, cssBindingName, /* @__PURE__ */ new Set());
3277
+ };
3278
+ }
3279
+ function resolveCssChainReference(path, node, cssBindingName, seen2) {
3280
+ const value = unwrapReferenceExpression(node);
3281
+ if (t4.isMemberExpression(value) && !value.computed && t4.isIdentifier(value.property, { name: "$" })) {
3282
+ return extractChain(value.object, cssBindingName);
3283
+ }
3284
+ if (!t4.isIdentifier(value) || seen2.has(value.name)) {
3285
+ return null;
3286
+ }
3287
+ const binding = path.scope.getBinding(value.name);
3288
+ if (!binding?.constant || !binding.path.isVariableDeclarator()) {
3289
+ return null;
3290
+ }
3291
+ const init = binding.path.node.init;
3292
+ if (!init || !t4.isExpression(init)) {
3293
+ return null;
3294
+ }
3295
+ seen2.add(value.name);
3296
+ return resolveCssChainReference(binding.path, init, cssBindingName, seen2);
3297
+ }
3298
+ function unwrapReferenceExpression(node) {
3299
+ let current = node;
3300
+ while (true) {
3301
+ if (t4.isParenthesizedExpression(current) || t4.isTSAsExpression(current) || t4.isTSTypeAssertion(current) || t4.isTSNonNullExpression(current) || t4.isTSSatisfiesExpression(current)) {
3302
+ current = current.expression;
3303
+ continue;
3304
+ }
3305
+ return current;
3306
+ }
3307
+ }
3258
3308
  function collectRuntimeLookups(chains) {
3259
3309
  const lookups = /* @__PURE__ */ new Map();
3260
3310
  for (const chain of chains) {
@@ -3461,6 +3511,9 @@ function resolveCssExpression(node, cssBindingName, mapping, filename) {
3461
3511
  for (const n of chain) {
3462
3512
  if (n.type === "if") return { error: "if() conditionals are not supported in .css.ts files" };
3463
3513
  if (n.type === "else") return { error: "else is not supported in .css.ts files" };
3514
+ if (n.type === "call" && n.name === "when") {
3515
+ return { error: "when() modifiers are not supported in .css.ts files" };
3516
+ }
3464
3517
  }
3465
3518
  const resolved = resolveFullChain(chain, mapping, cssBindingName);
3466
3519
  if (resolved.errors.length > 0) {