Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index.js « max-nesting-depth « rules « lib « stylelint « node_modules « assets - github.com/fourtyone11/origin-hugo-theme.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5f0624421265d6437b815526302e8ac20b2546e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';

const _ = require('lodash');
const hasBlock = require('../../utils/hasBlock');
const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
const optionsMatches = require('../../utils/optionsMatches');
const parser = require('postcss-selector-parser');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');

const ruleName = 'max-nesting-depth';

const messages = ruleMessages(ruleName, {
	expected: (depth) => `Expected nesting depth to be no more than ${depth}`,
});

function rule(max, options) {
	const isIgnoreAtRule = (node) =>
		node.type === 'atrule' && optionsMatches(options, 'ignoreAtRules', node.name);

	return (root, result) => {
		validateOptions(
			result,
			ruleName,
			{
				actual: max,
				possible: [_.isNumber],
			},
			{
				optional: true,
				actual: options,
				possible: {
					ignore: ['blockless-at-rules', 'pseudo-classes'],
					ignoreAtRules: [_.isString, _.isRegExp],
				},
			},
		);

		root.walkRules(checkStatement);
		root.walkAtRules(checkStatement);

		function checkStatement(statement) {
			if (isIgnoreAtRule(statement)) {
				return;
			}

			if (!hasBlock(statement)) {
				return;
			}

			if (statement.selector && !isStandardSyntaxRule(statement)) {
				return;
			}

			const depth = nestingDepth(statement);

			if (depth > max) {
				report({
					ruleName,
					result,
					node: statement,
					message: messages.expected(max),
				});
			}
		}
	};

	function nestingDepth(node, level = 0) {
		const parent = node.parent;

		if (isIgnoreAtRule(parent)) {
			return 0;
		}

		// The nesting depth level's computation has finished
		// when this function, recursively called, receives
		// a node that is not nested -- a direct child of the
		// root node
		if (parent.type === 'root' || (parent.type === 'atrule' && parent.parent.type === 'root')) {
			return level;
		}

		function containsPseudoClassesOnly(selector) {
			const normalized = parser().processSync(selector, { lossless: false });
			const selectors = normalized.split(',');

			return selectors.every((selector) => selector.startsWith('&:') && selector[2] !== ':');
		}

		if (
			(optionsMatches(options, 'ignore', 'blockless-at-rules') &&
				node.type === 'atrule' &&
				node.every((child) => child.type !== 'decl')) ||
			(optionsMatches(options, 'ignore', 'pseudo-classes') &&
				node.type === 'rule' &&
				containsPseudoClassesOnly(node.selector))
		) {
			return nestingDepth(parent, level);
		}

		// Unless any of the conditions above apply, we want to
		// add 1 to the nesting depth level and then check the parent,
		// continuing to add and move up the hierarchy
		// until we hit the root node
		return nestingDepth(parent, level + 1);
	}
}

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