Skip to content

Commit e03a837

Browse files
cursoragentskovhus
andcommitted
Make impure shared-ternary test case deterministic for visual rendering
Co-authored-by: Kenneth Skovhus <skovhus@users.noreply.github.com>
1 parent b746aa2 commit e03a837

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

test-cases/conditional-inlineStyleSharedTernary.input.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@ const InputRow = ({ hasRange }: { hasRange: boolean }) => (
2727
// different branch than the original inline style object when the call returns
2828
// a different value each time (e.g. `Math.random()` based logic, time-based
2929
// flags, side-effecting getters).
30-
const RandomBox = styled.div`
30+
const ImpureBox = styled.div`
3131
padding: 8px;
3232
`;
33-
const flipCoin = () => Math.random() > 0.5;
34-
const RandomRow = () => (
35-
<RandomBox
33+
// Deterministic at runtime so input/output renders match pixel-for-pixel, but
34+
// still a `CallExpression` at AST analysis time so the purity check trips and
35+
// the codemod falls back to the per-property dynamic style function.
36+
const isImpureFlag = () => true;
37+
const ImpureRow = () => (
38+
<ImpureBox
3639
style={{
37-
width: flipCoin() ? 48 : 96,
38-
color: flipCoin() ? "red" : "blue",
40+
width: isImpureFlag() ? 48 : 96,
41+
color: isImpureFlag() ? "red" : "blue",
3942
}}
4043
>
41-
random
42-
</RandomBox>
44+
call
45+
</ImpureBox>
4346
);
4447

4548
// A shared-ternary promotion for `<Box>` would generate the key
@@ -71,7 +74,7 @@ export const App = () => (
7174
<div style={{ display: "flex", gap: 8, padding: 16 }}>
7275
<InputRow hasRange={true} />
7376
<InputRow hasRange={false} />
74-
<RandomRow />
77+
<ImpureRow />
7578
<ActiveRow active={true} />
7679
</div>
7780
);

test-cases/conditional-inlineStyleSharedTernary.output.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ const InputRow = ({ hasRange }: { hasRange: boolean }) => (
1111
/>
1212
);
1313

14-
const flipCoin = () => Math.random() > 0.5;
15-
const RandomRow = () => (
16-
<div sx={styles.randomBox(flipCoin() ? 48 : 96, flipCoin() ? "red" : "blue")}>random</div>
14+
// Deterministic at runtime so input/output renders match pixel-for-pixel, but
15+
// still a `CallExpression` at AST analysis time so the purity check trips and
16+
// the codemod falls back to the per-property dynamic style function.
17+
const isImpureFlag = () => true;
18+
const ImpureRow = () => (
19+
<div sx={styles.impureBox(isImpureFlag() ? 48 : 96, isImpureFlag() ? "red" : "blue")}>call</div>
1720
);
1821

1922
const ActiveRow = ({ active }: { active: boolean }) => (
@@ -27,7 +30,7 @@ export const App = () => (
2730
<div style={{ display: "flex", gap: 8, padding: 16 }}>
2831
<InputRow hasRange={true} />
2932
<InputRow hasRange={false} />
30-
<RandomRow />
33+
<ImpureRow />
3134
<ActiveRow active={true} />
3235
</div>
3336
);
@@ -50,7 +53,7 @@ const styles = stylex.create({
5053
// different branch than the original inline style object when the call returns
5154
// a different value each time (e.g. `Math.random()` based logic, time-based
5255
// flags, side-effecting getters).
53-
randomBox: (width: number | string, color: string) => ({
56+
impureBox: (width: number | string, color: string) => ({
5457
padding: 8,
5558
width,
5659
color,

0 commit comments

Comments
 (0)