Skip to content

Commit 1bc4f73

Browse files
skovhusclaude
andcommitted
fix: only use named params for merged-base style functions
Derived style function keys like `boxBoxShadow` already encode the parameter name, so positional args are clearer: `styles.boxBoxShadow(shadow)`. Named params are now only applied when the function key IS the base style key (merged base), where the key alone doesn't communicate what the parameter means: `styles.box({ size: 16 })`. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3a99b07 commit 1bc4f73

File tree

53 files changed

+248
-688
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+248
-688
lines changed

src/internal/lower-rules/finalize-decl.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,10 @@ export function finalizeDeclProcessing(ctx: DeclProcessingState): void {
519519
});
520520

521521
// Convert single-positional-param style functions to use a named `props`
522-
// object parameter: `(color: string) => ({color})` → `(props: {color: string}) => ({color: props.color})`.
523-
convertStyleFnsToPropsPattern(state.j, styleFnDecls, styleFnFromProps);
522+
// object parameter, but only when the function key IS the base style key
523+
// (i.e., merged base). Separate derived keys like `boxBoxShadow` already
524+
// encode the parameter name, so positional args are clearer there.
525+
convertStyleFnsToPropsPattern(state.j, styleFnDecls, styleFnFromProps, decl.styleKey);
524526

525527
insertStyleFnDeclsAfterComponent(resolvedStyleObjects, styleFnDecls, {
526528
styleKey: decl.styleKey,
@@ -780,6 +782,7 @@ function convertStyleFnsToPropsPattern(
780782
j: Parameters<typeof literalToAst>[0],
781783
styleFnDecls: Map<string, unknown>,
782784
styleFnFromProps: NonNullable<StyledDecl["styleFnFromProps"]>,
785+
baseStyleKey: string,
783786
): void {
784787
// Build a set of fnKeys that have matching styleFnFromProps entries.
785788
// Only convert functions whose call sites are managed via styleFnFromProps;
@@ -788,6 +791,12 @@ function convertStyleFnsToPropsPattern(
788791
const managedFnKeys = new Set(styleFnFromProps.map((p) => p.fnKey));
789792

790793
for (const [fnKey, fnAst] of styleFnDecls.entries()) {
794+
// Only convert merged-base functions (fnKey === baseStyleKey). Separate
795+
// derived keys like `boxBoxShadow` already encode the parameter name,
796+
// so positional args are clearer: `styles.boxBoxShadow(shadow)`.
797+
if (fnKey !== baseStyleKey) {
798+
continue;
799+
}
791800
if (!managedFnKeys.has(fnKey)) {
792801
continue;
793802
}

test-cases/attrs-tabIndex.output.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ export function ScrollableFlex(props: Props) {
2020
[
2121
styles.scrollableFlex,
2222
applyBackground ? styles.scrollableFlexApplyBackground : undefined,
23-
props.gutter != null &&
24-
styles.scrollableFlexScrollbarGutter({
25-
scrollbarGutter: props.gutter,
26-
}),
23+
props.gutter != null && styles.scrollableFlexScrollbarGutter(props.gutter),
2724
sx,
2825
],
2926
className,
@@ -48,10 +45,7 @@ export function ScrollableDiv(
4845
[
4946
styles.scrollableDiv,
5047
applyBackground ? styles.scrollableDivApplyBackground : undefined,
51-
gutter != null &&
52-
styles.scrollableDivScrollbarGutter({
53-
scrollbarGutter: gutter,
54-
}),
48+
gutter != null && styles.scrollableDivScrollbarGutter(gutter),
5549
sx,
5650
],
5751
className,
@@ -93,8 +87,8 @@ const styles = stylex.create({
9387
scrollableFlexApplyBackground: {
9488
backgroundColor: $colors.bgBase,
9589
},
96-
scrollableFlexScrollbarGutter: (props: { scrollbarGutter: string }) => ({
97-
scrollbarGutter: props.scrollbarGutter,
90+
scrollableFlexScrollbarGutter: (scrollbarGutter: string) => ({
91+
scrollbarGutter,
9892
}),
9993
scrollableDiv: {
10094
overflowY: "auto",
@@ -109,7 +103,7 @@ const styles = stylex.create({
109103
scrollableDivApplyBackground: {
110104
backgroundColor: $colors.bgBase,
111105
},
112-
scrollableDivScrollbarGutter: (props: { scrollbarGutter: string }) => ({
113-
scrollbarGutter: props.scrollbarGutter,
106+
scrollableDivScrollbarGutter: (scrollbarGutter: string) => ({
107+
scrollbarGutter,
114108
}),
115109
});

test-cases/attrs.output.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@ function Input(props: InputProps) {
2323
size={small ? 5 : undefined}
2424
type="text"
2525
{...rest}
26-
sx={[
27-
styles.input,
28-
padding != null &&
29-
styles.inputPadding({
30-
padding: padding,
31-
}),
32-
]}
26+
sx={[styles.input, padding != null && styles.inputPadding(padding)]}
3327
/>
3428
);
3529
}
@@ -176,11 +170,7 @@ function DynamicHeightBox(props: DynamicHeightBoxProps) {
176170
<div
177171
sx={[
178172
styles.dynamicHeightBox,
179-
height
180-
? styles.dynamicHeightBoxHeight({
181-
height: `${height}px`,
182-
})
183-
: undefined,
173+
height ? styles.dynamicHeightBoxHeight(`${height}px`) : undefined,
184174
]}
185175
>
186176
{children}
@@ -220,8 +210,8 @@ const styles = stylex.create({
220210
color: "#bf4f74",
221211
},
222212
},
223-
inputPadding: (props: { padding: string }) => ({
224-
padding: props.padding,
213+
inputPadding: (padding: string) => ({
214+
padding,
225215
}),
226216
textInput: {
227217
height: 32,
@@ -265,7 +255,7 @@ const styles = stylex.create({
265255
display: "flex",
266256
alignItems: "center",
267257
},
268-
dynamicHeightBoxHeight: (props: { height: string }) => ({
269-
height: props.height,
258+
dynamicHeightBoxHeight: (height: string) => ({
259+
height,
270260
}),
271261
});

test-cases/basic-functionCssTemplate.output.tsx

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,7 @@ function ColoredBox(props: ColoredBoxProps) {
2626
const { children, color } = props;
2727

2828
return (
29-
<div
30-
sx={[
31-
styles.coloredBox,
32-
color != null &&
33-
styles.coloredBoxBackgroundColor({
34-
backgroundColor: color,
35-
}),
36-
]}
37-
>
29+
<div sx={[styles.coloredBox, color != null && styles.coloredBoxBackgroundColor(color)]}>
3830
{children}
3931
</div>
4032
);
@@ -49,36 +41,11 @@ export const App = () => (
4941
<FlexContainer align="right">
5042
<ColoredBox>Right aligned</ColoredBox>
5143
</FlexContainer>
52-
<div
53-
sx={[
54-
styles.borderBox,
55-
styles.borderBoxBorderColor({
56-
borderColor: "red",
57-
}),
58-
]}
59-
>
60-
Red border
61-
</div>
62-
<div
63-
sx={[
64-
styles.shadowBox,
65-
styles.shadowBoxBoxShadow({
66-
boxShadow: "0 2px 4px rgba(0,0,0,0.2)",
67-
}),
68-
]}
69-
>
44+
<div sx={[styles.borderBox, styles.borderBoxBorderColor("red")]}>Red border</div>
45+
<div sx={[styles.shadowBox, styles.shadowBoxBoxShadow("0 2px 4px rgba(0,0,0,0.2)")]}>
7046
With shadow
7147
</div>
72-
<div
73-
sx={[
74-
styles.blockBox,
75-
styles.blockBoxWidth({
76-
width: "50%",
77-
}),
78-
]}
79-
>
80-
Half width
81-
</div>
48+
<div sx={[styles.blockBox, styles.blockBoxWidth("50%")]}>Half width</div>
8249
</div>
8350
);
8451

@@ -99,8 +66,8 @@ const styles = stylex.create({
9966
backgroundColor: "lightgray",
10067
borderRadius: 4,
10168
},
102-
coloredBoxBackgroundColor: (props: { backgroundColor: string }) => ({
103-
backgroundColor: props.backgroundColor,
69+
coloredBoxBackgroundColor: (backgroundColor: string) => ({
70+
backgroundColor,
10471
}),
10572
// Non-destructured props pattern: (props) => css`...${props.color}...`
10673
borderBox: {
@@ -110,23 +77,23 @@ const styles = stylex.create({
11077
borderColor: "black",
11178
margin: 4,
11279
},
113-
borderBoxBorderColor: (props: { borderColor: string }) => ({
114-
borderColor: props.borderColor,
80+
borderBoxBorderColor: (borderColor: string) => ({
81+
borderColor,
11582
}),
11683
// Non-destructured props with different param name: (p) => css`...${p.color}...`
11784
shadowBox: {
11885
padding: 12,
11986
boxShadow: "none",
12087
},
121-
shadowBoxBoxShadow: (props: { boxShadow: string }) => ({
122-
boxShadow: props.boxShadow,
88+
shadowBoxBoxShadow: (boxShadow: string) => ({
89+
boxShadow,
12390
}),
12491
// Block body with return statement: (props) => { return css`...`; }
12592
blockBox: {
12693
display: "block",
12794
width: "100%",
12895
},
129-
blockBoxWidth: (props: { width: string }) => ({
130-
width: props.width,
96+
blockBoxWidth: (width: string) => ({
97+
width,
13198
}),
13299
});

test-cases/basic-sharedBase.output.tsx

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,10 @@ function PositionBase<C extends React.ElementType = "div">(
3939
{...rest}
4040
{...mergedSx(
4141
[
42-
top
43-
? styles.positionBaseTop({
44-
top: top,
45-
})
46-
: undefined,
47-
right
48-
? styles.positionBaseRight({
49-
right: right,
50-
})
51-
: undefined,
52-
bottom
53-
? styles.positionBaseBottom({
54-
bottom: bottom,
55-
})
56-
: undefined,
57-
left
58-
? styles.positionBaseLeft({
59-
left: left,
60-
})
61-
: undefined,
42+
top ? styles.positionBaseTop(top) : undefined,
43+
right ? styles.positionBaseRight(right) : undefined,
44+
bottom ? styles.positionBaseBottom(bottom) : undefined,
45+
left ? styles.positionBaseLeft(left) : undefined,
6246
sx,
6347
],
6448
className,
@@ -98,17 +82,17 @@ export function App() {
9882
}
9983

10084
const styles = stylex.create({
101-
positionBaseTop: (props: { top: string }) => ({
102-
top: props.top,
85+
positionBaseTop: (top: string) => ({
86+
top,
10387
}),
104-
positionBaseRight: (props: { right: string }) => ({
105-
right: props.right,
88+
positionBaseRight: (right: string) => ({
89+
right,
10690
}),
107-
positionBaseBottom: (props: { bottom: string }) => ({
108-
bottom: props.bottom,
91+
positionBaseBottom: (bottom: string) => ({
92+
bottom,
10993
}),
110-
positionBaseLeft: (props: { left: string }) => ({
111-
left: props.left,
94+
positionBaseLeft: (left: string) => ({
95+
left,
11296
}),
11397
relative: {
11498
position: "relative",

test-cases/conditional-helperCall.output.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ function Title(props: TitleProps) {
3838
sx={[
3939
styles.title,
4040
truncateTitle ? helpers.truncate : undefined,
41-
maxWidth
42-
? styles.titleMaxWidth({
43-
maxWidth: maxWidth,
44-
})
45-
: undefined,
41+
maxWidth ? styles.titleMaxWidth(maxWidth) : undefined,
4642
]}
4743
>
4844
{children}
@@ -74,7 +70,7 @@ const styles = stylex.create({
7470
title: {
7571
fontSize: 50,
7672
},
77-
titleMaxWidth: (props: { maxWidth: number }) => ({
78-
maxWidth: props.maxWidth,
73+
titleMaxWidth: (maxWidth: number) => ({
74+
maxWidth,
7975
}),
8076
});

test-cases/conditional-logicalAnd.output.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ function LayeredBox(props: LayeredBoxProps) {
1010
const { children, zIndex } = props;
1111

1212
return (
13-
<div
14-
sx={[
15-
styles.layeredBox,
16-
zIndex !== undefined &&
17-
styles.layeredBoxZIndex({
18-
zIndex: zIndex,
19-
}),
20-
]}
21-
>
13+
<div sx={[styles.layeredBox, zIndex !== undefined && styles.layeredBoxZIndex(zIndex)]}>
2214
{children}
2315
</div>
2416
);
@@ -84,8 +76,8 @@ const styles = stylex.create({
8476
layeredBox: {
8577
position: "absolute",
8678
},
87-
layeredBoxZIndex: (props: { zIndex: number }) => ({
88-
zIndex: props.zIndex,
79+
layeredBoxZIndex: (zIndex: number) => ({
80+
zIndex,
8981
}),
9082
grayscaleImage: {
9183
width: 100,

test-cases/conditional-nestedConditionRoot.output.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@ function Layer(props: LayerProps) {
88
const { children, $zIndex, $layer } = props;
99

1010
return (
11-
<div
12-
sx={[
13-
styles.layer,
14-
$layer.isTop
15-
? styles.layerZIndex({
16-
zIndex: $zIndex,
17-
})
18-
: undefined,
19-
]}
20-
>
11+
<div sx={[styles.layer, $layer.isTop ? styles.layerZIndex($zIndex) : undefined]}>
2112
{children}
2213
</div>
2314
);
@@ -42,7 +33,7 @@ const styles = stylex.create({
4233
backgroundColor: "#ddd",
4334
color: "#222",
4435
},
45-
layerZIndex: (props: { zIndex: string }) => ({
46-
zIndex: props.zIndex,
36+
layerZIndex: (zIndex: string) => ({
37+
zIndex,
4738
}),
4839
});

test-cases/conditional-nullishCoalescing.output.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,7 @@ type DividerProps = {
99
function Divider(props: DividerProps) {
1010
const { color } = props;
1111

12-
return (
13-
<hr
14-
sx={[
15-
styles.divider,
16-
color != null &&
17-
styles.dividerBackgroundColor({
18-
backgroundColor: color,
19-
}),
20-
]}
21-
/>
22-
);
12+
return <hr sx={[styles.divider, color != null && styles.dividerBackgroundColor(color)]} />;
2313
}
2414

2515
export const App = () => (
@@ -39,7 +29,7 @@ const styles = stylex.create({
3929
marginBlock: 16,
4030
marginInline: 0,
4131
},
42-
dividerBackgroundColor: (props: { backgroundColor: string }) => ({
43-
backgroundColor: props.backgroundColor,
32+
dividerBackgroundColor: (backgroundColor: string) => ({
33+
backgroundColor,
4434
}),
4535
});

0 commit comments

Comments
 (0)