|
| 1 | +import { ComponentValue, isCommentNode, isFunctionNode, isTokenNode, isWhitespaceNode } from '@csstools/css-parser-algorithms'; |
| 2 | +import { TokenType } from '@csstools/css-tokenizer'; |
| 3 | +import type { Node, Result } from 'postcss'; |
| 4 | +import { Token, TokenTransformOptions } from './data-formats/base/token'; |
| 5 | +import { parsedPluginOptions } from './options'; |
| 6 | +import { parseComponentValues } from './parse-component-values'; |
| 7 | + |
| 8 | +export function transform(tokens: Map<string, Token>, result: Result, postCSSNode: Node, source: string, opts: parsedPluginOptions) { |
| 9 | + const componentValues = parseComponentValues(source); |
| 10 | + |
| 11 | + let didChangeSomething = false; |
| 12 | + componentValues.forEach((componentValue, index) => { |
| 13 | + if (!('walk' in componentValue)) { |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + { |
| 18 | + const replacements = transformComponentValue(componentValue, tokens, result, postCSSNode, opts); |
| 19 | + if (replacements) { |
| 20 | + componentValues.splice(index, 1, ...replacements); |
| 21 | + didChangeSomething = true; |
| 22 | + return false; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + componentValue.walk((entry, nodeIndex) => { |
| 27 | + if (typeof nodeIndex === 'string') { |
| 28 | + // Should never happen in FunctionNode |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const replacements = transformComponentValue(entry.node, tokens, result, postCSSNode, opts); |
| 33 | + if (replacements) { |
| 34 | + entry.parent.value.splice(nodeIndex, 1, ...replacements); |
| 35 | + didChangeSomething = true; |
| 36 | + return false; |
| 37 | + } |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + if (!didChangeSomething) { |
| 42 | + return source; |
| 43 | + } |
| 44 | + |
| 45 | + return componentValues.map((x) => x.toString()).join(''); |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +function transformComponentValue(node: ComponentValue, tokens: Map<string, Token>, result: Result, postCSSNode: Node, opts: parsedPluginOptions) { |
| 50 | + if (!isFunctionNode(node)) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (node.nameTokenValue().toLowerCase() !== opts.valueFunctionName) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + let tokenName = ''; |
| 59 | + let operator = ''; |
| 60 | + let operatorSubject = ''; |
| 61 | + |
| 62 | + for (let i = 0; i < node.value.length; i++) { |
| 63 | + const subValue = node.value[i]; |
| 64 | + if (isWhitespaceNode(subValue) || isCommentNode(subValue)) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + if ( |
| 69 | + !tokenName && |
| 70 | + isTokenNode(subValue) && |
| 71 | + subValue.value[0] === TokenType.String |
| 72 | + ) { |
| 73 | + tokenName = subValue.value[4].value; |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + if ( |
| 78 | + tokenName && |
| 79 | + !operator && |
| 80 | + isTokenNode(subValue) && |
| 81 | + subValue.value[0] === TokenType.Ident && |
| 82 | + subValue.value[4].value.toLowerCase() === 'to' |
| 83 | + ) { |
| 84 | + operator = 'to'; |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + if ( |
| 89 | + tokenName && |
| 90 | + operator && |
| 91 | + isTokenNode(subValue) && |
| 92 | + subValue.value[0] === TokenType.Ident |
| 93 | + ) { |
| 94 | + operatorSubject = subValue.value[4].value; |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | + if (!tokenName) { |
| 102 | + postCSSNode.warn(result, 'Expected at least a single string literal for the design-token function.'); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const replacement = tokens.get(tokenName); |
| 107 | + if (!replacement) { |
| 108 | + postCSSNode.warn(result, `design-token: "${tokenName}" is not configured.`); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if (!operator) { |
| 113 | + return parseComponentValues(replacement.cssValue()); |
| 114 | + } |
| 115 | + |
| 116 | + const transformOptions: TokenTransformOptions = { |
| 117 | + pluginOptions: opts.unitsAndValues, |
| 118 | + }; |
| 119 | + |
| 120 | + if (operator === 'to') { |
| 121 | + if (!operatorSubject) { |
| 122 | + postCSSNode.warn(result, `Invalid or missing unit in "${node.toString()}"`); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + transformOptions.toUnit = operatorSubject; |
| 127 | + |
| 128 | + try { |
| 129 | + return parseComponentValues(replacement.cssValue(transformOptions)); |
| 130 | + } catch (err) { |
| 131 | + postCSSNode.warn(result, (err as Error).message); |
| 132 | + return; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments