UNPKG

4.62 kBJavaScriptView Raw
1'use strict';
2
3const atRuleParamIndex = require('../../utils/atRuleParamIndex');
4const declarationValueIndex = require('../../utils/declarationValueIndex');
5const getUnitFromValueNode = require('../../utils/getUnitFromValueNode');
6const mediaParser = require('postcss-media-query-parser').default;
7const optionsMatches = require('../../utils/optionsMatches');
8const report = require('../../utils/report');
9const ruleMessages = require('../../utils/ruleMessages');
10const validateObjectWithArrayProps = require('../../utils/validateObjectWithArrayProps');
11const validateOptions = require('../../utils/validateOptions');
12const valueParser = require('postcss-value-parser');
13const { isRegExp, isString } = require('../../utils/validateTypes');
14
15const ruleName = 'unit-disallowed-list';
16
17const messages = ruleMessages(ruleName, {
18 rejected: (unit) => `Unexpected unit "${unit}"`,
19});
20
21const meta = {
22 url: 'https://stylelint.io/user-guide/rules/list/unit-disallowed-list',
23};
24
25/**
26 * a function to retrieve only the media feature name
27 * could be externalized in an utils function if needed in other code
28 *
29 * @param {import('postcss-media-query-parser').Child} mediaFeatureNode
30 * @returns {string | undefined}
31 */
32const getMediaFeatureName = (mediaFeatureNode) => {
33 const value = mediaFeatureNode.value.toLowerCase();
34
35 const match = /((?:-?\w*)*)/.exec(value);
36
37 return match ? match[1] : undefined;
38};
39
40/** @type {import('stylelint').Rule} */
41const rule = (primary, secondaryOptions) => {
42 const list = [primary].flat();
43
44 return (root, result) => {
45 const validOptions = validateOptions(
46 result,
47 ruleName,
48 {
49 actual: list,
50 possible: [isString],
51 },
52 {
53 optional: true,
54 actual: secondaryOptions,
55 possible: {
56 ignoreProperties: [validateObjectWithArrayProps([isString, isRegExp])],
57 ignoreMediaFeatureNames: [validateObjectWithArrayProps([isString, isRegExp])],
58 },
59 },
60 );
61
62 if (!validOptions) {
63 return;
64 }
65
66 /**
67 * @param {import('postcss').Node} node
68 * @param {number} nodeIndex
69 * @param {import('postcss-value-parser').Node} valueNode
70 * @param {string | undefined} input
71 * @param {Record<string, unknown>} options
72 * @returns {void}
73 */
74 function check(node, nodeIndex, valueNode, input, options) {
75 const unit = getUnitFromValueNode(valueNode);
76
77 // There is not unit or it is not configured as a problem
78 if (!unit || (unit && !list.includes(unit.toLowerCase()))) {
79 return;
80 }
81
82 // The unit has an ignore option for the specific input
83 if (optionsMatches(options, unit.toLowerCase(), input)) {
84 return;
85 }
86
87 report({
88 index: nodeIndex + valueNode.sourceIndex,
89 message: messages.rejected(unit),
90 node,
91 result,
92 ruleName,
93 });
94 }
95
96 /**
97 * @template {import('postcss').AtRule} T
98 * @param {T} node
99 * @param {string} value
100 * @param {(node: T) => number} getIndex
101 * @returns {void}
102 */
103 function checkMedia(node, value, getIndex) {
104 mediaParser(node.params).walk(/^media-feature$/i, (mediaFeatureNode) => {
105 const mediaName = getMediaFeatureName(mediaFeatureNode);
106 const parentValue = mediaFeatureNode.parent.value;
107
108 valueParser(value).walk((valueNode) => {
109 // Ignore all non-word valueNode and
110 // the values not included in the parentValue string
111 if (valueNode.type !== 'word' || !parentValue.includes(valueNode.value)) {
112 return;
113 }
114
115 check(
116 node,
117 getIndex(node),
118 valueNode,
119 mediaName,
120 secondaryOptions ? secondaryOptions.ignoreMediaFeatureNames : {},
121 );
122 });
123 });
124 }
125
126 /**
127 * @template {import('postcss').Declaration} T
128 * @param {T} node
129 * @param {string} value
130 * @param {(node: T) => number} getIndex
131 * @returns {void}
132 */
133 function checkDecl(node, value, getIndex) {
134 // make sure multiplication operations (*) are divided - not handled
135 // by postcss-value-parser
136 value = value.replace(/\*/g, ',');
137
138 valueParser(value).walk((valueNode) => {
139 // Ignore wrong units within `url` function
140 if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
141 return false;
142 }
143
144 check(
145 node,
146 getIndex(node),
147 valueNode,
148 node.prop,
149 secondaryOptions ? secondaryOptions.ignoreProperties : {},
150 );
151 });
152 }
153
154 root.walkAtRules(/^media$/i, (atRule) => checkMedia(atRule, atRule.params, atRuleParamIndex));
155 root.walkDecls((decl) => checkDecl(decl, decl.value, declarationValueIndex));
156 };
157};
158
159rule.primaryOptionArray = true;
160
161rule.ruleName = ruleName;
162rule.messages = messages;
163rule.meta = meta;
164module.exports = rule;