Skip to content

Commit 59045df

Browse files
committed
conditionally apply migrations based on version number
1 parent 83bf845 commit 59045df

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

packages/@tailwindcss-upgrade/src/codemods/template/migrate-legacy-classes.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Candidate } from '../../../../tailwindcss/src/candidate'
55
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
66
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
77
import { DefaultMap } from '../../../../tailwindcss/src/utils/default-map'
8+
import * as version from '../../utils/version'
89
import { printCandidate } from './candidates'
910
import { isSafeMigration } from './is-safe-migration'
1011

@@ -75,6 +76,15 @@ export async function migrateLegacyClasses(
7576
end: number
7677
},
7778
): Promise<string> {
79+
// These migrations are only safe when migrating from v3 to v4.
80+
//
81+
// Migrating from `rounded` to `rounded-sm` once is fine (v3 -> v4). But if we
82+
// migrate again (v4 -> v4), then `rounded-sm` would be migrated to
83+
// `rounded-xs` which is incorrect because we already migrated this.
84+
if (!version.isMajor(3)) {
85+
return rawCandidate
86+
}
87+
7888
let defaultDesignSystem = await DESIGN_SYSTEMS.get(__dirname)
7989

8090
function* migrate(rawCandidate: string) {

packages/@tailwindcss-upgrade/src/codemods/template/migrate-simple-legacy-classes.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
22
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
3+
import * as version from '../../utils/version'
34
import { printCandidate } from './candidates'
45

56
// Classes that used to exist in Tailwind CSS v3, but do not exist in Tailwind
67
// CSS v4 anymore.
7-
const LEGACY_CLASS_MAP = {
8+
const LEGACY_CLASS_MAP: Record<string, string> = {
89
'overflow-ellipsis': 'text-ellipsis',
910

1011
'flex-grow': 'grow',
@@ -14,8 +15,15 @@ const LEGACY_CLASS_MAP = {
1415

1516
'decoration-clone': 'box-decoration-clone',
1617
'decoration-slice': 'box-decoration-slice',
18+
}
1719

18-
'outline-none': 'outline-hidden',
20+
// `outline-none` in v3 has the same meaning as `outline-hidden` in v4. However,
21+
// `outline-none` in v4 _also_ exists but has a different meaning.
22+
//
23+
// We can only migrate `outline-none` to `outline-hidden` if we are migrating a
24+
// v3 project to v4.
25+
if (version.isMajor(3)) {
26+
LEGACY_CLASS_MAP['outline-none'] = 'outline-hidden'
1927
}
2028

2129
let seenDesignSystems = new WeakSet<DesignSystem>()

packages/@tailwindcss-upgrade/src/codemods/template/migrate-variant-order.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@ import { walk, type AstNode } from '../../../../tailwindcss/src/ast'
22
import { type Variant } from '../../../../tailwindcss/src/candidate'
33
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
44
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
5+
import * as version from '../../utils/version'
56
import { printCandidate } from './candidates'
67

78
export function migrateVariantOrder(
89
designSystem: DesignSystem,
910
_userConfig: Config,
1011
rawCandidate: string,
1112
): string {
13+
// This migration is only needed for Tailwind CSS v3
14+
//
15+
// Changing the variant order when migrating from v3 to v4 is fine, but
16+
// migrating v4 to v4 would make it unsafe because the variant order would
17+
// flip-flop every time you run the migration.
18+
if (!version.isMajor(3)) {
19+
return rawCandidate
20+
}
21+
1222
for (let candidate of designSystem.parseCandidate(rawCandidate)) {
1323
if (candidate.variants.length <= 1) {
1424
continue

0 commit comments

Comments
 (0)