-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update template migration interface #14539
Merged
philipp-spiess
merged 2 commits into
next
from
chore/upgrade-template-migration-interface
Sep 27, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
23 changes: 13 additions & 10 deletions
23
packages/@tailwindcss-upgrade/src/template/codemods/bg-gradient.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 |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import type { Candidate } from '../../../../tailwindcss/src/candidate' | ||
import type { DesignSystem } from '../../../../tailwindcss/src/design-system' | ||
import { printCandidate } from '../candidates' | ||
|
||
const DIRECTIONS = ['t', 'tr', 'r', 'br', 'b', 'bl', 'l', 'tl'] | ||
|
||
export function bgGradient(candidate: Candidate): Candidate | null { | ||
if (candidate.kind === 'static' && candidate.root.startsWith('bg-gradient-to-')) { | ||
let direction = candidate.root.slice(15) | ||
export function bgGradient(designSystem: DesignSystem, rawCandidate: string): string { | ||
for (let candidate of designSystem.parseCandidate(rawCandidate)) { | ||
if (candidate.kind === 'static' && candidate.root.startsWith('bg-gradient-to-')) { | ||
let direction = candidate.root.slice(15) | ||
|
||
if (!DIRECTIONS.includes(direction)) { | ||
return null | ||
} | ||
if (!DIRECTIONS.includes(direction)) { | ||
continue | ||
} | ||
|
||
candidate.root = `bg-linear-to-${direction}` | ||
return candidate | ||
candidate.root = `bg-linear-to-${direction}` | ||
return printCandidate(candidate) | ||
} | ||
} | ||
return null | ||
return rawCandidate | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/@tailwindcss-upgrade/src/template/codemods/important.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 dedent from 'dedent' | ||
import { expect, test } from 'vitest' | ||
import { important } from './important' | ||
|
||
let html = dedent | ||
|
||
test.each([ | ||
['!flex', 'flex!'], | ||
['min-[calc(1000px+12em)]:!flex', 'min-[calc(1000px_+_12em)]:flex!'], | ||
['md:!block', 'md:block!'], | ||
|
||
// Does not change non-important candidates | ||
['bg-blue-500', 'bg-blue-500'], | ||
['min-[calc(1000px+12em)]:flex', 'min-[calc(1000px+12em)]:flex'], | ||
])('%s => %s', async (candidate, result) => { | ||
let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss";', { | ||
base: __dirname, | ||
}) | ||
|
||
expect(important(designSystem, candidate)).toEqual(result) | ||
}) | ||
27 changes: 27 additions & 0 deletions
27
packages/@tailwindcss-upgrade/src/template/codemods/important.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,27 @@ | ||
import type { DesignSystem } from '../../../../tailwindcss/src/design-system' | ||
import { printCandidate } from '../candidates' | ||
|
||
// In v3 the important modifier `!` sits in front of the utility itself, not | ||
// before any of the variants. In v4, we want it to be at the end of the utility | ||
// so that it's always in the same location regardless of whether you used | ||
// variants or not. | ||
// | ||
// So this: | ||
// | ||
// !flex md:!block | ||
// | ||
// Should turn into: | ||
// | ||
// flex! md:block! | ||
export function important(designSystem: DesignSystem, rawCandidate: string): string { | ||
for (let candidate of designSystem.parseCandidate(rawCandidate)) { | ||
if (candidate.important && candidate.raw[candidate.raw.length - 1] !== '!') { | ||
// The printCandidate function will already put the exclamation mark in | ||
// the right place, so we just need to mark this candidate as requiring a | ||
// migration. | ||
return printCandidate(candidate) | ||
} | ||
} | ||
|
||
return rawCandidate | ||
} |
37 changes: 0 additions & 37 deletions
37
packages/@tailwindcss-upgrade/src/template/codemods/migrate-important.test.ts
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
packages/@tailwindcss-upgrade/src/template/codemods/migrate-important.ts
This file was deleted.
Oops, something went wrong.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Migrated this test to what I found more useful in other migrations. Basically it only tests the migration function and not the text replace logic (we have other tests for this incl. integration tests)