(
rawCandidates: Iterable<string>,
designSystem: DesignSystem,
{
onInvalidCandidate,
respectImportant,
}: { onInvalidCandidate?: (candidate: string) => void; respectImportant?: boolean } = {},
)
| 9 | import { walk, WalkAction } from './walk' |
| 10 | |
| 11 | export function compileCandidates( |
| 12 | rawCandidates: Iterable<string>, |
| 13 | designSystem: DesignSystem, |
| 14 | { |
| 15 | onInvalidCandidate, |
| 16 | respectImportant, |
| 17 | }: { onInvalidCandidate?: (candidate: string) => void; respectImportant?: boolean } = {}, |
| 18 | ) { |
| 19 | let nodeSorting = new Map< |
| 20 | AstNode, |
| 21 | { properties: { order: number[]; count: number }; variants: bigint; candidate: string } |
| 22 | >() |
| 23 | let astNodes: AstNode[] = [] |
| 24 | let matches = new Map<string, Candidate[]>() |
| 25 | |
| 26 | // Parse candidates and variants |
| 27 | for (let rawCandidate of rawCandidates) { |
| 28 | if (designSystem.invalidCandidates.has(rawCandidate)) { |
| 29 | onInvalidCandidate?.(rawCandidate) |
| 30 | continue // Bail, invalid candidate |
| 31 | } |
| 32 | |
| 33 | let candidates = designSystem.parseCandidate(rawCandidate) |
| 34 | if (candidates.length === 0) { |
| 35 | onInvalidCandidate?.(rawCandidate) |
| 36 | continue // Bail, invalid candidate |
| 37 | } |
| 38 | |
| 39 | matches.set(rawCandidate, candidates) |
| 40 | } |
| 41 | |
| 42 | let flags = CompileAstFlags.None |
| 43 | |
| 44 | if (respectImportant ?? true) { |
| 45 | flags |= CompileAstFlags.RespectImportant |
| 46 | } |
| 47 | |
| 48 | let variantOrderMap = designSystem.getVariantOrder() |
| 49 | |
| 50 | // Create the AST |
| 51 | for (let [rawCandidate, candidates] of matches) { |
| 52 | let found = false |
| 53 | |
| 54 | for (let candidate of candidates) { |
| 55 | let rules = designSystem.compileAstNodes(candidate, flags) |
| 56 | if (rules.length === 0) continue |
| 57 | |
| 58 | found = true |
| 59 | |
| 60 | for (let { node, propertySort } of rules) { |
| 61 | // Track the variant order which is a number with each bit representing a |
| 62 | // variant. This allows us to sort the rules based on the order of |
| 63 | // variants used. |
| 64 | let variantOrder = 0n |
| 65 | for (let variant of candidate.variants) { |
| 66 | variantOrder |= 1n << BigInt(variantOrderMap.get(variant)!) |
| 67 | } |
| 68 |
no test coverage detected