Skip to content

Upgrade: don't migrate strings that name a union member or sit in a sentence - #20770

Open
MateuszNaKodach wants to merge 2 commits into
tailwindlabs:mainfrom
MateuszNaKodach:fix/upgrade-string-literal-names
Open

MateuszNaKodach wants to merge 2 commits into
tailwindlabs:mainfrom
MateuszNaKodach:fix/upgrade-string-literal-names

Conversation

@MateuszNaKodach

Copy link
Copy Markdown

Summary

Fixes #20769.

The template codemod rewrote bare words in strings that are not classes, which silently changed program logic:

-  | { kind: 'ring'; x: number }
+  | { kind: 'ring-3'; x: number }
-    case 'ring':
+    case 'ring-3':
-const help = `A badge with only an outline: you have enough ki.`
+const help = `A badge with only an outline-solid: you have enough ki.`

isSafeMigration already skips bare words in some known non-class positions (variant props, emit(, placeholder=, ...). This adds four more, all line-local like the existing ones (no whole-file scan):

  1. The value of a kind or type key, or a comparison with one: { kind: 'ring' }, effect.type !== "shadow".
  2. A switch case: case 'ring':.
  3. A member of a string literal union: type Shape = 'circle' | 'ring' (|| is not a union, so cn(isOpen || "shadow") still migrates).
  4. A word in a sentence: the string around the candidate has a . or ! followed by a space, or ends with one. A class list never has that.

Rule 4 looks at every bare candidate in every file type, so it is in its own commit and can be dropped on its own if you'd rather not take it.

Not covered: calls.push(['ring', ...args]) still becomes 'ring-3'. An array of strings looks the same as clsx(['ring', ...classes]), so I left it alone.

Question: would you rather go further and only migrate bare words in JS/TS files when they sit in a class context (class/className props, cn/clsx/cva/twMerge calls)? That would end this family of reports (#16305, #16796, #18922, #20435), but it would miss classes kept in plain variables or custom helpers, and it is the trade-off you chose against in #16305. Happy to try it if you want it.

Test plan

  • Added 12 cases to is-safe-migration.test.ts that must not migrate and 6 that must still migrate (guards for rules 1 to 4).
  • pnpm vitest run packages/@tailwindcss-upgrade: on main 12 failed, 476 passed; with this PR 488 passed.
  • pnpm vitest run --root=./integrations upgrade: 69 passed.
  • Ran the packed tool on a small v3 project with the snippets above: 4.3.5 and main both change 8 lines that hold no class; this branch changes 1 (the push(['ring', ...]) case) and still migrates all 7 real classes on the other 3 lines.

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.
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.
@MateuszNaKodach
MateuszNaKodach requested a review from a team as a code owner November 26, 2026 12:29
Copilot AI balanced review requested due to automatic review settings November 26, 2026 12:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Upgrade tool rewrites string literals that name a union member or sit in a sentence (kind: 'ring' -> 'ring-3')

2 participants