-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Migrate aria-*, data-* and supports-* variants from arbitrary values to bare values
#14644
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
5 commits
Select commit
Hold shift + click to select a range
9bd52a5
migrate `data-*` and `aria-*` variants from arbitrary values to bare
RobinMalfait 1181ec3
update changelog
RobinMalfait 37cc8d1
ensure only `foo="true"` is valid, and not `foo~="true"`
RobinMalfait 497a41e
migrate `supports-[foo]` to `supports-foo` if we can
RobinMalfait 524cdbc
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 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
31 changes: 31 additions & 0 deletions
31
packages/@tailwindcss-upgrade/src/template/codemods/arbitrary-value-to-bare-value.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,31 @@ | ||
| import { __unstable__loadDesignSystem } from '@tailwindcss/node' | ||
| import { expect, test } from 'vitest' | ||
| import { arbitraryValueToBareValue } from './arbitrary-value-to-bare-value' | ||
|
|
||
| test.each([ | ||
| ['data-[selected]:flex', 'data-selected:flex'], | ||
| ['data-[foo=bar]:flex', 'data-[foo=bar]:flex'], | ||
|
|
||
| ['supports-[gap]:flex', 'supports-gap:flex'], | ||
| ['supports-[display:grid]:flex', 'supports-[display:grid]:flex'], | ||
|
|
||
| ['group-data-[selected]:flex', 'group-data-selected:flex'], | ||
| ['group-data-[foo=bar]:flex', 'group-data-[foo=bar]:flex'], | ||
| ['group-has-data-[selected]:flex', 'group-has-data-selected:flex'], | ||
|
|
||
| ['aria-[selected]:flex', 'aria-[selected]:flex'], | ||
| ['aria-[selected="true"]:flex', 'aria-selected:flex'], | ||
| ['aria-[selected*="true"]:flex', 'aria-[selected*="true"]:flex'], | ||
|
|
||
| ['group-aria-[selected]:flex', 'group-aria-[selected]:flex'], | ||
| ['group-aria-[selected="true"]:flex', 'group-aria-selected:flex'], | ||
| ['group-has-aria-[selected]:flex', 'group-has-aria-[selected]:flex'], | ||
|
|
||
| ['max-lg:hover:data-[selected]:flex!', 'max-lg:hover:data-selected:flex!'], | ||
| ])('%s => %s', async (candidate, result) => { | ||
| let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss";', { | ||
| base: __dirname, | ||
| }) | ||
|
|
||
| expect(arbitraryValueToBareValue(designSystem, {}, candidate)).toEqual(result) | ||
| }) |
94 changes: 94 additions & 0 deletions
94
packages/@tailwindcss-upgrade/src/template/codemods/arbitrary-value-to-bare-value.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,94 @@ | ||
| import type { Config } from 'tailwindcss' | ||
| import { parseCandidate, type Candidate, type Variant } from '../../../../tailwindcss/src/candidate' | ||
| import type { DesignSystem } from '../../../../tailwindcss/src/design-system' | ||
| import { segment } from '../../../../tailwindcss/src/utils/segment' | ||
| import { printCandidate } from '../candidates' | ||
|
|
||
| export function arbitraryValueToBareValue( | ||
| designSystem: DesignSystem, | ||
| _userConfig: Config, | ||
| rawCandidate: string, | ||
| ): string { | ||
| for (let candidate of parseCandidate(rawCandidate, designSystem)) { | ||
| let clone = structuredClone(candidate) | ||
| let changed = false | ||
| for (let variant of variants(clone)) { | ||
| // Convert `data-[selected]` to `data-selected` | ||
| if ( | ||
| variant.kind === 'functional' && | ||
| variant.root === 'data' && | ||
| variant.value?.kind === 'arbitrary' && | ||
| !variant.value.value.includes('=') | ||
| ) { | ||
| changed = true | ||
| variant.value = { | ||
| kind: 'named', | ||
| value: variant.value.value, | ||
| } | ||
| } | ||
|
|
||
| // Convert `aria-[selected="true"]` to `aria-selected` | ||
| else if ( | ||
| variant.kind === 'functional' && | ||
| variant.root === 'aria' && | ||
| variant.value?.kind === 'arbitrary' && | ||
| (variant.value.value.endsWith('=true') || | ||
| variant.value.value.endsWith('="true"') || | ||
| variant.value.value.endsWith("='true'")) | ||
| ) { | ||
| let [key, _value] = segment(variant.value.value, '=') | ||
| if ( | ||
| // aria-[foo~="true"] | ||
| key[key.length - 1] === '~' || | ||
| // aria-[foo|="true"] | ||
| key[key.length - 1] === '|' || | ||
| // aria-[foo^="true"] | ||
| key[key.length - 1] === '^' || | ||
| // aria-[foo$="true"] | ||
| key[key.length - 1] === '$' || | ||
| // aria-[foo*="true"] | ||
| key[key.length - 1] === '*' | ||
| ) { | ||
| continue | ||
| } | ||
|
|
||
| changed = true | ||
| variant.value = { | ||
| kind: 'named', | ||
| value: variant.value.value.slice(0, variant.value.value.indexOf('=')), | ||
| } | ||
| } | ||
|
|
||
| // Convert `supports-[gap]` to `supports-gap` | ||
| else if ( | ||
| variant.kind === 'functional' && | ||
| variant.root === 'supports' && | ||
| variant.value?.kind === 'arbitrary' && | ||
| /^[a-z-][a-z0-9-]*$/i.test(variant.value.value) | ||
| ) { | ||
| changed = true | ||
| variant.value = { | ||
| kind: 'named', | ||
| value: variant.value.value, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return changed ? printCandidate(designSystem, clone) : rawCandidate | ||
| } | ||
|
|
||
| return rawCandidate | ||
| } | ||
|
|
||
| function* variants(candidate: Candidate) { | ||
| function* inner(variant: Variant): Iterable<Variant> { | ||
| yield variant | ||
| if (variant.kind === 'compound') { | ||
| yield* inner(variant.variant) | ||
| } | ||
| } | ||
|
|
||
| for (let variant of candidate.variants) { | ||
| yield* inner(variant) | ||
| } | ||
| } | ||
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.
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.
I had a regex that covered all of these cases but it was horrible so made it more explicit 🙈
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.
lol its almost all special regex chars 😂 — I don't think any of them need to be escaped in a character class tho right? (unless
^was listed first iirc). I think its this right/[~|^$*]$/.test(key)?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.
explicit list of cases is fine though. It definitely reads better.
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.
This is the one I used originally
/[^~|^$*]=(['"]?)true\1/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.
ahhh yeah that is kinda awful lol