Skip to content

Commit c67c0c5

Browse files
authored
Canonicalize dimensions (#19101)
1 parent 496a1e9 commit c67c0c5

File tree

10 files changed

+250
-140
lines changed

10 files changed

+250
-140
lines changed

packages/@tailwindcss-upgrade/src/codemods/template/migrate-arbitrary-variants.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { cloneCandidate } from '../../../../tailwindcss/src/candidate'
2+
import { createSignatureOptions } from '../../../../tailwindcss/src/canonicalize-candidates'
23
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
34
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
45
import {
@@ -14,8 +15,9 @@ export function migrateArbitraryVariants(
1415
_userConfig: Config | null,
1516
rawCandidate: string,
1617
): string {
17-
let signatures = computeVariantSignature.get(designSystem)
18-
let variants = preComputedVariants.get(designSystem)
18+
let signatureOptions = createSignatureOptions(designSystem)
19+
let signatures = computeVariantSignature.get(signatureOptions)
20+
let variants = preComputedVariants.get(signatureOptions)
1921

2022
for (let readonlyCandidate of designSystem.parseCandidate(rawCandidate)) {
2123
// We are only interested in the variants

packages/@tailwindcss-upgrade/src/codemods/template/migrate-modernize-arbitrary-values.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function migrate(designSystem: DesignSystem, userConfig: UserConfig | null, rawC
1717
migratePrefix,
1818
migrateModernizeArbitraryValues,
1919
migrateArbitraryVariants,
20-
(designSystem: DesignSystem, _, rawCandidate: string) => {
20+
(designSystem: DesignSystem, _: UserConfig | null, rawCandidate: string) => {
2121
return designSystem.canonicalizeCandidates([rawCandidate]).pop() ?? rawCandidate
2222
},
2323
]) {

packages/@tailwindcss-upgrade/src/codemods/template/migrate.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'node:fs/promises'
22
import path, { extname } from 'node:path'
3+
import { createSignatureOptions } from '../../../../tailwindcss/src/canonicalize-candidates'
34
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
45
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
56
import { computeUtilitySignature } from '../../../../tailwindcss/src/signatures'
@@ -39,11 +40,10 @@ export const DEFAULT_MIGRATIONS: Migration[] = [
3940
migrateModernizeArbitraryValues,
4041
]
4142

42-
let migrateCached = new DefaultMap<
43-
DesignSystem,
44-
DefaultMap<Config | null, DefaultMap<string, Promise<string>>>
45-
>((designSystem) => {
46-
return new DefaultMap((userConfig) => {
43+
let migrateCached = new DefaultMap((designSystem: DesignSystem) => {
44+
let options = createSignatureOptions(designSystem)
45+
46+
return new DefaultMap((userConfig: Config | null) => {
4747
return new DefaultMap(async (rawCandidate) => {
4848
let original = rawCandidate
4949

@@ -57,7 +57,7 @@ let migrateCached = new DefaultMap<
5757
// Verify that the candidate actually makes sense at all. E.g.: `duration`
5858
// is not a valid candidate, but it will parse because `duration-<number>`
5959
// exists.
60-
let signature = computeUtilitySignature.get(designSystem).get(rawCandidate)
60+
let signature = computeUtilitySignature.get(options).get(rawCandidate)
6161
if (typeof signature !== 'string') return original
6262

6363
return rawCandidate

packages/tailwindcss/src/canonicalize-candidates.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
import { describe, expect, test } from 'vitest'
44
import { __unstable__loadDesignSystem } from '.'
5+
import type { CanonicalizeOptions } from './intellisense'
56
import { DefaultMap } from './utils/default-map'
67

78
const css = String.raw
@@ -63,7 +64,12 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
6364
return candidate
6465
}
6566

66-
async function expectCanonicalization(input: string, candidate: string, expected: string) {
67+
async function expectCanonicalization(
68+
input: string,
69+
candidate: string,
70+
expected: string,
71+
options?: CanonicalizeOptions,
72+
) {
6773
candidate = prepare(candidate)
6874
expected = prepare(expected)
6975

@@ -72,7 +78,7 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
7278
}
7379

7480
let designSystem = await designSystems.get(__dirname).get(input)
75-
let [actual] = designSystem.canonicalizeCandidates([candidate])
81+
let [actual] = designSystem.canonicalizeCandidates([candidate], options)
7682

7783
try {
7884
expect(actual).toBe(expected)
@@ -937,3 +943,22 @@ describe('theme to var', () => {
937943
])
938944
})
939945
})
946+
947+
describe('options', () => {
948+
test('normalize `rem` units to `px`', async () => {
949+
let designSystem = await __unstable__loadDesignSystem(
950+
css`
951+
@tailwind utilities;
952+
@theme {
953+
--spacing: 0.25rem;
954+
}
955+
`,
956+
{ base: __dirname },
957+
)
958+
959+
expect(designSystem.canonicalizeCandidates(['m-[16px]'])).toEqual(['m-[16px]'])
960+
expect(designSystem.canonicalizeCandidates(['m-[16px]'], { rem: 16 })).toEqual(['m-4'])
961+
expect(designSystem.canonicalizeCandidates(['m-[16px]'], { rem: 64 })).toEqual(['m-1'])
962+
expect(designSystem.canonicalizeCandidates(['m-[16px]'])).toEqual(['m-[16px]']) // Ensure options don't influence shared state
963+
})
964+
})

0 commit comments

Comments
 (0)