-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add CSS codemod for missing @layer
#14504
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0cc1c3e
move `walk` utils in their own file
RobinMalfait 3458c24
use `OnceExit` instead of the visitor API
RobinMalfait bf1d086
add separate step for formatting nodes
RobinMalfait c3445b0
add separate step that hoists imports
RobinMalfait 58b8f8d
implement missing layer migration
RobinMalfait 4be746c
add generic migration test
RobinMalfait 7cfc018
use new migration plugins
RobinMalfait 2899891
cleanup trailing whitespace
RobinMalfait 2cbb8e7
fix test name
RobinMalfait 1063c0d
add integration test
RobinMalfait 4813d54
drop hoist imports
RobinMalfait 0a1b1e8
split test into better examples
RobinMalfait e5e07f9
update changelog
RobinMalfait 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
35 changes: 35 additions & 0 deletions
35
packages/@tailwindcss-upgrade/src/codemods/format-nodes.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,35 @@ | ||
import postcss, { type Plugin } from 'postcss' | ||
import { expect, it } from 'vitest' | ||
import { formatNodes } from './format-nodes' | ||
|
||
function markPretty(): Plugin { | ||
return { | ||
postcssPlugin: '@tailwindcss/upgrade/mark-pretty', | ||
OnceExit(root) { | ||
root.walkAtRules('utility', (atRule) => { | ||
atRule.raws.tailwind_pretty = true | ||
}) | ||
}, | ||
} | ||
} | ||
|
||
function migrate(input: string) { | ||
return postcss() | ||
.use(markPretty()) | ||
.use(formatNodes()) | ||
.process(input, { from: expect.getState().testPath }) | ||
.then((result) => result.css) | ||
} | ||
|
||
it('should format PostCSS nodes that are marked with tailwind_pretty', async () => { | ||
expect( | ||
await migrate(` | ||
@utility .foo { .foo { color: red; } }`), | ||
).toMatchInlineSnapshot(` | ||
"@utility .foo { | ||
.foo { | ||
color: red; | ||
} | ||
}" | ||
`) | ||
}) |
30 changes: 30 additions & 0 deletions
30
packages/@tailwindcss-upgrade/src/codemods/format-nodes.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,30 @@ | ||
import { parse, type ChildNode, type Plugin, type Root } from 'postcss' | ||
import { format } from 'prettier' | ||
import { walk, WalkAction } from '../utils/walk' | ||
|
||
// Prettier is used to generate cleaner output, but it's only used on the nodes | ||
// that were marked as `pretty` during the migration. | ||
export function formatNodes(): Plugin { | ||
async function migrate(root: Root) { | ||
// Find the nodes to format | ||
let nodesToFormat: ChildNode[] = [] | ||
walk(root, (child) => { | ||
if (child.raws.tailwind_pretty) { | ||
nodesToFormat.push(child) | ||
return WalkAction.Skip | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the custom |
||
} | ||
}) | ||
|
||
// Format the nodes | ||
await Promise.all( | ||
nodesToFormat.map(async (node) => { | ||
node.replaceWith(parse(await format(node.toString(), { parser: 'css', semi: true }))) | ||
}), | ||
) | ||
} | ||
|
||
return { | ||
postcssPlugin: '@tailwindcss/upgrade/format-nodes', | ||
OnceExit: migrate, | ||
} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
packages/@tailwindcss-upgrade/src/codemods/migrate-at-layer-utilities.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
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
65 changes: 65 additions & 0 deletions
65
packages/@tailwindcss-upgrade/src/codemods/migrate-missing-layers.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,65 @@ | ||
import dedent from 'dedent' | ||
import postcss from 'postcss' | ||
import { expect, it } from 'vitest' | ||
import { formatNodes } from './format-nodes' | ||
import { migrateMissingLayers } from './migrate-missing-layers' | ||
|
||
const css = dedent | ||
|
||
function migrate(input: string) { | ||
return postcss() | ||
.use(migrateMissingLayers()) | ||
.use(formatNodes()) | ||
.process(input, { from: expect.getState().testPath }) | ||
.then((result) => result.css) | ||
} | ||
|
||
it('should migrate rules between tailwind directives', async () => { | ||
expect( | ||
await migrate(css` | ||
@tailwind base; | ||
|
||
.base { | ||
} | ||
|
||
@tailwind components; | ||
|
||
.component-a { | ||
} | ||
.component-b { | ||
} | ||
|
||
@tailwind utilities; | ||
|
||
.utility-a { | ||
} | ||
.utility-b { | ||
} | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"@tailwind base; | ||
|
||
@layer base { | ||
.base { | ||
} | ||
} | ||
|
||
@tailwind components; | ||
|
||
@layer components { | ||
.component-a { | ||
} | ||
.component-b { | ||
} | ||
} | ||
|
||
@tailwind utilities; | ||
|
||
@layer utilities { | ||
.utility-a { | ||
} | ||
.utility-b { | ||
} | ||
}" | ||
`) | ||
}) |
Oops, something went wrong.
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.
Just a quick plugin to mark the nodes with
tailwind_pretty