-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add migration for simple legacy classes
- Loading branch information
1 parent
f92261f
commit 8a09726
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
packages/@tailwindcss-upgrade/src/template/codemods/simple-legacy-classes.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { __unstable__loadDesignSystem } from '@tailwindcss/node' | ||
import { expect, test } from 'vitest' | ||
import { simpleLegacyClasses } from './simple-legacy-classes' | ||
|
||
test.each([ | ||
['overflow-clip', 'text-clip'], | ||
['overflow-ellipsis', 'text-ellipsis'], | ||
['flex-grow-0', 'grow-0'], | ||
['flex-shrink-0', 'shrink-0'], | ||
['decoration-clone', 'box-decoration-clone'], | ||
['decoration-slice', 'box-decoration-slice'], | ||
|
||
['max-lg:hover:decoration-slice', 'max-lg:hover:box-decoration-slice'], | ||
['max-lg:hover:decoration-slice!', 'max-lg:hover:box-decoration-slice!'], | ||
['max-lg:hover:!decoration-slice', 'max-lg:hover:box-decoration-slice!'], | ||
])('%s => %s', async (candidate, result) => { | ||
let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss";', { | ||
base: __dirname, | ||
}) | ||
|
||
expect(simpleLegacyClasses(designSystem, {}, candidate)).toEqual(result) | ||
}) |
39 changes: 39 additions & 0 deletions
39
packages/@tailwindcss-upgrade/src/template/codemods/simple-legacy-classes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Config } from 'tailwindcss' | ||
import type { DesignSystem } from '../../../../tailwindcss/src/design-system' | ||
import { segment } from '../../../../tailwindcss/src/utils/segment' | ||
|
||
// Classes that used to exist in Tailwind CSS v3, but do not exist in Tailwind | ||
// CSS v4 anymore. | ||
const LEGACY_CLASS_MAP = { | ||
'overflow-clip': 'text-clip', | ||
'overflow-ellipsis': 'text-ellipsis', | ||
'flex-grow-0': 'grow-0', | ||
'flex-shrink-0': 'shrink-0', | ||
'decoration-clone': 'box-decoration-clone', | ||
'decoration-slice': 'box-decoration-slice', | ||
} | ||
|
||
export function simpleLegacyClasses( | ||
_designSystem: DesignSystem, | ||
_userConfig: Config, | ||
rawCandidate: string, | ||
): string { | ||
let variants = segment(rawCandidate, ':') | ||
let utility = variants.pop()! | ||
|
||
let important = false | ||
if (utility[0] === '!') { | ||
important = true | ||
utility = utility.slice(1) | ||
} else if (utility[utility?.length - 1] === '!') { | ||
important = true | ||
utility = utility.slice(0, -1) | ||
} | ||
|
||
if (utility in LEGACY_CLASS_MAP) { | ||
let replacement = LEGACY_CLASS_MAP[utility as keyof typeof LEGACY_CLASS_MAP] | ||
return `${variants.concat(replacement).join(':')}${important ? '!' : ''}` | ||
} | ||
|
||
return rawCandidate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters