diff --git a/.changelog/upgrade-string-literal-names.md b/.changelog/upgrade-string-literal-names.md new file mode 100644 index 000000000000..1a73327e7c66 --- /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 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 fcad21a8b299..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 @@ -152,6 +152,25 @@ 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'], + + // 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, { @@ -221,6 +240,16 @@ 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'], + + // 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 ad049032720e..7f39ad9c3ddf 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,9 @@ const CONDITIONAL_TEMPLATE_SYNTAX = [ ] const NEXT_PLACEHOLDER_PROP = /placeholder=\{?['"`]$/ const VUE_3_EMIT = /\b\$?emit\(['"`]$/ +const STRING_UNION_BEFORE = /(?])/ +const SENTENCE_END = /[.!](?:\s|$)/ export function isSafeMigration( rawCandidate: string, @@ -172,6 +182,26 @@ 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 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 ( @@ -335,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) @@ -351,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) => {