Skip to content

Commit 0260bbb

Browse files
committed
improve printing of candidates
1. Introduced a `printModifier`, so we can re-use it 2. For formatters that work on strings, we can cache the information instead of re-computing everything over and over again. This is useful due to the new migrations that happen to parse & print very often.
1 parent a1ca4bc commit 0260bbb

1 file changed

Lines changed: 38 additions & 23 deletions

File tree

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

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { Scanner } from '@tailwindcss/oxide'
2-
import type { Candidate, Variant } from '../../../../tailwindcss/src/candidate'
2+
import type {
3+
ArbitraryModifier,
4+
Candidate,
5+
NamedModifier,
6+
Variant,
7+
} from '../../../../tailwindcss/src/candidate'
38
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
9+
import { DefaultMap } from '../../../../tailwindcss/src/utils/default-map'
410
import * as ValueParser from '../../../../tailwindcss/src/value-parser'
511

612
export async function extractRawCandidates(
@@ -66,17 +72,7 @@ export function printCandidate(designSystem: DesignSystem, candidate: Candidate)
6672

6773
// Handle modifier
6874
if (candidate.kind === 'arbitrary' || candidate.kind === 'functional') {
69-
if (candidate.modifier) {
70-
let isVarValue = isVar(candidate.modifier.value)
71-
let value = isVarValue ? candidate.modifier.value.slice(4, -1) : candidate.modifier.value
72-
let [open, close] = isVarValue ? ['(', ')'] : ['[', ']']
73-
74-
if (candidate.modifier.kind === 'arbitrary') {
75-
base += `/${open}${printArbitraryValue(value)}${close}`
76-
} else if (candidate.modifier.kind === 'named') {
77-
base += `/${candidate.modifier.value}`
78-
}
79-
}
75+
base += printModifier(candidate.modifier)
8076
}
8177

8278
// Handle important
@@ -89,7 +85,23 @@ export function printCandidate(designSystem: DesignSystem, candidate: Candidate)
8985
return parts.join(':')
9086
}
9187

92-
function printVariant(variant: Variant) {
88+
export function printModifier(modifier: ArbitraryModifier | NamedModifier | null) {
89+
if (modifier === null) return ''
90+
91+
let isVarValue = isVar(modifier.value)
92+
let value = isVarValue ? modifier.value.slice(4, -1) : modifier.value
93+
let [open, close] = isVarValue ? ['(', ')'] : ['[', ']']
94+
95+
if (modifier.kind === 'arbitrary') {
96+
return `/${open}${printArbitraryValue(value)}${close}`
97+
} else if (modifier.kind === 'named') {
98+
return `/${modifier.value}`
99+
} else {
100+
modifier satisfies never
101+
}
102+
}
103+
104+
export function printVariant(variant: Variant) {
93105
// Handle static variants
94106
if (variant.kind === 'static') {
95107
return variant.root
@@ -130,19 +142,13 @@ function printVariant(variant: Variant) {
130142

131143
// Handle modifiers
132144
if (variant.kind === 'functional' || variant.kind === 'compound') {
133-
if (variant.modifier) {
134-
if (variant.modifier.kind === 'arbitrary') {
135-
base += `/[${printArbitraryValue(variant.modifier.value)}]`
136-
} else if (variant.modifier.kind === 'named') {
137-
base += `/${variant.modifier.value}`
138-
}
139-
}
145+
base += printModifier(variant.modifier)
140146
}
141147

142148
return base
143149
}
144150

145-
function printArbitraryValue(input: string) {
151+
const printArbitraryValueCache = new DefaultMap<string, string>((input) => {
146152
let ast = ValueParser.parse(input)
147153

148154
let drop = new Set<ValueParser.ValueAstNode>()
@@ -204,9 +210,12 @@ function printArbitraryValue(input: string) {
204210
recursivelyEscapeUnderscores(ast)
205211

206212
return ValueParser.toCss(ast)
213+
})
214+
export function printArbitraryValue(input: string) {
215+
return printArbitraryValueCache.get(input)
207216
}
208217

209-
function simplifyArbitraryVariant(input: string) {
218+
const simplifyArbitraryVariantCache = new DefaultMap<string, string>((input) => {
210219
let ast = ValueParser.parse(input)
211220

212221
// &:is(…)
@@ -226,6 +235,9 @@ function simplifyArbitraryVariant(input: string) {
226235
}
227236

228237
return input
238+
})
239+
function simplifyArbitraryVariant(input: string) {
240+
return simplifyArbitraryVariantCache.get(input)
229241
}
230242

231243
function recursivelyEscapeUnderscores(ast: ValueParser.ValueAstNode[]) {
@@ -272,9 +284,12 @@ function recursivelyEscapeUnderscores(ast: ValueParser.ValueAstNode[]) {
272284
}
273285
}
274286

275-
function isVar(value: string) {
287+
const isVarCache = new DefaultMap<string, boolean>((value) => {
276288
let ast = ValueParser.parse(value)
277289
return ast.length === 1 && ast[0].kind === 'function' && ast[0].value === 'var'
290+
})
291+
function isVar(value: string) {
292+
return isVarCache.get(value)
278293
}
279294

280295
function never(value: never): never {

0 commit comments

Comments
 (0)