From e5fab7be95684a7ff16f5f14d14313b1765a397f Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Sat, 17 May 2025 00:32:29 +0200 Subject: [PATCH 1/7] we should mark it as an unsafe migration We already know we are in v4 and up and it didn't compile. All migrations will fail but will still run, so now we can bail early. --- .../src/codemods/template/is-safe-migration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a344e56ed18f..883098de27c6 100644 --- a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts +++ b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts @@ -62,7 +62,7 @@ export function isSafeMigration( // So let's only skip if we couldn't parse and we are not in Tailwind CSS v3. // if (!candidate && version.isGreaterThan(3)) { - return true + return false } // Parsed a candidate succesfully, verify if it's a valid candidate From 1c5296837b8b4f2958e0fecbbb9b8842fd2b4f66 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Sat, 17 May 2025 00:33:46 +0200 Subject: [PATCH 2/7] fix typo --- .../src/codemods/template/is-safe-migration.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 883098de27c6..18e7b13e7d46 100644 --- a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts +++ b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts @@ -65,7 +65,7 @@ export function isSafeMigration( return false } - // Parsed a candidate succesfully, verify if it's a valid candidate + // Parsed a candidate successfully, verify if it's a valid candidate else if (candidate) { // When we have variants, we can assume that the candidate is safe to migrate // because that requires colons. From 56b094c1767d19e38ed1a3a7a83b7fd491d0035e Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Sat, 17 May 2025 01:04:17 +0200 Subject: [PATCH 3/7] compute `` positions once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows us to compute all the positions once and cache it per file contents. This is useful because otherwise we have to check the exact same file over and over and over again, but just for different positions. This also just computes the ranges of start and end positions. This will allow us to simply check if the candidate is between any of 2 pair points to know if we are in a `` or not. --- .../codemods/template/is-safe-migration.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) 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 18e7b13e7d46..96b56d768483 100644 --- a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts +++ b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts @@ -1,5 +1,6 @@ import { parseCandidate } from '../../../../tailwindcss/src/candidate' import type { DesignSystem } from '../../../../tailwindcss/src/design-system' +import { DefaultMap } from '../../../../tailwindcss/src/utils/default-map' import * as version from '../../utils/version' const QUOTES = ['"', "'", '`'] @@ -168,3 +169,30 @@ export function isSafeMigration( return true } + +// Assumptions: +// - All `` tag +// - All `` +// - No nested `', offset) + if (endTag === -1) return ranges + offset = endTag + 1 + + ranges.push(startTag, endTag) + } +}) From 7020cc4d98a76ea218118bec6ba77005b5896ada Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Sat, 17 May 2025 01:06:04 +0200 Subject: [PATCH 4/7] use ranges to compute if we are in a style block --- .../codemods/template/is-safe-migration.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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 96b56d768483..b8b845a7a991 100644 --- a/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts +++ b/packages/@tailwindcss-upgrade/src/codemods/template/is-safe-migration.ts @@ -45,13 +45,20 @@ export function isSafeMigration( // 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('') + location.contents.slice(location.end, location.end + 2)?.match(/^:\s/) ) { - return false + // Compute all `', offset) if (endTag === -1) return ranges