Skip to content

Design system driven upgrade migrations #17831

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 39 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
815a83c
add `Writable` types helper
RobinMalfait Apr 30, 2025
40fa9d7
add `memcpy` util
RobinMalfait Apr 30, 2025
551bbed
add signatures
RobinMalfait Apr 30, 2025
1d32882
add optimize modifier migration
RobinMalfait Apr 30, 2025
7838f27
add drop unnecessary data types migration
RobinMalfait Apr 30, 2025
48d449d
add arbitrary variants migration
RobinMalfait Apr 30, 2025
e064185
add arbitrary utilities migration
RobinMalfait Apr 30, 2025
b6a2a55
export candidate types
RobinMalfait Apr 30, 2025
56303f7
use new migrations
RobinMalfait Apr 30, 2025
24de47d
use memcpy
RobinMalfait Apr 30, 2025
7f23841
use migrate arbitrary value to bare value using signatures
RobinMalfait Apr 30, 2025
a2585e9
improve printing of candidates
RobinMalfait Apr 30, 2025
52de5ed
update apply migration test
RobinMalfait Apr 30, 2025
271f803
move test
RobinMalfait Apr 30, 2025
3705e1a
add walk variants util
RobinMalfait Apr 30, 2025
49da4f0
remove hardcoded list of variants
RobinMalfait Apr 30, 2025
1f69c0f
ensure incomputable signatures result in unique value
RobinMalfait Apr 30, 2025
5af9fa5
`--tw-sort` is the property, not the value
RobinMalfait Apr 30, 2025
fe5d717
prevent infinitely parsing the same value
RobinMalfait Apr 30, 2025
457e9b5
improve signature generation for variants
RobinMalfait Apr 30, 2025
100f524
update integration tests
RobinMalfait Apr 30, 2025
0f4c724
add tests to migrate to more specific utilities
RobinMalfait Apr 30, 2025
6aeecd6
try to migrate both arbitrary properties and arbitrary values
RobinMalfait Apr 30, 2025
fa15d91
handle negative in arbitrary scale
RobinMalfait Apr 30, 2025
f88fec3
update changelog
RobinMalfait Apr 30, 2025
892a417
prefer bare values over arbitrary values
RobinMalfait Apr 30, 2025
ba360ab
convert arbitrary rem value to bare value
RobinMalfait May 1, 2025
ccd8053
abstract parsing dimensions
RobinMalfait May 1, 2025
5d8b626
Update packages/@tailwindcss-upgrade/src/utils/dimension.ts
RobinMalfait May 1, 2025
34565f0
rename variables / comments
RobinMalfait May 1, 2025
42b621b
use existing `getClassList`
RobinMalfait May 1, 2025
1e9ce41
move printing candidate to core
RobinMalfait May 1, 2025
013f0b5
Update CHANGELOG.md
RobinMalfait May 2, 2025
5573c88
use try/finally for extra safety
RobinMalfait May 2, 2025
d5ac5cd
update changelog
RobinMalfait May 2, 2025
f2653ac
drop level of nesting
RobinMalfait May 2, 2025
cd92ff1
use `printModifier` helper
RobinMalfait May 2, 2025
987b9e7
add basic tests for _all_ migrations
RobinMalfait May 2, 2025
fbabe79
rename `memcpy` to `replaceObject`
RobinMalfait May 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add arbitrary variants migration
  • Loading branch information
RobinMalfait committed Apr 30, 2025
commit 48d449dbe26f8d3006d669809a6da3452c18cd0b
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import { __unstable__loadDesignSystem } from '@tailwindcss/node'
import { describe, expect, test } from 'vitest'
import { DefaultMap } from '../../../../tailwindcss/src/utils/default-map'
import { migrateArbitraryVariants } from './migrate-arbitrary-variants'

const css = String.raw
const designSystems = new DefaultMap((base: string) => {
return new DefaultMap((input: string) => {
return __unstable__loadDesignSystem(input, { base })
})
})

describe.each([['default'], ['important'], ['prefix']].slice(0, 1))('%s', (strategy) => {
let testName = '%s => %s (%#)'
if (strategy === 'with-variant') {
testName = testName.replaceAll('%s', 'focus:%s')
} else if (strategy === 'important') {
testName = testName.replaceAll('%s', '%s!')
} else if (strategy === 'prefix') {
testName = testName.replaceAll('%s', 'tw:%s')
}

// Basic input with minimal design system to keep the tests fast
let input = css`
@import 'tailwindcss' ${strategy === 'prefix' ? 'prefix(tw)' : ''};
@theme {
--*: initial;
}
`

test.each([
// Arbitrary variant to static variant
['[&:focus]:flex', 'focus:flex'],

// Arbitrary variant to static variant with at-rules
['[@media(scripting:_none)]:flex', 'noscript:flex'],

// Arbitrary variant to static utility at-rules and with slight differences
// in whitespace. This will require some canonicalization.
['[@media(scripting:none)]:flex', 'noscript:flex'],
['[@media(scripting:_none)]:flex', 'noscript:flex'],
['[@media_(scripting:_none)]:flex', 'noscript:flex'],

// With compound variants
['has-[&:focus]:flex', 'has-focus:flex'],
['not-[&:focus]:flex', 'not-focus:flex'],
['group-[&:focus]:flex', 'group-focus:flex'],
['peer-[&:focus]:flex', 'peer-focus:flex'],
['in-[&:focus]:flex', 'in-focus:flex'],
])(testName, async (candidate, result) => {
if (strategy === 'important') {
candidate = `${candidate}!`
result = `${result}!`
} else if (strategy === 'prefix') {
// Not only do we need to prefix the candidate, we also have to make
// sure that we prefix all CSS variables.
candidate = `tw:${candidate.replaceAll('var(--', 'var(--tw-')}`
result = `tw:${result.replaceAll('var(--', 'var(--tw-')}`
}

let designSystem = await designSystems.get(__dirname).get(input)
let migrated = migrateArbitraryVariants(designSystem, {}, candidate)
expect(migrated).toEqual(result)
})
})

test('unsafe migrations keep the candidate as-is', async () => {
// `hover:` also includes an `@media` query in addition to the `&:hover`
// state. Migration is not safe because the functionality would be different.
let candidate = '[&:hover]:flex'
let result = '[&:hover]:flex'
let input = css`
@import 'tailwindcss';
@theme {
--*: initial;
}
`

let designSystem = await designSystems.get(__dirname).get(input)
let migrated = migrateArbitraryVariants(designSystem, {}, candidate)
expect(migrated).toEqual(result)
})

test('make unsafe migration safe (1)', async () => {
// Overriding the `hover:` variant to only use a selector will make the
// migration safe.
let candidate = '[&:hover]:flex'
let result = 'hover:flex'
let input = css`
@import 'tailwindcss';
@theme {
--*: initial;
}
@variant hover (&:hover);
`

let designSystem = await designSystems.get(__dirname).get(input)
let migrated = migrateArbitraryVariants(designSystem, {}, candidate)
expect(migrated).toEqual(result)
})

test('make unsafe migration safe (2)', async () => {
// Overriding the `hover:` variant to only use a selector will make the
// migration safe. This time with the long-hand `@variant` syntax.
let candidate = '[&:hover]:flex'
let result = 'hover:flex'
let input = css`
@import 'tailwindcss';
@theme {
--*: initial;
}
@variant hover {
&:hover {
@slot;
}
}
`

let designSystem = await designSystems.get(__dirname).get(input)
let migrated = migrateArbitraryVariants(designSystem, {}, candidate)
expect(migrated).toEqual(result)
})

test('custom selector-based variants', async () => {
let candidate = '[&.macos]:flex'
let result = 'is-macos:flex'
let input = css`
@import 'tailwindcss';
@theme {
--*: initial;
}
@variant is-macos (&.macos);
`

let designSystem = await designSystems.get(__dirname).get(input)
let migrated = migrateArbitraryVariants(designSystem, {}, candidate)
expect(migrated).toEqual(result)
})

test('custom @media-based variants', async () => {
let candidate = '[@media(prefers-reduced-transparency:reduce)]:flex'
let result = 'transparency-safe:flex'
let input = css`
@import 'tailwindcss';
@theme {
--*: initial;
}
@variant transparency-safe {
@media (prefers-reduced-transparency: reduce) {
@slot;
}
}
`

let designSystem = await designSystems.get(__dirname).get(input)
let migrated = migrateArbitraryVariants(designSystem, {}, candidate)
expect(migrated).toEqual(result)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import type { Candidate, Variant } from '../../../../tailwindcss/src/candidate'
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
import { DefaultMap } from '../../../../tailwindcss/src/utils/default-map'
import { memcpy } from '../../utils/memcpy'
import type { Writable } from '../../utils/types'
import { printCandidate, printVariant } from './candidates'
import { computeVariantSignature } from './signatures'

const variantsLookup = new DefaultMap<DesignSystem, DefaultMap<string, string[]>>(
(designSystem) => {
let signatures = computeVariantSignature.get(designSystem)
let lookup = new DefaultMap<string, string[]>(() => [])

// Actual static variants
for (let [root, variant] of designSystem.variants.entries()) {
if (variant.kind === 'static') {
let signature = signatures.get(root)
if (signature === null) continue
lookup.get(signature).push(root)
}
}

return lookup
},
)

export function migrateArbitraryVariants(
designSystem: DesignSystem,
_userConfig: Config | null,
rawCandidate: string,
): string {
for (let readonlyCandidate of designSystem.parseCandidate(rawCandidate)) {
// We are only interested in the variants
if (readonlyCandidate.variants.length <= 0) return rawCandidate

// The below logic makes use of mutation. Since candidates in the
// DesignSystem are cached, we can't mutate them directly.
let candidate = structuredClone(readonlyCandidate) as Writable<typeof readonlyCandidate>

for (let variant of variants(candidate)) {
if (variant.kind === 'compound') continue

let targetString = printVariant(variant)
let targetSignature = computeVariantSignature.get(designSystem).get(targetString)
if (!targetSignature) continue

let foundVariants = variantsLookup.get(designSystem).get(targetSignature)
if (foundVariants.length !== 1) continue

let foundVariant = foundVariants[0]
let parsedVariant = designSystem.parseVariant(foundVariant)
if (parsedVariant === null) continue

memcpy(variant, parsedVariant)
}

return printCandidate(designSystem, candidate)
}

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)
}
}