/
index.js
172 lines (148 loc) · 4.78 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
'use strict';
const atRuleParamIndex = require('../../utils/atRuleParamIndex');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const getDimension = require('../../utils/getDimension');
const mediaParser = require('postcss-media-query-parser').default;
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateObjectWithArrayProps = require('../../utils/validateObjectWithArrayProps');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');
const { isRegExp, isString } = require('../../utils/validateTypes');
const ruleName = 'unit-disallowed-list';
const messages = ruleMessages(ruleName, {
rejected: (unit) => `Unexpected unit "${unit}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/unit-disallowed-list',
};
/**
* a function to retrieve only the media feature name
* could be externalized in an utils function if needed in other code
*
* @param {import('postcss-media-query-parser').Child} mediaFeatureNode
* @returns {string | undefined}
*/
const getMediaFeatureName = (mediaFeatureNode) => {
const value = mediaFeatureNode.value.toLowerCase();
const match = /((?:-?\w*)*)/.exec(value);
return match ? match[1] : undefined;
};
/** @type {import('stylelint').Rule<string | string[]>} */
const rule = (primary, secondaryOptions) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: primary,
possible: [isString],
},
{
optional: true,
actual: secondaryOptions,
possible: {
ignoreFunctions: [isString, isRegExp],
ignoreProperties: [validateObjectWithArrayProps(isString, isRegExp)],
ignoreMediaFeatureNames: [validateObjectWithArrayProps(isString, isRegExp)],
},
},
);
if (!validOptions) {
return;
}
const primaryValues = [primary].flat();
/**
* @param {import('postcss').Node} node
* @param {number} nodeIndex
* @param {import('postcss-value-parser').Node} valueNode
* @param {string | undefined} input
* @param {Record<string, unknown>} options
* @returns {void}
*/
function check(node, nodeIndex, valueNode, input, options) {
const { number, unit } = getDimension(valueNode);
// There is not unit or it is not configured as a problem
if (!unit || !number || (unit && !primaryValues.includes(unit.toLowerCase()))) {
return;
}
// The unit has an ignore option for the specific input
if (optionsMatches(options, unit.toLowerCase(), input)) {
return;
}
report({
index: nodeIndex + valueNode.sourceIndex + number.length,
endIndex: nodeIndex + valueNode.sourceEndIndex,
message: messages.rejected(unit),
node,
result,
ruleName,
});
}
/**
* @template {import('postcss').AtRule} T
* @param {T} node
* @param {string} value
* @param {(node: T) => number} getIndex
* @returns {void}
*/
function checkMedia(node, value, getIndex) {
mediaParser(node.params).walk(/^media-feature$/i, (mediaFeatureNode) => {
const mediaName = getMediaFeatureName(mediaFeatureNode);
const parentValue = mediaFeatureNode.parent.value;
valueParser(value).walk((valueNode) => {
// Ignore all non-word valueNode and
// the values not included in the parentValue string
if (valueNode.type !== 'word' || !parentValue.includes(valueNode.value)) {
return;
}
check(
node,
getIndex(node),
valueNode,
mediaName,
secondaryOptions ? secondaryOptions.ignoreMediaFeatureNames : {},
);
});
});
}
/**
* @template {import('postcss').Declaration} T
* @param {T} node
* @param {string} value
* @param {(node: T) => number} getIndex
* @returns {void}
*/
function checkDecl(node, value, getIndex) {
// make sure multiplication operations (*) are divided - not handled
// by postcss-value-parser
value = value.replace(/\*/g, ',');
valueParser(value).walk((valueNode) => {
const valueLowerCase = valueNode.value.toLowerCase();
// Ignore wrong units within `url` function
if (
valueNode.type === 'function' &&
(valueLowerCase === 'url' ||
optionsMatches(secondaryOptions, 'ignoreFunctions', valueLowerCase))
) {
return false;
}
check(
node,
getIndex(node),
valueNode,
node.prop,
secondaryOptions ? secondaryOptions.ignoreProperties : {},
);
});
}
root.walkAtRules(/^media$/i, (atRule) => checkMedia(atRule, atRule.params, atRuleParamIndex));
root.walkDecls((decl) => checkDecl(decl, decl.value, declarationValueIndex));
};
};
rule.primaryOptionArray = true;
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;