UNPKG

stylelint

Version:

A mighty CSS linter that helps you avoid errors and enforce conventions.

51 lines (42 loc) • 1.49 kB
import { atRuleRegexes, descriptorRegexes } from './regexes.mjs'; import getCustomPropertyNames from './getCustomPropertyNames.mjs'; import getDeclarationValue from './getDeclarationValue.mjs'; import getOrInsertComputed from './getOrInsertComputed.mjs'; import { isAtRule } from './typeGuards.mjs'; import isCustomProperty from './isCustomProperty.mjs'; /** @import { Declaration } from 'postcss' */ /** * Collect the values of each custom property in the given roots, in source order, including * the initial values of custom properties defined using `@property` * * @param {import('postcss').Root[]} roots * @returns {Map<string, string[]>} */ export default function getCustomPropertyValues(roots) { /** @type {Map<string, string[]>} */ const valuesByName = new Map(); for (const root of roots) { root.walkDecls((decl) => { const names = isInitialValueDescriptor(decl) ? getCustomPropertyNames(decl.parent) : [decl.prop]; for (const name of names) { if (!isCustomProperty(name)) continue; const values = getOrInsertComputed(valuesByName, name, () => []); values.push(getDeclarationValue(decl)); } }); } return valuesByName; } /** * @param {Declaration} decl * @returns {decl is Declaration & { parent: import('postcss').AtRule }} */ function isInitialValueDescriptor(decl) { return ( isAtRule(decl.parent) && atRuleRegexes.propertyName.test(decl.parent.name) && descriptorRegexes.initialValueName.test(decl.prop) ); }