styled-components-to-stylex-codemod 0.0.44 → 0.0.46
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/index.d.mts +1 -1
- package/dist/index.mjs +6 -2
- package/dist/{run-prepass-COJ2IOPl.mjs → run-prepass-u7NeybrS.mjs} +1 -1
- package/dist/{transform-types-CQjiY4Yp.d.mts → transform-types-_s0eXWNM.d.mts} +11 -2
- package/dist/transform.d.mts +1 -1
- package/dist/transform.mjs +1026 -120
- package/dist/{typescript-analysis-UWX_ePX0.mjs → typescript-analysis-9qBUYSi7.mjs} +182 -14
- package/package.json +1 -1
package/dist/transform.mjs
CHANGED
|
@@ -30,6 +30,50 @@ const STYLEX_LONGHAND_ONLY_SHORTHANDS = new Set([
|
|
|
30
30
|
"scroll-margin",
|
|
31
31
|
"scroll-padding"
|
|
32
32
|
]);
|
|
33
|
+
/** Mapping from CSS shorthand to the longhands that conflict with it. */
|
|
34
|
+
const SHORTHAND_LONGHANDS = {
|
|
35
|
+
margin: {
|
|
36
|
+
physical: [
|
|
37
|
+
"marginTop",
|
|
38
|
+
"marginRight",
|
|
39
|
+
"marginBottom",
|
|
40
|
+
"marginLeft"
|
|
41
|
+
],
|
|
42
|
+
logical: ["marginBlock", "marginInline"]
|
|
43
|
+
},
|
|
44
|
+
padding: {
|
|
45
|
+
physical: [
|
|
46
|
+
"paddingTop",
|
|
47
|
+
"paddingRight",
|
|
48
|
+
"paddingBottom",
|
|
49
|
+
"paddingLeft"
|
|
50
|
+
],
|
|
51
|
+
logical: ["paddingBlock", "paddingInline"]
|
|
52
|
+
},
|
|
53
|
+
scrollMargin: {
|
|
54
|
+
physical: [
|
|
55
|
+
"scrollMarginTop",
|
|
56
|
+
"scrollMarginRight",
|
|
57
|
+
"scrollMarginBottom",
|
|
58
|
+
"scrollMarginLeft"
|
|
59
|
+
],
|
|
60
|
+
logical: ["scrollMarginBlock", "scrollMarginInline"]
|
|
61
|
+
},
|
|
62
|
+
scrollPadding: {
|
|
63
|
+
physical: [
|
|
64
|
+
"scrollPaddingTop",
|
|
65
|
+
"scrollPaddingRight",
|
|
66
|
+
"scrollPaddingBottom",
|
|
67
|
+
"scrollPaddingLeft"
|
|
68
|
+
],
|
|
69
|
+
logical: ["scrollPaddingBlock", "scrollPaddingInline"]
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Mapping from logical longhands to their physical equivalents, derived from SHORTHAND_LONGHANDS.
|
|
74
|
+
* `marginBlock` -> `["marginTop", "marginBottom"]`
|
|
75
|
+
*/
|
|
76
|
+
const LOGICAL_TO_PHYSICAL = Object.fromEntries(Object.values(SHORTHAND_LONGHANDS).flatMap(({ physical, logical }) => [[logical[0], [physical[0], physical[2]]], [logical[1], [physical[1], physical[3]]]]));
|
|
33
77
|
function isStylexLonghandOnlyShorthand(prop) {
|
|
34
78
|
return STYLEX_LONGHAND_ONLY_SHORTHANDS.has(prop);
|
|
35
79
|
}
|
|
@@ -4498,6 +4542,40 @@ function buildChildStyleObjectList(childKeys, resolvedStyleObjects) {
|
|
|
4498
4542
|
return childStyleObjects;
|
|
4499
4543
|
}
|
|
4500
4544
|
//#endregion
|
|
4545
|
+
//#region src/internal/utilities/wrapped-component-interface.ts
|
|
4546
|
+
function wrappedComponentInterfaceFor(ctx, componentLocalName) {
|
|
4547
|
+
if (!ctx.adapter.useSxProp) return;
|
|
4548
|
+
const importInfo = ctx.importMap?.get(componentLocalName);
|
|
4549
|
+
if (importInfo) {
|
|
4550
|
+
const typedInterface = importInfo.source.kind === "absolutePath" ? typedComponentInterfaceFor(ctx, importInfo.source.value, importInfo.importedName === "default" ? [componentLocalName, importInfo.importedName] : [importInfo.importedName]) : void 0;
|
|
4551
|
+
const adapterResult = ctx.adapter.wrappedComponentInterface?.({
|
|
4552
|
+
localName: componentLocalName,
|
|
4553
|
+
importSource: importInfo.source.value,
|
|
4554
|
+
importedName: importInfo.importedName,
|
|
4555
|
+
filePath: ctx.file.path
|
|
4556
|
+
});
|
|
4557
|
+
if (adapterResult !== void 0) {
|
|
4558
|
+
if (adapterResult.acceptsSx && typedInterface?.sxExcludedProperties?.length) return {
|
|
4559
|
+
...adapterResult,
|
|
4560
|
+
sxExcludedProperties: mergeUniqueStrings(adapterResult.sxExcludedProperties, typedInterface.sxExcludedProperties)
|
|
4561
|
+
};
|
|
4562
|
+
return adapterResult;
|
|
4563
|
+
}
|
|
4564
|
+
return typedInterface;
|
|
4565
|
+
}
|
|
4566
|
+
return typedComponentInterfaceFor(ctx, ctx.file.path, [componentLocalName]);
|
|
4567
|
+
}
|
|
4568
|
+
function typedComponentInterfaceFor(ctx, filePath, componentNames) {
|
|
4569
|
+
const typedComponent = findTypeScriptComponentMetadata(ctx.options.crossFileInfo?.typeScriptMetadata, filePath, componentNames);
|
|
4570
|
+
if (typedComponent?.supportsSxProp) return {
|
|
4571
|
+
acceptsSx: true,
|
|
4572
|
+
sxExcludedProperties: typedComponent.sxExcludedProperties
|
|
4573
|
+
};
|
|
4574
|
+
}
|
|
4575
|
+
function mergeUniqueStrings(first, second) {
|
|
4576
|
+
return [...new Set([...first ?? [], ...second])];
|
|
4577
|
+
}
|
|
4578
|
+
//#endregion
|
|
4501
4579
|
//#region src/internal/transform-steps/analyze-before-emit.ts
|
|
4502
4580
|
const INLINE_USAGE_THRESHOLD = 1;
|
|
4503
4581
|
/**
|
|
@@ -5069,6 +5147,10 @@ function analyzeBeforeEmitStep(ctx) {
|
|
|
5069
5147
|
}
|
|
5070
5148
|
else ctx.resolvedStyleObjects.set(entry.styleKey, entry.styleValue);
|
|
5071
5149
|
}
|
|
5150
|
+
if (!validateSxRestrictedWrappedComponentStyles(ctx, styledDecls)) return returnResult({
|
|
5151
|
+
code: null,
|
|
5152
|
+
warnings: ctx.warnings
|
|
5153
|
+
}, "bail");
|
|
5072
5154
|
const existingStylexTarget = findExistingStylexStylesTarget({
|
|
5073
5155
|
ctx,
|
|
5074
5156
|
styledDeclNames,
|
|
@@ -5682,6 +5764,7 @@ function renameStaticAttrKeys(attrs, renames) {
|
|
|
5682
5764
|
*/
|
|
5683
5765
|
function collectAllStyleKeysForDecl(decl) {
|
|
5684
5766
|
const keys = [decl.styleKey];
|
|
5767
|
+
if (decl.adjacentSiblingStyleKey) keys.push(decl.adjacentSiblingStyleKey);
|
|
5685
5768
|
for (const key of Object.values(decl.variantStyleKeys ?? {})) keys.push(key);
|
|
5686
5769
|
for (const sf of decl.styleFnFromProps ?? []) keys.push(sf.fnKey);
|
|
5687
5770
|
for (const key of decl.extraStyleKeys ?? []) keys.push(key);
|
|
@@ -5692,7 +5775,21 @@ function collectAllStyleKeysForDecl(decl) {
|
|
|
5692
5775
|
for (const c of decl.enumVariant.cases) keys.push(c.styleKey);
|
|
5693
5776
|
}
|
|
5694
5777
|
for (const sbv of decl.staticBooleanVariants ?? []) keys.push(sbv.styleKey);
|
|
5778
|
+
for (const cs of decl.callSiteCombinedStyles ?? []) keys.push(cs.styleKey);
|
|
5779
|
+
for (const ps of decl.promotedStyleProps ?? []) if (!ps.mergeIntoBase) keys.push(ps.styleKey);
|
|
5695
5780
|
for (const pas of decl.pseudoAliasSelectors ?? []) keys.push(...pas.styleKeys);
|
|
5781
|
+
for (const pes of decl.pseudoExpandSelectors ?? []) keys.push(pes.styleKey);
|
|
5782
|
+
if (decl.attrWrapper) {
|
|
5783
|
+
const aw = decl.attrWrapper;
|
|
5784
|
+
for (const k of [
|
|
5785
|
+
aw.checkboxKey,
|
|
5786
|
+
aw.radioKey,
|
|
5787
|
+
aw.readonlyKey,
|
|
5788
|
+
aw.externalKey,
|
|
5789
|
+
aw.httpsKey,
|
|
5790
|
+
aw.pdfKey
|
|
5791
|
+
]) if (k) keys.push(k);
|
|
5792
|
+
}
|
|
5696
5793
|
return keys;
|
|
5697
5794
|
}
|
|
5698
5795
|
const AST_METADATA_KEYS = new Set([
|
|
@@ -6846,6 +6943,80 @@ function mergeAttrEntriesByAttrName(baseEntries, ownEntries) {
|
|
|
6846
6943
|
for (const entry of ownEntries ?? []) byAttrName.set(entry.attrName, entry);
|
|
6847
6944
|
return [...byAttrName.values()];
|
|
6848
6945
|
}
|
|
6946
|
+
function validateSxRestrictedWrappedComponentStyles(ctx, styledDecls) {
|
|
6947
|
+
if (!ctx.adapter.useSxProp || !ctx.resolvedStyleObjects) return true;
|
|
6948
|
+
for (const decl of styledDecls) {
|
|
6949
|
+
if (decl.skipTransform || decl.base.kind !== "component") continue;
|
|
6950
|
+
const componentInterface = wrappedComponentInterfaceFor(ctx, decl.base.ident);
|
|
6951
|
+
const excludedProperties = componentInterface?.sxExcludedProperties;
|
|
6952
|
+
if (componentInterface?.acceptsSx !== true || !excludedProperties?.length) continue;
|
|
6953
|
+
const excluded = new Set(excludedProperties);
|
|
6954
|
+
for (const styleKey of collectAllStyleKeysForDecl(decl)) {
|
|
6955
|
+
const style = ctx.resolvedStyleObjects.get(styleKey);
|
|
6956
|
+
if (!style || typeof style !== "object") continue;
|
|
6957
|
+
const rejectedProperty = findSxExcludedStyleProperty(style, excluded);
|
|
6958
|
+
if (!rejectedProperty) continue;
|
|
6959
|
+
ctx.warnings.push({
|
|
6960
|
+
severity: "error",
|
|
6961
|
+
type: "Wrapped component sx prop rejects logical CSS properties that cannot be preserved losslessly",
|
|
6962
|
+
loc: decl.loc,
|
|
6963
|
+
context: {
|
|
6964
|
+
localName: decl.localName,
|
|
6965
|
+
wrappedComponent: decl.base.ident,
|
|
6966
|
+
styleKey,
|
|
6967
|
+
property: rejectedProperty
|
|
6968
|
+
}
|
|
6969
|
+
});
|
|
6970
|
+
return false;
|
|
6971
|
+
}
|
|
6972
|
+
}
|
|
6973
|
+
return true;
|
|
6974
|
+
}
|
|
6975
|
+
function staticObjectPropertyName(prop) {
|
|
6976
|
+
if (!prop || typeof prop !== "object") return null;
|
|
6977
|
+
const p = prop;
|
|
6978
|
+
if (p.type !== "Property" && p.type !== "ObjectProperty" || p.computed) return null;
|
|
6979
|
+
const key = p.key;
|
|
6980
|
+
if (!key) return null;
|
|
6981
|
+
if (key.type === "Identifier") return key.name ?? null;
|
|
6982
|
+
if (key.type === "Literal" || key.type === "StringLiteral") return typeof key.value === "string" ? key.value : null;
|
|
6983
|
+
return null;
|
|
6984
|
+
}
|
|
6985
|
+
function findSxExcludedStyleProperty(style, excludedProperties) {
|
|
6986
|
+
if (isAstNode(style)) return findSxExcludedStylePropertyInAstNode(style, excludedProperties);
|
|
6987
|
+
for (const [key, value] of Object.entries(style)) {
|
|
6988
|
+
if (excludedProperties.has(key)) return key;
|
|
6989
|
+
if (value && typeof value === "object") {
|
|
6990
|
+
const nested = findSxExcludedStyleProperty(value, excludedProperties);
|
|
6991
|
+
if (nested) return nested;
|
|
6992
|
+
}
|
|
6993
|
+
}
|
|
6994
|
+
return null;
|
|
6995
|
+
}
|
|
6996
|
+
function findSxExcludedStylePropertyInAstNode(node, excludedProperties) {
|
|
6997
|
+
const n = node;
|
|
6998
|
+
if (n.type === "ObjectExpression") {
|
|
6999
|
+
for (const prop of n.properties ?? []) {
|
|
7000
|
+
const name = staticObjectPropertyName(prop);
|
|
7001
|
+
if (name && excludedProperties.has(name)) return name;
|
|
7002
|
+
const value = prop.value;
|
|
7003
|
+
if (value && typeof value === "object") {
|
|
7004
|
+
const nested = findSxExcludedStylePropertyInAstNode(value, excludedProperties);
|
|
7005
|
+
if (nested) return nested;
|
|
7006
|
+
}
|
|
7007
|
+
}
|
|
7008
|
+
return null;
|
|
7009
|
+
}
|
|
7010
|
+
if (n.type === "ArrowFunctionExpression" && n.body && typeof n.body === "object") return findSxExcludedStylePropertyInAstNode(n.body, excludedProperties);
|
|
7011
|
+
if (n.type === "BlockStatement" && Array.isArray(n.body)) for (const statement of n.body ?? []) {
|
|
7012
|
+
const s = statement;
|
|
7013
|
+
if (s.type === "ReturnStatement" && s.argument && typeof s.argument === "object") {
|
|
7014
|
+
const nested = findSxExcludedStylePropertyInAstNode(s.argument, excludedProperties);
|
|
7015
|
+
if (nested) return nested;
|
|
7016
|
+
}
|
|
7017
|
+
}
|
|
7018
|
+
return null;
|
|
7019
|
+
}
|
|
6849
7020
|
//#endregion
|
|
6850
7021
|
//#region src/internal/policy.ts
|
|
6851
7022
|
function collectCreateGlobalStyleWarnings(styledImports) {
|
|
@@ -7429,7 +7600,7 @@ function findUniversalSelectorLineOffset(rawCss) {
|
|
|
7429
7600
|
* @internal Exported for testing
|
|
7430
7601
|
*/
|
|
7431
7602
|
function findSelectorLineOffset(rawCss, selector) {
|
|
7432
|
-
const directMatch = rawCss.match(new RegExp(escapeRegExp(selector)));
|
|
7603
|
+
const directMatch = rawCss.match(new RegExp(escapeRegExp$1(selector)));
|
|
7433
7604
|
if (directMatch) return countNewlinesBefore(rawCss, directMatch.index);
|
|
7434
7605
|
const pseudoMatch = selector.match(/::?[a-z-]+(?:\([^)]*\))?/i);
|
|
7435
7606
|
if (pseudoMatch) {
|
|
@@ -7449,7 +7620,7 @@ function findSelectorLineOffset(rawCss, selector) {
|
|
|
7449
7620
|
}
|
|
7450
7621
|
const stripped = selector.replace(/^&\s+/, "").replace(/^&\s*[>+~]\s*/, "");
|
|
7451
7622
|
if (stripped !== selector) {
|
|
7452
|
-
const tagMatch = rawCss.match(new RegExp(`(?:^|[\\s>+~])${escapeRegExp(stripped)}\\s*[{,]`, "m"));
|
|
7623
|
+
const tagMatch = rawCss.match(new RegExp(`(?:^|[\\s>+~])${escapeRegExp$1(stripped)}\\s*[{,]`, "m"));
|
|
7453
7624
|
if (tagMatch) return countNewlinesBefore(rawCss, tagMatch.index + 1);
|
|
7454
7625
|
}
|
|
7455
7626
|
return 0;
|
|
@@ -7472,7 +7643,7 @@ function countNewlinesBefore(str, position) {
|
|
|
7472
7643
|
for (let i = 0; i < position && i < str.length; i++) if (str[i] === "\n") count++;
|
|
7473
7644
|
return count;
|
|
7474
7645
|
}
|
|
7475
|
-
function escapeRegExp(str) {
|
|
7646
|
+
function escapeRegExp$1(str) {
|
|
7476
7647
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
7477
7648
|
}
|
|
7478
7649
|
//#endregion
|
|
@@ -11353,7 +11524,7 @@ function collectKeyframesReferencedBySkippedDecls(ctx) {
|
|
|
11353
11524
|
function buildDuplicateKeyframesNames(ctx, preservedNames) {
|
|
11354
11525
|
const duplicates = /* @__PURE__ */ new Map();
|
|
11355
11526
|
if (preservedNames.size === 0) return duplicates;
|
|
11356
|
-
const usedNames = collectBindingNames$
|
|
11527
|
+
const usedNames = collectBindingNames$2(ctx);
|
|
11357
11528
|
const transformedReferences = collectKeyframesReferencedByDecls(ctx, (decl) => !decl.skipTransform);
|
|
11358
11529
|
for (const name of transformedReferences) {
|
|
11359
11530
|
if (!preservedNames.has(name) || duplicates.has(name)) continue;
|
|
@@ -11376,23 +11547,23 @@ function collectKeyframesReferencedByDecls(ctx, shouldVisitDecl) {
|
|
|
11376
11547
|
}
|
|
11377
11548
|
return referencedNames;
|
|
11378
11549
|
}
|
|
11379
|
-
function collectBindingNames$
|
|
11550
|
+
function collectBindingNames$2(ctx) {
|
|
11380
11551
|
const names = new Set(ctx.keyframesNames);
|
|
11381
11552
|
ctx.root.find(ctx.j.VariableDeclarator).forEach((path) => {
|
|
11382
|
-
collectPatternBindingNames(path.node.id, names);
|
|
11553
|
+
collectPatternBindingNames$1(path.node.id, names);
|
|
11383
11554
|
});
|
|
11384
11555
|
ctx.root.find(ctx.j.FunctionDeclaration).forEach((path) => {
|
|
11385
|
-
collectPatternBindingNames(path.node.id, names);
|
|
11556
|
+
collectPatternBindingNames$1(path.node.id, names);
|
|
11386
11557
|
});
|
|
11387
11558
|
ctx.root.find(ctx.j.ClassDeclaration).forEach((path) => {
|
|
11388
|
-
collectPatternBindingNames(path.node.id, names);
|
|
11559
|
+
collectPatternBindingNames$1(path.node.id, names);
|
|
11389
11560
|
});
|
|
11390
11561
|
return names;
|
|
11391
11562
|
}
|
|
11392
|
-
function collectPatternBindingNames(node, names) {
|
|
11563
|
+
function collectPatternBindingNames$1(node, names) {
|
|
11393
11564
|
if (!node || typeof node !== "object") return;
|
|
11394
11565
|
if (Array.isArray(node)) {
|
|
11395
|
-
for (const child of node) collectPatternBindingNames(child, names);
|
|
11566
|
+
for (const child of node) collectPatternBindingNames$1(child, names);
|
|
11396
11567
|
return;
|
|
11397
11568
|
}
|
|
11398
11569
|
const typed = node;
|
|
@@ -11403,7 +11574,7 @@ function collectPatternBindingNames(node, names) {
|
|
|
11403
11574
|
if (typed.type === "MemberExpression" || typed.type === "OptionalMemberExpression" || typed.type === "TSQualifiedName") return;
|
|
11404
11575
|
for (const key of Object.keys(node)) {
|
|
11405
11576
|
if (key === "loc" || key === "comments" || key === "leadingComments") continue;
|
|
11406
|
-
collectPatternBindingNames(node[key], names);
|
|
11577
|
+
collectPatternBindingNames$1(node[key], names);
|
|
11407
11578
|
}
|
|
11408
11579
|
}
|
|
11409
11580
|
function makeUniqueKeyframesDuplicateName(name, usedNames) {
|
|
@@ -13530,50 +13701,6 @@ function isFiniteNumericString(s) {
|
|
|
13530
13701
|
const n = Number(s);
|
|
13531
13702
|
return Number.isFinite(n) && String(n) === s;
|
|
13532
13703
|
}
|
|
13533
|
-
/** Mapping from CSS shorthand to the longhands that conflict with it */
|
|
13534
|
-
const SHORTHAND_LONGHANDS = {
|
|
13535
|
-
margin: {
|
|
13536
|
-
physical: [
|
|
13537
|
-
"marginTop",
|
|
13538
|
-
"marginRight",
|
|
13539
|
-
"marginBottom",
|
|
13540
|
-
"marginLeft"
|
|
13541
|
-
],
|
|
13542
|
-
logical: ["marginBlock", "marginInline"]
|
|
13543
|
-
},
|
|
13544
|
-
padding: {
|
|
13545
|
-
physical: [
|
|
13546
|
-
"paddingTop",
|
|
13547
|
-
"paddingRight",
|
|
13548
|
-
"paddingBottom",
|
|
13549
|
-
"paddingLeft"
|
|
13550
|
-
],
|
|
13551
|
-
logical: ["paddingBlock", "paddingInline"]
|
|
13552
|
-
},
|
|
13553
|
-
scrollMargin: {
|
|
13554
|
-
physical: [
|
|
13555
|
-
"scrollMarginTop",
|
|
13556
|
-
"scrollMarginRight",
|
|
13557
|
-
"scrollMarginBottom",
|
|
13558
|
-
"scrollMarginLeft"
|
|
13559
|
-
],
|
|
13560
|
-
logical: ["scrollMarginBlock", "scrollMarginInline"]
|
|
13561
|
-
},
|
|
13562
|
-
scrollPadding: {
|
|
13563
|
-
physical: [
|
|
13564
|
-
"scrollPaddingTop",
|
|
13565
|
-
"scrollPaddingRight",
|
|
13566
|
-
"scrollPaddingBottom",
|
|
13567
|
-
"scrollPaddingLeft"
|
|
13568
|
-
],
|
|
13569
|
-
logical: ["scrollPaddingBlock", "scrollPaddingInline"]
|
|
13570
|
-
}
|
|
13571
|
-
};
|
|
13572
|
-
/**
|
|
13573
|
-
* Mapping from logical longhands to their physical equivalents, derived from SHORTHAND_LONGHANDS.
|
|
13574
|
-
* `marginBlock` → `["marginTop", "marginBottom"]`
|
|
13575
|
-
*/
|
|
13576
|
-
const LOGICAL_TO_PHYSICAL = Object.fromEntries(Object.values(SHORTHAND_LONGHANDS).flatMap(({ physical, logical }) => [[logical[0], [physical[0], physical[2]]], [logical[1], [physical[1], physical[3]]]]));
|
|
13577
13704
|
/** Type guard: value is a simple string or number (not a conditional object) */
|
|
13578
13705
|
function isSimpleStyleValue(value) {
|
|
13579
13706
|
return typeof value === "string" || typeof value === "number";
|
|
@@ -14294,10 +14421,10 @@ function appendSxArrayArg(j, sxArgs, arg) {
|
|
|
14294
14421
|
sxArgs.push(normalizeOptionalSxArg(j, arg));
|
|
14295
14422
|
}
|
|
14296
14423
|
function normalizeOptionalSxArg(j, arg) {
|
|
14297
|
-
if (isUndefinedIdentifier(arg)) return j.nullLiteral();
|
|
14424
|
+
if (isUndefinedIdentifier$1(arg)) return j.nullLiteral();
|
|
14298
14425
|
if (isLogicalAndExpression(arg)) return j.conditionalExpression(arg.left, normalizeOptionalSxArg(j, arg.right), j.nullLiteral());
|
|
14299
14426
|
if (isConditionalExpression(arg)) {
|
|
14300
|
-
const alternate = isUndefinedIdentifier(arg.alternate) ? j.nullLiteral() : normalizeOptionalSxArg(j, arg.alternate);
|
|
14427
|
+
const alternate = isUndefinedIdentifier$1(arg.alternate) ? j.nullLiteral() : normalizeOptionalSxArg(j, arg.alternate);
|
|
14301
14428
|
return j.conditionalExpression(arg.test, normalizeOptionalSxArg(j, arg.consequent), alternate);
|
|
14302
14429
|
}
|
|
14303
14430
|
return arg;
|
|
@@ -14314,7 +14441,7 @@ function isConditionalExpression(arg) {
|
|
|
14314
14441
|
function isExpressionField(node, field) {
|
|
14315
14442
|
return !!node && typeof node === "object" && isPlainArrayElement(node[field]);
|
|
14316
14443
|
}
|
|
14317
|
-
function isUndefinedIdentifier(node) {
|
|
14444
|
+
function isUndefinedIdentifier$1(node) {
|
|
14318
14445
|
return getNodeType(node) === "Identifier" && node.name === "undefined";
|
|
14319
14446
|
}
|
|
14320
14447
|
function getNodeType(node) {
|
|
@@ -15713,7 +15840,10 @@ var WrapperEmitter = class {
|
|
|
15713
15840
|
* configure the hook.
|
|
15714
15841
|
*/
|
|
15715
15842
|
wrappedComponentAcceptsSxProp(componentLocalName) {
|
|
15716
|
-
|
|
15843
|
+
return this.wrappedComponentInterfaceFor(componentLocalName)?.acceptsSx === true;
|
|
15844
|
+
}
|
|
15845
|
+
wrappedComponentInterfaceFor(componentLocalName) {
|
|
15846
|
+
if (!this.useSxProp) return;
|
|
15717
15847
|
const importInfo = this.importMap.get(componentLocalName);
|
|
15718
15848
|
if (importInfo) {
|
|
15719
15849
|
const adapterResult = this.wrappedComponentInterface?.({
|
|
@@ -15722,18 +15852,21 @@ var WrapperEmitter = class {
|
|
|
15722
15852
|
importedName: importInfo.importedName,
|
|
15723
15853
|
filePath: this.filePath
|
|
15724
15854
|
});
|
|
15725
|
-
if (adapterResult !== void 0) return adapterResult
|
|
15855
|
+
if (adapterResult !== void 0) return adapterResult;
|
|
15726
15856
|
}
|
|
15727
15857
|
const typedComponent = this.typeScriptComponentMetadataFor(componentLocalName);
|
|
15728
15858
|
if (typedComponent) {
|
|
15729
|
-
if (typedComponent.supportsSxProp) return
|
|
15730
|
-
|
|
15859
|
+
if (typedComponent.supportsSxProp) return {
|
|
15860
|
+
acceptsSx: true,
|
|
15861
|
+
sxExcludedProperties: typedComponent.sxExcludedProperties
|
|
15862
|
+
};
|
|
15863
|
+
if (!this.hasSourceOverrideFor(componentLocalName)) return { acceptsSx: false };
|
|
15731
15864
|
}
|
|
15732
15865
|
return importInfo?.source.kind === "absolutePath" && transformedComponentAcceptsSx({
|
|
15733
15866
|
absolutePath: importInfo.source.value,
|
|
15734
15867
|
componentNames: importInfo.importedName === "default" ? [componentLocalName, importInfo.importedName] : [importInfo.importedName],
|
|
15735
15868
|
sourceOverrides: this.sourceOverrides
|
|
15736
|
-
});
|
|
15869
|
+
}) ? { acceptsSx: true } : void 0;
|
|
15737
15870
|
}
|
|
15738
15871
|
propsTypeNameFor(localName) {
|
|
15739
15872
|
return `${localName}Props`;
|
|
@@ -15879,7 +16012,7 @@ var WrapperEmitter = class {
|
|
|
15879
16012
|
return used.has("*") || used.has("style");
|
|
15880
16013
|
}
|
|
15881
16014
|
shouldAllowSxProp(d) {
|
|
15882
|
-
return (d.supportsExternalStyles ?? false) || d.typeScriptSupportsSxProp === true;
|
|
16015
|
+
return (d.supportsExternalStyles ?? false) || d.typeScriptSupportsSxProp === true || this.shouldAllowClassNameProp(d) || this.shouldAllowStyleProp(d);
|
|
15883
16016
|
}
|
|
15884
16017
|
typedComponentHasProp(componentLocalName, propName) {
|
|
15885
16018
|
const metadata = this.typeScriptComponentMetadataFor(componentLocalName);
|
|
@@ -15893,7 +16026,7 @@ var WrapperEmitter = class {
|
|
|
15893
16026
|
}
|
|
15894
16027
|
/**
|
|
15895
16028
|
* Returns true when the wrapped component's prepass metadata proves it does
|
|
15896
|
-
* NOT accept the given style prop (className/style) — i.e. the prop is
|
|
16029
|
+
* NOT accept the given external style prop (className/style/sx) — i.e. the prop is
|
|
15897
16030
|
* missing from both explicit and resolved props AND the component has no
|
|
15898
16031
|
* index signature. Used to decide whether to lift the prop onto the wrapper.
|
|
15899
16032
|
*
|
|
@@ -16556,15 +16689,16 @@ var WrapperEmitter = class {
|
|
|
16556
16689
|
return true;
|
|
16557
16690
|
}
|
|
16558
16691
|
inferredComponentWrapperPropsTypeText(args) {
|
|
16559
|
-
const { d, allowClassNameProp, allowStyleProp, allowSxProp, wrappedComponentIsInternalWrapper, hasExplicitPropsType, forceClassNameOptional, forceStyleOptional, wrappedComponent, forwardedAsPropTypeText = "React.ElementType", attrsProvidedPropOptions } = args;
|
|
16692
|
+
const { d, allowClassNameProp, allowStyleProp, allowSxProp, wrappedComponentIsInternalWrapper, wrappedComponentIsStyledWrapper, hasExplicitPropsType, forceClassNameOptional, forceStyleOptional, wrappedComponent, forwardedAsPropTypeText = "React.ElementType", attrsProvidedPropOptions } = args;
|
|
16560
16693
|
const lines = [];
|
|
16561
16694
|
const shouldAddStyleProps = d.supportsExternalStyles && !wrappedComponentIsInternalWrapper && !hasExplicitPropsType;
|
|
16562
16695
|
const liftableContext = !hasExplicitPropsType && !wrappedComponentIsInternalWrapper && Boolean(wrappedComponent);
|
|
16563
16696
|
const liftClassNameForUnsupportedWrapped = liftableContext && allowClassNameProp && this.wrappedRejectsStyleProp(wrappedComponent, "className");
|
|
16564
16697
|
const liftStyleForUnsupportedWrapped = liftableContext && allowStyleProp && this.wrappedRejectsStyleProp(wrappedComponent, "style");
|
|
16698
|
+
const shouldAddOwnSxProp = !hasExplicitPropsType && !wrappedComponentIsStyledWrapper && allowSxProp;
|
|
16565
16699
|
if (shouldAddStyleProps && allowClassNameProp || forceClassNameOptional || liftClassNameForUnsupportedWrapped) lines.push("className?: string");
|
|
16566
16700
|
if (shouldAddStyleProps && allowStyleProp || forceStyleOptional || liftStyleForUnsupportedWrapped) lines.push("style?: React.CSSProperties");
|
|
16567
|
-
if (
|
|
16701
|
+
if (shouldAddOwnSxProp) lines.push(SX_PROP_TYPE_TEXT);
|
|
16568
16702
|
if (this.hasForwardedAsUsage(d.localName)) lines.push(`forwardedAs?: ${forwardedAsPropTypeText}`);
|
|
16569
16703
|
const propsTarget = d.attrsInfo?.attrsAsTag ?? d.base.ident;
|
|
16570
16704
|
const base = this.componentPropsBaseType(propsTarget);
|
|
@@ -16581,7 +16715,8 @@ var WrapperEmitter = class {
|
|
|
16581
16715
|
}
|
|
16582
16716
|
}
|
|
16583
16717
|
const literal = lines.length > 0 ? `{ ${lines.join(", ")} }` : "{}";
|
|
16584
|
-
const
|
|
16718
|
+
const omittedUnique = [...new Set(omitted)];
|
|
16719
|
+
const baseMaybeOmitted = omittedUnique.length ? `Omit<${base}, ${omittedUnique.join(" | ")}>` : base;
|
|
16585
16720
|
return this.joinIntersection(literal !== "{}" ? literal : null, baseMaybeOmitted, ...renamedPropTypes);
|
|
16586
16721
|
}
|
|
16587
16722
|
isPropRequiredInPropsTypeLiteral(propsType, propName) {
|
|
@@ -16922,7 +17057,7 @@ function rewriteNode(j, node, propNames, propsIdentifier, shadowedNames) {
|
|
|
16922
17057
|
renameIdentifierReferences(n.body, propsIdentifier, replacementName);
|
|
16923
17058
|
}
|
|
16924
17059
|
const nextShadowedNames = new Set(shadowedNames);
|
|
16925
|
-
for (const param of readArray(n.params)) collectBindingNames(param, nextShadowedNames);
|
|
17060
|
+
for (const param of readArray(n.params)) collectBindingNames$1(param, nextShadowedNames);
|
|
16926
17061
|
rewriteNode(j, n.body, propNames, propsIdentifier, nextShadowedNames);
|
|
16927
17062
|
return;
|
|
16928
17063
|
}
|
|
@@ -16951,10 +17086,10 @@ function isFunctionLike(node) {
|
|
|
16951
17086
|
}
|
|
16952
17087
|
function functionBindsName(node, name) {
|
|
16953
17088
|
const bindingNames = /* @__PURE__ */ new Set();
|
|
16954
|
-
for (const param of readArray(node.params)) collectBindingNames(param, bindingNames);
|
|
17089
|
+
for (const param of readArray(node.params)) collectBindingNames$1(param, bindingNames);
|
|
16955
17090
|
return bindingNames.has(name);
|
|
16956
17091
|
}
|
|
16957
|
-
function collectBindingNames(node, names) {
|
|
17092
|
+
function collectBindingNames$1(node, names) {
|
|
16958
17093
|
if (!node || typeof node !== "object") return;
|
|
16959
17094
|
const n = node;
|
|
16960
17095
|
if (n.type === "Identifier" && typeof n.name === "string") {
|
|
@@ -16962,21 +17097,21 @@ function collectBindingNames(node, names) {
|
|
|
16962
17097
|
return;
|
|
16963
17098
|
}
|
|
16964
17099
|
if (n.type === "RestElement") {
|
|
16965
|
-
collectBindingNames(n.argument, names);
|
|
17100
|
+
collectBindingNames$1(n.argument, names);
|
|
16966
17101
|
return;
|
|
16967
17102
|
}
|
|
16968
17103
|
if (n.type === "AssignmentPattern") {
|
|
16969
|
-
collectBindingNames(n.left, names);
|
|
17104
|
+
collectBindingNames$1(n.left, names);
|
|
16970
17105
|
return;
|
|
16971
17106
|
}
|
|
16972
17107
|
if (n.type === "ObjectPattern") {
|
|
16973
17108
|
for (const property of readArray(n.properties)) {
|
|
16974
17109
|
const typedProperty = property;
|
|
16975
|
-
collectBindingNames(typedProperty.type === "Property" || typedProperty.type === "ObjectProperty" ? typedProperty.value : property, names);
|
|
17110
|
+
collectBindingNames$1(typedProperty.type === "Property" || typedProperty.type === "ObjectProperty" ? typedProperty.value : property, names);
|
|
16976
17111
|
}
|
|
16977
17112
|
return;
|
|
16978
17113
|
}
|
|
16979
|
-
if (n.type === "ArrayPattern") for (const element of readArray(n.elements)) collectBindingNames(element, names);
|
|
17114
|
+
if (n.type === "ArrayPattern") for (const element of readArray(n.elements)) collectBindingNames$1(element, names);
|
|
16980
17115
|
}
|
|
16981
17116
|
function renameBindingName(node, fromName, toName) {
|
|
16982
17117
|
if (!node || typeof node !== "object") return;
|
|
@@ -17027,7 +17162,7 @@ function renameIdentifierReferences(node, fromName, toName) {
|
|
|
17027
17162
|
}
|
|
17028
17163
|
if (n.type === "VariableDeclarator") {
|
|
17029
17164
|
const bindingNames = /* @__PURE__ */ new Set();
|
|
17030
|
-
collectBindingNames(n.id, bindingNames);
|
|
17165
|
+
collectBindingNames$1(n.id, bindingNames);
|
|
17031
17166
|
if (!bindingNames.has(fromName)) renameIdentifierReferences(n.init, fromName, toName);
|
|
17032
17167
|
return;
|
|
17033
17168
|
}
|
|
@@ -17038,23 +17173,23 @@ function renameIdentifierReferences(node, fromName, toName) {
|
|
|
17038
17173
|
}
|
|
17039
17174
|
function uniqueIdentifierName(node, baseName) {
|
|
17040
17175
|
const names = /* @__PURE__ */ new Set();
|
|
17041
|
-
collectIdentifierNames(node, names);
|
|
17176
|
+
collectIdentifierNames$1(node, names);
|
|
17042
17177
|
if (!names.has(baseName)) return baseName;
|
|
17043
17178
|
let index = 1;
|
|
17044
17179
|
while (names.has(`${baseName}${index}`)) index++;
|
|
17045
17180
|
return `${baseName}${index}`;
|
|
17046
17181
|
}
|
|
17047
|
-
function collectIdentifierNames(node, names) {
|
|
17182
|
+
function collectIdentifierNames$1(node, names) {
|
|
17048
17183
|
if (!node || typeof node !== "object") return;
|
|
17049
17184
|
if (Array.isArray(node)) {
|
|
17050
|
-
for (const item of node) collectIdentifierNames(item, names);
|
|
17185
|
+
for (const item of node) collectIdentifierNames$1(item, names);
|
|
17051
17186
|
return;
|
|
17052
17187
|
}
|
|
17053
17188
|
const n = node;
|
|
17054
17189
|
if (n.type === "Identifier" && typeof n.name === "string") names.add(n.name);
|
|
17055
17190
|
for (const key of Object.keys(n)) {
|
|
17056
17191
|
if (IGNORED_KEYS.has(key)) continue;
|
|
17057
|
-
collectIdentifierNames(n[key], names);
|
|
17192
|
+
collectIdentifierNames$1(n[key], names);
|
|
17058
17193
|
}
|
|
17059
17194
|
}
|
|
17060
17195
|
function replaceNode(target, replacement) {
|
|
@@ -17268,6 +17403,8 @@ function emitComponentWrappers(emitter) {
|
|
|
17268
17403
|
const explicitPropsTypeRef = resolveTypeReferenceName(d.propsType ?? null);
|
|
17269
17404
|
const wrapperReusesWrappedPropsType = explicitPropsTypeRef?.kind === "identifier" && explicitPropsTypeRef.name === emitter.resolveWrappedExplicitPropsTypeRef(renderedComponent)?.name;
|
|
17270
17405
|
const wrapperPropsExposeSx = wrappedAcceptsSx && !attrsProvidedPropNames.has("sx") && (wrappedLocalDecl && d.propsType && !wrapperReusesWrappedPropsType && !propsTypeOmitsProp(d.propsType, "sx") || propsTypeExposesForwardedSx(d.propsType, wrappedComponent) || !d.propsType || !wrappedLocalDecl);
|
|
17406
|
+
const exposeSxProp = wrapperPropsExposeSx || allowSxProp;
|
|
17407
|
+
const shouldAddOwnSxProp = exposeSxProp && !wrapperPropsExposeSx;
|
|
17271
17408
|
const wrappedClassNameRequired = allowClassNameProp && wrappedHasClassName && (baseComponentPropsType ? emitter.isPropRequiredInPropsTypeLiteral(baseComponentPropsType, "className") : emitter.typedComponentProp(wrappedComponent, "className")?.optional === false);
|
|
17272
17409
|
const wrappedStyleRequired = allowStyleProp && wrappedHasStyle && (baseComponentPropsType ? emitter.isPropRequiredInPropsTypeLiteral(baseComponentPropsType, "style") : emitter.typedComponentProp(wrappedComponent, "style")?.optional === false);
|
|
17273
17410
|
const forceClassNameOptional = !!wrappedClassNameRequired;
|
|
@@ -17309,7 +17446,7 @@ function emitComponentWrappers(emitter) {
|
|
|
17309
17446
|
const optionalProps = [];
|
|
17310
17447
|
if (forceClassNameOptional) optionalProps.push("className?: string");
|
|
17311
17448
|
if (forceStyleOptional) optionalProps.push("style?: React.CSSProperties");
|
|
17312
|
-
if (
|
|
17449
|
+
if (shouldAddOwnSxProp) optionalProps.push(SX_PROP_TYPE_TEXT);
|
|
17313
17450
|
if (hasForwardedAsUsage) optionalProps.push(`forwardedAs?: ${forwardedAsPropTypeText}`);
|
|
17314
17451
|
if (emitter.extendExistingInterface(explicitTypeName, baseWithOmit)) {
|
|
17315
17452
|
if (optionalProps.length > 0) emitter.injectMembersIntoInterface(explicitTypeName, optionalProps);
|
|
@@ -17326,7 +17463,7 @@ function emitComponentWrappers(emitter) {
|
|
|
17326
17463
|
appendAttrsProvidedPropOmissions(omitted, d.attrsInfo, attrsProvidedPropOptions);
|
|
17327
17464
|
const intrinsicBase = defaultTag ? `Omit<React.ComponentPropsWithRef<"${defaultTag}">, keyof ${explicitTypeName}${omitted.length ? ` | ${omitted.join(" | ")}` : ""}>` : null;
|
|
17328
17465
|
const optionalProps = [];
|
|
17329
|
-
if (
|
|
17466
|
+
if (shouldAddOwnSxProp) optionalProps.push(SX_PROP_TYPE_TEXT);
|
|
17330
17467
|
if (hasForwardedAsUsage) optionalProps.push(`forwardedAs?: ${forwardedAsPropTypeText}`);
|
|
17331
17468
|
inlineTypeText = emitter.joinIntersection(transformExplicitPropsTypeText({
|
|
17332
17469
|
canMutateExplicitType,
|
|
@@ -17349,7 +17486,7 @@ function emitComponentWrappers(emitter) {
|
|
|
17349
17486
|
const optionalStyleProps = [];
|
|
17350
17487
|
if (forceClassNameOptional) optionalStyleProps.push("className?: string");
|
|
17351
17488
|
if (forceStyleOptional) optionalStyleProps.push("style?: React.CSSProperties");
|
|
17352
|
-
if (
|
|
17489
|
+
if (shouldAddOwnSxProp) optionalStyleProps.push(SX_PROP_TYPE_TEXT);
|
|
17353
17490
|
const typeText = [
|
|
17354
17491
|
baseProps,
|
|
17355
17492
|
`Omit<React.ComponentPropsWithRef<C>, ${buildOmitUnion([
|
|
@@ -17371,14 +17508,16 @@ function emitComponentWrappers(emitter) {
|
|
|
17371
17508
|
].join(" & ");
|
|
17372
17509
|
emitNamedPropsType(d.localName, typeText, `C extends React.ElementType = typeof ${wrappedComponent}`);
|
|
17373
17510
|
} else {
|
|
17374
|
-
const
|
|
17511
|
+
const wrappedComponentIsStyledWrapper = wrapperDecls.some((decl) => decl.localName === wrappedComponent);
|
|
17512
|
+
const skipStyleProps = wrappedComponentIsStyledWrapper || wrappedHasClassName && wrappedHasStyle;
|
|
17375
17513
|
const hasExplicitPropsType = !!explicit;
|
|
17376
17514
|
const inferred = emitter.inferredComponentWrapperPropsTypeText({
|
|
17377
17515
|
d,
|
|
17378
17516
|
allowClassNameProp,
|
|
17379
17517
|
allowStyleProp,
|
|
17380
|
-
allowSxProp,
|
|
17518
|
+
allowSxProp: shouldAddOwnSxProp,
|
|
17381
17519
|
wrappedComponentIsInternalWrapper: skipStyleProps,
|
|
17520
|
+
wrappedComponentIsStyledWrapper,
|
|
17382
17521
|
hasExplicitPropsType,
|
|
17383
17522
|
forceClassNameOptional,
|
|
17384
17523
|
forceStyleOptional,
|
|
@@ -17394,10 +17533,13 @@ function emitComponentWrappers(emitter) {
|
|
|
17394
17533
|
const wrappedRejectsStyle = !!wrappedComponent && emitter.wrappedRejectsStyleProp(wrappedComponent, "style");
|
|
17395
17534
|
const shouldLiftClassNameOntoExplicit = !skipStyleProps && allowClassNameProp && wrappedRejectsClassName;
|
|
17396
17535
|
const shouldLiftStyleOntoExplicit = !skipStyleProps && allowStyleProp && wrappedRejectsStyle;
|
|
17397
|
-
|
|
17398
|
-
|
|
17399
|
-
|
|
17400
|
-
|
|
17536
|
+
const shouldLiftSxOntoExplicit = !wrappedComponentIsStyledWrapper && shouldAddOwnSxProp;
|
|
17537
|
+
const shouldInjectClassNameOntoExplicit = !skipStyleProps && allowClassNameProp && (d.supportsExternalStyles && !wrappedHasClassName || shouldLiftClassNameOntoExplicit);
|
|
17538
|
+
const shouldInjectStyleOntoExplicit = !skipStyleProps && allowStyleProp && (d.supportsExternalStyles && !wrappedHasStyle || shouldLiftStyleOntoExplicit);
|
|
17539
|
+
if (explicitWithExtras && (shouldInjectClassNameOntoExplicit || shouldInjectStyleOntoExplicit || shouldLiftClassNameOntoExplicit || shouldLiftStyleOntoExplicit || shouldLiftSxOntoExplicit)) explicitWithExtras = injectStylePropsIntoTypeLiteralString(explicitWithExtras, {
|
|
17540
|
+
className: shouldInjectClassNameOntoExplicit,
|
|
17541
|
+
style: shouldInjectStyleOntoExplicit,
|
|
17542
|
+
sx: shouldLiftSxOntoExplicit
|
|
17401
17543
|
});
|
|
17402
17544
|
const explicitWithRef = (explicitWithExtras ? transformExplicitPropsTypeText({
|
|
17403
17545
|
canMutateExplicitType,
|
|
@@ -17408,7 +17550,7 @@ function emitComponentWrappers(emitter) {
|
|
|
17408
17550
|
}) : null) ?? (refElementType ? `{ ref?: React.Ref<${refElementType}>; }` : null);
|
|
17409
17551
|
const typeText = explicitWithRef ? emitter.joinIntersection(explicitWithRef, inferred) : inferred;
|
|
17410
17552
|
const hasNoCustomProps = !explicitWithRef;
|
|
17411
|
-
if (hasNoCustomProps && shouldLowerForwardedAs && renderedAsProp?.baseTypeText && !
|
|
17553
|
+
if (hasNoCustomProps && shouldLowerForwardedAs && renderedAsProp?.baseTypeText && !exposeSxProp && !forceClassNameOptional && !forceStyleOptional) emitNamedPropsType(d.localName, emitter.joinIntersection(renderedAsProp.baseTypeText, `{ forwardedAs?: ${renderedAsProp.typeText} }`));
|
|
17412
17554
|
else if (hasNoCustomProps || explicitAttrsOmitUnion && explicitTypeExists) inlineTypeText = typeText;
|
|
17413
17555
|
else if (!emitNamedPropsType(d.localName, typeText)) inlineTypeText = typeText;
|
|
17414
17556
|
}
|
|
@@ -17519,7 +17661,7 @@ function emitComponentWrappers(emitter) {
|
|
|
17519
17661
|
if (node.type !== "TSTypeReference") return;
|
|
17520
17662
|
const typeName = resolveTypeIdentifierName(node);
|
|
17521
17663
|
if (!typeName) {
|
|
17522
|
-
for (const param of getTypeReferenceParams(node)) collectFromTypeNode(param);
|
|
17664
|
+
for (const param of getTypeReferenceParams$1(node)) collectFromTypeNode(param);
|
|
17523
17665
|
return;
|
|
17524
17666
|
}
|
|
17525
17667
|
if (visitedTypeNames.has(typeName)) return;
|
|
@@ -17637,14 +17779,14 @@ function emitComponentWrappers(emitter) {
|
|
|
17637
17779
|
const componentId = j.identifier("Component");
|
|
17638
17780
|
const forwardedAsId = j.identifier("forwardedAs");
|
|
17639
17781
|
const wrappedComponentExpr = buildWrappedComponentExpr();
|
|
17640
|
-
if (
|
|
17782
|
+
if (exposeSxProp) styleArgs.push(sxId);
|
|
17641
17783
|
if (attrsSxExpr) styleArgs.push(attrsSxExpr);
|
|
17642
17784
|
for (const attr of defaultAttrs) if (!destructureProps.includes(attr.jsxProp)) destructureProps.push(attr.jsxProp);
|
|
17643
17785
|
for (const attr of dynamicAttrs) if (!destructureProps.includes(attr.jsxProp)) destructureProps.push(attr.jsxProp);
|
|
17644
17786
|
const canForwardClassNameStyleThroughRest = wrappedAcceptsSx && !(staticClassNameExpr || attrsStaticStyleExpr || d.inlineStyleProps?.length);
|
|
17645
17787
|
const destructureClassName = allowClassNameProp && !canForwardClassNameStyleThroughRest;
|
|
17646
17788
|
const destructureStyle = allowStyleProp && !canForwardClassNameStyleThroughRest;
|
|
17647
|
-
const destructureSx =
|
|
17789
|
+
const destructureSx = exposeSxProp;
|
|
17648
17790
|
const destructurePropsForPattern = needsUseTheme ? destructureProps.filter((name) => name !== "theme") : destructureProps;
|
|
17649
17791
|
const patternProps = emitter.buildDestructurePatternProps({
|
|
17650
17792
|
baseProps: [
|
|
@@ -17670,7 +17812,7 @@ function emitComponentWrappers(emitter) {
|
|
|
17670
17812
|
styleId,
|
|
17671
17813
|
allowClassNameProp,
|
|
17672
17814
|
allowStyleProp,
|
|
17673
|
-
allowSxProp,
|
|
17815
|
+
allowSxProp: exposeSxProp,
|
|
17674
17816
|
inlineStyleProps: d.inlineStyleProps ?? [],
|
|
17675
17817
|
staticStyleExpr: attrsStaticStyleExpr,
|
|
17676
17818
|
staticClassNameExpr,
|
|
@@ -17909,16 +18051,16 @@ function buildRenamedTransientPropTypes(sourceTypeText, transientPropRenames) {
|
|
|
17909
18051
|
function parenthesizeTypeForIndexedAccess(typeText) {
|
|
17910
18052
|
return `(${typeText})`;
|
|
17911
18053
|
}
|
|
17912
|
-
function getTypeReferenceParams(type) {
|
|
18054
|
+
function getTypeReferenceParams$1(type) {
|
|
17913
18055
|
const typed = type;
|
|
17914
18056
|
return typed.typeParameters?.params ?? typed.typeArguments?.params ?? [];
|
|
17915
18057
|
}
|
|
17916
18058
|
function getUtilitySourceTypeParams(type) {
|
|
17917
|
-
if (!isUtilityTypeReference(resolveTypeReferenceName(type))) return null;
|
|
17918
|
-
const sourceType = getTypeReferenceParams(type)[0];
|
|
18059
|
+
if (!isUtilityTypeReference$1(resolveTypeReferenceName(type))) return null;
|
|
18060
|
+
const sourceType = getTypeReferenceParams$1(type)[0];
|
|
17919
18061
|
return sourceType ? [sourceType] : [];
|
|
17920
18062
|
}
|
|
17921
|
-
function isUtilityTypeReference(typeName) {
|
|
18063
|
+
function isUtilityTypeReference$1(typeName) {
|
|
17922
18064
|
if (!typeName) return false;
|
|
17923
18065
|
if (typeName.kind === "qualified") return typeName.namespace === "React" && typeName.name === "PropsWithChildren";
|
|
17924
18066
|
return [
|
|
@@ -18066,7 +18208,7 @@ function removeAttrsMembersInExistingType(emitter, typeName, attrsProvidedPropNa
|
|
|
18066
18208
|
for (const part of typed.types) visitType(part);
|
|
18067
18209
|
return;
|
|
18068
18210
|
}
|
|
18069
|
-
if (typed.type === "TSTypeReference") for (const param of getTypeReferenceParams(node)) visitType(param);
|
|
18211
|
+
if (typed.type === "TSTypeReference") for (const param of getTypeReferenceParams$1(node)) visitType(param);
|
|
18070
18212
|
};
|
|
18071
18213
|
root.find(j.TSTypeAliasDeclaration, { id: {
|
|
18072
18214
|
type: "Identifier",
|
|
@@ -18118,30 +18260,33 @@ function resolveTypeIdentifierName(type) {
|
|
|
18118
18260
|
function resolveTypeReferenceName(type) {
|
|
18119
18261
|
if (type?.type !== "TSTypeReference") return null;
|
|
18120
18262
|
const typeName = type.typeName;
|
|
18121
|
-
return getTypeReferenceName(typeName);
|
|
18263
|
+
return getTypeReferenceName$1(typeName);
|
|
18122
18264
|
}
|
|
18123
18265
|
function typeReferenceIsComponentPropsOfWrapped(type, wrappedComponent) {
|
|
18124
18266
|
if (type.type !== "TSTypeReference") return false;
|
|
18125
18267
|
const node = type;
|
|
18126
|
-
const typeName = getTypeReferenceName(node.typeName);
|
|
18268
|
+
const typeName = getTypeReferenceName$1(node.typeName);
|
|
18127
18269
|
if (!typeName) return false;
|
|
18128
|
-
if (!(typeName.kind === "identifier" && typeName.name
|
|
18270
|
+
if (!(typeName.kind === "identifier" && isReactComponentPropsUtilityName$1(typeName.name) || typeName.kind === "qualified" && typeName.namespace === "React" && isReactComponentPropsUtilityName$1(typeName.name))) return false;
|
|
18129
18271
|
return (node.typeParameters?.params ?? []).some((param) => {
|
|
18130
18272
|
const query = param;
|
|
18131
|
-
return query.type === "TSTypeQuery" && getTypeQueryExpressionName(query.exprName) === wrappedComponent;
|
|
18273
|
+
return query.type === "TSTypeQuery" && getTypeQueryExpressionName$1(query.exprName) === wrappedComponent;
|
|
18132
18274
|
});
|
|
18133
18275
|
}
|
|
18276
|
+
function isReactComponentPropsUtilityName$1(name) {
|
|
18277
|
+
return name === "ComponentProps" || name === "ComponentPropsWithRef" || name === "ComponentPropsWithoutRef";
|
|
18278
|
+
}
|
|
18134
18279
|
function isIntrinsicPassthroughType(emitter, type) {
|
|
18135
18280
|
const text = emitter.stringifyTsType(type);
|
|
18136
18281
|
return text !== null && /^React\.ComponentProps(?:WithRef)?<"[^"]+">$/.test(text);
|
|
18137
18282
|
}
|
|
18138
|
-
function getTypeQueryExpressionName(exprName) {
|
|
18283
|
+
function getTypeQueryExpressionName$1(exprName) {
|
|
18139
18284
|
const node = exprName;
|
|
18140
18285
|
if (!node) return null;
|
|
18141
18286
|
if (node.type === "Identifier" && node.name) return node.name;
|
|
18142
18287
|
if (node.type !== "TSQualifiedName") return null;
|
|
18143
|
-
const left = getTypeQueryExpressionName(node.left);
|
|
18144
|
-
const right = getTypeQueryExpressionName(node.right);
|
|
18288
|
+
const left = getTypeQueryExpressionName$1(node.left);
|
|
18289
|
+
const right = getTypeQueryExpressionName$1(node.right);
|
|
18145
18290
|
return left && right ? `${left}.${right}` : null;
|
|
18146
18291
|
}
|
|
18147
18292
|
function propsTypeOmitsProp(propsType, propName) {
|
|
@@ -18150,7 +18295,7 @@ function propsTypeOmitsProp(propsType, propName) {
|
|
|
18150
18295
|
visitAst(propsType, (node) => {
|
|
18151
18296
|
if (found || node.type !== "TSTypeReference") return;
|
|
18152
18297
|
const typeReference = node;
|
|
18153
|
-
const typeName = getTypeReferenceName(typeReference.typeName);
|
|
18298
|
+
const typeName = getTypeReferenceName$1(typeReference.typeName);
|
|
18154
18299
|
if (typeName?.kind !== "identifier" || typeName.name !== "Omit") return;
|
|
18155
18300
|
if ((typeReference.typeParameters?.params ?? []).some((param) => typeNodeContainsStringLiteral(param, propName))) found = true;
|
|
18156
18301
|
});
|
|
@@ -18174,7 +18319,7 @@ function visitAst(node, visitor) {
|
|
|
18174
18319
|
else if (child && typeof child === "object") visitAst(child, visitor);
|
|
18175
18320
|
}
|
|
18176
18321
|
}
|
|
18177
|
-
function getTypeReferenceName(typeName) {
|
|
18322
|
+
function getTypeReferenceName$1(typeName) {
|
|
18178
18323
|
if (typeName?.type === "Identifier" && typeName.name) return {
|
|
18179
18324
|
kind: "identifier",
|
|
18180
18325
|
name: typeName.name
|
|
@@ -18210,7 +18355,7 @@ function membersExposeProp(members, propName) {
|
|
|
18210
18355
|
function getHeritageTypeReferenceName(heritage) {
|
|
18211
18356
|
if (!heritage || typeof heritage !== "object") return null;
|
|
18212
18357
|
const node = heritage;
|
|
18213
|
-
return getTypeReferenceName(node.expression) ?? getTypeReferenceName(node.typeName);
|
|
18358
|
+
return getTypeReferenceName$1(node.expression) ?? getTypeReferenceName$1(node.typeName);
|
|
18214
18359
|
}
|
|
18215
18360
|
function importedPropsTypeExposesForwardedSx(args) {
|
|
18216
18361
|
const { emitter, typeName, wrappedComponent, seenTypeNames } = args;
|
|
@@ -28068,6 +28213,7 @@ function finalizeDeclProcessing(ctx) {
|
|
|
28068
28213
|
convertStyleFnsToPropsPattern(state.j, styleFnDecls, styleFnFromProps, decl.styleKey);
|
|
28069
28214
|
alignComputedCallArgStyleFnParams(styleFnDecls, styleFnFromProps);
|
|
28070
28215
|
unionStyleFnParamsFromStyleFnFromProps(state.j, decl, styleFnDecls, styleFnFromProps);
|
|
28216
|
+
narrowGuardedStyleFnParamTypes(state.j, decl, styleFnDecls, styleFnFromProps);
|
|
28071
28217
|
insertStyleFnDeclsAfterComponent(resolvedStyleObjects, styleFnDecls, {
|
|
28072
28218
|
styleKey: decl.styleKey,
|
|
28073
28219
|
extraStyleObjects,
|
|
@@ -28127,6 +28273,154 @@ function unionStyleFnParamsFromStyleFnFromProps(j, decl, styleFnDecls, styleFnFr
|
|
|
28127
28273
|
}
|
|
28128
28274
|
}
|
|
28129
28275
|
}
|
|
28276
|
+
function narrowGuardedStyleFnParamTypes(j, decl, styleFnDecls, styleFnFromProps) {
|
|
28277
|
+
const entriesByFnKey = /* @__PURE__ */ new Map();
|
|
28278
|
+
for (const entry of styleFnFromProps) {
|
|
28279
|
+
const entries = entriesByFnKey.get(entry.fnKey) ?? [];
|
|
28280
|
+
entries.push(entry);
|
|
28281
|
+
entriesByFnKey.set(entry.fnKey, entries);
|
|
28282
|
+
}
|
|
28283
|
+
for (const fnKey of styleFnDecls.keys()) {
|
|
28284
|
+
const fnAst = styleFnDecls.get(fnKey);
|
|
28285
|
+
if (!isArrowFunctionWithParams(fnAst)) continue;
|
|
28286
|
+
const firstParam = fnAst.params[0];
|
|
28287
|
+
if (!firstParam) continue;
|
|
28288
|
+
const entries = entriesByFnKey.get(fnKey);
|
|
28289
|
+
const paramName = readParamName(firstParam);
|
|
28290
|
+
const variantWhens = Object.entries(decl.variantStyleKeys ?? {}).filter(([, key]) => key === fnKey).map(([when]) => when);
|
|
28291
|
+
const extraStylexCalls = collectExtraStylexPropsArgCalls(decl, fnKey);
|
|
28292
|
+
const variantCallIsGuarded = paramName !== null && variantWhens.length > 0 && variantWhens.every((when) => conditionWhenGuardsProp(when, paramName));
|
|
28293
|
+
const extraStylexCallIsGuarded = extraStylexCalls.length > 0 && extraStylexCalls.every((call) => extraStylexCallGuardsPrimaryArg(call));
|
|
28294
|
+
if (!(entries ? entries.every(styleFnEntryGuardsPrimaryArg) : variantCallIsGuarded || extraStylexCallIsGuarded)) continue;
|
|
28295
|
+
removeUndefinedFromParamType(j, firstParam);
|
|
28296
|
+
}
|
|
28297
|
+
}
|
|
28298
|
+
function styleFnEntryGuardsPrimaryArg(entry) {
|
|
28299
|
+
if (entry.jsxProp === "__props" || entry.jsxProp === "__helper") return false;
|
|
28300
|
+
if (!styleFnEntryPrimaryArgIsProp(entry)) return false;
|
|
28301
|
+
if (entry.condition === "truthy") return true;
|
|
28302
|
+
if (entry.condition === "always") return false;
|
|
28303
|
+
return entry.conditionWhen ? conditionWhenGuardsProp(entry.conditionWhen, entry.jsxProp) : false;
|
|
28304
|
+
}
|
|
28305
|
+
function styleFnEntryPrimaryArgIsProp(entry) {
|
|
28306
|
+
if (!entry.callArg) return true;
|
|
28307
|
+
const argName = readIdentifierLikeName(entry.callArg);
|
|
28308
|
+
return argName !== null && normalizePropName(argName) === normalizePropName(entry.jsxProp);
|
|
28309
|
+
}
|
|
28310
|
+
function conditionWhenGuardsProp(when, propName) {
|
|
28311
|
+
const prop = escapeRegExp(normalizePropName(propName));
|
|
28312
|
+
const trimmed = when.trim();
|
|
28313
|
+
return new RegExp(`^${prop}$`).test(trimmed) || new RegExp(`^${prop}\\s*\\|\\|\\s*false$`).test(trimmed) || new RegExp(`^${prop}\\s*!=\\s*null$`).test(trimmed) || new RegExp(`^${prop}\\s*!==?\\s*undefined$`).test(trimmed);
|
|
28314
|
+
}
|
|
28315
|
+
function collectExtraStylexPropsArgCalls(decl, fnKey) {
|
|
28316
|
+
const calls = [];
|
|
28317
|
+
for (const entry of decl.extraStylexPropsArgs ?? []) {
|
|
28318
|
+
const call = readExtraStylexPropsArgCall(entry.expr);
|
|
28319
|
+
if (call?.fnKey === fnKey) calls.push({
|
|
28320
|
+
when: entry.when,
|
|
28321
|
+
condition: call.condition,
|
|
28322
|
+
argName: call.argName
|
|
28323
|
+
});
|
|
28324
|
+
}
|
|
28325
|
+
return calls;
|
|
28326
|
+
}
|
|
28327
|
+
function extraStylexCallGuardsPrimaryArg(call) {
|
|
28328
|
+
if (call.argName === null) return false;
|
|
28329
|
+
if (call.when !== void 0) return conditionWhenGuardsProp(call.when, call.argName);
|
|
28330
|
+
return call.condition !== null && conditionExprGuardsProp(call.condition, call.argName);
|
|
28331
|
+
}
|
|
28332
|
+
function readExtraStylexPropsArgCall(expr) {
|
|
28333
|
+
const direct = readStyleFnCall(expr);
|
|
28334
|
+
if (direct) return {
|
|
28335
|
+
...direct,
|
|
28336
|
+
condition: null
|
|
28337
|
+
};
|
|
28338
|
+
if (!expr || typeof expr !== "object" || expr.type !== "ConditionalExpression") return null;
|
|
28339
|
+
const conditional = expr;
|
|
28340
|
+
if (!conditional.test || !conditional.consequent) return null;
|
|
28341
|
+
const consequentCall = readStyleFnCall(conditional.consequent);
|
|
28342
|
+
return consequentCall ? {
|
|
28343
|
+
...consequentCall,
|
|
28344
|
+
condition: conditional.test
|
|
28345
|
+
} : null;
|
|
28346
|
+
}
|
|
28347
|
+
function readStyleFnCall(expr) {
|
|
28348
|
+
if (!expr || typeof expr !== "object" || expr.type !== "CallExpression") return null;
|
|
28349
|
+
const call = expr;
|
|
28350
|
+
const callee = call.callee;
|
|
28351
|
+
if (!callee || typeof callee !== "object") return null;
|
|
28352
|
+
const member = callee;
|
|
28353
|
+
if (member.type !== "MemberExpression" || member.computed) return null;
|
|
28354
|
+
const property = member.property;
|
|
28355
|
+
const fnKey = property && typeof property === "object" && property.type === "Identifier" ? property.name ?? null : null;
|
|
28356
|
+
if (!fnKey) return null;
|
|
28357
|
+
return {
|
|
28358
|
+
fnKey,
|
|
28359
|
+
argName: call.arguments?.[0] ? readIdentifierLikeName(call.arguments[0]) : null
|
|
28360
|
+
};
|
|
28361
|
+
}
|
|
28362
|
+
function conditionExprGuardsProp(condition, propName) {
|
|
28363
|
+
if (!condition || typeof condition !== "object") return false;
|
|
28364
|
+
const normalizedProp = normalizePropName(propName);
|
|
28365
|
+
const conditionName = readIdentifierLikeName(condition);
|
|
28366
|
+
if (conditionName !== null) return normalizePropName(conditionName) === normalizedProp;
|
|
28367
|
+
const typed = condition;
|
|
28368
|
+
if (typed.type === "LogicalExpression" && typed.operator === "||") return typed.left !== void 0 && typed.right !== void 0 && conditionExprGuardsProp(typed.left, propName) && isFalseLiteral(typed.right);
|
|
28369
|
+
if (typed.type === "BinaryExpression" && typed.left && typed.right) {
|
|
28370
|
+
const leftName = readIdentifierLikeName(typed.left);
|
|
28371
|
+
if (!(leftName !== null && normalizePropName(leftName) === normalizedProp)) return false;
|
|
28372
|
+
return typed.operator === "!=" && isNullLiteral(typed.right) || (typed.operator === "!==" || typed.operator === "!=") && isUndefinedIdentifier(typed.right);
|
|
28373
|
+
}
|
|
28374
|
+
return false;
|
|
28375
|
+
}
|
|
28376
|
+
function isFalseLiteral(node) {
|
|
28377
|
+
return !!node && typeof node === "object" && (node.type === "BooleanLiteral" && node.value === false || node.type === "Literal" && node.value === false);
|
|
28378
|
+
}
|
|
28379
|
+
function isNullLiteral(node) {
|
|
28380
|
+
return !!node && typeof node === "object" && (node.type === "NullLiteral" || node.type === "Literal" && node.value === null);
|
|
28381
|
+
}
|
|
28382
|
+
function isUndefinedIdentifier(node) {
|
|
28383
|
+
return !!node && typeof node === "object" && node.type === "Identifier" && node.name === "undefined";
|
|
28384
|
+
}
|
|
28385
|
+
function readIdentifierLikeName(node) {
|
|
28386
|
+
let current = node;
|
|
28387
|
+
while (current && typeof current === "object") {
|
|
28388
|
+
const typed = current;
|
|
28389
|
+
if (typed.type === "Identifier") return typed.name ?? null;
|
|
28390
|
+
if (typed.type === "ParenthesizedExpression" || typed.type === "TSAsExpression" || typed.type === "TSNonNullExpression") {
|
|
28391
|
+
current = typed.expression;
|
|
28392
|
+
continue;
|
|
28393
|
+
}
|
|
28394
|
+
if (typed.type === "TemplateLiteral" && typed.expressions?.length === 1) {
|
|
28395
|
+
current = typed.expressions[0];
|
|
28396
|
+
continue;
|
|
28397
|
+
}
|
|
28398
|
+
break;
|
|
28399
|
+
}
|
|
28400
|
+
return null;
|
|
28401
|
+
}
|
|
28402
|
+
function normalizePropName(name) {
|
|
28403
|
+
return name.startsWith("$") ? name.slice(1) : name;
|
|
28404
|
+
}
|
|
28405
|
+
function escapeRegExp(value) {
|
|
28406
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
28407
|
+
}
|
|
28408
|
+
function isArrowFunctionWithParams(node) {
|
|
28409
|
+
return !!node && typeof node === "object" && node.type === "ArrowFunctionExpression" && Array.isArray(node.params);
|
|
28410
|
+
}
|
|
28411
|
+
function readParamName(param) {
|
|
28412
|
+
return param && typeof param === "object" && param.type === "Identifier" ? param.name ?? null : null;
|
|
28413
|
+
}
|
|
28414
|
+
function removeUndefinedFromParamType(j, param) {
|
|
28415
|
+
const typedParam = param;
|
|
28416
|
+
const typeAnnotation = typedParam.typeAnnotation?.typeAnnotation;
|
|
28417
|
+
if (!typeAnnotation || typeof typeAnnotation !== "object") return;
|
|
28418
|
+
const typeNode = typeAnnotation;
|
|
28419
|
+
if (typeNode.type !== "TSUnionType" || !typeNode.types) return;
|
|
28420
|
+
const narrowedTypes = typeNode.types.filter((member) => !(member && typeof member === "object" && member.type === "TSUndefinedKeyword"));
|
|
28421
|
+
if (narrowedTypes.length === typeNode.types.length || narrowedTypes.length === 0) return;
|
|
28422
|
+
typedParam.typeAnnotation = narrowedTypes.length === 1 ? j.tsTypeAnnotation(narrowedTypes[0]) : j.tsTypeAnnotation(j.tsUnionType(narrowedTypes));
|
|
28423
|
+
}
|
|
28130
28424
|
function buildStyleFnParam(j, decl, propName) {
|
|
28131
28425
|
const param = j.identifier(propName);
|
|
28132
28426
|
const typeNode = typeNodeFromPropTypeText(j, decl.typeScriptPropTypes?.get(propName));
|
|
@@ -36245,7 +36539,7 @@ function lowerRules(ctx) {
|
|
|
36245
36539
|
}
|
|
36246
36540
|
const snapshot = snapshotStateForDecl(state);
|
|
36247
36541
|
state.currentDecl = decl;
|
|
36248
|
-
const outcome = processOneDecl(state, decl);
|
|
36542
|
+
const outcome = processOneDecl(ctx, state, decl);
|
|
36249
36543
|
state.currentDecl = null;
|
|
36250
36544
|
if (outcome === "skip") {
|
|
36251
36545
|
restoreStateSnapshot(state, snapshot);
|
|
@@ -36301,16 +36595,24 @@ function lowerRules(ctx) {
|
|
|
36301
36595
|
* Returns `"skip"` if the decl marked itself skipped (per-decl bail), `"bail"` if
|
|
36302
36596
|
* something set the file-level bail, or `"ok"` on success.
|
|
36303
36597
|
*/
|
|
36304
|
-
function processOneDecl(state, decl) {
|
|
36305
|
-
const
|
|
36306
|
-
|
|
36307
|
-
|
|
36308
|
-
if (
|
|
36309
|
-
|
|
36310
|
-
|
|
36311
|
-
|
|
36312
|
-
|
|
36313
|
-
|
|
36598
|
+
function processOneDecl(ctx, state, decl) {
|
|
36599
|
+
const previousUseLogicalProperties = getUseLogicalProperties();
|
|
36600
|
+
const componentInterface = decl.base.kind === "component" ? wrappedComponentInterfaceFor(ctx, decl.base.ident) : void 0;
|
|
36601
|
+
const forcePhysicalDirectionalShorthands = componentInterface?.acceptsSx === true && componentInterface.sxExcludedProperties?.some((prop) => LOGICAL_TO_PHYSICAL[prop]) === true;
|
|
36602
|
+
if (forcePhysicalDirectionalShorthands) setUseLogicalProperties(false);
|
|
36603
|
+
try {
|
|
36604
|
+
const declState = createDeclProcessingState(state, decl);
|
|
36605
|
+
if (!preScanCssHelperPlaceholders(declState)) return decl.skipTransform ? "skip" : "bail";
|
|
36606
|
+
processDeclRules(declState);
|
|
36607
|
+
if (decl.skipTransform) return "skip";
|
|
36608
|
+
if (state.bail) return "bail";
|
|
36609
|
+
finalizeDeclProcessing(declState);
|
|
36610
|
+
if (decl.skipTransform) return "skip";
|
|
36611
|
+
if (state.bail) return "bail";
|
|
36612
|
+
return "ok";
|
|
36613
|
+
} finally {
|
|
36614
|
+
if (forcePhysicalDirectionalShorthands) setUseLogicalProperties(previousUseLogicalProperties);
|
|
36615
|
+
}
|
|
36314
36616
|
}
|
|
36315
36617
|
function markPartialImportedComponentRoots(ctx, state) {
|
|
36316
36618
|
for (const decl of state.styledDecls) if (shouldSkipPartialImportedComponentRoot(ctx, decl)) decl.skipTransform = true;
|
|
@@ -37190,6 +37492,609 @@ function isNonJsxTypeScriptModule(path) {
|
|
|
37190
37492
|
return false;
|
|
37191
37493
|
}
|
|
37192
37494
|
//#endregion
|
|
37495
|
+
//#region src/internal/transform-steps/propagate-sx-from-classname.ts
|
|
37496
|
+
/**
|
|
37497
|
+
* Adds an `sx` prop to typed wrapper components that pass their `className`
|
|
37498
|
+
* prop through to converted styled-component wrappers.
|
|
37499
|
+
*/
|
|
37500
|
+
function propagateSxFromClassNameStep(ctx) {
|
|
37501
|
+
const convertedWrappers = collectConvertedWrappers(ctx);
|
|
37502
|
+
if (convertedWrappers.names.size === 0) return CONTINUE;
|
|
37503
|
+
const stylexStyleImports = collectStylexStyleImports(ctx);
|
|
37504
|
+
const components = collectComponentCandidates(ctx);
|
|
37505
|
+
for (const component of components) {
|
|
37506
|
+
if (convertedWrappers.names.has(component.name)) continue;
|
|
37507
|
+
const propsBinding = getPropsBinding(component.fn);
|
|
37508
|
+
if (!propsBinding) continue;
|
|
37509
|
+
const propsType = getPropsType(ctx, propsBinding.typeName, component.fnPath, convertedWrappers.names, stylexStyleImports);
|
|
37510
|
+
if (!propsType) continue;
|
|
37511
|
+
const classNameMember = propsType.kind === "mutable" ? getPropsTypeMember(propsType.members, "className") : null;
|
|
37512
|
+
const sxMember = propsType.kind === "mutable" ? getPropsTypeMember(propsType.members, "sx") : null;
|
|
37513
|
+
const propsTypeAlreadyHasSx = propsType.kind === "mutable" ? Boolean(sxMember) : propsType.hasSx;
|
|
37514
|
+
const propsTypeHasCompatibleSx = propsType.kind === "mutable" ? !sxMember || isStylexSxMember(sxMember, stylexStyleImports) : propsType.hasSx;
|
|
37515
|
+
if (!(propsType.kind === "mutable" ? Boolean(classNameMember) : propsType.hasClassName) || !propsTypeHasCompatibleSx) continue;
|
|
37516
|
+
const sxExpression = buildSxExpression(ctx.j, propsBinding);
|
|
37517
|
+
if (!addSxToForwardedChildren({
|
|
37518
|
+
ctx,
|
|
37519
|
+
fn: component.fn,
|
|
37520
|
+
fnPath: component.fnPath,
|
|
37521
|
+
convertedWrapperBindings: convertedWrappers.bindings,
|
|
37522
|
+
propsBinding,
|
|
37523
|
+
sxExpression
|
|
37524
|
+
})) continue;
|
|
37525
|
+
if (propsType.kind === "mutable" && classNameMember && !propsTypeAlreadyHasSx) addSxMemberAfterClassName({
|
|
37526
|
+
j: ctx.j,
|
|
37527
|
+
propsType,
|
|
37528
|
+
classNameMember
|
|
37529
|
+
});
|
|
37530
|
+
addSxDestructure(ctx, propsBinding);
|
|
37531
|
+
ctx.markChanged();
|
|
37532
|
+
}
|
|
37533
|
+
return CONTINUE;
|
|
37534
|
+
}
|
|
37535
|
+
function collectConvertedWrappers(ctx) {
|
|
37536
|
+
const names = /* @__PURE__ */ new Set();
|
|
37537
|
+
const styledDecls = ctx.styledDecls;
|
|
37538
|
+
for (const decl of styledDecls ?? []) if (decl.needsWrapperComponent && !decl.isCssHelper && !decl.skipTransform) names.add(decl.localName);
|
|
37539
|
+
return {
|
|
37540
|
+
names,
|
|
37541
|
+
bindings: collectTopLevelBindingPaths(ctx, names)
|
|
37542
|
+
};
|
|
37543
|
+
}
|
|
37544
|
+
function collectStylexStyleImports(ctx) {
|
|
37545
|
+
const namespaceNames = /* @__PURE__ */ new Set();
|
|
37546
|
+
const styleTypeNames = /* @__PURE__ */ new Set();
|
|
37547
|
+
ctx.root.find(ctx.j.ImportDeclaration, { source: { value: "@stylexjs/stylex" } }).forEach((path) => {
|
|
37548
|
+
for (const specifier of path.node.specifiers ?? []) if (specifier.type === "ImportNamespaceSpecifier" && specifier.local?.name) namespaceNames.add(specifier.local.name);
|
|
37549
|
+
else if (specifier.type === "ImportSpecifier" && specifier.imported?.type === "Identifier" && specifier.imported.name === "StyleXStyles" && specifier.local?.name) styleTypeNames.add(specifier.local.name);
|
|
37550
|
+
});
|
|
37551
|
+
return {
|
|
37552
|
+
namespaceNames,
|
|
37553
|
+
styleTypeNames
|
|
37554
|
+
};
|
|
37555
|
+
}
|
|
37556
|
+
function collectTopLevelBindingPaths(ctx, names) {
|
|
37557
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
37558
|
+
const { root, j } = ctx;
|
|
37559
|
+
root.find(j.FunctionDeclaration).forEach((path) => {
|
|
37560
|
+
const name = path.node.id?.name;
|
|
37561
|
+
if (!name || !names.has(name) || !isTopLevelValueBindingPath(path)) return;
|
|
37562
|
+
bindings.set(name, path);
|
|
37563
|
+
});
|
|
37564
|
+
root.find(j.VariableDeclarator).forEach((path) => {
|
|
37565
|
+
const name = path.node.id?.type === "Identifier" ? path.node.id.name : void 0;
|
|
37566
|
+
if (!name || !names.has(name) || !isTopLevelValueBindingPath(path)) return;
|
|
37567
|
+
bindings.set(name, path);
|
|
37568
|
+
});
|
|
37569
|
+
return bindings;
|
|
37570
|
+
}
|
|
37571
|
+
function collectComponentCandidates(ctx) {
|
|
37572
|
+
const candidates = [];
|
|
37573
|
+
const { root, j } = ctx;
|
|
37574
|
+
root.find(j.FunctionDeclaration).forEach((path) => {
|
|
37575
|
+
const name = path.node.id?.name;
|
|
37576
|
+
if (name) candidates.push({
|
|
37577
|
+
name,
|
|
37578
|
+
fn: path.node,
|
|
37579
|
+
fnPath: path
|
|
37580
|
+
});
|
|
37581
|
+
});
|
|
37582
|
+
root.find(j.VariableDeclarator).forEach((path) => {
|
|
37583
|
+
const name = path.node.id?.type === "Identifier" ? path.node.id.name : void 0;
|
|
37584
|
+
const init = path.node.init;
|
|
37585
|
+
if (name && init && (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression")) candidates.push({
|
|
37586
|
+
name,
|
|
37587
|
+
fn: init,
|
|
37588
|
+
fnPath: path.get("init")
|
|
37589
|
+
});
|
|
37590
|
+
});
|
|
37591
|
+
return candidates;
|
|
37592
|
+
}
|
|
37593
|
+
function getPropsBinding(fn) {
|
|
37594
|
+
const firstParam = fn.params?.[0];
|
|
37595
|
+
if (!firstParam) return null;
|
|
37596
|
+
const typeName = getTypeReferenceName(firstParam.typeAnnotation?.typeAnnotation);
|
|
37597
|
+
if (!typeName) return null;
|
|
37598
|
+
if (firstParam.type === "ObjectPattern") {
|
|
37599
|
+
const classNameLocal = getObjectPatternValueName((firstParam.properties ?? []).find((prop) => getObjectPatternKeyName(prop) === "className"));
|
|
37600
|
+
if (!classNameLocal) return null;
|
|
37601
|
+
const hasSx = hasObjectPatternProp(firstParam, "sx");
|
|
37602
|
+
return {
|
|
37603
|
+
kind: "destructured",
|
|
37604
|
+
pattern: firstParam,
|
|
37605
|
+
typeName,
|
|
37606
|
+
classNameLocal,
|
|
37607
|
+
sxLocal: hasSx ? getExistingSxLocal(firstParam) : getAvailableSxLocalName(fn),
|
|
37608
|
+
restLocal: hasSx ? null : getObjectPatternRestLocal(firstParam)
|
|
37609
|
+
};
|
|
37610
|
+
}
|
|
37611
|
+
if (firstParam.type === "Identifier") return {
|
|
37612
|
+
kind: "propsObject",
|
|
37613
|
+
propsLocal: firstParam.name,
|
|
37614
|
+
typeName
|
|
37615
|
+
};
|
|
37616
|
+
return null;
|
|
37617
|
+
}
|
|
37618
|
+
function getTypeReferenceName(typeAnnotation) {
|
|
37619
|
+
if (typeAnnotation?.type === "TSTypeReference" && typeAnnotation.typeName?.type === "Identifier") return typeAnnotation.typeName.name;
|
|
37620
|
+
return null;
|
|
37621
|
+
}
|
|
37622
|
+
function getPropsType(ctx, typeName, referencePath, convertedWrapperNames, stylexStyleImports) {
|
|
37623
|
+
if (isTypeParameterInScope(referencePath, typeName)) return null;
|
|
37624
|
+
const { root, j } = ctx;
|
|
37625
|
+
let bestPath;
|
|
37626
|
+
let bestDepth = -1;
|
|
37627
|
+
const considerTypePath = (path) => {
|
|
37628
|
+
const containerPath = getLexicalContainerPath(path);
|
|
37629
|
+
if (!containerPath || !pathContains(containerPath, referencePath)) return;
|
|
37630
|
+
const depth = pathDepth(containerPath);
|
|
37631
|
+
if (depth > bestDepth) {
|
|
37632
|
+
bestPath = path;
|
|
37633
|
+
bestDepth = depth;
|
|
37634
|
+
}
|
|
37635
|
+
};
|
|
37636
|
+
root.find(j.TSInterfaceDeclaration, { id: {
|
|
37637
|
+
type: "Identifier",
|
|
37638
|
+
name: typeName
|
|
37639
|
+
} }).forEach((path) => {
|
|
37640
|
+
considerTypePath(path);
|
|
37641
|
+
});
|
|
37642
|
+
root.find(j.TSTypeAliasDeclaration, { id: {
|
|
37643
|
+
type: "Identifier",
|
|
37644
|
+
name: typeName
|
|
37645
|
+
} }).forEach((path) => {
|
|
37646
|
+
considerTypePath(path);
|
|
37647
|
+
});
|
|
37648
|
+
const matched = bestPath?.node;
|
|
37649
|
+
if (!matched) return null;
|
|
37650
|
+
if (matched.type === "TSInterfaceDeclaration") return {
|
|
37651
|
+
kind: "mutable",
|
|
37652
|
+
members: matched.body?.body ?? [],
|
|
37653
|
+
replaceMembers(members) {
|
|
37654
|
+
matched.body.body = members;
|
|
37655
|
+
}
|
|
37656
|
+
};
|
|
37657
|
+
if (matched.type !== "TSTypeAliasDeclaration") return null;
|
|
37658
|
+
if (matched.typeAnnotation?.type !== "TSTypeLiteral") return getReadonlyAliasPropsType(matched.typeAnnotation, convertedWrapperNames, stylexStyleImports);
|
|
37659
|
+
return {
|
|
37660
|
+
kind: "mutable",
|
|
37661
|
+
members: matched.typeAnnotation.members ?? [],
|
|
37662
|
+
replaceMembers(members) {
|
|
37663
|
+
matched.typeAnnotation.members = members;
|
|
37664
|
+
}
|
|
37665
|
+
};
|
|
37666
|
+
}
|
|
37667
|
+
function getReadonlyAliasPropsType(typeAnnotation, convertedWrapperNames, stylexStyleImports) {
|
|
37668
|
+
const surface = getPropsSurface(typeAnnotation, convertedWrapperNames, stylexStyleImports);
|
|
37669
|
+
if (!surface?.hasClassName || surface.sx !== "stylex") return null;
|
|
37670
|
+
return {
|
|
37671
|
+
kind: "readonly",
|
|
37672
|
+
hasClassName: true,
|
|
37673
|
+
hasSx: true
|
|
37674
|
+
};
|
|
37675
|
+
}
|
|
37676
|
+
function getPropsSurface(typeAnnotation, convertedWrapperNames, stylexStyleImports) {
|
|
37677
|
+
if (!typeAnnotation) return null;
|
|
37678
|
+
if (isComponentPropsOfConvertedWrapper(typeAnnotation, convertedWrapperNames)) return {
|
|
37679
|
+
hasClassName: true,
|
|
37680
|
+
sx: "stylex"
|
|
37681
|
+
};
|
|
37682
|
+
if (typeAnnotation.type === "TSIntersectionType") {
|
|
37683
|
+
let hasSurface = false;
|
|
37684
|
+
let hasClassName = false;
|
|
37685
|
+
let sx = "missing";
|
|
37686
|
+
for (const part of typeAnnotation.types ?? []) {
|
|
37687
|
+
const partSurface = getPropsSurface(part, convertedWrapperNames, stylexStyleImports);
|
|
37688
|
+
if (!partSurface) continue;
|
|
37689
|
+
hasSurface = true;
|
|
37690
|
+
hasClassName ||= partSurface.hasClassName;
|
|
37691
|
+
sx = mergeSxSurfaces(sx, partSurface.sx);
|
|
37692
|
+
}
|
|
37693
|
+
return hasSurface ? {
|
|
37694
|
+
hasClassName,
|
|
37695
|
+
sx
|
|
37696
|
+
} : null;
|
|
37697
|
+
}
|
|
37698
|
+
if (typeAnnotation.type === "TSParenthesizedType") return getPropsSurface(typeAnnotation.typeAnnotation, convertedWrapperNames, stylexStyleImports);
|
|
37699
|
+
if (typeAnnotation.type === "TSTypeLiteral") {
|
|
37700
|
+
const members = typeAnnotation.members ?? [];
|
|
37701
|
+
const sxMember = getPropsTypeMember(members, "sx");
|
|
37702
|
+
return {
|
|
37703
|
+
hasClassName: propsTypeHasProp(members, "className"),
|
|
37704
|
+
sx: sxMember ? getSxMemberSurface(sxMember, stylexStyleImports) : "missing"
|
|
37705
|
+
};
|
|
37706
|
+
}
|
|
37707
|
+
if (isOmitTypeReference(typeAnnotation)) {
|
|
37708
|
+
const params = getTypeReferenceParams(typeAnnotation);
|
|
37709
|
+
const sourceSurface = getPropsSurface(params[0], convertedWrapperNames, stylexStyleImports);
|
|
37710
|
+
const omittedProps = getStringLiteralTypeNames(params[1]);
|
|
37711
|
+
if (!sourceSurface || !omittedProps) return null;
|
|
37712
|
+
return {
|
|
37713
|
+
hasClassName: sourceSurface.hasClassName && !omittedProps.has("className"),
|
|
37714
|
+
sx: omittedProps.has("sx") ? "missing" : sourceSurface.sx
|
|
37715
|
+
};
|
|
37716
|
+
}
|
|
37717
|
+
if (isPickTypeReference(typeAnnotation)) {
|
|
37718
|
+
const params = getTypeReferenceParams(typeAnnotation);
|
|
37719
|
+
const sourceSurface = getPropsSurface(params[0], convertedWrapperNames, stylexStyleImports);
|
|
37720
|
+
const pickedProps = getStringLiteralTypeNames(params[1]);
|
|
37721
|
+
if (!sourceSurface || !pickedProps) return null;
|
|
37722
|
+
return {
|
|
37723
|
+
hasClassName: sourceSurface.hasClassName && pickedProps.has("className"),
|
|
37724
|
+
sx: pickedProps.has("sx") ? sourceSurface.sx : "missing"
|
|
37725
|
+
};
|
|
37726
|
+
}
|
|
37727
|
+
return null;
|
|
37728
|
+
}
|
|
37729
|
+
function mergeSxSurfaces(left, right) {
|
|
37730
|
+
if (left === "nonStylex" || right === "nonStylex") return "nonStylex";
|
|
37731
|
+
if (left === "stylex" || right === "stylex") return "stylex";
|
|
37732
|
+
return "missing";
|
|
37733
|
+
}
|
|
37734
|
+
function isComponentPropsOfConvertedWrapper(typeAnnotation, convertedWrapperNames) {
|
|
37735
|
+
if (typeAnnotation?.type !== "TSTypeReference" || !isComponentPropsTypeName(typeAnnotation)) return false;
|
|
37736
|
+
return getTypeReferenceParams(typeAnnotation).some((param) => {
|
|
37737
|
+
if (param?.type !== "TSTypeQuery") return false;
|
|
37738
|
+
const componentName = getTypeQueryExpressionName(param.exprName);
|
|
37739
|
+
return componentName ? convertedWrapperNames.has(componentName) : false;
|
|
37740
|
+
});
|
|
37741
|
+
}
|
|
37742
|
+
function isComponentPropsTypeName(typeReference) {
|
|
37743
|
+
const typeName = typeReference.typeName;
|
|
37744
|
+
if (typeName?.type === "Identifier") return isReactComponentPropsUtilityName(typeName.name);
|
|
37745
|
+
return typeName?.type === "TSQualifiedName" && typeName.left?.type === "Identifier" && typeName.left.name === "React" && typeName.right?.type === "Identifier" && isReactComponentPropsUtilityName(typeName.right.name);
|
|
37746
|
+
}
|
|
37747
|
+
function isReactComponentPropsUtilityName(name) {
|
|
37748
|
+
return name === "ComponentProps" || name === "ComponentPropsWithRef" || name === "ComponentPropsWithoutRef";
|
|
37749
|
+
}
|
|
37750
|
+
function isOmitTypeReference(typeAnnotation) {
|
|
37751
|
+
return isUtilityTypeReference(typeAnnotation, "Omit");
|
|
37752
|
+
}
|
|
37753
|
+
function isPickTypeReference(typeAnnotation) {
|
|
37754
|
+
return isUtilityTypeReference(typeAnnotation, "Pick");
|
|
37755
|
+
}
|
|
37756
|
+
function isUtilityTypeReference(typeAnnotation, name) {
|
|
37757
|
+
return typeAnnotation?.type === "TSTypeReference" && typeAnnotation.typeName?.type === "Identifier" && typeAnnotation.typeName.name === name;
|
|
37758
|
+
}
|
|
37759
|
+
function getTypeReferenceParams(typeReference) {
|
|
37760
|
+
return typeReference.typeParameters?.params ?? typeReference.typeArguments?.params ?? [];
|
|
37761
|
+
}
|
|
37762
|
+
function getTypeQueryExpressionName(exprName) {
|
|
37763
|
+
if (exprName?.type === "Identifier") return exprName.name;
|
|
37764
|
+
return null;
|
|
37765
|
+
}
|
|
37766
|
+
function getStringLiteralTypeNames(typeAnnotation) {
|
|
37767
|
+
if (!typeAnnotation) return null;
|
|
37768
|
+
if (typeAnnotation.type === "TSLiteralType") {
|
|
37769
|
+
const value = typeAnnotation.literal?.value;
|
|
37770
|
+
return typeof value === "string" ? new Set([value]) : null;
|
|
37771
|
+
}
|
|
37772
|
+
if (typeAnnotation.type === "TSUnionType") {
|
|
37773
|
+
const names = /* @__PURE__ */ new Set();
|
|
37774
|
+
for (const part of typeAnnotation.types ?? []) {
|
|
37775
|
+
const partNames = getStringLiteralTypeNames(part);
|
|
37776
|
+
if (!partNames) return null;
|
|
37777
|
+
for (const name of partNames) names.add(name);
|
|
37778
|
+
}
|
|
37779
|
+
return names;
|
|
37780
|
+
}
|
|
37781
|
+
return null;
|
|
37782
|
+
}
|
|
37783
|
+
function getLexicalContainerPath(path) {
|
|
37784
|
+
let current = path.parentPath;
|
|
37785
|
+
while (current) {
|
|
37786
|
+
if (current.node?.type === "Program" || current.node?.type === "BlockStatement" || current.node?.type === "TSModuleBlock") return current;
|
|
37787
|
+
current = current.parentPath;
|
|
37788
|
+
}
|
|
37789
|
+
return null;
|
|
37790
|
+
}
|
|
37791
|
+
function isTypeParameterInScope(referencePath, typeName) {
|
|
37792
|
+
let current = referencePath;
|
|
37793
|
+
while (current) {
|
|
37794
|
+
if (hasTypeParameterNamed(current.node, typeName)) return true;
|
|
37795
|
+
current = current.parentPath;
|
|
37796
|
+
}
|
|
37797
|
+
return false;
|
|
37798
|
+
}
|
|
37799
|
+
function hasTypeParameterNamed(node, typeName) {
|
|
37800
|
+
if (!node || typeof node !== "object") return false;
|
|
37801
|
+
return (node.typeParameters?.params ?? []).some((param) => getTypeParameterName(param) === typeName);
|
|
37802
|
+
}
|
|
37803
|
+
function getTypeParameterName(param) {
|
|
37804
|
+
if (!param || typeof param !== "object") return null;
|
|
37805
|
+
const node = param;
|
|
37806
|
+
if (typeof node.name === "string") return node.name;
|
|
37807
|
+
return typeof node.id?.name === "string" ? node.id.name : null;
|
|
37808
|
+
}
|
|
37809
|
+
function pathContains(containerPath, descendantPath) {
|
|
37810
|
+
let current = descendantPath;
|
|
37811
|
+
while (current) {
|
|
37812
|
+
if (current.node === containerPath.node) return true;
|
|
37813
|
+
current = current.parentPath;
|
|
37814
|
+
}
|
|
37815
|
+
return false;
|
|
37816
|
+
}
|
|
37817
|
+
function pathDepth(path) {
|
|
37818
|
+
let depth = 0;
|
|
37819
|
+
let current = path.parentPath;
|
|
37820
|
+
while (current) {
|
|
37821
|
+
depth++;
|
|
37822
|
+
current = current.parentPath;
|
|
37823
|
+
}
|
|
37824
|
+
return depth;
|
|
37825
|
+
}
|
|
37826
|
+
function propsTypeHasProp(members, propName) {
|
|
37827
|
+
return members.some((member) => getMemberName(member) === propName);
|
|
37828
|
+
}
|
|
37829
|
+
function getPropsTypeMember(members, propName) {
|
|
37830
|
+
return members.find((member) => getMemberName(member) === propName) ?? null;
|
|
37831
|
+
}
|
|
37832
|
+
function getMemberName(member) {
|
|
37833
|
+
const key = member?.key;
|
|
37834
|
+
if (!key) return null;
|
|
37835
|
+
if (key.type === "Identifier") return key.name;
|
|
37836
|
+
if (key.type === "StringLiteral" || key.type === "Literal") return typeof key.value === "string" ? key.value : null;
|
|
37837
|
+
return null;
|
|
37838
|
+
}
|
|
37839
|
+
function getSxMemberSurface(member, stylexStyleImports) {
|
|
37840
|
+
return isStylexSxMember(member, stylexStyleImports) ? "stylex" : "nonStylex";
|
|
37841
|
+
}
|
|
37842
|
+
function isStylexSxMember(member, stylexStyleImports) {
|
|
37843
|
+
return isStylexStylesType(member?.typeAnnotation?.typeAnnotation, stylexStyleImports);
|
|
37844
|
+
}
|
|
37845
|
+
function isStylexStylesType(typeAnnotation, stylexStyleImports) {
|
|
37846
|
+
if (!typeAnnotation) return false;
|
|
37847
|
+
if (typeAnnotation.type === "TSParenthesizedType") return isStylexStylesType(typeAnnotation.typeAnnotation, stylexStyleImports);
|
|
37848
|
+
if (typeAnnotation.type !== "TSTypeReference") return false;
|
|
37849
|
+
const typeName = typeAnnotation.typeName;
|
|
37850
|
+
if (typeName?.type === "Identifier") return stylexStyleImports.styleTypeNames.has(typeName.name);
|
|
37851
|
+
return typeName?.type === "TSQualifiedName" && typeName.left?.type === "Identifier" && stylexStyleImports.namespaceNames.has(typeName.left.name) && typeName.right?.type === "Identifier" && typeName.right.name === "StyleXStyles";
|
|
37852
|
+
}
|
|
37853
|
+
function addSxMemberAfterClassName(args) {
|
|
37854
|
+
const { j, propsType, classNameMember } = args;
|
|
37855
|
+
const sxMember = createSxMember(j, classNameMember.comments);
|
|
37856
|
+
const nextMembers = [];
|
|
37857
|
+
for (const member of propsType.members) {
|
|
37858
|
+
nextMembers.push(member);
|
|
37859
|
+
if (member === classNameMember) nextMembers.push(sxMember);
|
|
37860
|
+
}
|
|
37861
|
+
propsType.replaceMembers(nextMembers);
|
|
37862
|
+
}
|
|
37863
|
+
function createSxMember(j, sourceComments) {
|
|
37864
|
+
const parsed = j("type _Props = { sx?: stylex.StyleXStyles }").find(j.TSPropertySignature).nodes()[0];
|
|
37865
|
+
parsed.comments = cloneSxComments(sourceComments);
|
|
37866
|
+
return parsed;
|
|
37867
|
+
}
|
|
37868
|
+
function cloneSxComments(sourceComments) {
|
|
37869
|
+
if (!sourceComments || sourceComments.length === 0) return;
|
|
37870
|
+
return sourceComments.map((comment) => ({
|
|
37871
|
+
...comment,
|
|
37872
|
+
value: rewriteClassNameComment(comment.value)
|
|
37873
|
+
}));
|
|
37874
|
+
}
|
|
37875
|
+
function rewriteClassNameComment(value) {
|
|
37876
|
+
if (typeof value !== "string") return value;
|
|
37877
|
+
return value.replace(/\bAdditional\s+className\s+for\b/gi, "StyleX styles applied to").replace(/\bAdditional\s+class\s+name\s+for\b/gi, "StyleX styles applied to").replace(/\bclassName\b/gi, "StyleX styles").replace(/\bclass name\b/gi, "StyleX styles");
|
|
37878
|
+
}
|
|
37879
|
+
function buildSxExpression(j, propsBinding) {
|
|
37880
|
+
if (propsBinding.kind === "destructured") {
|
|
37881
|
+
if (propsBinding.restLocal) return j.memberExpression(j.identifier(propsBinding.restLocal), j.identifier("sx"));
|
|
37882
|
+
return j.identifier(propsBinding.sxLocal);
|
|
37883
|
+
}
|
|
37884
|
+
return j.memberExpression(j.identifier(propsBinding.propsLocal), j.identifier("sx"));
|
|
37885
|
+
}
|
|
37886
|
+
function addSxToForwardedChildren(args) {
|
|
37887
|
+
const { ctx, fn, fnPath, convertedWrapperBindings, propsBinding, sxExpression } = args;
|
|
37888
|
+
if (!fn.body) return false;
|
|
37889
|
+
const propsBindingScope = findPropsBindingScope(fnPath, propsBinding);
|
|
37890
|
+
if (!propsBindingScope) return false;
|
|
37891
|
+
let changed = false;
|
|
37892
|
+
ctx.j(fnPath).find(ctx.j.JSXOpeningElement).forEach((path) => {
|
|
37893
|
+
const opening = path.node;
|
|
37894
|
+
const childName = opening.name?.type === "JSXIdentifier" ? opening.name.name : void 0;
|
|
37895
|
+
const convertedBindingScope = childName ? convertedWrapperBindings.get(childName) : void 0;
|
|
37896
|
+
if (!childName || !convertedBindingScope || !isConvertedChildBinding(ctx, path, childName, convertedBindingScope) || hasJsxAttr(opening, "sx")) return;
|
|
37897
|
+
const classNameAttr = getJsxAttr(opening, "className");
|
|
37898
|
+
if (!classNameAttr || !isForwardedClassNameAttr(classNameAttr, propsBinding) || !isInPropsBindingScope(path, propsBinding, propsBindingScope)) return;
|
|
37899
|
+
insertSxAttribute(ctx, opening, sxExpression);
|
|
37900
|
+
changed = true;
|
|
37901
|
+
});
|
|
37902
|
+
return changed;
|
|
37903
|
+
}
|
|
37904
|
+
function insertSxAttribute(ctx, opening, sxExpression) {
|
|
37905
|
+
const sxAttr = ctx.j.jsxAttribute(ctx.j.jsxIdentifier("sx"), ctx.j.jsxExpressionContainer(cloneExpression(sxExpression)));
|
|
37906
|
+
const attributes = opening.attributes ?? [];
|
|
37907
|
+
const firstSpreadIndex = attributes.findIndex((attr) => attr?.type === "JSXSpreadAttribute");
|
|
37908
|
+
const insertionIndex = firstSpreadIndex === -1 ? attributes.length : firstSpreadIndex;
|
|
37909
|
+
opening.attributes = [
|
|
37910
|
+
...attributes.slice(0, insertionIndex),
|
|
37911
|
+
sxAttr,
|
|
37912
|
+
...attributes.slice(insertionIndex)
|
|
37913
|
+
];
|
|
37914
|
+
}
|
|
37915
|
+
function isConvertedChildBinding(ctx, path, childName, convertedBindingPath) {
|
|
37916
|
+
return findNearestValueBindingPath(ctx, path, childName)?.node === convertedBindingPath.node;
|
|
37917
|
+
}
|
|
37918
|
+
function findNearestValueBindingPath(ctx, referencePath, name) {
|
|
37919
|
+
let bestPath = null;
|
|
37920
|
+
let bestDepth = -1;
|
|
37921
|
+
const considerBindingPath = (bindingPath, containerPath) => {
|
|
37922
|
+
if (!containerPath || !pathContains(containerPath, referencePath)) return;
|
|
37923
|
+
const depth = pathDepth(containerPath);
|
|
37924
|
+
if (depth > bestDepth) {
|
|
37925
|
+
bestPath = bindingPath;
|
|
37926
|
+
bestDepth = depth;
|
|
37927
|
+
}
|
|
37928
|
+
};
|
|
37929
|
+
ctx.root.find(ctx.j.FunctionDeclaration).forEach((path) => {
|
|
37930
|
+
if (path.node.id?.name === name) considerBindingPath(path, getLexicalContainerPath(path));
|
|
37931
|
+
if (functionHasParamBinding(path.node, name)) considerBindingPath(path, path);
|
|
37932
|
+
});
|
|
37933
|
+
ctx.root.find(ctx.j.FunctionExpression).forEach((path) => {
|
|
37934
|
+
if (functionHasParamBinding(path.node, name)) considerBindingPath(path, path);
|
|
37935
|
+
});
|
|
37936
|
+
ctx.root.find(ctx.j.ArrowFunctionExpression).forEach((path) => {
|
|
37937
|
+
if (functionHasParamBinding(path.node, name)) considerBindingPath(path, path);
|
|
37938
|
+
});
|
|
37939
|
+
ctx.root.find(ctx.j.ClassDeclaration).forEach((path) => {
|
|
37940
|
+
if (path.node.id?.name === name) considerBindingPath(path, getLexicalContainerPath(path));
|
|
37941
|
+
});
|
|
37942
|
+
ctx.root.find(ctx.j.ClassExpression).forEach((path) => {
|
|
37943
|
+
if (path.node.id?.name === name) considerBindingPath(path, path);
|
|
37944
|
+
});
|
|
37945
|
+
ctx.root.find(ctx.j.VariableDeclarator).forEach((path) => {
|
|
37946
|
+
if (patternHasBindingName(path.node.id, name)) considerBindingPath(path, getLexicalContainerPath(path));
|
|
37947
|
+
});
|
|
37948
|
+
return bestPath;
|
|
37949
|
+
}
|
|
37950
|
+
function isTopLevelValueBindingPath(path) {
|
|
37951
|
+
return getLexicalContainerPath(path)?.node?.type === "Program";
|
|
37952
|
+
}
|
|
37953
|
+
function functionHasParamBinding(fn, name) {
|
|
37954
|
+
return (fn.params ?? []).some((param) => patternHasBindingName(param, name));
|
|
37955
|
+
}
|
|
37956
|
+
function patternHasBindingName(node, name) {
|
|
37957
|
+
const names = /* @__PURE__ */ new Set();
|
|
37958
|
+
collectPatternBindingNames(node, names);
|
|
37959
|
+
return names.has(name);
|
|
37960
|
+
}
|
|
37961
|
+
function findPropsBindingScope(fnPath, propsBinding) {
|
|
37962
|
+
return findBindingScope(fnPath, propsBinding.kind === "destructured" ? propsBinding.classNameLocal : propsBinding.propsLocal);
|
|
37963
|
+
}
|
|
37964
|
+
function isInPropsBindingScope(path, propsBinding, bindingScope) {
|
|
37965
|
+
return isInBindingScope(path, propsBinding.kind === "destructured" ? propsBinding.classNameLocal : propsBinding.propsLocal, bindingScope);
|
|
37966
|
+
}
|
|
37967
|
+
function findBindingScope(path, bindingName) {
|
|
37968
|
+
let scope;
|
|
37969
|
+
try {
|
|
37970
|
+
scope = path.scope?.lookup?.(bindingName);
|
|
37971
|
+
} catch {
|
|
37972
|
+
return null;
|
|
37973
|
+
}
|
|
37974
|
+
return scope && typeof scope === "object" ? scope : null;
|
|
37975
|
+
}
|
|
37976
|
+
function isInBindingScope(path, bindingName, bindingScope) {
|
|
37977
|
+
return findBindingScope(path, bindingName) === bindingScope;
|
|
37978
|
+
}
|
|
37979
|
+
function addSxDestructure(ctx, propsBinding) {
|
|
37980
|
+
if (propsBinding.kind !== "destructured" || propsBinding.restLocal || hasObjectPatternProp(propsBinding.pattern, "sx")) return;
|
|
37981
|
+
const properties = [...propsBinding.pattern.properties ?? []];
|
|
37982
|
+
const restIndex = properties.findIndex(isObjectPatternRestElement);
|
|
37983
|
+
properties.splice(restIndex === -1 ? properties.length : restIndex, 0, ctx.patternProp("sx", ctx.j.identifier(propsBinding.sxLocal)));
|
|
37984
|
+
propsBinding.pattern.properties = properties;
|
|
37985
|
+
}
|
|
37986
|
+
function isObjectPatternRestElement(prop) {
|
|
37987
|
+
return prop?.type === "RestElement" || prop?.type === "RestProperty";
|
|
37988
|
+
}
|
|
37989
|
+
function getObjectPatternRestLocal(pattern) {
|
|
37990
|
+
const rest = (pattern.properties ?? []).find(isObjectPatternRestElement);
|
|
37991
|
+
return rest?.argument?.type === "Identifier" ? rest.argument.name : null;
|
|
37992
|
+
}
|
|
37993
|
+
function getJsxAttr(opening, attrName) {
|
|
37994
|
+
return (opening.attributes ?? []).find((attr) => attr?.type === "JSXAttribute" && attr.name?.name === attrName) ?? null;
|
|
37995
|
+
}
|
|
37996
|
+
function hasJsxAttr(opening, attrName) {
|
|
37997
|
+
return Boolean(getJsxAttr(opening, attrName));
|
|
37998
|
+
}
|
|
37999
|
+
function isForwardedClassNameAttr(attr, propsBinding) {
|
|
38000
|
+
const expr = attr.value?.type === "JSXExpressionContainer" ? attr.value.expression : null;
|
|
38001
|
+
if (!expr) return false;
|
|
38002
|
+
if (propsBinding.kind === "destructured") return expr.type === "Identifier" && expr.name === propsBinding.classNameLocal;
|
|
38003
|
+
return expr.type === "MemberExpression" && expr.object?.type === "Identifier" && expr.object.name === propsBinding.propsLocal && expr.property?.type === "Identifier" && expr.property.name === "className";
|
|
38004
|
+
}
|
|
38005
|
+
function getObjectPatternKeyName(prop) {
|
|
38006
|
+
if (prop?.type !== "Property" && prop?.type !== "ObjectProperty") return null;
|
|
38007
|
+
const key = prop.key;
|
|
38008
|
+
if (key?.type === "Identifier") return key.name;
|
|
38009
|
+
if (key?.type === "StringLiteral" || key?.type === "Literal") return typeof key.value === "string" ? key.value : null;
|
|
38010
|
+
return null;
|
|
38011
|
+
}
|
|
38012
|
+
function getObjectPatternValueName(prop) {
|
|
38013
|
+
if (!prop) return null;
|
|
38014
|
+
const value = prop.value;
|
|
38015
|
+
if (value?.type === "Identifier") return value.name;
|
|
38016
|
+
if (value?.type === "AssignmentPattern" && value.left?.type === "Identifier") return value.left.name;
|
|
38017
|
+
return null;
|
|
38018
|
+
}
|
|
38019
|
+
function hasObjectPatternProp(pattern, propName) {
|
|
38020
|
+
return (pattern.properties ?? []).some((prop) => getObjectPatternKeyName(prop) === propName);
|
|
38021
|
+
}
|
|
38022
|
+
function getExistingSxLocal(pattern) {
|
|
38023
|
+
return getObjectPatternValueName((pattern.properties ?? []).find((prop) => getObjectPatternKeyName(prop) === "sx")) ?? "sx";
|
|
38024
|
+
}
|
|
38025
|
+
function getAvailableSxLocalName(fn) {
|
|
38026
|
+
const usedNames = /* @__PURE__ */ new Set();
|
|
38027
|
+
for (const param of fn.params ?? []) {
|
|
38028
|
+
collectPatternBindingNames(param, usedNames);
|
|
38029
|
+
collectIdentifierNames(param, usedNames);
|
|
38030
|
+
}
|
|
38031
|
+
collectBindingNames(fn.body, usedNames);
|
|
38032
|
+
collectIdentifierNames(fn.body, usedNames);
|
|
38033
|
+
if (!usedNames.has("sx")) return "sx";
|
|
38034
|
+
if (!usedNames.has("sxProp")) return "sxProp";
|
|
38035
|
+
let suffix = 2;
|
|
38036
|
+
while (usedNames.has(`sxProp${suffix}`)) suffix++;
|
|
38037
|
+
return `sxProp${suffix}`;
|
|
38038
|
+
}
|
|
38039
|
+
function collectIdentifierNames(node, names, visited = /* @__PURE__ */ new WeakSet()) {
|
|
38040
|
+
if (!node || typeof node !== "object") return;
|
|
38041
|
+
if (visited.has(node)) return;
|
|
38042
|
+
visited.add(node);
|
|
38043
|
+
const astNode = node;
|
|
38044
|
+
if (astNode.type === "Identifier" && typeof astNode.name === "string") {
|
|
38045
|
+
names.add(astNode.name);
|
|
38046
|
+
return;
|
|
38047
|
+
}
|
|
38048
|
+
for (const value of Object.values(astNode)) {
|
|
38049
|
+
if (!value) continue;
|
|
38050
|
+
if (Array.isArray(value)) for (const item of value) collectIdentifierNames(item, names, visited);
|
|
38051
|
+
else if (typeof value === "object") collectIdentifierNames(value, names, visited);
|
|
38052
|
+
}
|
|
38053
|
+
}
|
|
38054
|
+
function collectBindingNames(node, names, visited = /* @__PURE__ */ new WeakSet()) {
|
|
38055
|
+
if (!node || typeof node !== "object") return;
|
|
38056
|
+
if (visited.has(node)) return;
|
|
38057
|
+
visited.add(node);
|
|
38058
|
+
const astNode = node;
|
|
38059
|
+
if (astNode.type === "Identifier" && typeof astNode.name === "string") return;
|
|
38060
|
+
if (typeof astNode.id === "object" && astNode.id !== null) collectPatternBindingNames(astNode.id, names);
|
|
38061
|
+
if (Array.isArray(astNode.params)) for (const param of astNode.params) collectPatternBindingNames(param, names);
|
|
38062
|
+
for (const value of Object.values(astNode)) {
|
|
38063
|
+
if (!value || value === astNode.id || value === astNode.params) continue;
|
|
38064
|
+
if (Array.isArray(value)) for (const item of value) collectBindingNames(item, names, visited);
|
|
38065
|
+
else if (typeof value === "object") collectBindingNames(value, names, visited);
|
|
38066
|
+
}
|
|
38067
|
+
}
|
|
38068
|
+
function collectPatternBindingNames(node, names) {
|
|
38069
|
+
if (!node || typeof node !== "object") return;
|
|
38070
|
+
const astNode = node;
|
|
38071
|
+
if (astNode.type === "Identifier" && typeof astNode.name === "string") {
|
|
38072
|
+
names.add(astNode.name);
|
|
38073
|
+
return;
|
|
38074
|
+
}
|
|
38075
|
+
if (astNode.type === "RestElement") {
|
|
38076
|
+
collectPatternBindingNames(astNode.argument, names);
|
|
38077
|
+
return;
|
|
38078
|
+
}
|
|
38079
|
+
if (astNode.type === "AssignmentPattern") {
|
|
38080
|
+
collectPatternBindingNames(astNode.left, names);
|
|
38081
|
+
return;
|
|
38082
|
+
}
|
|
38083
|
+
if (astNode.type === "ArrayPattern") {
|
|
38084
|
+
for (const element of astNode.elements ?? []) collectPatternBindingNames(element, names);
|
|
38085
|
+
return;
|
|
38086
|
+
}
|
|
38087
|
+
if (astNode.type === "ObjectPattern") for (const property of astNode.properties ?? []) {
|
|
38088
|
+
if (!property || typeof property !== "object") continue;
|
|
38089
|
+
const prop = property;
|
|
38090
|
+
if (prop.type === "RestElement") collectPatternBindingNames(prop.argument, names);
|
|
38091
|
+
else collectPatternBindingNames(prop.value, names);
|
|
38092
|
+
}
|
|
38093
|
+
}
|
|
38094
|
+
function cloneExpression(expression) {
|
|
38095
|
+
return JSON.parse(JSON.stringify(expression));
|
|
38096
|
+
}
|
|
38097
|
+
//#endregion
|
|
37193
38098
|
//#region src/internal/transform-steps/reinsert-static-props.ts
|
|
37194
38099
|
/**
|
|
37195
38100
|
* Step: reinsert static property assignments after wrapper emission.
|
|
@@ -38301,6 +39206,7 @@ function transformWithWarnings(file, api, options) {
|
|
|
38301
39206
|
rewriteJsxStep,
|
|
38302
39207
|
emitWrappersStep,
|
|
38303
39208
|
emitBridgeExportsStep,
|
|
39209
|
+
propagateSxFromClassNameStep,
|
|
38304
39210
|
upgradePolymorphicAsPropTypesStep,
|
|
38305
39211
|
ensureMergerImportStep,
|
|
38306
39212
|
reinsertStaticPropsStep,
|