Skip to content

Commit 893ab46

Browse files
handle a call in the condition and a parenthesised branch
review pointed out two holes. a comma inside a call like isActive(foo, bar) ended the match early, and a branch wrapped in parens put a ( between the operator and the literal a parenthesised group is now consumed as a unit so its commas stay inside it, and parens are allowed right before the literal. a bare comma or brace still ends the match so a neighbouring prop is still out of reach
1 parent d8000d7 commit 893ab46

2 files changed

Lines changed: 7 additions & 3 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
@@ -123,6 +123,9 @@ describe('is-safe-migration', async () => {
123123
[`<Button variant={first ? "default" : "outline"} />`, 'outline'],
124124
[`<Button variant={variant ?? "outline"} />`, 'outline'],
125125
[`<Button variant={required ? 'secondary' : 'outline'} />`, 'outline'],
126+
[`<Button variant={isActive(foo, bar) ? "outline" : "ghost"} />`, 'outline'],
127+
[`<Button variant={cond ? ("outline") : "ghost"} />`, 'outline'],
128+
[`<Button variant={cond ? "ghost" : ("outline")} />`, 'outline'],
126129
])('does not replace classes in invalid positions #%#', async (example, candidate) => {
127130
expect(
128131
await migrateCandidate(designSystem, {}, candidate, {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ const CONDITIONAL_TEMPLATE_SYNTAX = [
1818
/wire:[^\s]*?$/,
1919

2020
// shadcn/ui variants, including a ternary or nullish branch between the prop
21-
// and the literal. Braces and commas are excluded so the match cannot reach
22-
// out of its own value into a neighbouring prop such as `className`.
23-
/variant\s*[:=]\s*\{?(?:[^{},]*?(?:\?\?|\?|:)\s*)?['"`]$/,
21+
// and the literal. A parenthesised group is consumed whole so a call in the
22+
// condition keeps its own commas, while a bare comma or brace still ends the
23+
// match so it cannot reach out into a neighbouring prop such as `className`.
24+
/variant\s*[:=]\s*\{?(?:(?:[^{},()]|\([^()]*\))*?(?:\?\?|\?|:)\s*)?\(*\s*['"`]$/,
2425
]
2526
const NEXT_PLACEHOLDER_PROP = /placeholder=\{?['"`]$/
2627
const VUE_3_EMIT = /\b\$?emit\(['"`]$/

0 commit comments

Comments
 (0)