-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
154 lines (128 loc) · 3.8 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
// @ts-nocheck
'use strict';
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 postcss = require('postcss');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
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 INHERIT_KEYWORD = 'inherit';
const INITIAL_KEYWORD = 'initial';
const NORMAL_KEYWORD = 'normal';
const WEIGHTS_WITH_KEYWORD_EQUIVALENTS = new Set(['400', '700']);
function rule(expectation, options) {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: expectation,
possible: ['numeric', 'named-where-possible'],
},
{
actual: options,
possible: {
ignore: ['relative'],
},
optional: true,
},
);
if (!validOptions) {
return;
}
root.walkDecls((decl) => {
if (decl.prop.toLowerCase() === 'font-weight') {
checkWeight(decl.value, decl);
}
if (decl.prop.toLowerCase() === 'font') {
checkFont(decl);
}
});
function checkFont(decl) {
const valueList = postcss.list.space(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 = valueList.some(isNumbery);
for (const value of postcss.list.space(decl.value)) {
if (
(value.toLowerCase() === NORMAL_KEYWORD && !hasNumericFontWeight) ||
isNumbery(value) ||
(value.toLowerCase() !== NORMAL_KEYWORD &&
keywordSets.fontWeightKeywords.has(value.toLowerCase()))
) {
checkWeight(value, decl);
return;
}
}
}
function checkWeight(weightValue, decl) {
if (!isStandardSyntaxValue(weightValue)) {
return;
}
if (isVariable(weightValue)) {
return;
}
if (
weightValue.toLowerCase() === INHERIT_KEYWORD ||
weightValue.toLowerCase() === INITIAL_KEYWORD
) {
return;
}
if (
optionsMatches(options, 'ignore', 'relative') &&
keywordSets.fontWeightRelativeKeywords.has(weightValue.toLowerCase())
) {
return;
}
const weightValueOffset = decl.value.indexOf(weightValue);
if (expectation === 'numeric') {
if (decl.parent.type === 'atrule' && decl.parent.name.toLowerCase() === 'font-face') {
const weightValueNumbers = postcss.list.space(weightValue);
if (!weightValueNumbers.every(isNumbery)) {
return complain(messages.expected('numeric'));
}
return;
}
if (!isNumbery(weightValue)) {
return complain(messages.expected('numeric'));
}
}
if (expectation === 'named-where-possible') {
if (isNumbery(weightValue)) {
if (WEIGHTS_WITH_KEYWORD_EQUIVALENTS.has(weightValue)) {
complain(messages.expected('named'));
}
return;
}
if (
!keywordSets.fontWeightKeywords.has(weightValue.toLowerCase()) &&
weightValue.toLowerCase() !== NORMAL_KEYWORD
) {
return complain(messages.invalidNamed(weightValue));
}
}
function complain(message) {
report({
ruleName,
result,
message,
node: decl,
index: declarationValueIndex(decl) + weightValueOffset,
});
}
}
};
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;