-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Template migrations: Add automatic var injection codemods #14526
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b378d29
Template migrations: Add automatic var injection codemods
philipp-spiess c2b28ac
Add automatic var injection exceptions
philipp-spiess 821c816
Check for the exact value
philipp-spiess 6674b67
Also check for exceptions in modifier/variant arbitrary places
philipp-spiess ae04af1
Update packages/@tailwindcss-upgrade/src/template/codemods/automatic-…
philipp-spiess 6135579
Fixup test from Robin
philipp-spiess 5522f31
Clean up the `[_--myVar]` workaround
philipp-spiess 514c13c
Merge remote-tracking branch 'origin/next' into feat/automatic-var-in…
philipp-spiess 0679e8a
Fix merge conflict
philipp-spiess ebacd7c
Remove file
philipp-spiess e72c333
Update packages/@tailwindcss-upgrade/src/template/codemods/automatic-…
philipp-spiess 947eccd
Update packages/@tailwindcss-upgrade/src/template/codemods/automatic-…
philipp-spiess dd686fb
Merge remote-tracking branch 'origin/next' into feat/automatic-var-in…
philipp-spiess 897c8b2
Fixup CHANGELOG
philipp-spiess 80f6719
Add missing import
philipp-spiess 0192f66
Merge remote-tracking branch 'origin/next' into feat/automatic-var-in…
philipp-spiess 0687077
Clean changelog
philipp-spiess 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 hidden or 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 hidden or 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
59 changes: 59 additions & 0 deletions
59
packages/@tailwindcss-upgrade/src/template/codemods/automatic-var-injection.test.ts
This file contains hidden or 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,59 @@ | ||
| import { __unstable__loadDesignSystem } from '@tailwindcss/node' | ||
| import { expect, test } from 'vitest' | ||
| import { automaticVarInjection } from './automatic-var-injection' | ||
|
|
||
| test.each([ | ||
| // Arbitrary candidates | ||
| ['[color:--my-color]', '[color:var(--my-color)]'], | ||
| ['[--my-color:red]', '[--my-color:red]'], | ||
| ['[--my-color:--my-other-color]', '[--my-color:var(--my-other-color)]'], | ||
|
|
||
| // Arbitrary values for functional candidates | ||
| ['bg-[--my-color]', 'bg-[var(--my-color)]'], | ||
| ['bg-[color:--my-color]', 'bg-[color:var(--my-color)]'], | ||
| ['border-[length:--my-length]', 'border-[length:var(--my-length)]'], | ||
| ['border-[line-width:--my-width]', 'border-[line-width:var(--my-width)]'], | ||
|
|
||
| // Can clean up the workaround for opting out of automatic var injection | ||
| ['bg-[_--my-color]', 'bg-[--my-color]'], | ||
| ['bg-[color:_--my-color]', 'bg-[color:--my-color]'], | ||
| ['border-[length:_--my-length]', 'border-[length:--my-length]'], | ||
| ['border-[line-width:_--my-width]', 'border-[line-width:--my-width]'], | ||
|
|
||
| // Modifiers | ||
| ['[color:--my-color]/[--my-opacity]', '[color:var(--my-color)]/[var(--my-opacity)]'], | ||
| ['bg-red-500/[--my-opacity]', 'bg-red-500/[var(--my-opacity)]'], | ||
| ['bg-[--my-color]/[--my-opacity]', 'bg-[var(--my-color)]/[var(--my-opacity)]'], | ||
| ['bg-[color:--my-color]/[--my-opacity]', 'bg-[color:var(--my-color)]/[var(--my-opacity)]'], | ||
|
|
||
| // Can clean up the workaround for opting out of automatic var injection | ||
| ['[color:--my-color]/[_--my-opacity]', '[color:var(--my-color)]/[--my-opacity]'], | ||
| ['bg-red-500/[_--my-opacity]', 'bg-red-500/[--my-opacity]'], | ||
| ['bg-[--my-color]/[_--my-opacity]', 'bg-[var(--my-color)]/[--my-opacity]'], | ||
| ['bg-[color:--my-color]/[_--my-opacity]', 'bg-[color:var(--my-color)]/[--my-opacity]'], | ||
|
|
||
| // Variants | ||
| ['supports-[--test]:flex', 'supports-[var(--test)]:flex'], | ||
| ['supports-[_--test]:flex', 'supports-[--test]:flex'], | ||
|
|
||
| // Some properties never had var() injection in v3. | ||
| ['[scroll-timeline-name:--myTimeline]', '[scroll-timeline-name:--myTimeline]'], | ||
| ['[timeline-scope:--myScope]', '[timeline-scope:--myScope]'], | ||
| ['[view-timeline-name:--myTimeline]', '[view-timeline-name:--myTimeline]'], | ||
| ['[font-palette:--myPalette]', '[font-palette:--myPalette]'], | ||
| ['[anchor-name:--myAnchor]', '[anchor-name:--myAnchor]'], | ||
| ['[anchor-scope:--myScope]', '[anchor-scope:--myScope]'], | ||
| ['[position-anchor:--myAnchor]', '[position-anchor:--myAnchor]'], | ||
| ['[position-try-options:--myAnchor]', '[position-try-options:--myAnchor]'], | ||
| ['[scroll-timeline:--myTimeline]', '[scroll-timeline:--myTimeline]'], | ||
| ['[animation-timeline:--myAnimation]', '[animation-timeline:--myAnimation]'], | ||
| ['[view-timeline:--myTimeline]', '[view-timeline:--myTimeline]'], | ||
| ['[position-try:--myAnchor]', '[position-try:--myAnchor]'], | ||
| ])('%s => %s', async (candidate, result) => { | ||
| let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss";', { | ||
| base: __dirname, | ||
| }) | ||
|
|
||
| let migrated = automaticVarInjection(designSystem, candidate) | ||
| expect(migrated).toEqual(result) | ||
| }) | ||
155 changes: 155 additions & 0 deletions
155
packages/@tailwindcss-upgrade/src/template/codemods/automatic-var-injection.ts
This file contains hidden or 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,155 @@ | ||
| import { walk, WalkAction } from '../../../../tailwindcss/src/ast' | ||
| import type { Candidate, Variant } from '../../../../tailwindcss/src/candidate' | ||
| import type { DesignSystem } from '../../../../tailwindcss/src/design-system' | ||
| import { printCandidate } from '../candidates' | ||
|
|
||
| export function automaticVarInjection(designSystem: DesignSystem, rawCandidate: string): string { | ||
| for (let candidate of designSystem.parseCandidate(rawCandidate)) { | ||
| let didChange = false | ||
|
|
||
| // Add `var(…)` in modifier position, e.g.: | ||
| // | ||
| // `bg-red-500/[--my-opacity]` => `bg-red-500/[var(--my-opacity)]` | ||
| if ( | ||
| 'modifier' in candidate && | ||
| candidate.modifier?.kind === 'arbitrary' && | ||
| !isAutomaticVarInjectionException(designSystem, candidate, candidate.modifier.value) | ||
| ) { | ||
| let { value, didChange: modifierDidChange } = injectVar(candidate.modifier.value) | ||
| candidate.modifier.value = value | ||
| didChange ||= modifierDidChange | ||
| } | ||
|
|
||
| // Add `var(…)` to all variants, e.g.: | ||
| // | ||
| // `supports-[--test]:flex'` => `supports-[var(--test)]:flex` | ||
| for (let variant of candidate.variants) { | ||
| let didChangeVariant = injectVarIntoVariant(designSystem, variant) | ||
| if (didChangeVariant) { | ||
| didChange = true | ||
| } | ||
| } | ||
|
|
||
| // Add `var(…)` to arbitrary candidates, e.g.: | ||
| // | ||
| // `[color:--my-color]` => `[color:var(--my-color)]` | ||
| if ( | ||
| candidate.kind === 'arbitrary' && | ||
| !isAutomaticVarInjectionException(designSystem, candidate, candidate.value) | ||
| ) { | ||
| let { value, didChange: valueDidChange } = injectVar(candidate.value) | ||
| candidate.value = value | ||
| didChange ||= valueDidChange | ||
| } | ||
|
|
||
| // Add `var(…)` to arbitrary values for functional candidates, e.g.: | ||
| // | ||
| // `bg-[--my-color]` => `bg-[var(--my-color)]` | ||
| if ( | ||
| candidate.kind === 'functional' && | ||
| candidate.value && | ||
| candidate.value.kind === 'arbitrary' && | ||
| !isAutomaticVarInjectionException(designSystem, candidate, candidate.value.value) | ||
| ) { | ||
| let { value, didChange: valueDidChange } = injectVar(candidate.value.value) | ||
| candidate.value.value = value | ||
| didChange ||= valueDidChange | ||
| } | ||
|
|
||
| if (didChange) { | ||
| return printCandidate(candidate) | ||
| } | ||
| } | ||
| return rawCandidate | ||
| } | ||
|
|
||
| function injectVar(value: string): { value: string; didChange: boolean } { | ||
| let didChange = false | ||
| if (value.startsWith('--')) { | ||
| value = `var(${value})` | ||
| didChange = true | ||
| } else if (value.startsWith(' --')) { | ||
| value = value.slice(1) | ||
| didChange = true | ||
| } | ||
| return { value, didChange } | ||
| } | ||
|
|
||
| function injectVarIntoVariant(designSystem: DesignSystem, variant: Variant): boolean { | ||
| let didChange = false | ||
| if ( | ||
| variant.kind === 'functional' && | ||
| variant.value && | ||
| variant.value.kind === 'arbitrary' && | ||
| !isAutomaticVarInjectionException( | ||
| designSystem, | ||
| createEmptyCandidate(variant), | ||
| variant.value.value, | ||
| ) | ||
| ) { | ||
| let { value, didChange: valueDidChange } = injectVar(variant.value.value) | ||
| variant.value.value = value | ||
| didChange ||= valueDidChange | ||
| } | ||
|
|
||
| if (variant.kind === 'compound') { | ||
| let compoundDidChange = injectVarIntoVariant(designSystem, variant.variant) | ||
| if (compoundDidChange) { | ||
| didChange = true | ||
| } | ||
| } | ||
|
|
||
| return didChange | ||
| } | ||
|
|
||
| function createEmptyCandidate(variant: Variant) { | ||
| return { | ||
| kind: 'arbitrary' as const, | ||
| property: 'color', | ||
| value: 'red', | ||
| modifier: null, | ||
| variants: [variant], | ||
| important: false, | ||
| raw: 'candidate', | ||
| } satisfies Candidate | ||
| } | ||
|
|
||
| const AUTO_VAR_INJECTION_EXCEPTIONS = new Set([ | ||
| // Concrete properties | ||
| 'scroll-timeline-name', | ||
| 'timeline-scope', | ||
| 'view-timeline-name', | ||
| 'font-palette', | ||
| 'anchor-name', | ||
| 'anchor-scope', | ||
| 'position-anchor', | ||
| 'position-try-options', | ||
|
|
||
| // Shorthand properties | ||
| 'scroll-timeline', | ||
| 'animation-timeline', | ||
| 'view-timeline', | ||
| 'position-try', | ||
| ]) | ||
| // Some properties never had var() injection in v3. We need to convert the candidate to CSS | ||
| // so we can check the properties used by the utility. | ||
| function isAutomaticVarInjectionException( | ||
| designSystem: DesignSystem, | ||
| candidate: Candidate, | ||
| value: string, | ||
| ): boolean { | ||
| let ast = designSystem.compileAstNodes(candidate).map((n) => n.node) | ||
|
|
||
| let isException = false | ||
| walk(ast, (node) => { | ||
| if ( | ||
| node.kind === 'declaration' && | ||
| AUTO_VAR_INJECTION_EXCEPTIONS.has(node.property) && | ||
| node.value == value | ||
| ) { | ||
| isException = true | ||
philipp-spiess marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return WalkAction.Stop | ||
| } | ||
| }) | ||
| return isException | ||
| } | ||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.