Skip to content

Commit fb04282

Browse files
committed
do not migrate declarations in <style>…</style> blocks
However, we also can't just ignore `<style></style>` blocks _if_ they include `@apply` because we still want to migrate those instances.
1 parent f42a442 commit fb04282

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,37 @@ export function isSafeMigration(
2222
location: { contents: string; start: number; end: number },
2323
designSystem: DesignSystem,
2424
): boolean {
25+
// Ensure we are not migrating a candidate in a `<style>` block. The heuristic
26+
// would be if the candidate is preceded by a whitespace and followed by a
27+
// colon and whitespace.
28+
//
29+
// E.g.:
30+
// ```vue
31+
// <template>
32+
// <div class="foo"></div>
33+
// </template>
34+
//
35+
//
36+
// <style>
37+
// .foo {
38+
// flex-shrink: 0;
39+
// ^ ^^
40+
// }
41+
// </style>
42+
// ```
43+
if (
44+
// Whitespace before the candidate
45+
location.contents[location.start - 1]?.match(/\s/) &&
46+
// A colon followed by whitespace after the candidate
47+
location.contents.slice(location.end, location.end + 2)?.match(/^:\s/) &&
48+
// A `<style` block is present before the candidate
49+
location.contents.slice(0, location.start).includes('<style') &&
50+
// `</style>` is present after the candidate
51+
location.contents.slice(location.end).includes('</style>')
52+
) {
53+
return false
54+
}
55+
2556
let [candidate] = Array.from(parseCandidate(rawCandidate, designSystem))
2657

2758
// If we can't parse the candidate, then it's not a candidate at all. However,

0 commit comments

Comments
 (0)