/
index.js
211 lines (174 loc) · 5.33 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
const valueParser = require('postcss-value-parser');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const isNumbery = require('../../utils/isNumbery');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const isVariable = require('../../utils/isVariable');
const keywordSets = require('../../reference/keywordSets');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const { isAtRule } = require('../../utils/typeGuards');
const ruleName = 'font-weight-notation';
const messages = ruleMessages(ruleName, {
expected: (type) => `Expected ${type} font-weight notation`,
invalidNamed: (name) => `Unexpected invalid font-weight name "${name}"`,
});
const meta = {
url: 'https://stylelint.io/user-guide/rules/list/font-weight-notation',
};
const INHERIT_KEYWORD = 'inherit';
const INITIAL_KEYWORD = 'initial';
const NORMAL_KEYWORD = 'normal';
const WEIGHTS_WITH_KEYWORD_EQUIVALENTS = new Set(['400', '700']);
/** @type {import('stylelint').Rule} */
const rule = (primary, secondaryOptions) => {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: primary,
possible: ['numeric', 'named-where-possible'],
},
{
actual: secondaryOptions,
possible: {
ignore: ['relative'],
},
optional: true,
},
);
if (!validOptions) {
return;
}
root.walkDecls(/^font(-weight)?$/i, (decl) => {
const prop = decl.prop.toLowerCase();
if (prop === 'font-weight') {
checkWeight(decl, decl.value);
} else if (prop === 'font') {
checkFont(decl);
}
});
/**
* @param {import('postcss').Declaration} decl
*/
function checkFont(decl) {
const valueNodes = findFontWeights(decl.value);
// We do not need to more carefully distinguish font-weight
// numbers from unitless line-heights because line-heights in
// `font` values need to be part of a font-size/line-height pair
const hasNumericFontWeight = valueNodes.some(({ value }) => isNumbery(value));
for (const valueNode of valueNodes) {
const value = valueNode.value;
const lowerValue = value.toLowerCase();
if (
(lowerValue === NORMAL_KEYWORD && !hasNumericFontWeight) ||
isNumbery(value) ||
(lowerValue !== NORMAL_KEYWORD && keywordSets.fontWeightKeywords.has(lowerValue))
) {
checkWeight(decl, value, valueNode);
return;
}
}
}
/**
* @param {import('postcss').Declaration} decl
* @param {string} weightValue
* @param {import('postcss-value-parser').Node} [weightValueNode]
*/
function checkWeight(decl, weightValue, weightValueNode) {
if (!isStandardSyntaxValue(weightValue)) {
return;
}
if (isVariable(weightValue)) {
return;
}
if (includesOnlyFunction(weightValue)) {
return;
}
const lowerWeightValue = weightValue.toLowerCase();
if (lowerWeightValue === INHERIT_KEYWORD || lowerWeightValue === INITIAL_KEYWORD) {
return;
}
if (
optionsMatches(secondaryOptions, 'ignore', 'relative') &&
keywordSets.fontWeightRelativeKeywords.has(lowerWeightValue)
) {
return;
}
if (primary === 'numeric') {
const parent = decl.parent;
if (parent && isAtRule(parent) && parent.name.toLowerCase() === 'font-face') {
// @font-face allows multiple values.
for (const valueNode of findFontWeights(weightValue)) {
if (!isNumbery(valueNode.value)) {
return complain(messages.expected('numeric'), valueNode);
}
}
return;
}
if (!isNumbery(weightValue)) {
return complain(messages.expected('numeric'), weightValueNode);
}
}
if (primary === 'named-where-possible') {
if (isNumbery(weightValue)) {
if (WEIGHTS_WITH_KEYWORD_EQUIVALENTS.has(weightValue)) {
complain(messages.expected('named'), weightValueNode);
}
return;
}
if (
!keywordSets.fontWeightKeywords.has(lowerWeightValue) &&
lowerWeightValue !== NORMAL_KEYWORD
) {
return complain(messages.invalidNamed(weightValue), weightValueNode);
}
}
/**
* @param {string} message
* @param {import('postcss-value-parser').Node} [valueNode]
*/
function complain(message, valueNode) {
const index = declarationValueIndex(decl) + (valueNode ? valueNode.sourceIndex : 0);
report({
ruleName,
result,
message,
node: decl,
index,
});
}
}
};
};
/**
* @param {string} value
* @returns {import('postcss-value-parser').Node[]}
*/
function findFontWeights(value) {
return valueParser(value).nodes.filter((node, index, nodes) => {
if (node.type !== 'word') return false;
// Exclude `<font-size>/<line-height>` format like `16px/3`.
const prevNode = nodes[index - 1];
const nextNode = nodes[index + 1];
if (prevNode && prevNode.type === 'div') return false;
if (nextNode && nextNode.type === 'div') return false;
return true;
});
}
/**
* @param {string} value
* @returns {boolean}
*/
function includesOnlyFunction(value) {
return valueParser(value).nodes.every(({ type }) => {
return type === 'function' || type === 'comment' || type === 'space';
});
}
rule.ruleName = ruleName;
rule.messages = messages;
rule.meta = meta;
module.exports = rule;