(ast: AstNode[], designSystem: DesignSystem)
| 1257 | } |
| 1258 | |
| 1259 | export function substituteAtVariant(ast: AstNode[], designSystem: DesignSystem): Features { |
| 1260 | let features = Features.None |
| 1261 | walk(ast, (variantNode) => { |
| 1262 | if (variantNode.kind !== 'at-rule' || variantNode.name !== '@variant') return |
| 1263 | |
| 1264 | let nodes: AstNode[] = [] |
| 1265 | let compoundVariants = segment(variantNode.params, ',') |
| 1266 | for (let [idx, compoundVariant] of compoundVariants.entries()) { |
| 1267 | // Starting with the `&` rule node |
| 1268 | // |
| 1269 | // Only clone the nodes when we have multiple compound variants to deal |
| 1270 | // with. The last one can use the original nodes. We do need unique AST |
| 1271 | // nodes for sourcemap `dst` location information. |
| 1272 | let node = styleRule( |
| 1273 | '&', |
| 1274 | idx === compoundVariants.length - 1 |
| 1275 | ? variantNode.nodes |
| 1276 | : variantNode.nodes.map(cloneAstNode), |
| 1277 | ) |
| 1278 | |
| 1279 | let stackedVariants = segment(compoundVariant, ':') |
| 1280 | for (let i = stackedVariants.length - 1; i >= 0; --i) { |
| 1281 | let variant = stackedVariants[i].trim() |
| 1282 | |
| 1283 | if (!variant) { |
| 1284 | throw new Error(`Cannot use \`@variant\` with empty variant`) |
| 1285 | } |
| 1286 | |
| 1287 | let variantAst = designSystem.parseVariant(variant) |
| 1288 | if (variantAst === null) { |
| 1289 | throw new Error(`Cannot use \`@variant\` with unknown variant: ${variant}`) |
| 1290 | } |
| 1291 | |
| 1292 | let result = applyVariant(node, variantAst, designSystem.variants) |
| 1293 | if (result === null) { |
| 1294 | throw new Error(`Cannot use \`@variant\` with variant: ${variant}`) |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | nodes.push(node) |
| 1299 | } |
| 1300 | |
| 1301 | // Update the variant at-rule node, to be the `&` rule node |
| 1302 | features |= Features.Variants |
| 1303 | return WalkAction.Replace(nodes) |
| 1304 | }) |
| 1305 | return features |
| 1306 | } |
no test coverage detected