Skip to content
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
ensure incomputable signatures result in unique value
  • Loading branch information
RobinMalfait committed Apr 30, 2025
commit 1f69c0fc69151cd5ee79c53a15bb817a4e2eee30
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const preComputedUtilities = new DefaultMap<DesignSystem, DefaultMap<string, str
// Actual static utilities
for (let root of ds.utilities.keys('static')) {
let signature = signatures.get(root)
if (signature === null) continue
if (typeof signature !== 'string') continue
lookup.get(signature).push(root)
}

Expand All @@ -36,12 +36,12 @@ const preComputedUtilities = new DefaultMap<DesignSystem, DefaultMap<string, str
for (let value of values) {
let candidateString = value === null ? root : `${root}-${value}`
let signature = signatures.get(candidateString)
if (signature !== null) lookup.get(signature).push(candidateString)
if (typeof signature === 'string') lookup.get(signature).push(candidateString)

if (supportsNegative) {
let negativeCandidateString = `-${candidateString}`
let signature = signatures.get(negativeCandidateString)
if (signature !== null) lookup.get(signature).push(negativeCandidateString)
if (typeof signature === 'string') lookup.get(signature).push(negativeCandidateString)
}
}

Expand All @@ -57,12 +57,12 @@ const preComputedUtilities = new DefaultMap<DesignSystem, DefaultMap<string, str
let candidateString =
value === null ? `${root}/${modifier}` : `${root}-${value}/${modifier}`
let signature = signatures.get(candidateString)
if (signature !== null) lookup.get(signature).push(candidateString)
if (typeof signature === 'string') lookup.get(signature).push(candidateString)

if (supportsNegative) {
let negativeCandidateString = `-${candidateString}`
let signature = signatures.get(negativeCandidateString)
if (signature !== null) lookup.get(signature).push(negativeCandidateString)
if (typeof signature === 'string') lookup.get(signature).push(negativeCandidateString)
}
}
}
Expand Down Expand Up @@ -142,7 +142,7 @@ export function migrateArbitraryUtilities(

// Compute the signature for the target candidate
let targetSignature = signatures.get(targetCandidateString)
if (targetSignature === null) continue
if (typeof targetSignature !== 'string') continue

// Try a few options to find a suitable replacement utility
for (let replacementCandidate of tryReplacements(targetSignature, targetCandidate)) {
Expand Down Expand Up @@ -200,7 +200,7 @@ export function migrateArbitraryUtilities(
let targetSignatureWithoutModifier = signatures.get(
printCandidate(designSystem, candidateWithoutModifier),
)
if (targetSignatureWithoutModifier !== null) {
if (typeof targetSignatureWithoutModifier === 'string') {
for (let replacementCandidate of tryReplacements(
targetSignatureWithoutModifier,
candidateWithoutModifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const variantsLookup = new DefaultMap<DesignSystem, DefaultMap<string, string[]>
for (let [root, variant] of designSystem.variants.entries()) {
if (variant.kind === 'static') {
let signature = signatures.get(root)
if (signature === null) continue
if (typeof signature !== 'string') continue
lookup.get(signature).push(root)
}
}
Expand All @@ -30,6 +30,9 @@ export function migrateArbitraryVariants(
_userConfig: Config | null,
rawCandidate: string,
): string {
let signatures = computeVariantSignature.get(designSystem)
let variants = variantsLookup.get(designSystem)

for (let readonlyCandidate of designSystem.parseCandidate(rawCandidate)) {
// We are only interested in the variants
if (readonlyCandidate.variants.length <= 0) return rawCandidate
Expand All @@ -42,10 +45,10 @@ export function migrateArbitraryVariants(
if (variant.kind === 'compound') continue

let targetString = printVariant(variant)
let targetSignature = computeVariantSignature.get(designSystem).get(targetString)
if (!targetSignature) continue
let targetSignature = signatures.get(targetString)
if (typeof targetSignature !== 'string') continue

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

let foundVariant = foundVariants[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
// Drop unnecessary modifiers
['bg-red-500/[100%]', 'bg-red-500'],
['bg-red-500/100', 'bg-red-500'],

// Keep modifiers on classes that don't _really_ exist
['group/name', 'group/name'],
])(testName, async (candidate, expected) => {
if (strategy === 'with-variant') {
candidate = `focus:${candidate}`
Expand Down
24 changes: 14 additions & 10 deletions packages/@tailwindcss-upgrade/src/codemods/template/signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { printArbitraryValue } from './candidates'

// Given a utility, compute a signature that represents the utility. The
// signature will be a normalised form of the generated CSS for the utility, or
// null if the utility is not valid. The class in the selector will be replaced
// with the `.x` selector.
// a unique symbol if the utility is not valid. The class in the selector will
// be replaced with the `.x` selector.
//
// This function should only be passed the base utility so `flex`, `hover:flex`
// and `focus:flex` will all use just `flex`. Variants are handled separately.
Expand All @@ -25,9 +25,9 @@ import { printArbitraryValue } from './candidates'
// These produce the same signature, therefore they represent the same utility.
export const computeUtilitySignature = new DefaultMap<
DesignSystem,
DefaultMap<string, string | null>
DefaultMap<string, string | Symbol>
>((designSystem) => {
return new DefaultMap<string, string | null>((utility) => {
return new DefaultMap<string, string | Symbol>((utility) => {
try {
// Ensure the prefix is added to the utility if it is not already present.
utility =
Expand Down Expand Up @@ -181,15 +181,17 @@ export const computeUtilitySignature = new DefaultMap<
let signature = toCss(ast)
return signature
} catch {
return null
// A unique symbol is returned to ensure that 2 signatures resulting in
// `null` are not considered equal.
return Symbol()
}
})
})

// Given a variant, compute a signature that represents the variant. The
// signature will be a normalised form of the generated CSS for the variant, or
// null if the variant is not valid. The class in the selector will be replaced
// with `.x`.
// a unique symbol if the variant is not valid. The class in the selector will
// be replaced with `.x`.
//
// E.g.:
//
Expand All @@ -201,9 +203,9 @@ export const computeUtilitySignature = new DefaultMap<
// These produce the same signature, therefore they represent the same variant.
export const computeVariantSignature = new DefaultMap<
DesignSystem,
DefaultMap<string, string | null>
DefaultMap<string, string | Symbol>
>((designSystem) => {
return new DefaultMap<string, string | null>((variant) => {
return new DefaultMap<string, string | Symbol>((variant) => {
try {
// Ensure the prefix is added to the utility if it is not already present.
variant =
Expand Down Expand Up @@ -238,7 +240,9 @@ export const computeVariantSignature = new DefaultMap<
let signature = toCss(ast)
return signature
} catch {
return null
// A unique symbol is returned to ensure that 2 signatures resulting in
// `null` are not considered equal.
return Symbol()
}
})
})
Expand Down