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
use migrate arbitrary value to bare value using signatures
This will now make use of the signatures system so that we can verify
that a migration is 100% successful.
  • Loading branch information
RobinMalfait committed Apr 30, 2025
commit 7f2384166490f56d0511fb9fe076e2ca7e5ede83
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test.each([
'data-[selected]:aria-[selected="true"]:aspect-[12/34]',
'data-selected:aria-selected:aspect-12/34',
],
])('%s => %s', async (candidate, result) => {
])('%s => %s (%#)', async (candidate, result) => {
let designSystem = await __unstable__loadDesignSystem('@import "tailwindcss";', {
base: __dirname,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,126 +1,45 @@
import { parseCandidate, type Candidate, type Variant } from '../../../../tailwindcss/src/candidate'
import {
parseCandidate,
type Candidate,
type NamedUtilityValue,
type Variant,
} from '../../../../tailwindcss/src/candidate'
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
import { isPositiveInteger } from '../../../../tailwindcss/src/utils/infer-data-type'
import {
isPositiveInteger,
isValidSpacingMultiplier,
} from '../../../../tailwindcss/src/utils/infer-data-type'
import { segment } from '../../../../tailwindcss/src/utils/segment'
import { printCandidate } from './candidates'
import { computeUtilitySignature } from './signatures'

export function migrateArbitraryValueToBareValue(
designSystem: DesignSystem,
_userConfig: Config | null,
rawCandidate: string,
): string {
let signatures = computeUtilitySignature.get(designSystem)

for (let candidate of parseCandidate(rawCandidate, designSystem)) {
let clone = structuredClone(candidate)
let changed = false

// Convert [subgrid] to subgrid
if (
clone.kind === 'functional' &&
clone.value?.kind === 'arbitrary' &&
clone.value.value === 'subgrid' &&
(clone.root === 'grid-cols' || clone.root == 'grid-rows')
) {
changed = true
clone.value = {
kind: 'named',
value: 'subgrid',
fraction: null,
}
}

// Convert utilities that accept bare values ending in %
if (
clone.kind === 'functional' &&
clone.value?.kind === 'arbitrary' &&
clone.value.dataType === null &&
(clone.root === 'from' ||
clone.root === 'via' ||
clone.root === 'to' ||
clone.root === 'font-stretch')
) {
if (clone.value.value.endsWith('%') && isPositiveInteger(clone.value.value.slice(0, -1))) {
let percentage = parseInt(clone.value.value)
if (
clone.root === 'from' ||
clone.root === 'via' ||
clone.root === 'to' ||
(clone.root === 'font-stretch' && percentage >= 50 && percentage <= 200)
) {
changed = true
clone.value = {
kind: 'named',
value: clone.value.value,
fraction: null,
// Migrate arbitrary values to bare values
if (clone.kind === 'functional' && clone.value?.kind === 'arbitrary') {
let expectedSignature = signatures.get(rawCandidate)
if (expectedSignature !== null) {
for (let value of tryValueReplacements(clone)) {
let newSignature = signatures.get(printCandidate(designSystem, { ...clone, value }))
if (newSignature === expectedSignature) {
changed = true
clone.value = value
break
}
}
}
}

// Convert arbitrary values with positive integers to bare values
// Convert arbitrary values with fractions to bare values
else if (
clone.kind === 'functional' &&
clone.value?.kind === 'arbitrary' &&
clone.value.dataType === null
) {
if (clone.root === 'leading') {
// leading-[1] -> leading-none
if (clone.value.value === '1') {
changed = true
clone.value = {
kind: 'named',
value: 'none',
fraction: null,
}
}

// Keep leading-[<number>] as leading-[<number>]
else {
continue
}
}

let parts = segment(clone.value.value, '/')
if (parts.every((part) => isPositiveInteger(part))) {
changed = true

let currentValue = clone.value
let currentModifier = clone.modifier

// E.g.: `col-start-[12]`
// ^^
if (parts.length === 1) {
clone.value = {
kind: 'named',
value: clone.value.value,
fraction: null,
}
}

// E.g.: `aspect-[12/34]`
// ^^ ^^
else {
clone.value = {
kind: 'named',
value: parts[0],
fraction: clone.value.value,
}
clone.modifier = {
kind: 'named',
value: parts[1],
}
}

// Double check that the new value compiles correctly
if (designSystem.compileAstNodes(clone).length === 0) {
clone.value = currentValue
clone.modifier = currentModifier
changed = false
}
}
}

for (let variant of variants(clone)) {
// Convert `data-[selected]` to `data-selected`
if (
Expand Down Expand Up @@ -201,3 +120,75 @@ function* variants(candidate: Candidate) {
yield* inner(variant)
}
}

// Convert functional utilities with arbitrary values to bare values if we can.
// We know that bare values can only be:
//
// 1. A number (with increments of .25)
// 2. A percentage (with increments of .25 followed by a `%`)
// 3. A ratio with whole numbers
//
// Not a bare value per se, but if we are dealing with a keyword, that could
// potentially also look like a bare value (aka no `[` or `]`). E.g.:
// ```diff
// grid-cols-[subgrid]
// grid-cols-subgrid
// ```
function* tryValueReplacements(
candidate: Extract<Candidate, { kind: 'functional' }>,
value: string = candidate.value?.value ?? '',
seen: Set<string> = new Set(),
): Generator<NamedUtilityValue> {
if (seen.has(value)) return
seen.add(value)

// 0. Just try to drop the square brackets and see if it works
// 1. A number (with increments of .25)
yield {
kind: 'named',
value,
fraction: null,
}

// 2. A percentage (with increments of .25 followed by a `%`)
// Try to drop the `%` and see if it works
if (value.endsWith('%') && isValidSpacingMultiplier(value.slice(0, -1))) {
yield {
kind: 'named',
value: value.slice(0, -1),
fraction: null,
}
}

// 3. A ratio with whole numbers
if (value.includes('/')) {
let [numerator, denominator] = value.split('/')
if (isPositiveInteger(numerator) && isPositiveInteger(denominator)) {
yield {
kind: 'named',
value: numerator,
fraction: `${numerator}/${denominator}`,
}
}
}

// It could also be that we have `20px`, we can try just `20` and see if it
// results in the same signature.
let allNumbersAndFractions = new Set<string>()

// Figure out all numbers and fractions in the value
for (let match of value.matchAll(/(\d+\/\d+)|(\d+\.?\d+)/g)) {
allNumbersAndFractions.add(match[0].trim())
}

// Sort the numbers and fractions where the smallest length comes first. This
// will result in the smallest replacement.
let options = Array.from(allNumbersAndFractions).sort((a, z) => {
return a.length - z.length
})

// Try all the options
for (let option of options) {
yield* tryValueReplacements(candidate, option, seen)
}
}