Skip to content

Commit 3730f8a

Browse files
stop the variant guard reaching into a neighbouring attribute
review caught an over match i introduced. vue writes attributes without braces so the gap happily crossed variant="ghost" and carried on into :class, which suppressed a class that should have migrated quotes may now only be crossed inside a brace, where a quoted ternary branch is ordinary. outside one a quote closes the attribute value so the match stops there. nested parens in a condition are handled while i was in here
1 parent 893ab46 commit 3730f8a

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ describe('is-safe-migration', async () => {
124124
[`<Button variant={variant ?? "outline"} />`, 'outline'],
125125
[`<Button variant={required ? 'secondary' : 'outline'} />`, 'outline'],
126126
[`<Button variant={isActive(foo, bar) ? "outline" : "ghost"} />`, 'outline'],
127+
[`<Button variant={isActive(getState(foo, bar), x) ? "outline" : "ghost"} />`, 'outline'],
127128
[`<Button variant={cond ? ("outline") : "ghost"} />`, 'outline'],
128129
[`<Button variant={cond ? "ghost" : ("outline")} />`, 'outline'],
129130
])('does not replace classes in invalid positions #%#', async (example, candidate) => {
@@ -158,6 +159,7 @@ describe('is-safe-migration', async () => {
158159
'shadow',
159160
'shadow-sm',
160161
],
162+
[`<div variant="ghost" :class="active ? 'shadow' : 'none'"></div>`, 'shadow', 'shadow-sm'],
161163
])('replaces classes in valid positions #%#', async (example, candidate, expected) => {
162164
expect(
163165
await migrateCandidate(designSystem, {}, candidate, {

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import { DefaultMap } from '../../../../tailwindcss/src/utils/default-map'
44
import * as version from '../../utils/version'
55

66
const LOGICAL_OPERATORS = ['&&', '||', '?', '===', '==', '!=', '!==', '>', '>=', '<', '<=']
7+
8+
// A parenthesised group with one level of nesting, so a call in a condition keeps
9+
// its own commas instead of ending the match early.
10+
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('|')
20+
721
const CONDITIONAL_TEMPLATE_SYNTAX = [
822
// Skip any generic attributes like `xxx="shadow"`,
923
// including Vue conditions like `v-if="something && shadow"`
@@ -18,10 +32,8 @@ const CONDITIONAL_TEMPLATE_SYNTAX = [
1832
/wire:[^\s]*?$/,
1933

2034
// shadcn/ui variants, including a ternary or nullish branch between the prop
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*['"`]$/,
35+
// and the literal
36+
new RegExp(String.raw`variant\s*[:=]\s*(?:${VARIANT_CONDITION})?\(*\s*['"\`]$`),
2537
]
2638
const NEXT_PLACEHOLDER_PROP = /placeholder=\{?['"`]$/
2739
const VUE_3_EMIT = /\b\$?emit\(['"`]$/

0 commit comments

Comments
 (0)