@homebound/truss 2.19.1 → 2.19.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.
@@ -1749,7 +1749,9 @@ 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(ctx, chain) {
1753
+ const { mapping } = ctx;
1754
+ const initialContext = ctx.initialContext ?? emptyConditionContext();
1753
1755
  const parts = [];
1754
1756
  const markers = [];
1755
1757
  const nestedErrors = [];
@@ -1779,7 +1781,7 @@ function resolveFullChain(chain, mapping, cssBindingName, initialContext = empty
1779
1781
  }
1780
1782
  parts.push({
1781
1783
  type: "unconditional",
1782
- segments: resolveChain(currentNodes, mapping, currentNodesStartContext, cssBindingName)
1784
+ segments: resolveChain({ ...ctx, initialContext: currentNodesStartContext }, currentNodes)
1783
1785
  });
1784
1786
  currentNodes = [];
1785
1787
  currentNodesStartContext = cloneConditionContext(currentContext);
@@ -1801,8 +1803,8 @@ function resolveFullChain(chain, mapping, cssBindingName, initialContext = empty
1801
1803
  const branchContext = cloneConditionContext(currentContext);
1802
1804
  const thenNodes = mediaStart.thenNodes ? [...mediaStart.thenNodes, ...filteredChain.slice(i + 1, elseIndex)] : filteredChain.slice(i, elseIndex);
1803
1805
  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);
1806
+ const thenSegs = resolveChain({ ...ctx, initialContext: branchContext }, thenNodes);
1807
+ const elseSegs = resolveChain({ ...ctx, initialContext: branchContext }, elseNodes);
1806
1808
  parts.push({ type: "unconditional", segments: [...thenSegs, ...elseSegs] });
1807
1809
  i = filteredChain.length;
1808
1810
  break;
@@ -1810,7 +1812,7 @@ function resolveFullChain(chain, mapping, cssBindingName, initialContext = empty
1810
1812
  }
1811
1813
  if (isWhenObjectCall(node)) {
1812
1814
  flushCurrentNodes();
1813
- const resolved = resolveWhenObjectSelectors(node, mapping, cssBindingName, currentContext);
1815
+ const resolved = resolveWhenObjectSelectors(ctx, node, currentContext);
1814
1816
  parts.push(...resolved.parts);
1815
1817
  markers.push(...resolved.markers);
1816
1818
  nestedErrors.push(...resolved.errors);
@@ -1846,8 +1848,8 @@ function resolveFullChain(chain, mapping, cssBindingName, initialContext = empty
1846
1848
  }
1847
1849
  i++;
1848
1850
  }
1849
- const thenSegs = resolveChain(thenNodes, mapping, branchContext, cssBindingName);
1850
- const elseSegs = resolveChain(elseNodes, mapping, branchContext, cssBindingName);
1851
+ const thenSegs = resolveChain({ ...ctx, initialContext: branchContext }, thenNodes);
1852
+ const elseSegs = resolveChain({ ...ctx, initialContext: branchContext }, elseNodes);
1851
1853
  parts.push({
1852
1854
  type: "conditional",
1853
1855
  conditionNode: node.conditionNode,
@@ -1874,7 +1876,8 @@ function resolveFullChain(chain, mapping, cssBindingName, initialContext = empty
1874
1876
  function isWhenObjectCall(node) {
1875
1877
  return node.type === "call" && node.name === "when" && node.args.length === 1 && node.args[0].type === "ObjectExpression";
1876
1878
  }
1877
- function resolveWhenObjectSelectors(node, mapping, cssBindingName, initialContext) {
1879
+ function resolveWhenObjectSelectors(ctx, node, initialContext) {
1880
+ const { cssBindingName } = ctx;
1878
1881
  if (!cssBindingName) {
1879
1882
  return {
1880
1883
  parts: [],
@@ -1901,16 +1904,13 @@ function resolveWhenObjectSelectors(node, mapping, cssBindingName, initialContex
1901
1904
  throw new UnsupportedPatternError(`when({ ... }) selector keys must be string literals`);
1902
1905
  }
1903
1906
  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);
1907
+ const innerChain = resolveWhenObjectValueChain(ctx, value);
1908
1908
  if (!innerChain) {
1909
1909
  throw new UnsupportedPatternError(`when({ ... }) values must be Css.*.$ expressions`);
1910
1910
  }
1911
1911
  const selectorContext = cloneConditionContext(initialContext);
1912
1912
  selectorContext.pseudoClass = property.key.value;
1913
- const resolved = resolveFullChain(innerChain, mapping, cssBindingName, selectorContext);
1913
+ const resolved = resolveFullChain({ ...ctx, initialContext: selectorContext }, innerChain);
1914
1914
  parts.push(...resolved.parts);
1915
1915
  markers.push(...resolved.markers);
1916
1916
  errors.push(...resolved.errors);
@@ -1924,6 +1924,13 @@ function resolveWhenObjectSelectors(node, mapping, cssBindingName, initialContex
1924
1924
  }
1925
1925
  return { parts, markers, errors: [...new Set(errors)] };
1926
1926
  }
1927
+ function resolveWhenObjectValueChain(ctx, value) {
1928
+ const { cssBindingName, resolveCssChainReference: resolveCssChainReference2 } = ctx;
1929
+ if (cssBindingName && value.type === "MemberExpression" && !value.computed && value.property.type === "Identifier" && value.property.name === "$") {
1930
+ return extractChain(value.object, cssBindingName);
1931
+ }
1932
+ return resolveCssChainReference2?.(value) ?? null;
1933
+ }
1927
1934
  function flattenWhenObjectParts(resolved) {
1928
1935
  const segments = [];
1929
1936
  for (const part of resolved.parts) {
@@ -1984,7 +1991,9 @@ function invertMediaQuery(query) {
1984
1991
  }
1985
1992
  return query.replace("@media", "@media not");
1986
1993
  }
1987
- function resolveChain(chain, mapping, initialContext = emptyConditionContext(), cssBindingName) {
1994
+ function resolveChain(ctx, chain) {
1995
+ const { mapping, cssBindingName } = ctx;
1996
+ const initialContext = ctx.initialContext ?? emptyConditionContext();
1988
1997
  const segments = [];
1989
1998
  const context = cloneConditionContext(initialContext);
1990
1999
  for (const node of chain) {
@@ -2084,7 +2093,7 @@ 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(ctx, node, context);
2088
2097
  segments.push(...flattenWhenObjectParts(resolved2));
2089
2098
  continue;
2090
2099
  }
@@ -3098,7 +3107,8 @@ function transformTruss(code, filename, mapping, options = {}) {
3098
3107
  if (parentPath && parentPath.isMemberExpression() && t4.isIdentifier(parentPath.node.property, { name: "$" })) {
3099
3108
  return;
3100
3109
  }
3101
- const resolvedChain = resolveFullChain(chain, mapping, cssBindingName);
3110
+ const resolveCssChainReference2 = buildCssChainReferenceResolver(path, cssBindingName);
3111
+ const resolvedChain = resolveFullChain({ mapping, cssBindingName, resolveCssChainReference: resolveCssChainReference2 }, chain);
3102
3112
  sites.push({ path, resolvedChain });
3103
3113
  const line = path.node.loc?.start.line ?? null;
3104
3114
  for (const err of resolvedChain.errors) {
@@ -3255,6 +3265,40 @@ function isInsideWhenObjectValue(path, cssBindingName) {
3255
3265
  }
3256
3266
  return false;
3257
3267
  }
3268
+ function buildCssChainReferenceResolver(path, cssBindingName) {
3269
+ return (node) => {
3270
+ return resolveCssChainReference(path, node, cssBindingName, /* @__PURE__ */ new Set());
3271
+ };
3272
+ }
3273
+ function resolveCssChainReference(path, node, cssBindingName, seen2) {
3274
+ const value = unwrapReferenceExpression(node);
3275
+ if (t4.isMemberExpression(value) && !value.computed && t4.isIdentifier(value.property, { name: "$" })) {
3276
+ return extractChain(value.object, cssBindingName);
3277
+ }
3278
+ if (!t4.isIdentifier(value) || seen2.has(value.name)) {
3279
+ return null;
3280
+ }
3281
+ const binding = path.scope.getBinding(value.name);
3282
+ if (!binding?.constant || !binding.path.isVariableDeclarator()) {
3283
+ return null;
3284
+ }
3285
+ const init = binding.path.node.init;
3286
+ if (!init || !t4.isExpression(init)) {
3287
+ return null;
3288
+ }
3289
+ seen2.add(value.name);
3290
+ return resolveCssChainReference(binding.path, init, cssBindingName, seen2);
3291
+ }
3292
+ function unwrapReferenceExpression(node) {
3293
+ let current = node;
3294
+ while (true) {
3295
+ if (t4.isParenthesizedExpression(current) || t4.isTSAsExpression(current) || t4.isTSTypeAssertion(current) || t4.isTSNonNullExpression(current) || t4.isTSSatisfiesExpression(current)) {
3296
+ current = current.expression;
3297
+ continue;
3298
+ }
3299
+ return current;
3300
+ }
3301
+ }
3258
3302
  function collectRuntimeLookups(chains) {
3259
3303
  const lookups = /* @__PURE__ */ new Map();
3260
3304
  for (const chain of chains) {
@@ -3461,8 +3505,11 @@ function resolveCssExpression(node, cssBindingName, mapping, filename) {
3461
3505
  for (const n of chain) {
3462
3506
  if (n.type === "if") return { error: "if() conditionals are not supported in .css.ts files" };
3463
3507
  if (n.type === "else") return { error: "else is not supported in .css.ts files" };
3508
+ if (n.type === "call" && n.name === "when") {
3509
+ return { error: "when() modifiers are not supported in .css.ts files" };
3510
+ }
3464
3511
  }
3465
- const resolved = resolveFullChain(chain, mapping, cssBindingName);
3512
+ const resolved = resolveFullChain({ mapping, cssBindingName }, chain);
3466
3513
  if (resolved.errors.length > 0) {
3467
3514
  return { error: resolved.errors[0] };
3468
3515
  }