@homebound/truss 2.0.4 → 2.0.6
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 +174 -211
- package/build/plugin/index.js.map +1 -1
- package/package.json +1 -1
package/build/plugin/index.js
CHANGED
|
@@ -1238,6 +1238,13 @@ import _generate from "@babel/generator";
|
|
|
1238
1238
|
import * as t3 from "@babel/types";
|
|
1239
1239
|
var generate = _generate.default ?? _generate;
|
|
1240
1240
|
var traverse = _traverse.default ?? _traverse;
|
|
1241
|
+
function formatDroppedPropertyKey(prop) {
|
|
1242
|
+
if (t3.isObjectProperty(prop)) {
|
|
1243
|
+
if (t3.isIdentifier(prop.key)) return prop.key.name;
|
|
1244
|
+
if (t3.isStringLiteral(prop.key)) return prop.key.value;
|
|
1245
|
+
}
|
|
1246
|
+
return formatNodeSnippet(prop);
|
|
1247
|
+
}
|
|
1241
1248
|
function rewriteExpressionSites(options) {
|
|
1242
1249
|
for (const site of options.sites) {
|
|
1243
1250
|
const propsArgs = buildPropsArgsFromChain(site.resolvedChain, options);
|
|
@@ -1259,8 +1266,18 @@ function rewriteExpressionSites(options) {
|
|
|
1259
1266
|
}
|
|
1260
1267
|
site.path.replaceWith(buildStyleArrayExpression(propsArgs, site.path.node.loc?.start.line ?? null, options));
|
|
1261
1268
|
}
|
|
1262
|
-
rewriteCssSpreadCalls(
|
|
1263
|
-
|
|
1269
|
+
rewriteCssSpreadCalls(
|
|
1270
|
+
options.ast,
|
|
1271
|
+
options.cssBindingName,
|
|
1272
|
+
options.asStyleArrayHelperName,
|
|
1273
|
+
options.needsAsStyleArrayHelper
|
|
1274
|
+
);
|
|
1275
|
+
rewriteStyleObjectExpressions(
|
|
1276
|
+
options.ast,
|
|
1277
|
+
options.skippedCssPropMessages,
|
|
1278
|
+
options.asStyleArrayHelperName,
|
|
1279
|
+
options.needsAsStyleArrayHelper
|
|
1280
|
+
);
|
|
1264
1281
|
rewriteCssAttributeExpressions(
|
|
1265
1282
|
options.ast,
|
|
1266
1283
|
options.filename,
|
|
@@ -1338,7 +1355,7 @@ function buildPropsArgs(segments, options) {
|
|
|
1338
1355
|
if (seg.styleArrayArg) {
|
|
1339
1356
|
args.push(
|
|
1340
1357
|
t3.spreadElement(
|
|
1341
|
-
|
|
1358
|
+
buildUnknownSpreadFallback(
|
|
1342
1359
|
seg.styleArrayArg,
|
|
1343
1360
|
options.asStyleArrayHelperName,
|
|
1344
1361
|
options.needsAsStyleArrayHelper
|
|
@@ -1377,13 +1394,6 @@ function rewriteCssAttributeExpressions(ast, filename, debug, stylexNamespaceNam
|
|
|
1377
1394
|
const value = path.node.value;
|
|
1378
1395
|
if (!t3.isJSXExpressionContainer(value)) return;
|
|
1379
1396
|
if (!t3.isExpression(value.expression)) return;
|
|
1380
|
-
if (!isCssRewriteableExpression(value.expression, path)) {
|
|
1381
|
-
skippedCssPropMessages.push({
|
|
1382
|
-
message: explainSkippedCssRewrite(value.expression, path),
|
|
1383
|
-
line: path.node.loc?.start.line ?? null
|
|
1384
|
-
});
|
|
1385
|
-
return;
|
|
1386
|
-
}
|
|
1387
1397
|
const propsArgs = lowerCssExpressionToPropsArgs(
|
|
1388
1398
|
value.expression,
|
|
1389
1399
|
path,
|
|
@@ -1447,11 +1457,8 @@ function buildDebugElements(line, options) {
|
|
|
1447
1457
|
options.needsTrussDebugInfo.current = true;
|
|
1448
1458
|
return [t3.newExpression(t3.identifier(options.trussDebugInfoName), [t3.stringLiteral(`${options.filename}:${line}`)])];
|
|
1449
1459
|
}
|
|
1450
|
-
function isCssRewriteableExpression(expr, path) {
|
|
1451
|
-
return !!lowerCssExpressionToPropsArgs(expr, path, "asStyleArray", { current: false });
|
|
1452
|
-
}
|
|
1453
1460
|
function lowerCssExpressionToPropsArgs(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1454
|
-
return buildStyleObjectPropsArgs(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) ?? buildStyleArrayLikePropsArgsFromExpression(expr, path
|
|
1461
|
+
return buildStyleObjectPropsArgs(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) ?? buildStyleArrayLikePropsArgsFromExpression(expr, path);
|
|
1455
1462
|
}
|
|
1456
1463
|
function explainSkippedCssRewrite(expr, path) {
|
|
1457
1464
|
if (t3.isObjectExpression(expr)) {
|
|
@@ -1459,7 +1466,7 @@ function explainSkippedCssRewrite(expr, path) {
|
|
|
1459
1466
|
if (!t3.isSpreadElement(prop)) {
|
|
1460
1467
|
return `[truss] Unsupported pattern: Could not rewrite css prop: object contains a non-spread property (${formatNodeSnippet(expr)})`;
|
|
1461
1468
|
}
|
|
1462
|
-
const normalizedArg =
|
|
1469
|
+
const normalizedArg = normalizeStyleExpression(prop.argument);
|
|
1463
1470
|
if (!normalizedArg) {
|
|
1464
1471
|
return `[truss] Unsupported pattern: Could not rewrite css prop: spread argument is not style-array-like (${formatNodeSnippet(prop.argument)})`;
|
|
1465
1472
|
}
|
|
@@ -1503,51 +1510,46 @@ function removeExistingClassNameAttribute(path) {
|
|
|
1503
1510
|
}
|
|
1504
1511
|
function buildStyleObjectPropsArgs(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1505
1512
|
if (!t3.isObjectExpression(expr) || expr.properties.length === 0) return null;
|
|
1506
|
-
const
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
} else {
|
|
1522
|
-
propsArgs.push(t3.spreadElement(buildSafeSpreadArgument(normalizedArg)));
|
|
1523
|
-
}
|
|
1524
|
-
}
|
|
1525
|
-
return propsArgs.length > 0 ? propsArgs : null;
|
|
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
|
+
});
|
|
1526
1528
|
}
|
|
1527
|
-
function buildStyleArrayLikePropsArgsFromExpression(expr, path
|
|
1528
|
-
const normalizedExpr =
|
|
1529
|
+
function buildStyleArrayLikePropsArgsFromExpression(expr, path) {
|
|
1530
|
+
const normalizedExpr = normalizeStyleExpression(expr);
|
|
1529
1531
|
if (!normalizedExpr) return null;
|
|
1530
|
-
return buildStyleArrayLikePropsArgs(normalizedExpr, path
|
|
1532
|
+
return buildStyleArrayLikePropsArgs(normalizedExpr, path);
|
|
1531
1533
|
}
|
|
1532
|
-
function buildStyleArrayLikePropsArgs(expr, path
|
|
1533
|
-
if (seen.has(expr)) return null;
|
|
1534
|
-
seen.add(expr);
|
|
1534
|
+
function buildStyleArrayLikePropsArgs(expr, path) {
|
|
1535
1535
|
if (t3.isArrayExpression(expr)) {
|
|
1536
1536
|
const propsArgs = [];
|
|
1537
1537
|
for (const el of expr.elements) {
|
|
1538
1538
|
if (!el) continue;
|
|
1539
1539
|
if (t3.isSpreadElement(el)) {
|
|
1540
|
-
const normalizedArg =
|
|
1540
|
+
const normalizedArg = normalizeStyleExpression(el.argument);
|
|
1541
1541
|
if (!normalizedArg) {
|
|
1542
1542
|
propsArgs.push(t3.spreadElement(el.argument));
|
|
1543
1543
|
continue;
|
|
1544
1544
|
}
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1545
|
+
if (t3.isArrayExpression(normalizedArg)) {
|
|
1546
|
+
const nestedArgs = buildStyleArrayLikePropsArgs(normalizedArg, path);
|
|
1547
|
+
if (nestedArgs) {
|
|
1548
|
+
propsArgs.push(...nestedArgs);
|
|
1549
|
+
continue;
|
|
1550
|
+
}
|
|
1550
1551
|
}
|
|
1552
|
+
propsArgs.push(t3.spreadElement(buildSafeSpreadArgument(normalizedArg)));
|
|
1551
1553
|
continue;
|
|
1552
1554
|
}
|
|
1553
1555
|
propsArgs.push(el);
|
|
@@ -1559,20 +1561,22 @@ function buildStyleArrayLikePropsArgs(expr, path, seen) {
|
|
|
1559
1561
|
}
|
|
1560
1562
|
return null;
|
|
1561
1563
|
}
|
|
1562
|
-
function
|
|
1563
|
-
if (!(t3.isIdentifier(expr) || t3.isMemberExpression(expr) || t3.isCallExpression(expr))) return null;
|
|
1564
|
-
return [t3.spreadElement(buildUnknownObjectSpreadFallback(expr, asStyleArrayHelperName, needsAsStyleArrayHelper))];
|
|
1565
|
-
}
|
|
1566
|
-
function rewriteStyleObjectExpressions(ast) {
|
|
1564
|
+
function rewriteStyleObjectExpressions(ast, messages, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1567
1565
|
traverse(ast, {
|
|
1568
1566
|
ObjectExpression(path) {
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1567
|
+
if (!hasStyleArraySpread(path.node, path)) return;
|
|
1568
|
+
const result = flattenStyleObject(path.node, path, asStyleArrayHelperName, needsAsStyleArrayHelper);
|
|
1569
|
+
if (result.droppedPropertyKeys.length > 0) {
|
|
1570
|
+
messages.push({
|
|
1571
|
+
message: `[truss] Unsupported pattern: Dropped non-spread properties from style composition object (${result.droppedPropertyKeys.join(", ")})`,
|
|
1572
|
+
line: path.node.loc?.start.line ?? null
|
|
1573
|
+
});
|
|
1574
|
+
}
|
|
1575
|
+
path.replaceWith(t3.arrayExpression(result.elements));
|
|
1572
1576
|
}
|
|
1573
1577
|
});
|
|
1574
1578
|
}
|
|
1575
|
-
function rewriteCssSpreadCalls(ast, cssBindingName) {
|
|
1579
|
+
function rewriteCssSpreadCalls(ast, cssBindingName, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1576
1580
|
traverse(ast, {
|
|
1577
1581
|
CallExpression(path) {
|
|
1578
1582
|
if (!isCssSpreadCall(path.node, cssBindingName)) return;
|
|
@@ -1580,180 +1584,148 @@ function rewriteCssSpreadCalls(ast, cssBindingName) {
|
|
|
1580
1584
|
if (!arg || t3.isSpreadElement(arg) || !t3.isExpression(arg) || path.node.arguments.length !== 1) return;
|
|
1581
1585
|
const styleObject = unwrapStyleObjectExpression(arg);
|
|
1582
1586
|
if (!styleObject) return;
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
path.replaceWith(
|
|
1587
|
+
if (!hasStyleArraySpread(styleObject, path)) return;
|
|
1588
|
+
const result = flattenStyleObject(styleObject, path, asStyleArrayHelperName, needsAsStyleArrayHelper);
|
|
1589
|
+
path.replaceWith(t3.arrayExpression(result.elements));
|
|
1586
1590
|
}
|
|
1587
1591
|
});
|
|
1588
1592
|
}
|
|
1589
|
-
function
|
|
1590
|
-
return tryBuildStyleArrayFromObjectExpression(path.node, path);
|
|
1591
|
-
}
|
|
1592
|
-
function tryBuildStyleArrayFromObjectExpression(expr, path) {
|
|
1593
|
-
if (expr.properties.length === 0) return null;
|
|
1594
|
-
let sawStyleArray = false;
|
|
1593
|
+
function flattenStyleObject(expr, path, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1595
1594
|
const elements = [];
|
|
1595
|
+
const droppedPropertyKeys = [];
|
|
1596
1596
|
for (const prop of expr.properties) {
|
|
1597
1597
|
if (!t3.isSpreadElement(prop)) {
|
|
1598
|
-
|
|
1599
|
-
}
|
|
1600
|
-
const normalizedArg = normalizeStyleArrayLikeExpression(
|
|
1601
|
-
prop.argument,
|
|
1602
|
-
path,
|
|
1603
|
-
/* @__PURE__ */ new Set()
|
|
1604
|
-
// I.e. `...Css.df.$`, `...(cond ? Css.df.$ : {})`, or `...styles.wrapper`
|
|
1605
|
-
);
|
|
1606
|
-
if (!normalizedArg) {
|
|
1607
|
-
elements.push(t3.spreadElement(buildInlineAsStyleArrayExpression(prop.argument)));
|
|
1598
|
+
droppedPropertyKeys.push(formatDroppedPropertyKey(prop));
|
|
1608
1599
|
continue;
|
|
1609
1600
|
}
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1601
|
+
const normalized = normalizeStyleExpression(prop.argument);
|
|
1602
|
+
if (!normalized) {
|
|
1603
|
+
elements.push(
|
|
1604
|
+
t3.spreadElement(buildUnknownSpreadFallback(prop.argument, asStyleArrayHelperName, needsAsStyleArrayHelper))
|
|
1605
|
+
);
|
|
1615
1606
|
continue;
|
|
1616
1607
|
}
|
|
1617
|
-
|
|
1608
|
+
if (t3.isArrayExpression(normalized)) {
|
|
1609
|
+
elements.push(...normalized.elements);
|
|
1610
|
+
} else if (expressionContainsArray(normalized, path)) {
|
|
1611
|
+
elements.push(t3.spreadElement(buildSafeSpreadArgument(normalized)));
|
|
1612
|
+
} else {
|
|
1613
|
+
elements.push(
|
|
1614
|
+
t3.spreadElement(buildUnknownSpreadFallback(normalized, asStyleArrayHelperName, needsAsStyleArrayHelper))
|
|
1615
|
+
// I.e. `...borderBottomStyles`
|
|
1616
|
+
);
|
|
1617
|
+
}
|
|
1618
1618
|
}
|
|
1619
|
-
|
|
1620
|
-
return t3.arrayExpression(elements);
|
|
1619
|
+
return { elements, droppedPropertyKeys };
|
|
1621
1620
|
}
|
|
1622
|
-
function
|
|
1623
|
-
return
|
|
1621
|
+
function hasStyleArraySpread(expr, path) {
|
|
1622
|
+
return expr.properties.some(function(prop) {
|
|
1623
|
+
return t3.isSpreadElement(prop) && expressionContainsArray(prop.argument, path);
|
|
1624
|
+
});
|
|
1624
1625
|
}
|
|
1625
|
-
function
|
|
1626
|
-
if (t3.
|
|
1627
|
-
if (t3.
|
|
1628
|
-
return
|
|
1626
|
+
function expressionContainsArray(expr, path) {
|
|
1627
|
+
if (t3.isArrayExpression(expr)) return true;
|
|
1628
|
+
if (t3.isConditionalExpression(expr)) {
|
|
1629
|
+
return expressionContainsArray(expr.consequent, path) || expressionContainsArray(expr.alternate, path);
|
|
1629
1630
|
}
|
|
1630
|
-
|
|
1631
|
+
if (t3.isLogicalExpression(expr)) {
|
|
1632
|
+
return expressionContainsArray(expr.left, path) || expressionContainsArray(expr.right, path);
|
|
1633
|
+
}
|
|
1634
|
+
if (t3.isObjectExpression(expr)) {
|
|
1635
|
+
return expr.properties.some(function(p) {
|
|
1636
|
+
return t3.isSpreadElement(p) && expressionContainsArray(p.argument, path);
|
|
1637
|
+
});
|
|
1638
|
+
}
|
|
1639
|
+
if (t3.isIdentifier(expr)) {
|
|
1640
|
+
const binding = path.scope.getBinding(expr.name);
|
|
1641
|
+
if (binding?.path.isVariableDeclarator()) {
|
|
1642
|
+
const init = binding.path.node.init;
|
|
1643
|
+
if (init) return expressionContainsArray(init, binding.path);
|
|
1644
|
+
}
|
|
1645
|
+
return false;
|
|
1646
|
+
}
|
|
1647
|
+
if (t3.isMemberExpression(expr) && t3.isIdentifier(expr.object)) {
|
|
1648
|
+
const binding = path.scope.getBinding(expr.object.name);
|
|
1649
|
+
if (binding?.path.isVariableDeclarator()) {
|
|
1650
|
+
const init = binding.path.node.init;
|
|
1651
|
+
if (init && t3.isObjectExpression(init)) {
|
|
1652
|
+
const propName = getStaticMemberPropertyName(expr, path);
|
|
1653
|
+
if (propName) {
|
|
1654
|
+
for (const prop of init.properties) {
|
|
1655
|
+
if (!t3.isObjectProperty(prop) || prop.computed) continue;
|
|
1656
|
+
if (!isMatchingPropertyName(prop.key, propName)) continue;
|
|
1657
|
+
if (t3.isExpression(prop.value)) {
|
|
1658
|
+
return expressionContainsArray(prop.value, binding.path);
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
return false;
|
|
1665
|
+
}
|
|
1666
|
+
if (t3.isCallExpression(expr)) {
|
|
1667
|
+
const returnExpr = getCallReturnExpression(expr, path);
|
|
1668
|
+
return returnExpr ? expressionContainsArray(returnExpr, path) : false;
|
|
1669
|
+
}
|
|
1670
|
+
return false;
|
|
1631
1671
|
}
|
|
1632
|
-
function
|
|
1633
|
-
if (seen.has(expr)) return null;
|
|
1634
|
-
seen.add(expr);
|
|
1672
|
+
function normalizeStyleExpression(expr) {
|
|
1635
1673
|
if (t3.isArrayExpression(expr)) return expr;
|
|
1674
|
+
if (isEmptyObjectExpression(expr)) return t3.arrayExpression([]);
|
|
1636
1675
|
if (t3.isLogicalExpression(expr) && expr.operator === "&&") {
|
|
1637
|
-
const consequent =
|
|
1676
|
+
const consequent = normalizeStyleExpression(expr.right);
|
|
1638
1677
|
if (!consequent) return null;
|
|
1639
1678
|
return t3.conditionalExpression(expr.left, consequent, t3.arrayExpression([]));
|
|
1640
1679
|
}
|
|
1641
1680
|
if (t3.isLogicalExpression(expr) && (expr.operator === "||" || expr.operator === "??")) {
|
|
1642
|
-
const left =
|
|
1643
|
-
const right =
|
|
1681
|
+
const left = normalizeStyleExpression(expr.left);
|
|
1682
|
+
const right = normalizeStyleBranch(expr.right);
|
|
1644
1683
|
if (!left || !right) return null;
|
|
1645
1684
|
return t3.logicalExpression(expr.operator, left, right);
|
|
1646
1685
|
}
|
|
1647
1686
|
if (t3.isConditionalExpression(expr)) {
|
|
1648
|
-
const consequent =
|
|
1649
|
-
const alternate =
|
|
1687
|
+
const consequent = normalizeStyleBranch(expr.consequent);
|
|
1688
|
+
const alternate = normalizeStyleBranch(expr.alternate);
|
|
1650
1689
|
if (!consequent || !alternate) return null;
|
|
1651
1690
|
return t3.conditionalExpression(expr.test, consequent, alternate);
|
|
1652
1691
|
}
|
|
1653
1692
|
if (t3.isIdentifier(expr) || t3.isMemberExpression(expr) || t3.isCallExpression(expr)) {
|
|
1654
|
-
|
|
1655
|
-
nestedSeen.delete(expr);
|
|
1656
|
-
if (isStyleArrayLike(expr, path, nestedSeen)) return expr;
|
|
1693
|
+
return expr;
|
|
1657
1694
|
}
|
|
1658
1695
|
return null;
|
|
1659
1696
|
}
|
|
1660
|
-
function
|
|
1661
|
-
if (isEmptyObjectExpression(expr))
|
|
1662
|
-
return t3.arrayExpression([]);
|
|
1663
|
-
}
|
|
1697
|
+
function normalizeStyleBranch(expr) {
|
|
1698
|
+
if (isEmptyObjectExpression(expr)) return t3.arrayExpression([]);
|
|
1664
1699
|
if (t3.isObjectExpression(expr)) {
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
}
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
return isStyleArrayLikeBranch(expr.consequent, path, seen) && isStyleArrayLikeBranch(expr.alternate, path, seen);
|
|
1681
|
-
}
|
|
1682
|
-
if (t3.isIdentifier(expr)) {
|
|
1683
|
-
const binding = path.scope.getBinding(expr.name);
|
|
1684
|
-
const bindingPath = binding?.path;
|
|
1685
|
-
if (!bindingPath || !bindingPath.isVariableDeclarator()) return false;
|
|
1686
|
-
const init = bindingPath.node.init;
|
|
1687
|
-
return !!(init && isStyleArrayLike(init, bindingPath, seen));
|
|
1688
|
-
}
|
|
1689
|
-
if (t3.isCallExpression(expr)) {
|
|
1690
|
-
const returnExpr = getCallStyleArrayLikeExpression(expr, path);
|
|
1691
|
-
return returnExpr ? isStyleArrayLike(returnExpr, path, seen) : true;
|
|
1692
|
-
}
|
|
1693
|
-
if (t3.isMemberExpression(expr)) {
|
|
1694
|
-
const object = expr.object;
|
|
1695
|
-
if (!t3.isIdentifier(object)) return false;
|
|
1696
|
-
const binding = path.scope.getBinding(object.name);
|
|
1697
|
-
const bindingPath = binding?.path;
|
|
1698
|
-
if (!bindingPath || !bindingPath.isVariableDeclarator()) return false;
|
|
1699
|
-
const init = bindingPath.node.init;
|
|
1700
|
-
if (!init || !t3.isObjectExpression(init)) return false;
|
|
1701
|
-
const propertyName = getStaticMemberPropertyName(expr, path);
|
|
1702
|
-
if (!propertyName) return false;
|
|
1703
|
-
for (const prop of init.properties) {
|
|
1704
|
-
if (!t3.isObjectProperty(prop) || prop.computed) continue;
|
|
1705
|
-
if (!isMatchingPropertyName(prop.key, propertyName)) continue;
|
|
1706
|
-
const value = prop.value;
|
|
1707
|
-
return t3.isExpression(value) && isStyleArrayLike(value, bindingPath, seen);
|
|
1700
|
+
if (expr.properties.length === 0) return t3.arrayExpression([]);
|
|
1701
|
+
const allSpreads = expr.properties.every(function(p) {
|
|
1702
|
+
return t3.isSpreadElement(p);
|
|
1703
|
+
});
|
|
1704
|
+
if (!allSpreads) return null;
|
|
1705
|
+
const elements = [];
|
|
1706
|
+
for (const prop of expr.properties) {
|
|
1707
|
+
const spread = prop;
|
|
1708
|
+
const normalized = normalizeStyleExpression(spread.argument);
|
|
1709
|
+
if (!normalized) return null;
|
|
1710
|
+
if (t3.isArrayExpression(normalized)) {
|
|
1711
|
+
elements.push(...normalized.elements);
|
|
1712
|
+
} else {
|
|
1713
|
+
elements.push(t3.spreadElement(buildSafeSpreadArgument(normalized)));
|
|
1714
|
+
}
|
|
1708
1715
|
}
|
|
1716
|
+
return t3.arrayExpression(elements);
|
|
1709
1717
|
}
|
|
1710
|
-
return
|
|
1718
|
+
return normalizeStyleExpression(expr);
|
|
1711
1719
|
}
|
|
1712
|
-
function
|
|
1713
|
-
|
|
1714
|
-
seen.add(expr);
|
|
1715
|
-
if (t3.isArrayExpression(expr)) return true;
|
|
1716
|
-
if (t3.isLogicalExpression(expr) && expr.operator === "&&") {
|
|
1717
|
-
return isKnownStyleArrayLike(expr.right, path, seen);
|
|
1718
|
-
}
|
|
1719
|
-
if (t3.isLogicalExpression(expr) && (expr.operator === "||" || expr.operator === "??")) {
|
|
1720
|
-
return isKnownStyleArrayLike(expr.left, path, seen) && isStyleArrayLikeBranch(expr.right, path, seen);
|
|
1721
|
-
}
|
|
1722
|
-
if (t3.isConditionalExpression(expr)) {
|
|
1723
|
-
return isStyleArrayLikeBranch(expr.consequent, path, seen) && isStyleArrayLikeBranch(expr.alternate, path, seen);
|
|
1724
|
-
}
|
|
1725
|
-
if (t3.isIdentifier(expr)) {
|
|
1726
|
-
const binding = path.scope.getBinding(expr.name);
|
|
1727
|
-
const bindingPath = binding?.path;
|
|
1728
|
-
if (!bindingPath || !bindingPath.isVariableDeclarator()) return false;
|
|
1729
|
-
const init = bindingPath.node.init;
|
|
1730
|
-
return !!(init && isKnownStyleArrayLike(init, bindingPath, seen));
|
|
1731
|
-
}
|
|
1732
|
-
if (t3.isCallExpression(expr)) {
|
|
1733
|
-
const returnExpr = getCallStyleArrayLikeExpression(expr, path);
|
|
1734
|
-
return !!(returnExpr && isKnownStyleArrayLike(returnExpr, path, seen));
|
|
1735
|
-
}
|
|
1736
|
-
if (t3.isMemberExpression(expr)) {
|
|
1737
|
-
const object = expr.object;
|
|
1738
|
-
if (!t3.isIdentifier(object)) return false;
|
|
1739
|
-
const binding = path.scope.getBinding(object.name);
|
|
1740
|
-
const bindingPath = binding?.path;
|
|
1741
|
-
if (!bindingPath || !bindingPath.isVariableDeclarator()) return false;
|
|
1742
|
-
const init = bindingPath.node.init;
|
|
1743
|
-
if (!init || !t3.isObjectExpression(init)) return false;
|
|
1744
|
-
const propertyName = getStaticMemberPropertyName(expr, path);
|
|
1745
|
-
if (!propertyName) return false;
|
|
1746
|
-
for (const prop of init.properties) {
|
|
1747
|
-
if (!t3.isObjectProperty(prop) || prop.computed) continue;
|
|
1748
|
-
if (!isMatchingPropertyName(prop.key, propertyName)) continue;
|
|
1749
|
-
const value = prop.value;
|
|
1750
|
-
return t3.isExpression(value) && isKnownStyleArrayLike(value, bindingPath, seen);
|
|
1751
|
-
}
|
|
1752
|
-
}
|
|
1753
|
-
return false;
|
|
1720
|
+
function isCssSpreadCall(expr, cssBindingName) {
|
|
1721
|
+
return t3.isMemberExpression(expr.callee) && !expr.callee.computed && t3.isIdentifier(expr.callee.object, { name: cssBindingName }) && t3.isIdentifier(expr.callee.property, { name: "spread" });
|
|
1754
1722
|
}
|
|
1755
|
-
function
|
|
1756
|
-
|
|
1723
|
+
function unwrapStyleObjectExpression(expr) {
|
|
1724
|
+
if (t3.isObjectExpression(expr)) return expr;
|
|
1725
|
+
if (t3.isTSAsExpression(expr) || t3.isTSSatisfiesExpression(expr) || t3.isTSNonNullExpression(expr)) {
|
|
1726
|
+
return unwrapStyleObjectExpression(expr.expression);
|
|
1727
|
+
}
|
|
1728
|
+
return null;
|
|
1757
1729
|
}
|
|
1758
1730
|
function isMatchingPropertyName(key, name) {
|
|
1759
1731
|
return t3.isIdentifier(key) && key.name === name || t3.isStringLiteral(key) && key.value === name;
|
|
@@ -1761,17 +1733,10 @@ function isMatchingPropertyName(key, name) {
|
|
|
1761
1733
|
function isEmptyObjectExpression(expr) {
|
|
1762
1734
|
return t3.isObjectExpression(expr) && expr.properties.length === 0;
|
|
1763
1735
|
}
|
|
1764
|
-
function
|
|
1736
|
+
function buildUnknownSpreadFallback(expr, asStyleArrayHelperName, needsAsStyleArrayHelper) {
|
|
1765
1737
|
needsAsStyleArrayHelper.current = true;
|
|
1766
1738
|
return t3.callExpression(t3.identifier(asStyleArrayHelperName), [expr]);
|
|
1767
1739
|
}
|
|
1768
|
-
function buildInlineAsStyleArrayExpression(expr) {
|
|
1769
|
-
return t3.conditionalExpression(
|
|
1770
|
-
t3.callExpression(t3.memberExpression(t3.identifier("Array"), t3.identifier("isArray")), [expr]),
|
|
1771
|
-
expr,
|
|
1772
|
-
t3.conditionalExpression(expr, t3.arrayExpression([expr]), t3.arrayExpression([]))
|
|
1773
|
-
);
|
|
1774
|
-
}
|
|
1775
1740
|
function buildSafeSpreadArgument(expr) {
|
|
1776
1741
|
return t3.isConditionalExpression(expr) || t3.isLogicalExpression(expr) ? t3.parenthesizedExpression(expr) : expr;
|
|
1777
1742
|
}
|
|
@@ -1784,14 +1749,13 @@ function getStaticMemberPropertyName(expr, path) {
|
|
|
1784
1749
|
}
|
|
1785
1750
|
if (t3.isIdentifier(expr.property)) {
|
|
1786
1751
|
const binding = path.scope.getBinding(expr.property.name);
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
const init = bindingPath.node.init;
|
|
1752
|
+
if (!binding?.path.isVariableDeclarator()) return null;
|
|
1753
|
+
const init = binding.path.node.init;
|
|
1790
1754
|
return t3.isStringLiteral(init) ? init.value : null;
|
|
1791
1755
|
}
|
|
1792
1756
|
return null;
|
|
1793
1757
|
}
|
|
1794
|
-
function
|
|
1758
|
+
function getCallReturnExpression(expr, path) {
|
|
1795
1759
|
const localReturnExpr = getLocalFunctionReturnExpression(expr, path);
|
|
1796
1760
|
if (localReturnExpr) return localReturnExpr;
|
|
1797
1761
|
const firstArg = expr.arguments[0];
|
|
@@ -1803,13 +1767,12 @@ function getCallStyleArrayLikeExpression(expr, path) {
|
|
|
1803
1767
|
function getLocalFunctionReturnExpression(expr, path) {
|
|
1804
1768
|
if (!t3.isIdentifier(expr.callee)) return null;
|
|
1805
1769
|
const binding = path.scope.getBinding(expr.callee.name);
|
|
1806
|
-
|
|
1807
|
-
if (
|
|
1808
|
-
|
|
1809
|
-
return getFunctionLikeReturnExpression(bindingPath.node);
|
|
1770
|
+
if (!binding) return null;
|
|
1771
|
+
if (binding.path.isFunctionDeclaration()) {
|
|
1772
|
+
return getFunctionLikeReturnExpression(binding.path.node);
|
|
1810
1773
|
}
|
|
1811
|
-
if (
|
|
1812
|
-
const init =
|
|
1774
|
+
if (binding.path.isVariableDeclarator()) {
|
|
1775
|
+
const init = binding.path.node.init;
|
|
1813
1776
|
if (init && (t3.isArrowFunctionExpression(init) || t3.isFunctionExpression(init))) {
|
|
1814
1777
|
return getFunctionLikeReturnExpression(init);
|
|
1815
1778
|
}
|