Repository navigation
Upgrade: don't migrate strings that name a union member or sit in a sentence - #20770
Open
MateuszNaKodach wants to merge 2 commits into
Open
MateuszNaKodach wants to merge 2 commits into
MateuszNaKodach wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #20769.
The template codemod rewrote bare words in strings that are not classes, which silently changed program logic:
isSafeMigrationalready skips bare words in some known non-class positions (variantprops,emit(,placeholder=, ...). This adds four more, all line-local like the existing ones (no whole-file scan):kindortypekey, or a comparison with one:{ kind: 'ring' },effect.type !== "shadow".switchcase:case 'ring':.type Shape = 'circle' | 'ring'(||is not a union, socn(isOpen || "shadow")still migrates)..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 asclsx(['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/classNameprops,cn/clsx/cva/twMergecalls)? 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
is-safe-migration.test.tsthat must not migrate and 6 that must still migrate (guards for rules 1 to 4).pnpm vitest run packages/@tailwindcss-upgrade: onmain12 failed, 476 passed; with this PR 488 passed.pnpm vitest run --root=./integrations upgrade: 69 passed.mainboth change 8 lines that hold no class; this branch changes 1 (thepush(['ring', ...])case) and still migrates all 7 real classes on the other 3 lines.