UNPKG

stylelint

Version:

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

86 lines (71 loc) 2.73 kB
import flattenNestedSelectorsForRule from '../../utils/flattenNestedSelectorsForRule.mjs'; import isContextFunctionalPseudoClass from '../../utils/isContextFunctionalPseudoClass.mjs'; import isNonNegativeInteger from '../../utils/isNonNegativeInteger.mjs'; import isStandardSyntaxRule from '../../utils/isStandardSyntaxRule.mjs'; import isStandardSyntaxSelector from '../../utils/isStandardSyntaxSelector.mjs'; import report from '../../utils/report.mjs'; import ruleMessages from '../../utils/ruleMessages.mjs'; import validateOptions from '../../utils/validateOptions.mjs'; const ruleName = 'selector-max-class'; const messages = ruleMessages(ruleName, { expected: (selector, max) => `Expected "${selector}" to have no more than ${max} ${max === 1 ? 'class' : 'classes'}`, }); const meta = { url: 'https://stylelint.io/user-guide/rules/selector-max-class', }; /** @type {import('stylelint').Rule} */ const rule = (primary) => { return (root, result) => { const validOptions = validateOptions(result, ruleName, { actual: primary, possible: isNonNegativeInteger, }); if (!validOptions) { return; } /** * @param {import('postcss-selector-parser').Container<string | undefined>} resolvedSelectorNode * @param {import('postcss-selector-parser').Container<string | undefined>} selectorNode * @param {import('postcss').Rule} ruleNode */ function checkSelector(resolvedSelectorNode, selectorNode, ruleNode) { const count = resolvedSelectorNode.reduce((total, childNode) => { // Only traverse inside actual selectors and context functional pseudo-classes if (childNode.type === 'selector' || isContextFunctionalPseudoClass(childNode)) { checkSelector(childNode, selectorNode, ruleNode); } if (childNode.type === 'class') total += 1; return total; }, 0); if (selectorNode.type !== 'root' && selectorNode.type !== 'pseudo' && count > primary) { const index = selectorNode.first?.sourceIndex ?? 0; const selectorStr = selectorNode.toString().trim(); report({ ruleName, result, node: ruleNode, message: messages.expected, messageArgs: [selectorStr, primary], index, endIndex: index + selectorStr.length, }); } } root.walkRules((ruleNode) => { if (!isStandardSyntaxRule(ruleNode)) { return; } if (!isStandardSyntaxSelector(ruleNode.selector)) return; flattenNestedSelectorsForRule(ruleNode, result).forEach(({ selector, resolvedSelectors }) => { resolvedSelectors.forEach((resolvedSelector) => { checkSelector(resolvedSelector, selector, ruleNode); }); }); }); }; }; rule.ruleName = ruleName; rule.messages = messages; rule.meta = meta; export default rule;