From df1cd01b46d9f0128da5b3be61ebb912412fe36c Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 25 Nov 2026 14:55:21 +0100 Subject: [PATCH 1/2] Upgrade: don't migrate strings that name a union member A TypeScript discriminant like `{ kind: 'ring' }` or `case 'ring':` was rewritten to `'ring-3'`. Every use is renamed at once, so the code still type-checks and the logic changes silently. Skip a bare word that is the value of a `kind` or `type` key, a `switch` case, or a member of a string literal union. All checks only read the current line, like the existing heuristics. --- .changelog/upgrade-string-literal-names.md | 3 +++ .../template/is-safe-migration.test.ts | 17 +++++++++++++++++ .../codemods/template/is-safe-migration.ts | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 .changelog/upgrade-string-literal-names.md diff --git a/.changelog/upgrade-string-literal-names.md b/.changelog/upgrade-string-literal-names.md new file mode 100644 index 000000000000..871eac472ab1 --- /dev/null +++ b/.changelog/upgrade-string-literal-names.md @@ -0,0 +1,3 @@ +### Fixed + +- Upgrade: Do not migrate a string that names a union member, like `{ kind: 'ring' }`, `case 'ring':` or `type Shape = 'ring' | 'circle'` diff --git a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts index fcad21a8b299..a4c57a48433c 100644 --- a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts +++ b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts @@ -152,6 +152,17 @@ describe('is-safe-migration', async () => { [``, 'outline'], [`@php($variant = $active ? 'outline' : 'ghost')`, 'outline'], [`$variant = $attributes->get('variant') ?? 'outline';`, 'outline'], + + // TypeScript discriminated unions and other string literal names + [` | { kind: 'ring'; x: number; y: number }`, 'ring'], + [`effects.push({ kind: 'ring', x, y })`, 'ring'], + [`if (effect.kind === 'ring') {}`, 'ring'], + [`if (effect.type !== "shadow") {}`, 'shadow'], + [`const chart = { axisPointer: { type: 'shadow' } }`, 'shadow'], + [` case 'ring':`, 'ring'], + [`switch (effect.kind) { case "ring": break }`, 'ring'], + [`type Shape = 'ring' | 'circle'`, 'ring'], + [`type Shape = 'circle' | 'ring'`, 'ring'], ])('does not replace classes in invalid positions #%#', async (example, candidate) => { expect( await migrateCandidate(designSystem, {}, candidate, { @@ -221,6 +232,12 @@ describe('is-safe-migration', async () => { 'shadow', 'shadow-sm', ], + + // Only the value of `kind` or `type` itself is a name + [`Button({ type: "submit", className: "shadow" })`, 'shadow', 'shadow-sm'], + [`cva("", { variants: { kind: { primary: "shadow" } } })`, 'shadow', 'shadow-sm'], + [`const kinds = cn("shadow")`, 'shadow', 'shadow-sm'], + [`cn(isOpen || "shadow")`, 'shadow', 'shadow-sm'], ])('replaces classes in valid positions #%#', async (example, candidate, expected) => { expect( await migrateCandidate(designSystem, {}, candidate, { diff --git a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts index ad049032720e..de15026647f1 100644 --- a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts +++ b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts @@ -14,6 +14,13 @@ const CONDITIONAL_TEMPLATE_SYNTAX = [ // JavaScript / TypeScript /addEventListener\(['"`]$/, + // A `kind` or `type` value names a member of a union, e.g. `{ kind: 'ring' }` + // or `effect.kind === 'ring'` + /\b(?:kind|type)\s*(?::|[!=]==?)\s*['"`]$/, + + // A `switch` case, e.g. `case 'ring':` + /\bcase\s+['"`]$/, + // Alpine /wire:[^\s]*?$/, @@ -22,6 +29,8 @@ const CONDITIONAL_TEMPLATE_SYNTAX = [ ] const NEXT_PLACEHOLDER_PROP = /placeholder=\{?['"`]$/ const VUE_3_EMIT = /\b\$?emit\(['"`]$/ +const STRING_UNION_BEFORE = /(?])/ export function isSafeMigration( rawCandidate: string, @@ -172,6 +181,16 @@ export function isSafeMigration( return false } + // Heuristic: Disallow a member of a string literal union type + // + // E.g.: `type Shape = 'ring' | 'circle'` + if ( + STRING_UNION_BEFORE.test(currentLineBeforeCandidate) || + STRING_UNION_AFTER.test(currentLineAfterCandidate) + ) { + return false + } + // Heuristic: Disallow logical operators preceding or following the candidate for (let operator of LOGICAL_OPERATORS) { if ( From 3ea4697893e8179861ed4894ee6500bbe662cc76 Mon Sep 17 00:00:00 2001 From: Mateusz Nowak Date: Wed, 25 Nov 2026 14:55:37 +0100 Subject: [PATCH 2/2] Upgrade: don't migrate a word inside a sentence Prose in a string, like `A badge with only an outline: ...`, had `outline` rewritten to `outline-solid`. A list of classes never has a `.` or `!` followed by a space or at its end, so skip a bare word when the string around it has one. Only the string that holds the candidate is checked, so punctuation elsewhere on the line, like text after a `class` attribute, does not count. --- .changelog/upgrade-string-literal-names.md | 2 +- .../template/is-safe-migration.test.ts | 12 +++++++ .../codemods/template/is-safe-migration.ts | 32 ++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/.changelog/upgrade-string-literal-names.md b/.changelog/upgrade-string-literal-names.md index 871eac472ab1..1a73327e7c66 100644 --- a/.changelog/upgrade-string-literal-names.md +++ b/.changelog/upgrade-string-literal-names.md @@ -1,3 +1,3 @@ ### Fixed -- Upgrade: Do not migrate a string that names a union member, like `{ kind: 'ring' }`, `case 'ring':` or `type Shape = 'ring' | 'circle'` +- Upgrade: Do not migrate a string that names a union member or sits in a sentence, like `{ kind: 'ring' }`, `case 'ring':`, `type Shape = 'ring' | 'circle'` or `title="Add a shadow to the card."` diff --git a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts index a4c57a48433c..af107690b11f 100644 --- a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts +++ b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts @@ -163,6 +163,14 @@ describe('is-safe-migration', async () => { [`switch (effect.kind) { case "ring": break }`, 'ring'], [`type Shape = 'ring' | 'circle'`, 'ring'], [`type Shape = 'circle' | 'ring'`, 'ring'], + + // A word in a sentence + [ + 'const help = `A badge with only an outline: you have enough ki, but not the range.`', + 'outline', + ], + [`

`, 'shadow'], + [`alert('No ring! Try again')`, 'ring'], ])('does not replace classes in invalid positions #%#', async (example, candidate) => { expect( await migrateCandidate(designSystem, {}, candidate, { @@ -238,6 +246,10 @@ describe('is-safe-migration', async () => { [`cva("", { variants: { kind: { primary: "shadow" } } })`, 'shadow', 'shadow-sm'], [`const kinds = cn("shadow")`, 'shadow', 'shadow-sm'], [`cn(isOpen || "shadow")`, 'shadow', 'shadow-sm'], + + // Punctuation outside of the class string does not make it a sentence + [`

Hello. World!
`, 'shadow', 'shadow-sm'], + [`
`, 'shadow', 'shadow-sm'], ])('replaces classes in valid positions #%#', async (example, candidate, expected) => { expect( await migrateCandidate(designSystem, {}, candidate, { diff --git a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts index de15026647f1..7f39ad9c3ddf 100644 --- a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts +++ b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts @@ -31,6 +31,7 @@ const NEXT_PLACEHOLDER_PROP = /placeholder=\{?['"`]$/ const VUE_3_EMIT = /\b\$?emit\(['"`]$/ const STRING_UNION_BEFORE = /(?])/ +const SENTENCE_END = /[.!](?:\s|$)/ export function isSafeMigration( rawCandidate: string, @@ -191,6 +192,16 @@ export function isSafeMigration( return false } + // Heuristic: Disallow a word in a sentence. A list of classes never contains + // a `.` or `!` followed by a space or at its end. + // + // E.g.: `title="Add a shadow to the card."` + if ( + SENTENCE_END.test(stringAroundCandidate(currentLineBeforeCandidate, currentLineAfterCandidate)) + ) { + return false + } + // Heuristic: Disallow logical operators preceding or following the candidate for (let operator of LOGICAL_OPERATORS) { if ( @@ -354,7 +365,14 @@ const EQUALS = 0x3d const GREATER_THAN = 0x3e function isMiddleOfString(line: string): boolean { + return openQuoteIndex(line) !== -1 +} + +// The index of the quote that opens the string the end of the line is in, or +// -1 when the line ends outside of a string +function openQuoteIndex(line: string): number { let currentQuote: number | null = null + let index = -1 for (let i = 0; i < line.length; i++) { let char = line.charCodeAt(i) @@ -370,17 +388,29 @@ function isMiddleOfString(line: string): boolean { // Found matching quote, we are outside of a string if (currentQuote === char) { currentQuote = null + index = -1 } // Found a quote, we are inside a string else if (currentQuote === null) { currentQuote = char + index = i } break } } - return currentQuote !== null + return index +} + +// The contents of the string around the candidate, without the candidate +// +// shortcut: an escaped quote after the candidate ends the string early, which +// only makes the sentence heuristic miss a sentence. +function stringAroundCandidate(before: string, after: string): string { + let start = openQuoteIndex(before) + let end = after.indexOf(before[start]) + return before.slice(start + 1) + (end === -1 ? after : after.slice(0, end)) } const inlineStyleAttributeValueRanges = new DefaultMap((source: string) => {