Sha256: 0ce36e2399831c16fc20119bf7e00d6c675adfe1e675cfadad984b7f58720b03

Contents?: true

Size: 1.96 KB

Versions: 23

Compression:

Stored size: 1.96 KB

Contents

'use strict';

const isLogicalCombination = require('../../utils/isLogicalCombination');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const parseSelector = require('../../utils/parseSelector');
const report = require('../../utils/report');
const resolvedNestedSelector = require('postcss-resolve-nested-selector');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');

const ruleName = 'selector-max-class';

const messages = ruleMessages(ruleName, {
	expected: (selector, max) =>
		`Expected "${selector}" to have no more than ${max} ${max === 1 ? 'class' : 'classes'}`,
});

function rule(max) {
	return (root, result) => {
		const validOptions = validateOptions(result, ruleName, {
			actual: max,
			possible: [
				function (max) {
					return typeof max === 'number' && max >= 0;
				},
			],
		});

		if (!validOptions) {
			return;
		}

		function checkSelector(selectorNode, ruleNode) {
			const count = selectorNode.reduce((total, childNode) => {
				// Only traverse inside actual selectors and logical combinations
				if (childNode.type === 'selector' || isLogicalCombination(childNode)) {
					checkSelector(childNode, ruleNode);
				}

				return (total += childNode.type === 'class' ? 1 : 0);
			}, 0);

			if (selectorNode.type !== 'root' && selectorNode.type !== 'pseudo' && count > max) {
				report({
					ruleName,
					result,
					node: ruleNode,
					message: messages.expected(selectorNode, max),
					word: selectorNode,
				});
			}
		}

		root.walkRules((ruleNode) => {
			if (!isStandardSyntaxRule(ruleNode)) {
				return;
			}

			ruleNode.selectors.forEach((selector) => {
				resolvedNestedSelector(selector, ruleNode).forEach((resolvedSelector) => {
					parseSelector(resolvedSelector, result, ruleNode, (container) =>
						checkSelector(container, ruleNode),
					);
				});
			});
		});
	};
}

rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
trusty-cms-5.0.7 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-5.0.6 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-5.0.5 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-5.0.4 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-5.0.3 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-5.0.2 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-5.0.1 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.3.5 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-5.0.0 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.3.4 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.3.3 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.3.2 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.3.1 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.3 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.2.3 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.2.2 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.2.1 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.2 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.1.9 node_modules/stylelint/lib/rules/selector-max-class/index.js
trusty-cms-4.1.8 node_modules/stylelint/lib/rules/selector-max-class/index.js