-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.js
169 lines (137 loc) · 4.27 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
// @ts-nocheck
'use strict';
const _ = require('lodash');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const isStandardSyntaxFunction = require('../../utils/isStandardSyntaxFunction');
const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
const keywordSets = require('../../reference/keywordSets');
const namedColorDataHex = require('../../reference/namedColorData');
const optionsMatches = require('../../utils/optionsMatches');
const propertySets = require('../../reference/propertySets');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const valueParser = require('postcss-value-parser');
const generateColorFuncs = require('./generateColorFuncs');
const ruleName = 'color-named';
const messages = ruleMessages(ruleName, {
expected: (named, original) => `Expected "${original}" to be "${named}"`,
rejected: (named) => `Unexpected named color "${named}"`,
});
// Todo tested on case insensivity
const NODE_TYPES = new Set(['word', 'function']);
function rule(expectation, options) {
return (root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: expectation,
possible: ['never', 'always-where-possible'],
},
{
actual: options,
possible: {
ignoreProperties: [_.isString, _.isRegExp],
ignore: ['inside-function'],
},
optional: true,
},
);
if (!validOptions) {
return;
}
const namedColors = Object.keys(namedColorDataHex);
const namedColorData = {};
namedColors.forEach((name) => {
const hex = namedColorDataHex[name];
namedColorData[name] = {
hex,
func: generateColorFuncs(hex[0]),
};
});
root.walkDecls((decl) => {
if (propertySets.acceptCustomIdents.has(decl.prop)) {
return;
}
// Return early if the property is to be ignored
if (optionsMatches(options, 'ignoreProperties', decl.prop)) {
return;
}
valueParser(decl.value).walk((node) => {
const value = node.value;
const type = node.type;
const sourceIndex = node.sourceIndex;
if (optionsMatches(options, 'ignore', 'inside-function') && type === 'function') {
return false;
}
if (!isStandardSyntaxFunction(node)) {
return false;
}
if (!isStandardSyntaxValue(value)) {
return;
}
// Return early if neither a word nor a function
if (!NODE_TYPES.has(type)) {
return;
}
// Check for named colors for "never" option
if (
expectation === 'never' &&
type === 'word' &&
namedColors.includes(value.toLowerCase())
) {
complain(messages.rejected(value), decl, declarationValueIndex(decl) + sourceIndex);
return;
}
// Check "always-where-possible" option ...
if (expectation !== 'always-where-possible') {
return;
}
// First by checking for alternative color function representations ...
if (type === 'function' && keywordSets.colorFunctionNames.has(value.toLowerCase())) {
// Remove all spaces to match what's in `representations`
const normalizedFunctionString = valueParser.stringify(node).replace(/\s+/g, '');
let namedColor;
for (let i = 0, l = namedColors.length; i < l; i++) {
namedColor = namedColors[i];
if (namedColorData[namedColor].func.includes(normalizedFunctionString.toLowerCase())) {
complain(
messages.expected(namedColor, normalizedFunctionString),
decl,
declarationValueIndex(decl) + sourceIndex,
);
return; // Exit as soon as a problem is found
}
}
return;
}
// Then by checking for alternative hex representations
let namedColor;
for (let i = 0, l = namedColors.length; i < l; i++) {
namedColor = namedColors[i];
if (namedColorData[namedColor].hex.includes(value.toLowerCase())) {
complain(
messages.expected(namedColor, value),
decl,
declarationValueIndex(decl) + sourceIndex,
);
return; // Exit as soon as a problem is found
}
}
});
});
function complain(message, node, index) {
report({
result,
ruleName,
message,
node,
index,
});
}
};
}
rule.ruleName = ruleName;
rule.messages = messages;
module.exports = rule;