Skip to content

Commit 99d920d

Browse files
split the variant guard into one rule per syntax form
three rounds of review kept finding holes in a single pattern because one regex was trying to know where the value ends in three different syntaxes at once each form now stops at what actually ends it. a brace for a jsx expression, the opening quote for an attribute so it cannot run into a neighbouring one, a comma or brace for an object literal where quotes are normal the array already tests each rule on its own so this needs no other change
1 parent 3730f8a commit 99d920d

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

‎packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ describe('is-safe-migration', async () => {
125125
[`<Button variant={required ? 'secondary' : 'outline'} />`, 'outline'],
126126
[`<Button variant={isActive(foo, bar) ? "outline" : "ghost"} />`, 'outline'],
127127
[`<Button variant={isActive(getState(foo, bar), x) ? "outline" : "ghost"} />`, 'outline'],
128+
[`<div :variant="active ? 'ghost' : 'outline'"></div>`, 'outline'],
129+
[`Button({ variant: (value ?? "outline") })`, 'outline'],
130+
[`Button({ variant: theme === "dark" ? "outline" : "ghost" })`, 'outline'],
128131
[`<Button variant={cond ? ("outline") : "ghost"} />`, 'outline'],
129132
[`<Button variant={cond ? "ghost" : ("outline")} />`, 'outline'],
130133
])('does not replace classes in invalid positions #%#', async (example, candidate) => {

‎packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@ const LOGICAL_OPERATORS = ['&&', '||', '?', '===', '==', '!=', '!==', '>', '>=',
88
// A parenthesised group with one level of nesting, so a call in a condition keeps
99
// its own commas instead of ending the match early.
1010
const PAREN_GROUP = String.raw`\((?:[^()]|\([^()]*\))*\)`
11-
// What may sit between a `variant` prop and its string literal. Inside a brace a
12-
// quoted branch is ordinary, so quotes can be crossed but braces cannot. Outside
13-
// one a quote closes the attribute value, so crossing it would let the guard
14-
// reach a neighbouring attribute such as Vue's `:class`.
15-
const VARIANT_CONDITION = [
16-
String.raw`\{(?:[^{}]|${PAREN_GROUP})*?(?:\?\?|\?|:)\s*`,
17-
String.raw`(?:[^{},()'"\`]|${PAREN_GROUP})*?(?:\?\?|\?|:)\s*`,
18-
String.raw`\{`,
19-
].join('|')
11+
// A ternary or nullish operator, then whatever sits before the string literal.
12+
const CONDITION_TAIL = String.raw`(?:\?\?|\?|:)\s*\(*\s*['"\`]$`
2013

2114
const CONDITIONAL_TEMPLATE_SYNTAX = [
2215
// Skip any generic attributes like `xxx="shadow"`,
@@ -31,9 +24,25 @@ const CONDITIONAL_TEMPLATE_SYNTAX = [
3124
// Alpine
3225
/wire:[^\s]*?$/,
3326

34-
// shadcn/ui variants, including a ternary or nullish branch between the prop
35-
// and the literal
36-
new RegExp(String.raw`variant\s*[:=]\s*(?:${VARIANT_CONDITION})?\(*\s*['"\`]$`),
27+
// shadcn/ui variants. A conditional can sit between the prop and the literal,
28+
// so each form below stops at whatever actually ends its own value. Getting
29+
// that boundary wrong in either direction is costly: too narrow and a prop is
30+
// rewritten as a class, too wide and a real class stops being migrated.
31+
32+
// `variant="outline"`, `variant={"outline"}`, `variant: "outline"`
33+
/variant\s*[:=]\s*\{?['"`]$/,
34+
35+
// `variant={cond ? "outline" : "ghost"}` — a brace ends the value, and a
36+
// quoted branch inside it is ordinary
37+
new RegExp(String.raw`variant\s*[:=]\s*\{(?:[^{}]|${PAREN_GROUP})*?${CONDITION_TAIL}`),
38+
39+
// `:variant="active ? 'outline' : 'ghost'"` — the opening quote ends the value,
40+
// so the match cannot run on into a neighbouring attribute such as `:class`
41+
new RegExp(String.raw`variant\s*=\s*(["'])(?:(?!\1)[^{}])*?${CONDITION_TAIL}`),
42+
43+
// `{ variant: theme === "dark" ? "outline" : "ghost" }` — a comma or brace ends
44+
// the value, quotes do not
45+
new RegExp(String.raw`variant\s*:\s*(?:[^{},]|${PAREN_GROUP})*?${CONDITION_TAIL}`),
3746
]
3847
const NEXT_PLACEHOLDER_PROP = /placeholder=\{?['"`]$/
3948
const VUE_3_EMIT = /\b\$?emit\(['"`]$/

0 commit comments

Comments
 (0)