-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
151 lines (113 loc) · 3.51 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
'use strict';
const valueParser = require('postcss-value-parser');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const getDeclarationValue = require('../../utils/getDeclarationValue');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const setDeclarationValue = require('../../utils/setDeclarationValue');
const validateOptions = require('../../utils/validateOptions');
const ruleName = 'hue-degree-notation';
const messages = ruleMessages(ruleName, {
expected: (unfixed, fixed) => `Expected "${unfixed}" to be "${fixed}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/hue-degree-notation',
fixable: true,
};
const HUE_FIRST_ARG_FUNCS = ['hsl', 'hsla', 'hwb'];
const HUE_THIRD_ARG_FUNCS = ['lch'];
const HUE_FUNCS = new Set([...HUE_FIRST_ARG_FUNCS, ...HUE_THIRD_ARG_FUNCS]);
const HAS_HUE_COLOR_FUNC = new RegExp(`\\b(?:${[...HUE_FUNCS].join('|')})\\(`, 'i');
/** @type {import('stylelint').Rule} */
const rule = (primary, _secondaryOptions, context) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, {
actual: primary,
possible: ['angle', 'number'],
});
if (!validOptions) return;
root.walkDecls((decl) => {
if (!HAS_HUE_COLOR_FUNC.test(decl.value)) return;
let needsFix = false;
const parsedValue = valueParser(getDeclarationValue(decl));
parsedValue.walk((node) => {
if (node.type !== 'function') return;
if (!HUE_FUNCS.has(node.value.toLowerCase())) return;
const hue = findHue(node);
if (!hue) return;
const { value } = hue;
if (!isStandardSyntaxValue(value)) return;
if (!isDegree(value) && !isNumber(value)) return;
if (primary === 'angle' && isDegree(value)) return;
if (primary === 'number' && isNumber(value)) return;
const fixed = primary === 'angle' ? asDegree(value) : asNumber(value);
const unfixed = value;
if (context.fix) {
hue.value = fixed;
needsFix = true;
return;
}
const valueIndex = declarationValueIndex(decl);
report({
message: messages.expected,
messageArgs: [unfixed, fixed],
node: decl,
index: valueIndex + hue.sourceIndex,
endIndex: valueIndex + hue.sourceEndIndex,
result,
ruleName,
});
});
if (needsFix) {
setDeclarationValue(decl, parsedValue.toString());
}
});
};
};
/**
* @param {string} value
*/
function asDegree(value) {
return `${value}deg`;
}
/**
* @param {string} value
*/
function asNumber(value) {
const dimension = valueParser.unit(value);
if (dimension) return dimension.number;
throw new TypeError(`The "${value}" value must have a unit`);
}
/**
* @param {import('postcss-value-parser').FunctionNode} node
*/
function findHue(node) {
const args = node.nodes.filter(({ type }) => type === 'word' || type === 'function');
const value = node.value.toLowerCase();
if (HUE_FIRST_ARG_FUNCS.includes(value)) {
return args[0];
}
if (HUE_THIRD_ARG_FUNCS.includes(value)) {
return args[2];
}
return undefined;
}
/**
* @param {string} value
*/
function isDegree(value) {
const dimension = valueParser.unit(value);
return dimension && dimension.unit.toLowerCase() === 'deg';
}
/**
* @param {string} value
*/
function isNumber(value) {
const dimension = valueParser.unit(value);
return dimension && dimension.unit === '';
}
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;