diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f48d1c51fe2..a8a414fec022 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing yet! +### Fixed + +- Upgrade: Do not migrate declarations that look like candidates in ` + `, + 'src/input.css': css` + @import 'tailwindcss'; + + .foo { + flex-shrink: 1; + } + + .bar { + @apply !underline; + } + `, + }, + }, + async ({ exec, fs, expect }) => { + await exec('npx @tailwindcss/upgrade') + + expect(await fs.dumpFiles('./src/**/*.{css,vue}')).toMatchInlineSnapshot(` + " + --- ./src/index.vue --- + + + + + --- ./src/input.css --- + @import 'tailwindcss'; + + .foo { + flex-shrink: 1; + } + + .bar { + @apply underline!; + } + " + `) + }, +) + function withBOM(text: string): string { return '\uFEFF' + text } 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 dd540d5c57e7..a344e56ed18f 100644 --- a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts +++ b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts @@ -22,6 +22,37 @@ export function isSafeMigration( location: { contents: string; start: number; end: number }, designSystem: DesignSystem, ): boolean { + // Ensure we are not migrating a candidate in a ` + // ``` + if ( + // Whitespace before the candidate + location.contents[location.start - 1]?.match(/\s/) && + // A colon followed by whitespace after the candidate + location.contents.slice(location.end, location.end + 2)?.match(/^:\s/) && + // A `` is present after the candidate + location.contents.slice(location.end).includes('') + ) { + return false + } + let [candidate] = Array.from(parseCandidate(rawCandidate, designSystem)) // If we can't parse the candidate, then it's not a candidate at all. However,