Skip to content

Commit 75e4e2f

Browse files
let a condition contain an object literal
review found two more, both an object literal inside the condition. a vue binding calling isActive({ x: 1 }) and a jsx expression starting with an inline object an object group is now allowed as a unit the same way a parenthesised group already was, so the braces inside it do not read as the end of the value
1 parent 99d920d commit 75e4e2f

2 files changed

Lines changed: 12 additions & 3 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
@@ -128,6 +128,8 @@ describe('is-safe-migration', async () => {
128128
[`<div :variant="active ? 'ghost' : 'outline'"></div>`, 'outline'],
129129
[`Button({ variant: (value ?? "outline") })`, 'outline'],
130130
[`Button({ variant: theme === "dark" ? "outline" : "ghost" })`, 'outline'],
131+
[`<div :variant="isActive({ x: 1 }) ? 'ghost' : 'outline'"></div>`, 'outline'],
132+
[`<Button variant={{ tone: "dark" }.tone === "dark" ? "outline" : "ghost"} />`, 'outline'],
131133
[`<Button variant={cond ? ("outline") : "ghost"} />`, 'outline'],
132134
[`<Button variant={cond ? "ghost" : ("outline")} />`, 'outline'],
133135
])('does not replace classes in invalid positions #%#', async (example, candidate) => {

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ 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+
// An object literal with one level of nesting, so a condition may contain one
12+
// without the surrounding value looking like it ended there.
13+
const OBJECT_GROUP = String.raw`\{(?:[^{}]|\{[^{}]*\})*\}`
1114
// A ternary or nullish operator, then whatever sits before the string literal.
1215
const CONDITION_TAIL = String.raw`(?:\?\?|\?|:)\s*\(*\s*['"\`]$`
1316

@@ -34,15 +37,19 @@ const CONDITIONAL_TEMPLATE_SYNTAX = [
3437

3538
// `variant={cond ? "outline" : "ghost"}` — a brace ends the value, and a
3639
// quoted branch inside it is ordinary
37-
new RegExp(String.raw`variant\s*[:=]\s*\{(?:[^{}]|${PAREN_GROUP})*?${CONDITION_TAIL}`),
40+
new RegExp(
41+
String.raw`variant\s*[:=]\s*\{(?:[^{}]|${PAREN_GROUP}|${OBJECT_GROUP})*?${CONDITION_TAIL}`,
42+
),
3843

3944
// `:variant="active ? 'outline' : 'ghost'"` — the opening quote ends the value,
4045
// so the match cannot run on into a neighbouring attribute such as `:class`
41-
new RegExp(String.raw`variant\s*=\s*(["'])(?:(?!\1)[^{}])*?${CONDITION_TAIL}`),
46+
new RegExp(String.raw`variant\s*=\s*(["'])(?:(?!\1)[^{}]|${OBJECT_GROUP})*?${CONDITION_TAIL}`),
4247

4348
// `{ variant: theme === "dark" ? "outline" : "ghost" }` — a comma or brace ends
4449
// the value, quotes do not
45-
new RegExp(String.raw`variant\s*:\s*(?:[^{},]|${PAREN_GROUP})*?${CONDITION_TAIL}`),
50+
new RegExp(
51+
String.raw`variant\s*:\s*(?:[^{},]|${PAREN_GROUP}|${OBJECT_GROUP})*?${CONDITION_TAIL}`,
52+
),
4653
]
4754
const NEXT_PLACEHOLDER_PROP = /placeholder=\{?['"`]$/
4855
const VUE_3_EMIT = /\b\$?emit\(['"`]$/

0 commit comments

Comments
 (0)