@homebound/truss 2.0.5 → 2.0.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/build/plugin/index.js +141 -320
- package/build/plugin/index.js.map +1 -1
- package/package.json +1 -1
package/build/plugin/index.js
CHANGED
|
@@ -1355,7 +1355,7 @@ function buildPropsArgs(segments, options) {
|
|
|
1355
1355
|
if (seg.styleArrayArg) {
|
|
1356
1356
|
args.push(
|
|
1357
1357
|
t3.spreadElement(
|
|
1358
|
-
|
|
1358
|
+
buildUnknownSpreadFallback(
|
|
1359
1359
|
seg.styleArrayArg,
|
|
1360
1360
|
options.asStyleArrayHelperName,
|
|
1361
1361
|
options.needsAsStyleArrayHelper
|
|
@@ -1394,13 +1394,6 @@ function rewriteCssAttributeExpressions(ast, filename, debug, stylexNamespaceNam
|
|
|
1394
1394
|
const value = path.node.value;
|
|
1395
1395
|
if (!t3.isJSXExpressionContainer(value)) return;
|
|
1396
1396
|
if (!t3.isExpression(value.expression)) return;
|
|
1397
|
-
if (!isCssRewriteableExpression(value.expression, path)) {
|
|
1398
|
-
skippedCssPropMessages.push({
|
|
1399
|
-
message: explainSkippedCssRewrite(value.expression, path),
|
|
1400
|
-
line: path.node.loc?.start.line ?? null
|
|
1401
|
-
});
|
|
1402
|
-
return;
|
|
1403
|
-
}
|
|
1404
1397
|
const propsArgs = lowerCssExpressionToPropsArgs(
|
|
1405
1398
|
value.expression,
|
|
1406
1399
|
path,
|
|
@@ -1464,11 +1457,8 @@ function buildDebugElements(line, options) {
|
|
|
1464
1457
|
options.needsTrussDebugInfo.current = true;
|
|
1465
1458
|
return [t3.newExpression(t3.identifier(options.trussDebugInfoName), [t3.stringLiteral(`${options.filename}:${line}`)])];
|
|
1466
1459
|
}
|
|
1467
|
-
function isCssRewriteableExpression(expr, path) {
|
|
1468
|
-
return !!lowerCssExpressionToPropsArgs(expr, path, "asStyleArray", { current: false });
|
|
1469
|
-
}
|
|
1470
1460
|
function lowerCssExpressionToPropsArgs(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1471
|
-
return buildStyleObjectPropsArgs(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) ?? buildStyleArrayLikePropsArgsFromExpression(expr, path
|
|
1461
|
+
return buildStyleObjectPropsArgs(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) ?? buildStyleArrayLikePropsArgsFromExpression(expr, path);
|
|
1472
1462
|
}
|
|
1473
1463
|
function explainSkippedCssRewrite(expr, path) {
|
|
1474
1464
|
if (t3.isObjectExpression(expr)) {
|
|
@@ -1476,7 +1466,7 @@ function explainSkippedCssRewrite(expr, path) {
|
|
|
1476
1466
|
if (!t3.isSpreadElement(prop)) {
|
|
1477
1467
|
return `[truss] Unsupported pattern: Could not rewrite css prop: object contains a non-spread property (${formatNodeSnippet(expr)})`;
|
|
1478
1468
|
}
|
|
1479
|
-
const normalizedArg =
|
|
1469
|
+
const normalizedArg = normalizeStyleExpression(prop.argument);
|
|
1480
1470
|
if (!normalizedArg) {
|
|
1481
1471
|
return `[truss] Unsupported pattern: Could not rewrite css prop: spread argument is not style-array-like (${formatNodeSnippet(prop.argument)})`;
|
|
1482
1472
|
}
|
|
@@ -1520,53 +1510,46 @@ function removeExistingClassNameAttribute(path) {
|
|
|
1520
1510
|
}
|
|
1521
1511
|
function buildStyleObjectPropsArgs(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1522
1512
|
if (!t3.isObjectExpression(expr) || expr.properties.length === 0) return null;
|
|
1523
|
-
const
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
}
|
|
1538
|
-
if (result.droppedPropertyKeys.length > 0) return null;
|
|
1539
|
-
return buildPropsArgsFromStyleCompositionEntries(
|
|
1540
|
-
result.entries,
|
|
1541
|
-
path,
|
|
1542
|
-
asStyleArrayHelperName,
|
|
1543
|
-
needsAsStyleArrayHelper
|
|
1544
|
-
);
|
|
1513
|
+
const allSpreads = expr.properties.every(function(prop) {
|
|
1514
|
+
return t3.isSpreadElement(prop);
|
|
1515
|
+
});
|
|
1516
|
+
if (!allSpreads) return null;
|
|
1517
|
+
if (hasStyleArraySpread(expr, path)) {
|
|
1518
|
+
const result = flattenStyleObject(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper);
|
|
1519
|
+
return result.elements.filter(Boolean);
|
|
1520
|
+
}
|
|
1521
|
+
return expr.properties.map(function(prop) {
|
|
1522
|
+
const spread = prop;
|
|
1523
|
+
return t3.spreadElement(
|
|
1524
|
+
buildUnknownSpreadFallback(spread.argument, asStyleArrayHelperName, needsAsStyleArrayHelper)
|
|
1525
|
+
// I.e. `css={{ ...css }}` or `css={{ ...xss }}`
|
|
1526
|
+
);
|
|
1527
|
+
});
|
|
1545
1528
|
}
|
|
1546
|
-
function buildStyleArrayLikePropsArgsFromExpression(expr, path
|
|
1547
|
-
const normalizedExpr =
|
|
1529
|
+
function buildStyleArrayLikePropsArgsFromExpression(expr, path) {
|
|
1530
|
+
const normalizedExpr = normalizeStyleExpression(expr);
|
|
1548
1531
|
if (!normalizedExpr) return null;
|
|
1549
|
-
return buildStyleArrayLikePropsArgs(normalizedExpr, path
|
|
1532
|
+
return buildStyleArrayLikePropsArgs(normalizedExpr, path);
|
|
1550
1533
|
}
|
|
1551
|
-
function buildStyleArrayLikePropsArgs(expr, path
|
|
1552
|
-
if (seen.has(expr)) return null;
|
|
1553
|
-
seen.add(expr);
|
|
1534
|
+
function buildStyleArrayLikePropsArgs(expr, path) {
|
|
1554
1535
|
if (t3.isArrayExpression(expr)) {
|
|
1555
1536
|
const propsArgs = [];
|
|
1556
1537
|
for (const el of expr.elements) {
|
|
1557
1538
|
if (!el) continue;
|
|
1558
1539
|
if (t3.isSpreadElement(el)) {
|
|
1559
|
-
const normalizedArg =
|
|
1540
|
+
const normalizedArg = normalizeStyleExpression(el.argument);
|
|
1560
1541
|
if (!normalizedArg) {
|
|
1561
1542
|
propsArgs.push(t3.spreadElement(el.argument));
|
|
1562
1543
|
continue;
|
|
1563
1544
|
}
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1545
|
+
if (t3.isArrayExpression(normalizedArg)) {
|
|
1546
|
+
const nestedArgs = buildStyleArrayLikePropsArgs(normalizedArg, path);
|
|
1547
|
+
if (nestedArgs) {
|
|
1548
|
+
propsArgs.push(...nestedArgs);
|
|
1549
|
+
continue;
|
|
1550
|
+
}
|
|
1569
1551
|
}
|
|
1552
|
+
propsArgs.push(t3.spreadElement(buildSafeSpreadArgument(normalizedArg)));
|
|
1570
1553
|
continue;
|
|
1571
1554
|
}
|
|
1572
1555
|
propsArgs.push(el);
|
|
@@ -1581,15 +1564,15 @@ function buildStyleArrayLikePropsArgs(expr, path, seen) {
|
|
|
1581
1564
|
function rewriteStyleObjectExpressions(ast, messages, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1582
1565
|
traverse(ast, {
|
|
1583
1566
|
ObjectExpression(path) {
|
|
1584
|
-
|
|
1585
|
-
|
|
1567
|
+
if (!hasStyleArraySpread(path.node, path)) return;
|
|
1568
|
+
const result = flattenStyleObject(path.node, path, asStyleArrayHelperName, needsAsStyleArrayHelper);
|
|
1586
1569
|
if (result.droppedPropertyKeys.length > 0) {
|
|
1587
1570
|
messages.push({
|
|
1588
1571
|
message: `[truss] Unsupported pattern: Dropped non-spread properties from style composition object (${result.droppedPropertyKeys.join(", ")})`,
|
|
1589
1572
|
line: path.node.loc?.start.line ?? null
|
|
1590
1573
|
});
|
|
1591
1574
|
}
|
|
1592
|
-
path.replaceWith(result.
|
|
1575
|
+
path.replaceWith(t3.arrayExpression(result.elements));
|
|
1593
1576
|
}
|
|
1594
1577
|
});
|
|
1595
1578
|
}
|
|
@@ -1601,258 +1584,147 @@ function rewriteCssSpreadCalls(ast, cssBindingName, asStyleArrayHelperName, need
|
|
|
1601
1584
|
if (!arg || t3.isSpreadElement(arg) || !t3.isExpression(arg) || path.node.arguments.length !== 1) return;
|
|
1602
1585
|
const styleObject = unwrapStyleObjectExpression(arg);
|
|
1603
1586
|
if (!styleObject) return;
|
|
1604
|
-
const result =
|
|
1605
|
-
|
|
1606
|
-
path,
|
|
1607
|
-
asStyleArrayHelperName,
|
|
1608
|
-
needsAsStyleArrayHelper
|
|
1609
|
-
);
|
|
1610
|
-
if (!result.rewritten) return;
|
|
1611
|
-
path.replaceWith(result.rewritten);
|
|
1587
|
+
const result = flattenStyleObject(styleObject, path, asStyleArrayHelperName, needsAsStyleArrayHelper);
|
|
1588
|
+
path.replaceWith(t3.arrayExpression(result.elements));
|
|
1612
1589
|
}
|
|
1613
1590
|
});
|
|
1614
1591
|
}
|
|
1615
|
-
function
|
|
1616
|
-
|
|
1617
|
-
}
|
|
1618
|
-
function tryBuildStyleArrayFromObjectExpression(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1619
|
-
const result = collectStyleCompositionEntries(expr, path);
|
|
1620
|
-
if (!result) return { rewritten: null, droppedPropertyKeys: [] };
|
|
1621
|
-
const elements = buildArrayElementsFromStyleCompositionEntries(
|
|
1622
|
-
result.entries,
|
|
1623
|
-
path,
|
|
1624
|
-
asStyleArrayHelperName,
|
|
1625
|
-
needsAsStyleArrayHelper
|
|
1626
|
-
);
|
|
1627
|
-
return { rewritten: t3.arrayExpression(elements), droppedPropertyKeys: result.droppedPropertyKeys };
|
|
1628
|
-
}
|
|
1629
|
-
function collectStyleCompositionEntries(expr, path) {
|
|
1630
|
-
if (expr.properties.length === 0) return null;
|
|
1631
|
-
let sawKnownStyle = false;
|
|
1632
|
-
const entries = [];
|
|
1592
|
+
function flattenStyleObject(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1593
|
+
const elements = [];
|
|
1633
1594
|
const droppedPropertyKeys = [];
|
|
1634
1595
|
for (const prop of expr.properties) {
|
|
1635
1596
|
if (!t3.isSpreadElement(prop)) {
|
|
1636
1597
|
droppedPropertyKeys.push(formatDroppedPropertyKey(prop));
|
|
1637
1598
|
continue;
|
|
1638
1599
|
}
|
|
1639
|
-
const
|
|
1640
|
-
if (!
|
|
1641
|
-
|
|
1600
|
+
const normalized = normalizeStyleExpression(prop.argument);
|
|
1601
|
+
if (!normalized) {
|
|
1602
|
+
elements.push(
|
|
1603
|
+
t3.spreadElement(buildUnknownSpreadFallback(prop.argument, asStyleArrayHelperName, needsAsStyleArrayHelper))
|
|
1604
|
+
);
|
|
1642
1605
|
continue;
|
|
1643
1606
|
}
|
|
1644
|
-
if (
|
|
1645
|
-
|
|
1646
|
-
|
|
1607
|
+
if (t3.isArrayExpression(normalized)) {
|
|
1608
|
+
elements.push(...normalized.elements);
|
|
1609
|
+
} else if (expressionContainsArray(normalized, path)) {
|
|
1610
|
+
elements.push(t3.spreadElement(buildSafeSpreadArgument(normalized)));
|
|
1647
1611
|
} else {
|
|
1648
|
-
|
|
1612
|
+
elements.push(
|
|
1613
|
+
t3.spreadElement(buildUnknownSpreadFallback(normalized, asStyleArrayHelperName, needsAsStyleArrayHelper))
|
|
1614
|
+
// I.e. `...borderBottomStyles`
|
|
1615
|
+
);
|
|
1649
1616
|
}
|
|
1650
1617
|
}
|
|
1651
|
-
return
|
|
1618
|
+
return { elements, droppedPropertyKeys };
|
|
1652
1619
|
}
|
|
1653
|
-
function
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
if (nestedArgs && t3.isArrayExpression(entry.expr)) {
|
|
1658
|
-
propsArgs.push(...nestedArgs);
|
|
1659
|
-
continue;
|
|
1660
|
-
}
|
|
1661
|
-
propsArgs.push(
|
|
1662
|
-
t3.spreadElement(
|
|
1663
|
-
entry.kind === "known" ? buildSafeSpreadArgument(entry.expr) : buildUnknownObjectSpreadFallback(entry.expr, asStyleArrayHelperName, needsAsStyleArrayHelper)
|
|
1664
|
-
)
|
|
1665
|
-
);
|
|
1666
|
-
}
|
|
1667
|
-
return propsArgs;
|
|
1620
|
+
function hasStyleArraySpread(expr, path) {
|
|
1621
|
+
return expr.properties.some(function(prop) {
|
|
1622
|
+
return t3.isSpreadElement(prop) && expressionContainsArray(prop.argument, path);
|
|
1623
|
+
});
|
|
1668
1624
|
}
|
|
1669
|
-
function
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1625
|
+
function expressionContainsArray(expr, path) {
|
|
1626
|
+
if (t3.isArrayExpression(expr)) return true;
|
|
1627
|
+
if (t3.isConditionalExpression(expr)) {
|
|
1628
|
+
return expressionContainsArray(expr.consequent, path) || expressionContainsArray(expr.alternate, path);
|
|
1629
|
+
}
|
|
1630
|
+
if (t3.isLogicalExpression(expr)) {
|
|
1631
|
+
return expressionContainsArray(expr.left, path) || expressionContainsArray(expr.right, path);
|
|
1632
|
+
}
|
|
1633
|
+
if (t3.isObjectExpression(expr)) {
|
|
1634
|
+
return expr.properties.some(function(p) {
|
|
1635
|
+
return t3.isSpreadElement(p) && expressionContainsArray(p.argument, path);
|
|
1636
|
+
});
|
|
1637
|
+
}
|
|
1638
|
+
if (t3.isIdentifier(expr)) {
|
|
1639
|
+
const binding = path.scope.getBinding(expr.name);
|
|
1640
|
+
if (binding?.path.isVariableDeclarator()) {
|
|
1641
|
+
const init = binding.path.node.init;
|
|
1642
|
+
if (init) return expressionContainsArray(init, binding.path);
|
|
1643
|
+
}
|
|
1644
|
+
return false;
|
|
1645
|
+
}
|
|
1646
|
+
if (t3.isMemberExpression(expr) && t3.isIdentifier(expr.object)) {
|
|
1647
|
+
const binding = path.scope.getBinding(expr.object.name);
|
|
1648
|
+
if (binding?.path.isVariableDeclarator()) {
|
|
1649
|
+
const init = binding.path.node.init;
|
|
1650
|
+
if (init && t3.isObjectExpression(init)) {
|
|
1651
|
+
const propName = getStaticMemberPropertyName(expr, path);
|
|
1652
|
+
if (propName) {
|
|
1653
|
+
for (const prop of init.properties) {
|
|
1654
|
+
if (!t3.isObjectProperty(prop) || prop.computed) continue;
|
|
1655
|
+
if (!isMatchingPropertyName(prop.key, propName)) continue;
|
|
1656
|
+
if (t3.isExpression(prop.value)) {
|
|
1657
|
+
return expressionContainsArray(prop.value, binding.path);
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1676
1662
|
}
|
|
1677
|
-
|
|
1678
|
-
t3.spreadElement(
|
|
1679
|
-
entry.kind === "known" ? buildSafeSpreadArgument(entry.expr) : asStyleArrayHelperName && needsAsStyleArrayHelper ? buildUnknownObjectSpreadFallback(entry.expr, asStyleArrayHelperName, needsAsStyleArrayHelper) : buildInlineAsStyleArrayExpression(entry.expr)
|
|
1680
|
-
)
|
|
1681
|
-
);
|
|
1663
|
+
return false;
|
|
1682
1664
|
}
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
return t3.isMemberExpression(expr.callee) && !expr.callee.computed && t3.isIdentifier(expr.callee.object, { name: cssBindingName }) && t3.isIdentifier(expr.callee.property, { name: "spread" });
|
|
1687
|
-
}
|
|
1688
|
-
function unwrapStyleObjectExpression(expr) {
|
|
1689
|
-
if (t3.isObjectExpression(expr)) return expr;
|
|
1690
|
-
if (t3.isTSAsExpression(expr) || t3.isTSSatisfiesExpression(expr) || t3.isTSNonNullExpression(expr)) {
|
|
1691
|
-
return unwrapStyleObjectExpression(expr.expression);
|
|
1665
|
+
if (t3.isCallExpression(expr)) {
|
|
1666
|
+
const returnExpr = getCallReturnExpression(expr, path);
|
|
1667
|
+
return returnExpr ? expressionContainsArray(returnExpr, path) : false;
|
|
1692
1668
|
}
|
|
1693
|
-
return
|
|
1669
|
+
return false;
|
|
1694
1670
|
}
|
|
1695
|
-
function
|
|
1696
|
-
if (seen.has(expr)) return null;
|
|
1697
|
-
seen.add(expr);
|
|
1671
|
+
function normalizeStyleExpression(expr) {
|
|
1698
1672
|
if (t3.isArrayExpression(expr)) return expr;
|
|
1673
|
+
if (isEmptyObjectExpression(expr)) return t3.arrayExpression([]);
|
|
1699
1674
|
if (t3.isLogicalExpression(expr) && expr.operator === "&&") {
|
|
1700
|
-
const consequent =
|
|
1675
|
+
const consequent = normalizeStyleExpression(expr.right);
|
|
1701
1676
|
if (!consequent) return null;
|
|
1702
1677
|
return t3.conditionalExpression(expr.left, consequent, t3.arrayExpression([]));
|
|
1703
1678
|
}
|
|
1704
1679
|
if (t3.isLogicalExpression(expr) && (expr.operator === "||" || expr.operator === "??")) {
|
|
1705
|
-
const left =
|
|
1706
|
-
const right =
|
|
1680
|
+
const left = normalizeStyleExpression(expr.left);
|
|
1681
|
+
const right = normalizeStyleBranch(expr.right);
|
|
1707
1682
|
if (!left || !right) return null;
|
|
1708
1683
|
return t3.logicalExpression(expr.operator, left, right);
|
|
1709
1684
|
}
|
|
1710
1685
|
if (t3.isConditionalExpression(expr)) {
|
|
1711
|
-
const consequent =
|
|
1712
|
-
const alternate =
|
|
1686
|
+
const consequent = normalizeStyleBranch(expr.consequent);
|
|
1687
|
+
const alternate = normalizeStyleBranch(expr.alternate);
|
|
1713
1688
|
if (!consequent || !alternate) return null;
|
|
1714
1689
|
return t3.conditionalExpression(expr.test, consequent, alternate);
|
|
1715
1690
|
}
|
|
1716
1691
|
if (t3.isIdentifier(expr) || t3.isMemberExpression(expr) || t3.isCallExpression(expr)) {
|
|
1717
|
-
|
|
1718
|
-
nestedSeen.delete(expr);
|
|
1719
|
-
if (isStyleArrayLike(expr, path, nestedSeen)) return expr;
|
|
1692
|
+
return expr;
|
|
1720
1693
|
}
|
|
1721
1694
|
return null;
|
|
1722
1695
|
}
|
|
1723
|
-
function
|
|
1724
|
-
if (isEmptyObjectExpression(expr))
|
|
1725
|
-
return t3.arrayExpression([]);
|
|
1726
|
-
}
|
|
1696
|
+
function normalizeStyleBranch(expr) {
|
|
1697
|
+
if (isEmptyObjectExpression(expr)) return t3.arrayExpression([]);
|
|
1727
1698
|
if (t3.isObjectExpression(expr)) {
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
}
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
return isStyleArrayLikeBranch(expr.consequent, path, seen) && isStyleArrayLikeBranch(expr.alternate, path, seen);
|
|
1744
|
-
}
|
|
1745
|
-
if (t3.isIdentifier(expr)) {
|
|
1746
|
-
const possibleValues = resolvePossibleStyleValues(expr, path, new Set(seen));
|
|
1747
|
-
if (possibleValues) {
|
|
1748
|
-
return possibleValues.every(function(value) {
|
|
1749
|
-
return isStyleArrayLike(value, path, new Set(seen));
|
|
1750
|
-
});
|
|
1751
|
-
}
|
|
1752
|
-
const binding = path.scope.getBinding(expr.name);
|
|
1753
|
-
const bindingPath = binding?.path;
|
|
1754
|
-
if (!bindingPath || !bindingPath.isVariableDeclarator()) return false;
|
|
1755
|
-
const init = bindingPath.node.init;
|
|
1756
|
-
return !!(init && isStyleArrayLike(init, bindingPath, seen));
|
|
1757
|
-
}
|
|
1758
|
-
if (t3.isCallExpression(expr)) {
|
|
1759
|
-
const possibleValues = resolvePossibleStyleValues(expr, path, new Set(seen));
|
|
1760
|
-
if (possibleValues) {
|
|
1761
|
-
return possibleValues.every(function(value) {
|
|
1762
|
-
return isStyleArrayLike(value, path, new Set(seen));
|
|
1763
|
-
});
|
|
1764
|
-
}
|
|
1765
|
-
const returnExpr = getCallStyleArrayLikeExpression(expr, path);
|
|
1766
|
-
return returnExpr ? isStyleArrayLike(returnExpr, path, seen) : true;
|
|
1767
|
-
}
|
|
1768
|
-
if (t3.isMemberExpression(expr)) {
|
|
1769
|
-
const possibleValues = resolvePossibleStyleValues(expr, path, new Set(seen));
|
|
1770
|
-
if (possibleValues) {
|
|
1771
|
-
return possibleValues.every(function(value) {
|
|
1772
|
-
return isStyleArrayLike(value, path, new Set(seen));
|
|
1773
|
-
});
|
|
1774
|
-
}
|
|
1775
|
-
const object = expr.object;
|
|
1776
|
-
if (!t3.isIdentifier(object)) return false;
|
|
1777
|
-
const binding = path.scope.getBinding(object.name);
|
|
1778
|
-
const bindingPath = binding?.path;
|
|
1779
|
-
if (!bindingPath || !bindingPath.isVariableDeclarator()) return false;
|
|
1780
|
-
const init = bindingPath.node.init;
|
|
1781
|
-
if (!init || !t3.isObjectExpression(init)) return false;
|
|
1782
|
-
const propertyName = getStaticMemberPropertyName(expr, path);
|
|
1783
|
-
if (!propertyName) return false;
|
|
1784
|
-
for (const prop of init.properties) {
|
|
1785
|
-
if (!t3.isObjectProperty(prop) || prop.computed) continue;
|
|
1786
|
-
if (!isMatchingPropertyName(prop.key, propertyName)) continue;
|
|
1787
|
-
const value = prop.value;
|
|
1788
|
-
return t3.isExpression(value) && isStyleArrayLike(value, bindingPath, seen);
|
|
1699
|
+
if (expr.properties.length === 0) return t3.arrayExpression([]);
|
|
1700
|
+
const allSpreads = expr.properties.every(function(p) {
|
|
1701
|
+
return t3.isSpreadElement(p);
|
|
1702
|
+
});
|
|
1703
|
+
if (!allSpreads) return null;
|
|
1704
|
+
const elements = [];
|
|
1705
|
+
for (const prop of expr.properties) {
|
|
1706
|
+
const spread = prop;
|
|
1707
|
+
const normalized = normalizeStyleExpression(spread.argument);
|
|
1708
|
+
if (!normalized) return null;
|
|
1709
|
+
if (t3.isArrayExpression(normalized)) {
|
|
1710
|
+
elements.push(...normalized.elements);
|
|
1711
|
+
} else {
|
|
1712
|
+
elements.push(t3.spreadElement(buildSafeSpreadArgument(normalized)));
|
|
1713
|
+
}
|
|
1789
1714
|
}
|
|
1715
|
+
return t3.arrayExpression(elements);
|
|
1790
1716
|
}
|
|
1791
|
-
return
|
|
1717
|
+
return normalizeStyleExpression(expr);
|
|
1792
1718
|
}
|
|
1793
|
-
function
|
|
1794
|
-
|
|
1795
|
-
seen.add(expr);
|
|
1796
|
-
if (t3.isArrayExpression(expr)) return true;
|
|
1797
|
-
if (t3.isLogicalExpression(expr) && expr.operator === "&&") {
|
|
1798
|
-
return isKnownStyleArrayLike(expr.right, path, seen);
|
|
1799
|
-
}
|
|
1800
|
-
if (t3.isLogicalExpression(expr) && (expr.operator === "||" || expr.operator === "??")) {
|
|
1801
|
-
return isKnownStyleArrayLike(expr.left, path, seen) && isStyleArrayLikeBranch(expr.right, path, seen);
|
|
1802
|
-
}
|
|
1803
|
-
if (t3.isConditionalExpression(expr)) {
|
|
1804
|
-
return isStyleArrayLikeBranch(expr.consequent, path, seen) && isStyleArrayLikeBranch(expr.alternate, path, seen);
|
|
1805
|
-
}
|
|
1806
|
-
if (t3.isIdentifier(expr)) {
|
|
1807
|
-
const possibleValues = resolvePossibleStyleValues(expr, path, new Set(seen));
|
|
1808
|
-
if (possibleValues) {
|
|
1809
|
-
return possibleValues.every(function(value) {
|
|
1810
|
-
return isKnownStyleArrayLike(value, path, new Set(seen));
|
|
1811
|
-
});
|
|
1812
|
-
}
|
|
1813
|
-
const binding = path.scope.getBinding(expr.name);
|
|
1814
|
-
const bindingPath = binding?.path;
|
|
1815
|
-
if (!bindingPath || !bindingPath.isVariableDeclarator()) return false;
|
|
1816
|
-
const init = bindingPath.node.init;
|
|
1817
|
-
return !!(init && isKnownStyleArrayLike(init, bindingPath, seen));
|
|
1818
|
-
}
|
|
1819
|
-
if (t3.isCallExpression(expr)) {
|
|
1820
|
-
const possibleValues = resolvePossibleStyleValues(expr, path, new Set(seen));
|
|
1821
|
-
if (possibleValues) {
|
|
1822
|
-
return possibleValues.every(function(value) {
|
|
1823
|
-
return isKnownStyleArrayLike(value, path, new Set(seen));
|
|
1824
|
-
});
|
|
1825
|
-
}
|
|
1826
|
-
const returnExpr = getCallStyleArrayLikeExpression(expr, path);
|
|
1827
|
-
return !!(returnExpr && isKnownStyleArrayLike(returnExpr, path, seen));
|
|
1828
|
-
}
|
|
1829
|
-
if (t3.isMemberExpression(expr)) {
|
|
1830
|
-
const possibleValues = resolvePossibleStyleValues(expr, path, new Set(seen));
|
|
1831
|
-
if (possibleValues) {
|
|
1832
|
-
return possibleValues.every(function(value) {
|
|
1833
|
-
return isKnownStyleArrayLike(value, path, new Set(seen));
|
|
1834
|
-
});
|
|
1835
|
-
}
|
|
1836
|
-
const object = expr.object;
|
|
1837
|
-
if (!t3.isIdentifier(object)) return false;
|
|
1838
|
-
const binding = path.scope.getBinding(object.name);
|
|
1839
|
-
const bindingPath = binding?.path;
|
|
1840
|
-
if (!bindingPath || !bindingPath.isVariableDeclarator()) return false;
|
|
1841
|
-
const init = bindingPath.node.init;
|
|
1842
|
-
if (!init || !t3.isObjectExpression(init)) return false;
|
|
1843
|
-
const propertyName = getStaticMemberPropertyName(expr, path);
|
|
1844
|
-
if (!propertyName) return false;
|
|
1845
|
-
for (const prop of init.properties) {
|
|
1846
|
-
if (!t3.isObjectProperty(prop) || prop.computed) continue;
|
|
1847
|
-
if (!isMatchingPropertyName(prop.key, propertyName)) continue;
|
|
1848
|
-
const value = prop.value;
|
|
1849
|
-
return t3.isExpression(value) && isKnownStyleArrayLike(value, bindingPath, seen);
|
|
1850
|
-
}
|
|
1851
|
-
}
|
|
1852
|
-
return false;
|
|
1719
|
+
function isCssSpreadCall(expr, cssBindingName) {
|
|
1720
|
+
return t3.isMemberExpression(expr.callee) && !expr.callee.computed && t3.isIdentifier(expr.callee.object, { name: cssBindingName }) && t3.isIdentifier(expr.callee.property, { name: "spread" });
|
|
1853
1721
|
}
|
|
1854
|
-
function
|
|
1855
|
-
|
|
1722
|
+
function unwrapStyleObjectExpression(expr) {
|
|
1723
|
+
if (t3.isObjectExpression(expr)) return expr;
|
|
1724
|
+
if (t3.isTSAsExpression(expr) || t3.isTSSatisfiesExpression(expr) || t3.isTSNonNullExpression(expr)) {
|
|
1725
|
+
return unwrapStyleObjectExpression(expr.expression);
|
|
1726
|
+
}
|
|
1727
|
+
return null;
|
|
1856
1728
|
}
|
|
1857
1729
|
function isMatchingPropertyName(key, name) {
|
|
1858
1730
|
return t3.isIdentifier(key) && key.name === name || t3.isStringLiteral(key) && key.value === name;
|
|
@@ -1860,18 +1732,10 @@ function isMatchingPropertyName(key, name) {
|
|
|
1860
1732
|
function isEmptyObjectExpression(expr) {
|
|
1861
1733
|
return t3.isObjectExpression(expr) && expr.properties.length === 0;
|
|
1862
1734
|
}
|
|
1863
|
-
function
|
|
1735
|
+
function buildUnknownSpreadFallback(expr, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1864
1736
|
needsAsStyleArrayHelper.current = true;
|
|
1865
1737
|
return t3.callExpression(t3.identifier(asStyleArrayHelperName), [expr]);
|
|
1866
1738
|
}
|
|
1867
|
-
function buildInlineAsStyleArrayExpression(expr) {
|
|
1868
|
-
return t3.conditionalExpression(
|
|
1869
|
-
t3.callExpression(t3.memberExpression(t3.identifier("Array"), t3.identifier("isArray")), [expr]),
|
|
1870
|
-
expr,
|
|
1871
|
-
t3.conditionalExpression(expr, t3.arrayExpression([expr]), t3.arrayExpression([]))
|
|
1872
|
-
// I.e. `xss` becomes `[xss]` when truthy
|
|
1873
|
-
);
|
|
1874
|
-
}
|
|
1875
1739
|
function buildSafeSpreadArgument(expr) {
|
|
1876
1740
|
return t3.isConditionalExpression(expr) || t3.isLogicalExpression(expr) ? t3.parenthesizedExpression(expr) : expr;
|
|
1877
1741
|
}
|
|
@@ -1884,14 +1748,13 @@ function getStaticMemberPropertyName(expr, path) {
|
|
|
1884
1748
|
}
|
|
1885
1749
|
if (t3.isIdentifier(expr.property)) {
|
|
1886
1750
|
const binding = path.scope.getBinding(expr.property.name);
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
const init = bindingPath.node.init;
|
|
1751
|
+
if (!binding?.path.isVariableDeclarator()) return null;
|
|
1752
|
+
const init = binding.path.node.init;
|
|
1890
1753
|
return t3.isStringLiteral(init) ? init.value : null;
|
|
1891
1754
|
}
|
|
1892
1755
|
return null;
|
|
1893
1756
|
}
|
|
1894
|
-
function
|
|
1757
|
+
function getCallReturnExpression(expr, path) {
|
|
1895
1758
|
const localReturnExpr = getLocalFunctionReturnExpression(expr, path);
|
|
1896
1759
|
if (localReturnExpr) return localReturnExpr;
|
|
1897
1760
|
const firstArg = expr.arguments[0];
|
|
@@ -1900,57 +1763,15 @@ function getCallStyleArrayLikeExpression(expr, path) {
|
|
|
1900
1763
|
}
|
|
1901
1764
|
return null;
|
|
1902
1765
|
}
|
|
1903
|
-
function resolvePossibleStyleValues(expr, path, seen) {
|
|
1904
|
-
if (seen.has(expr)) return null;
|
|
1905
|
-
seen.add(expr);
|
|
1906
|
-
if (t3.isObjectExpression(expr) || t3.isArrayExpression(expr)) {
|
|
1907
|
-
return [expr];
|
|
1908
|
-
}
|
|
1909
|
-
if (t3.isIdentifier(expr)) {
|
|
1910
|
-
const binding = path.scope.getBinding(expr.name);
|
|
1911
|
-
const bindingPath = binding?.path;
|
|
1912
|
-
if (!bindingPath || !bindingPath.isVariableDeclarator()) return null;
|
|
1913
|
-
const init = bindingPath.node.init;
|
|
1914
|
-
return init && t3.isExpression(init) ? resolvePossibleStyleValues(init, bindingPath, seen) : null;
|
|
1915
|
-
}
|
|
1916
|
-
if (t3.isCallExpression(expr)) {
|
|
1917
|
-
const returnExpr = getCallStyleArrayLikeExpression(expr, path);
|
|
1918
|
-
return returnExpr ? resolvePossibleStyleValues(returnExpr, path, seen) : null;
|
|
1919
|
-
}
|
|
1920
|
-
if (t3.isMemberExpression(expr)) {
|
|
1921
|
-
const object = expr.object;
|
|
1922
|
-
if (!t3.isExpression(object)) return null;
|
|
1923
|
-
const objectValues = resolvePossibleStyleValues(object, path, seen);
|
|
1924
|
-
if (!objectValues) return null;
|
|
1925
|
-
const propertyName = getStaticMemberPropertyName(expr, path);
|
|
1926
|
-
const values = [];
|
|
1927
|
-
for (const objectValue of objectValues) {
|
|
1928
|
-
if (!t3.isObjectExpression(objectValue)) continue;
|
|
1929
|
-
for (const prop of objectValue.properties) {
|
|
1930
|
-
if (!t3.isObjectProperty(prop) || prop.computed || !t3.isExpression(prop.value)) continue;
|
|
1931
|
-
if (propertyName) {
|
|
1932
|
-
if (isMatchingPropertyName(prop.key, propertyName)) {
|
|
1933
|
-
values.push(prop.value);
|
|
1934
|
-
}
|
|
1935
|
-
} else {
|
|
1936
|
-
values.push(prop.value);
|
|
1937
|
-
}
|
|
1938
|
-
}
|
|
1939
|
-
}
|
|
1940
|
-
return values.length > 0 ? values : null;
|
|
1941
|
-
}
|
|
1942
|
-
return null;
|
|
1943
|
-
}
|
|
1944
1766
|
function getLocalFunctionReturnExpression(expr, path) {
|
|
1945
1767
|
if (!t3.isIdentifier(expr.callee)) return null;
|
|
1946
1768
|
const binding = path.scope.getBinding(expr.callee.name);
|
|
1947
|
-
|
|
1948
|
-
if (
|
|
1949
|
-
|
|
1950
|
-
return getFunctionLikeReturnExpression(bindingPath.node);
|
|
1769
|
+
if (!binding) return null;
|
|
1770
|
+
if (binding.path.isFunctionDeclaration()) {
|
|
1771
|
+
return getFunctionLikeReturnExpression(binding.path.node);
|
|
1951
1772
|
}
|
|
1952
|
-
if (
|
|
1953
|
-
const init =
|
|
1773
|
+
if (binding.path.isVariableDeclarator()) {
|
|
1774
|
+
const init = binding.path.node.init;
|
|
1954
1775
|
if (init && (t3.isArrowFunctionExpression(init) || t3.isFunctionExpression(init))) {
|
|
1955
1776
|
return getFunctionLikeReturnExpression(init);
|
|
1956
1777
|
}
|