encapsulate invalidRawCandidates

This commit is contained in:
Robin Malfait 2024-03-08 12:13:23 +01:00
parent d7ec348412
commit 145df6ca53
No known key found for this signature in database
3 changed files with 13 additions and 6 deletions

View file

@ -24,7 +24,7 @@ export function compileCandidates(
if (throwOnInvalidCandidate) {
throw new Error(`Cannot apply unknown utility class: ${rawCandidate}`)
}
designSystem.invalidRawCandidates.add(rawCandidate)
designSystem.markInvalidCandidate(rawCandidate)
continue // Bail, invalid candidate
}
candidates.set(candidate, rawCandidate)
@ -42,7 +42,7 @@ export function compileCandidates(
if (throwOnInvalidCandidate) {
throw new Error(`Cannot apply unknown utility class: ${rawCandidate}`)
}
designSystem.invalidRawCandidates.add(rawCandidate)
designSystem.markInvalidCandidate(rawCandidate)
continue next
}

View file

@ -25,7 +25,8 @@ export type DesignSystem = {
getUsedVariants(): ReturnType<typeof parseVariant>[]
getAstNodeSize(): number
invalidRawCandidates: Set<string>
markInvalidCandidate(candidate: string): void
isInvalidCandidate(candidate: string): boolean
}
export function buildDesignSystem(theme: Theme): DesignSystem {
@ -41,7 +42,6 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
theme,
utilities,
variants,
invalidRawCandidates,
candidatesToCss(classes: string[]) {
let result: (string | null)[] = []
@ -83,6 +83,13 @@ export function buildDesignSystem(theme: Theme): DesignSystem {
getAstNodeSize() {
return compiledAstNodes.size
},
markInvalidCandidate(candidate: string) {
invalidRawCandidates.add(candidate)
},
isInvalidCandidate(candidate: string) {
return invalidRawCandidates.has(candidate)
},
}
return designSystem

View file

@ -179,7 +179,7 @@ export function compile(
// and should be ignored.
let allValidCandidates = new Set<string>()
for (let rawCandidate of rawCandidates) {
if (!designSystem.invalidRawCandidates.has(rawCandidate)) {
if (!designSystem.isInvalidCandidate(rawCandidate)) {
allValidCandidates.add(rawCandidate)
}
}
@ -190,7 +190,7 @@ export function compile(
// Add all new candidates unless we know that they are invalid.
let previousSize = allValidCandidates.size
for (let candidate of newRawCandidates) {
if (!designSystem.invalidRawCandidates.has(candidate)) {
if (!designSystem.isInvalidCandidate(candidate)) {
allValidCandidates.add(candidate)
}
}