-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
137 lines (105 loc) · 3.71 KB
/
index.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
const {
isMediaQueryInvalid,
isMediaFeature,
isMediaFeatureValue,
} = require('@csstools/media-query-list-parser');
const atRuleParamIndex = require('../../utils/atRuleParamIndex');
const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
const optionsMatches = require('../../utils/optionsMatches');
const parseMediaQuery = require('../../utils/parseMediaQuery');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateObjectWithArrayProps = require('../../utils/validateObjectWithArrayProps');
const validateOptions = require('../../utils/validateOptions');
const vendor = require('../../utils/vendor');
const { isString, isRegExp } = require('../../utils/validateTypes');
const ruleName = 'media-feature-name-value-allowed-list';
const messages = ruleMessages(ruleName, {
rejected: (name, value) => `Unexpected value "${value}" for name "${name}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/media-feature-name-value-allowed-list',
};
/** @type {import('stylelint').Rule<Record<string, string | RegExp | Array<string | RegExp>>>} */
const rule = (primary) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: [validateObjectWithArrayProps(isString, isRegExp)],
});
if (!validOptions) {
return;
}
root.walkAtRules(/^media$/i, (atRule) => {
parseMediaQuery(atRule).forEach((mediaQuery) => {
if (isMediaQueryInvalid(mediaQuery)) return;
const initialState = {
mediaFeatureName: '',
unprefixedMediaFeatureName: '',
};
mediaQuery.walk(({ node, state }) => {
if (!state) return;
if (isMediaFeature(node)) {
state.mediaFeatureName = node.getName();
state.unprefixedMediaFeatureName = vendor.unprefixed(node.getName());
return;
}
if (!isMediaFeatureValue(node)) return;
const { mediaFeatureName, unprefixedMediaFeatureName } = state;
if (!mediaFeatureName || !unprefixedMediaFeatureName) return;
const componentValues = [node.value].flat();
const value = componentValues.map((x) => x.toString()).join('');
const allowedValuesKey = Object.keys(primary).find((featureName) =>
matchesStringOrRegExp(unprefixedMediaFeatureName, featureName),
);
if (allowedValuesKey == null) {
return;
}
if (optionsMatches(primary, allowedValuesKey, value)) {
return;
}
const atRuleIndex = atRuleParamIndex(atRule);
const [startIndex, endIndex] = startAndEndIndex(componentValues);
report({
index: atRuleIndex + startIndex,
endIndex: atRuleIndex + endIndex + 1,
message: messages.rejected,
messageArgs: [mediaFeatureName, value],
node: atRule,
ruleName,
result,
});
}, initialState);
});
});
};
};
/**
* A recursive function that returns the start and end boundaries of a node.
*
* @template {import('@csstools/css-tokenizer').CSSToken} T
* @param {Array<{ tokens(): Array<T> }> | { tokens(): Array<T> }} node
* @returns {[number, number]}
*/
function startAndEndIndex(node) {
if (Array.isArray(node)) {
const nodeStart = node[0];
if (!nodeStart) return [0, 0];
const nodeEnd = node[node.length - 1] || nodeStart;
const [startA] = startAndEndIndex(nodeStart);
const [, endB] = startAndEndIndex(nodeEnd);
return [startA, endB];
}
const tokens = node.tokens();
const firstToken = tokens[0];
const lastToken = tokens[tokens.length - 1];
if (!firstToken || !lastToken) return [0, 0];
const start = firstToken[2];
const end = lastToken[3];
return [start, end];
}
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;