[@tailwindcss/upgrade] Don’t migrate inline style properties#19918
Conversation
WalkthroughThe codemod package was updated to avoid migrating CSS declarations inside inline 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
|
The |
|
@afurm is that an actual issue you have in your projects? |
- treat inline style attributes as unsafe migration contexts - avoid misclassifying CSS property names like flex-grow and flex-shrink - add regression coverage for inline style attribute cases
- skip inline style attribute values before candidate parsing - prevent CSS property names like flex-grow from being treated as utilities - keep template migration safety checks focused on actual class-like content
964fc54 to
6a50128
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/`@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts:
- Line 23: The INLINE_STYLE_ATTRIBUTE regex and checks around
currentLineBeforeCandidate in is-safe-migration.ts are too strict (they expect
the opening quote to be the end of the tested string) and miss cases where the
style value begins with whitespace or a newline; update the detection so the
pattern that detects a style attribute (INLINE_STYLE_ATTRIBUTE) matches an
opening quote followed optionally by whitespace/newline (or simply match style=
followed by a quote without anchoring to the end) and adjust the logic in the
function referencing currentLineBeforeCandidate to use that relaxed match; add
regression tests for "<div style=\" flex-grow: 1\"></div>" and "<div style=\"\n
flex-grow: 1\"></div>" to verify the migration no longer rewrites inside inline
style values.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 621b1e93-d321-4c07-8613-c72bf9b0a701
📒 Files selected for processing (2)
packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.tspackages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts
RobinMalfait
left a comment
There was a problem hiding this comment.
Thanks! I also added some additional tests to make sure that multi-line style attributes don't get migrated either.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts (1)
51-54: Add a direct:styleregression caseThese new tests cover
style=...well, but not the:stylebranch that the matcher also supports. A single:stylecase would harden this fix against regressions.Suggested test additions
[`<div style="flex-grow: 1"></div>\n`, 'flex-grow'], [`<div style='flex-shrink: 0'></div>\n`, 'flex-shrink'], [`<div style=" flex-shrink: 0"></div>\n`, 'flex-shrink'], [`<div style="\nflex-shrink: 0\n"></div>\n`, 'flex-shrink'], + [`<div :style="'flex-grow: 1'"></div>\n`, 'flex-grow'], + [`<div :style="{ 'flex-shrink': 0 }"></div>\n`, 'flex-shrink'],🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/`@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts around lines 51 - 54, Add a regression test for the matcher’s :style branch by inserting a single test case into the existing test array in is-safe-migration.test.ts that mirrors one of the existing style= cases but uses the :style attribute form (so the matcher that handles ":style" is exercised); ensure the new entry asserts the same token (e.g., 'flex-grow' or 'flex-shrink') and follows the same array structure and newline formatting as the surrounding tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/`@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts:
- Around line 283-285: The tag-end search using source.indexOf('>') (near
variables tagStart and tagEnd) is quote-unaware and can stop on a '>' inside a
quoted attribute value; replace it with a quote-aware scanner (or small helper
like findTagEnd) that walks from tagStart+1, tracks single- and double-quote
states and escapes, and only returns the '>' when not inside quotes; update both
occurrences (the tagEnd calculation at tagStart and the similar code around
lines 360-362) to use this scanner so inline style attributes aren't mis-parsed
and ranges remain valid.
---
Nitpick comments:
In
`@packages/`@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.ts:
- Around line 51-54: Add a regression test for the matcher’s :style branch by
inserting a single test case into the existing test array in
is-safe-migration.test.ts that mirrors one of the existing style= cases but uses
the :style attribute form (so the matcher that handles ":style" is exercised);
ensure the new entry asserts the same token (e.g., 'flex-grow' or 'flex-shrink')
and follows the same array structure and newline formatting as the surrounding
tests.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 1b53abb3-a119-4b91-96cb-c9519b720fca
📒 Files selected for processing (3)
CHANGELOG.mdpackages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.test.tspackages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts
✅ Files skipped from review due to trivial changes (1)
- CHANGELOG.md
| let tagEnd = source.indexOf('>', tagStart + 1) | ||
| if (tagEnd === -1) return ranges | ||
|
|
There was a problem hiding this comment.
Quote-unaware tag-end scan can miss inline style ranges
At Line 283, source.indexOf('>') can stop at a > inside a quoted attribute value. Then Line 361 treats the value as invalid and exits early, so candidates inside inline style can still be migrated.
Proposed fix
+function findTagEnd(source: string, from: number): number {
+ let quote: number | null = null
+ for (let i = from; i < source.length; i++) {
+ let char = source.charCodeAt(i)
+
+ if (quote === null) {
+ if (char === SINGLE_QUOTE || char === DOUBLE_QUOTE) {
+ quote = char
+ } else if (char === GREATER_THAN) {
+ return i
+ }
+ continue
+ }
+
+ if (char === quote) {
+ quote = null
+ }
+ }
+
+ return -1
+}
+
const inlineStyleAttributeValueRanges = new DefaultMap((source: string) => {
let ranges: number[] = []
let offset = 0
@@
- let tagEnd = source.indexOf('>', tagStart + 1)
+ let tagEnd = findTagEnd(source, tagStart + 1)
if (tagEnd === -1) return rangesAlso applies to: 360-362
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/`@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts
around lines 283 - 285, The tag-end search using source.indexOf('>') (near
variables tagStart and tagEnd) is quote-unaware and can stop on a '>' inside a
quoted attribute value; replace it with a quote-aware scanner (or small helper
like findTagEnd) that walks from tagStart+1, tracks single- and double-quote
states and escapes, and only returns the '>' when not inside quotes; update both
occurrences (the tagEnd calculation at tagStart and the similar code around
lines 360-362) to use this scanner so inline style attributes aren't mis-parsed
and ranges remain valid.
Prevent the upgrade tool from rewriting CSS properties inside inline
styleattributes.This fixes cases like
style="flex-grow: 1"being changed tostyle="grow: 1"and adds regression tests.