stylelint-config-crisp 1.0.38 → 1.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/index.js +12 -1
- package/package.json +1 -1
- package/rules/media-queries-comment.js +189 -0
- package/rules/no-inline-colors.js +456 -0
package/README.md
CHANGED
|
@@ -48,6 +48,7 @@ This is the list of configured syntaxes, plugins and rules used by Stylelint Con
|
|
|
48
48
|
| [color-function-notation](https://stylelint.io/user-guide/rules/color-function-notation/) | Color functions must use legacy notation (`rgba(12, 122, 231, 0.2)` instead of `rgb(12 122 231 / 0.2)`) |
|
|
49
49
|
| [color-hex-length](https://stylelint.io/user-guide/rules/color-hex-length/) | Hex colors must use long notation |
|
|
50
50
|
| [color-no-invalid-hex](https://stylelint.io/user-guide/rules/color-no-invalid-hex/) | Disallows invalid hex colors |
|
|
51
|
+
| [custom-property-pattern](https://stylelint.io/user-guide/rules/custom-property-pattern/) | CSS variables must use kebab-case (`--blue-crisp`, `--grey-90`, `--background-level-0`) |
|
|
51
52
|
| [declaration-block-no-redundant-longhand-properties](https://stylelint.io/user-guide/rules/declaration-block-no-redundant-longhand-properties/) | Disallows redundant longhand properties <br /> - `inset` shorthand is ignored (prefer using the longhand properties `top`, `right`, `bottom` and `left` altogether) <br /> - `overflow` shorthand is ignored (prefer using the longhand properties `overflow-x` and `overflow-y` altogether)|
|
|
52
53
|
| [declaration-block-single-line-max-declarations](https://stylelint.io/user-guide/rules/declaration-block-single-line-max-declarations/) | Allows only 1 declaration per line |
|
|
53
54
|
| [declaration-property-value-disallowed-list](https://stylelint.io/user-guide/rules/declaration-property-value-disallowed-list/) | A list of disallowed property and value pairs within declaration <br /> - `^border: none` is disallowed (prefer `0`, for consistency) <br /> - `outline: none` is disallowed (prefer `0`, for consistency) |
|
|
@@ -99,6 +100,8 @@ This is the list of configured syntaxes, plugins and rules used by Stylelint Con
|
|
|
99
100
|
| :- | :- |
|
|
100
101
|
| [at-rule-conditional-parentheses](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/at-rule-conditional-parentheses.js) | Enforces parentheses in conditions of at-rules (`@if`, `@elseif`, `@while`) |
|
|
101
102
|
| [filename-class-match](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/filename-class-match.js) | Ensures that class name is derivated from filename (this rule is only applied to Vue files) |
|
|
103
|
+
| [media-queries-comment](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/media-queries-comment.js) | Requires the first breakpoint query (`@media`, `@include mq`, `@container`) in a block to be prefixed with `// --> MEDIA-QUERIES <--` at the same indentation |
|
|
104
|
+
| [no-inline-colors](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/no-inline-colors.js) | Disallows inline colors (hex, named colors, raw `rgb()` / `hsl()`, Sass color functions and `$color-source--*` variables). Colors must be consumed as CSS variables via `rgb(var(--token))`. Files named `colors.scss`, `codemirror.scss`, `fireworks.scss`, `magic-check.scss` and `reset.scss` are ignored |
|
|
102
105
|
| [rule-empty-line-before](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/rule-empty-line-before.js) | Requires an empty line before multi-line rules the same way [rule-empty-line-before](https://stylelint.io/user-guide/rules/rule-empty-line-before/) does, but properly handles Sass interpolation `#{}` in selectors |
|
|
103
106
|
| [selector-class-interpolation](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/selector-class-interpolation.js) | Enforces class selectors to use Sass interpolation (this rule is only applied to Vue files) |
|
|
104
107
|
|
package/index.js
CHANGED
|
@@ -14,6 +14,8 @@ module.exports = {
|
|
|
14
14
|
plugins: [
|
|
15
15
|
"./rules/at-rule-conditional-parentheses",
|
|
16
16
|
"./rules/filename-class-match",
|
|
17
|
+
"./rules/media-queries-comment",
|
|
18
|
+
"./rules/no-inline-colors",
|
|
17
19
|
"./rules/rule-empty-line-before",
|
|
18
20
|
"./rules/selector-class-interpolation",
|
|
19
21
|
|
|
@@ -35,6 +37,13 @@ module.exports = {
|
|
|
35
37
|
"color-function-notation": "legacy",
|
|
36
38
|
"color-hex-length": "long",
|
|
37
39
|
"color-no-invalid-hex": true,
|
|
40
|
+
"custom-property-pattern": [
|
|
41
|
+
"^([a-z][a-z0-9]*)(-[a-z0-9]+)*$",
|
|
42
|
+
|
|
43
|
+
{
|
|
44
|
+
"message": "Expected CSS variable name to be kebab-case"
|
|
45
|
+
}
|
|
46
|
+
],
|
|
38
47
|
"declaration-block-no-redundant-longhand-properties": [
|
|
39
48
|
true,
|
|
40
49
|
|
|
@@ -123,7 +132,9 @@ module.exports = {
|
|
|
123
132
|
// Crisp rules
|
|
124
133
|
"crisp/at-rule-conditional-parentheses": true,
|
|
125
134
|
"crisp/filename-class-match": true,
|
|
135
|
+
"crisp/media-queries-comment": true,
|
|
136
|
+
"crisp/no-inline-colors": true,
|
|
126
137
|
"crisp/rule-empty-line-before": true,
|
|
127
138
|
"crisp/selector-class-interpolation": true
|
|
128
139
|
}
|
|
129
|
-
}
|
|
140
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
const stylelint = require("stylelint");
|
|
2
|
+
|
|
3
|
+
const ruleName = "crisp/media-queries-comment";
|
|
4
|
+
|
|
5
|
+
const MEDIA_QUERIES_COMMENT = "--> MEDIA-QUERIES <--";
|
|
6
|
+
const MEDIA_QUERIES_COMMENT_LINE = `// ${MEDIA_QUERIES_COMMENT}`;
|
|
7
|
+
|
|
8
|
+
const SIZE_QUERY_PATTERN = /(?:max|min)-(?:width|height)|(?:width|height)\s*[<>]=?/;
|
|
9
|
+
const LOOSE_MEDIA_QUERIES_COMMENT_PATTERN = /^\s*(?:-->\s*)?MEDIA[-\s]QUERIES(?:\s*<--)?\s*$/i;
|
|
10
|
+
|
|
11
|
+
const messages = stylelint.utils.ruleMessages(ruleName, {
|
|
12
|
+
expected: `Expected "${MEDIA_QUERIES_COMMENT_LINE}" comment before the first media query`,
|
|
13
|
+
expectedIndent: `Expected "${MEDIA_QUERIES_COMMENT_LINE}" comment to match media query indentation`
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const ruleFunction = (primaryOption, secondaryOptions, context) => {
|
|
17
|
+
return (root, result) => {
|
|
18
|
+
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
|
|
19
|
+
actual: primaryOption,
|
|
20
|
+
possible: [true]
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
if (!validOptions) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
root.walkAtRules((atRule) => {
|
|
28
|
+
if (!isBreakpointAtRule(atRule) || !isFirstBreakpointAtRule(atRule)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Nested breakpoint queries inherit the parent section comment
|
|
33
|
+
if (hasBreakpointAtRuleAncestor(atRule)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const indent = getNodeIndent(atRule);
|
|
38
|
+
const previousNode = atRule.prev();
|
|
39
|
+
|
|
40
|
+
if (isExactMediaQueriesComment(previousNode)) {
|
|
41
|
+
if (getNodeIndent(previousNode) !== indent) {
|
|
42
|
+
if (context.fix) {
|
|
43
|
+
setNodeIndent(previousNode, indent);
|
|
44
|
+
} else {
|
|
45
|
+
stylelint.utils.report({
|
|
46
|
+
ruleName,
|
|
47
|
+
result,
|
|
48
|
+
node: previousNode,
|
|
49
|
+
message: messages.expectedIndent
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
ensureEmptyLineBefore(atRule, indent, context);
|
|
55
|
+
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (context.fix) {
|
|
60
|
+
if (isLooseMediaQueriesComment(previousNode)) {
|
|
61
|
+
previousNode.text = MEDIA_QUERIES_COMMENT;
|
|
62
|
+
previousNode.raws.inline = true;
|
|
63
|
+
previousNode.raws.left = " ";
|
|
64
|
+
previousNode.raws.right = "";
|
|
65
|
+
previousNode.raws.text = MEDIA_QUERIES_COMMENT;
|
|
66
|
+
|
|
67
|
+
setNodeIndent(previousNode, indent);
|
|
68
|
+
ensureEmptyLineBefore(atRule, indent, context);
|
|
69
|
+
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
insertMediaQueriesComment(atRule, indent);
|
|
74
|
+
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
stylelint.utils.report({
|
|
79
|
+
ruleName,
|
|
80
|
+
result,
|
|
81
|
+
node: atRule,
|
|
82
|
+
message: messages.expected
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// Check if an at-rule is a breakpoint media / container query
|
|
89
|
+
function isBreakpointAtRule(atRule) {
|
|
90
|
+
if (atRule.name === "media") {
|
|
91
|
+
return SIZE_QUERY_PATTERN.test(atRule.params);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (atRule.name === "include") {
|
|
95
|
+
return /^mq\s*\(/.test(atRule.params.trim());
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (atRule.name === "container") {
|
|
99
|
+
return SIZE_QUERY_PATTERN.test(atRule.params);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Check if this is the first breakpoint at-rule among siblings
|
|
106
|
+
function isFirstBreakpointAtRule(atRule) {
|
|
107
|
+
let previousNode = atRule.prev();
|
|
108
|
+
|
|
109
|
+
while (previousNode) {
|
|
110
|
+
if (previousNode.type === "atrule" && isBreakpointAtRule(previousNode)) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
previousNode = previousNode.prev();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Check if a breakpoint at-rule ancestor already opened the section
|
|
121
|
+
function hasBreakpointAtRuleAncestor(atRule) {
|
|
122
|
+
let parent = atRule.parent;
|
|
123
|
+
|
|
124
|
+
while (parent) {
|
|
125
|
+
if (parent.type === "atrule" && isBreakpointAtRule(parent)) {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
parent = parent.parent;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Check if a node is the canonical media-queries section comment
|
|
136
|
+
function isExactMediaQueriesComment(node) {
|
|
137
|
+
return node && node.type === "comment" && node.text.trim() === MEDIA_QUERIES_COMMENT;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Check if a node is a media-queries section comment in any known format
|
|
141
|
+
function isLooseMediaQueriesComment(node) {
|
|
142
|
+
return node && node.type === "comment" && LOOSE_MEDIA_QUERIES_COMMENT_PATTERN.test(node.text);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Return the indentation of a node
|
|
146
|
+
function getNodeIndent(node) {
|
|
147
|
+
const before = node.raws.before || "";
|
|
148
|
+
const match = before.match(/(?:\r?\n)([ \t]*)$/);
|
|
149
|
+
|
|
150
|
+
if (match) {
|
|
151
|
+
return match[1];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return "";
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Set the indentation of a node
|
|
158
|
+
function setNodeIndent(node, indent) {
|
|
159
|
+
const before = node.raws.before || "";
|
|
160
|
+
|
|
161
|
+
if (/(?:\r?\n)[ \t]*$/.test(before)) {
|
|
162
|
+
node.raws.before = before.replace(/[ \t]*$/, indent);
|
|
163
|
+
} else {
|
|
164
|
+
node.raws.before = `\n${indent}`;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Ensure an empty line between the section comment and the at-rule
|
|
169
|
+
function ensureEmptyLineBefore(atRule, indent, context) {
|
|
170
|
+
if (/(\r?\n\s*\r?\n)/.test(atRule.raws.before || "")) {
|
|
171
|
+
setNodeIndent(atRule, indent);
|
|
172
|
+
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (context.fix) {
|
|
177
|
+
atRule.raws.before = `\n\n${indent}`;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Insert the media-queries section comment before the at-rule
|
|
182
|
+
function insertMediaQueriesComment(atRule, indent) {
|
|
183
|
+
const before = atRule.raws.before || "\n";
|
|
184
|
+
const prefix = before.replace(/[ \t]*$/, "");
|
|
185
|
+
|
|
186
|
+
atRule.raws.before = `${prefix}${indent}${MEDIA_QUERIES_COMMENT_LINE}\n\n${indent}`;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
module.exports = stylelint.createPlugin(ruleName, ruleFunction);
|
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
const stylelint = require("stylelint");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
|
|
4
|
+
const ruleName = "crisp/no-inline-colors";
|
|
5
|
+
const messages = stylelint.utils.ruleMessages(ruleName, {
|
|
6
|
+
rejected: (color) => {
|
|
7
|
+
return `Unexpected inline color "${color}", use a CSS variable (rgb(var(--token)))`;
|
|
8
|
+
},
|
|
9
|
+
|
|
10
|
+
rejectedSource: (variable) => {
|
|
11
|
+
return `Unexpected color source variable "${variable}", use the CSS variable equivalent`;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const DEFAULT_IGNORE_FILES = [
|
|
16
|
+
"colors.scss",
|
|
17
|
+
"reset.scss"
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
const HEX_COLOR_PATTERN = /#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b/g;
|
|
21
|
+
|
|
22
|
+
const SOURCE_VARIABLE_PATTERN = /\$color-source--[\w-]+/g;
|
|
23
|
+
|
|
24
|
+
const RGB_CHANNELS_PATTERN = /^\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*$/;
|
|
25
|
+
|
|
26
|
+
const SKIP_VALUE_PATTERN = /url\((?:[^)]*)\)|(["'])(?:\\.|(?!\1).)*\1/gi;
|
|
27
|
+
|
|
28
|
+
const FUNCTION_NAME_CHAR_PATTERN = /[\w.-]/;
|
|
29
|
+
|
|
30
|
+
const FUNCTION_NAME_PATTERN = /^[a-z_]/;
|
|
31
|
+
|
|
32
|
+
const VAR_FUNCTION_PATTERN = /\bvar\s*\(/;
|
|
33
|
+
|
|
34
|
+
const SASS_VARIABLE_PATTERN = /\$[\w-]+/;
|
|
35
|
+
|
|
36
|
+
const NUMERIC_ARGS_PATTERN = /^[\d.]+%?$/;
|
|
37
|
+
|
|
38
|
+
const COLOR_FUNCTIONS = new Set([
|
|
39
|
+
"rgb",
|
|
40
|
+
"rgba",
|
|
41
|
+
"hsl",
|
|
42
|
+
"hsla",
|
|
43
|
+
"hwb",
|
|
44
|
+
"lab",
|
|
45
|
+
"lch",
|
|
46
|
+
"oklab",
|
|
47
|
+
"oklch",
|
|
48
|
+
"color"
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
const SASS_COLOR_FUNCTIONS = new Set([
|
|
52
|
+
"lighten",
|
|
53
|
+
"darken",
|
|
54
|
+
"mix",
|
|
55
|
+
"adjust-color",
|
|
56
|
+
"scale-color",
|
|
57
|
+
"change-color",
|
|
58
|
+
"saturate",
|
|
59
|
+
"desaturate",
|
|
60
|
+
"grayscale",
|
|
61
|
+
"complement",
|
|
62
|
+
"invert",
|
|
63
|
+
"opacify",
|
|
64
|
+
"transparentize",
|
|
65
|
+
"fade-in",
|
|
66
|
+
"fade-out",
|
|
67
|
+
"color.adjust",
|
|
68
|
+
"color.mix",
|
|
69
|
+
"color.scale",
|
|
70
|
+
"color.change",
|
|
71
|
+
"color.complement",
|
|
72
|
+
"color.grayscale",
|
|
73
|
+
"color.invert",
|
|
74
|
+
"color.saturate",
|
|
75
|
+
"color.desaturate"
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
const CSS_FILTER_FUNCTIONS = new Set([
|
|
79
|
+
"invert",
|
|
80
|
+
"grayscale",
|
|
81
|
+
"saturate"
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
const NAMED_COLORS = new Set([
|
|
85
|
+
"aliceblue",
|
|
86
|
+
"antiquewhite",
|
|
87
|
+
"aqua",
|
|
88
|
+
"aquamarine",
|
|
89
|
+
"azure",
|
|
90
|
+
"beige",
|
|
91
|
+
"bisque",
|
|
92
|
+
"black",
|
|
93
|
+
"blanchedalmond",
|
|
94
|
+
"blue",
|
|
95
|
+
"blueviolet",
|
|
96
|
+
"brown",
|
|
97
|
+
"burlywood",
|
|
98
|
+
"cadetblue",
|
|
99
|
+
"chartreuse",
|
|
100
|
+
"chocolate",
|
|
101
|
+
"coral",
|
|
102
|
+
"cornflowerblue",
|
|
103
|
+
"cornsilk",
|
|
104
|
+
"crimson",
|
|
105
|
+
"cyan",
|
|
106
|
+
"darkblue",
|
|
107
|
+
"darkcyan",
|
|
108
|
+
"darkgoldenrod",
|
|
109
|
+
"darkgray",
|
|
110
|
+
"darkgreen",
|
|
111
|
+
"darkgrey",
|
|
112
|
+
"darkkhaki",
|
|
113
|
+
"darkmagenta",
|
|
114
|
+
"darkolivegreen",
|
|
115
|
+
"darkorange",
|
|
116
|
+
"darkorchid",
|
|
117
|
+
"darkred",
|
|
118
|
+
"darksalmon",
|
|
119
|
+
"darkseagreen",
|
|
120
|
+
"darkslateblue",
|
|
121
|
+
"darkslategray",
|
|
122
|
+
"darkslategrey",
|
|
123
|
+
"darkturquoise",
|
|
124
|
+
"darkviolet",
|
|
125
|
+
"deeppink",
|
|
126
|
+
"deepskyblue",
|
|
127
|
+
"dimgray",
|
|
128
|
+
"dimgrey",
|
|
129
|
+
"dodgerblue",
|
|
130
|
+
"firebrick",
|
|
131
|
+
"floralwhite",
|
|
132
|
+
"forestgreen",
|
|
133
|
+
"fuchsia",
|
|
134
|
+
"gainsboro",
|
|
135
|
+
"ghostwhite",
|
|
136
|
+
"gold",
|
|
137
|
+
"goldenrod",
|
|
138
|
+
"gray",
|
|
139
|
+
"green",
|
|
140
|
+
"greenyellow",
|
|
141
|
+
"grey",
|
|
142
|
+
"honeydew",
|
|
143
|
+
"hotpink",
|
|
144
|
+
"indianred",
|
|
145
|
+
"indigo",
|
|
146
|
+
"ivory",
|
|
147
|
+
"khaki",
|
|
148
|
+
"lavender",
|
|
149
|
+
"lavenderblush",
|
|
150
|
+
"lawngreen",
|
|
151
|
+
"lemonchiffon",
|
|
152
|
+
"lightblue",
|
|
153
|
+
"lightcoral",
|
|
154
|
+
"lightcyan",
|
|
155
|
+
"lightgoldenrodyellow",
|
|
156
|
+
"lightgray",
|
|
157
|
+
"lightgreen",
|
|
158
|
+
"lightgrey",
|
|
159
|
+
"lightpink",
|
|
160
|
+
"lightsalmon",
|
|
161
|
+
"lightseagreen",
|
|
162
|
+
"lightskyblue",
|
|
163
|
+
"lightslategray",
|
|
164
|
+
"lightslategrey",
|
|
165
|
+
"lightsteelblue",
|
|
166
|
+
"lightyellow",
|
|
167
|
+
"lime",
|
|
168
|
+
"limegreen",
|
|
169
|
+
"linen",
|
|
170
|
+
"magenta",
|
|
171
|
+
"maroon",
|
|
172
|
+
"mediumaquamarine",
|
|
173
|
+
"mediumblue",
|
|
174
|
+
"mediumorchid",
|
|
175
|
+
"mediumpurple",
|
|
176
|
+
"mediumseagreen",
|
|
177
|
+
"mediumslateblue",
|
|
178
|
+
"mediumspringgreen",
|
|
179
|
+
"mediumturquoise",
|
|
180
|
+
"mediumvioletred",
|
|
181
|
+
"midnightblue",
|
|
182
|
+
"mintcream",
|
|
183
|
+
"mistyrose",
|
|
184
|
+
"moccasin",
|
|
185
|
+
"navajowhite",
|
|
186
|
+
"navy",
|
|
187
|
+
"oldlace",
|
|
188
|
+
"olive",
|
|
189
|
+
"olivedrab",
|
|
190
|
+
"orange",
|
|
191
|
+
"orangered",
|
|
192
|
+
"orchid",
|
|
193
|
+
"palegoldenrod",
|
|
194
|
+
"palegreen",
|
|
195
|
+
"paleturquoise",
|
|
196
|
+
"palevioletred",
|
|
197
|
+
"papayawhip",
|
|
198
|
+
"peachpuff",
|
|
199
|
+
"peru",
|
|
200
|
+
"pink",
|
|
201
|
+
"plum",
|
|
202
|
+
"powderblue",
|
|
203
|
+
"purple",
|
|
204
|
+
"rebeccapurple",
|
|
205
|
+
"red",
|
|
206
|
+
"rosybrown",
|
|
207
|
+
"royalblue",
|
|
208
|
+
"saddlebrown",
|
|
209
|
+
"salmon",
|
|
210
|
+
"sandybrown",
|
|
211
|
+
"seagreen",
|
|
212
|
+
"seashell",
|
|
213
|
+
"sienna",
|
|
214
|
+
"silver",
|
|
215
|
+
"skyblue",
|
|
216
|
+
"slateblue",
|
|
217
|
+
"slategray",
|
|
218
|
+
"slategrey",
|
|
219
|
+
"snow",
|
|
220
|
+
"springgreen",
|
|
221
|
+
"steelblue",
|
|
222
|
+
"tan",
|
|
223
|
+
"teal",
|
|
224
|
+
"thistle",
|
|
225
|
+
"tomato",
|
|
226
|
+
"turquoise",
|
|
227
|
+
"violet",
|
|
228
|
+
"wheat",
|
|
229
|
+
"white",
|
|
230
|
+
"whitesmoke",
|
|
231
|
+
"yellow",
|
|
232
|
+
"yellowgreen"
|
|
233
|
+
]);
|
|
234
|
+
|
|
235
|
+
const NAMED_COLOR_PATTERN = new RegExp(
|
|
236
|
+
`(?<![-$@#.\\w])(${Array.from(NAMED_COLORS).join("|")})(?![-$\\w])`,
|
|
237
|
+
"gi"
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
const ruleFunction = (primaryOption, secondaryOptions = {}) => {
|
|
241
|
+
return (root, result) => {
|
|
242
|
+
const validOptions = stylelint.utils.validateOptions(result, ruleName, {
|
|
243
|
+
actual: primaryOption,
|
|
244
|
+
possible: [true]
|
|
245
|
+
}, {
|
|
246
|
+
actual: secondaryOptions,
|
|
247
|
+
possible: {
|
|
248
|
+
ignoreFiles: [isString]
|
|
249
|
+
},
|
|
250
|
+
optional: true
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
if (!validOptions) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const ignoreFiles = secondaryOptions.ignoreFiles || DEFAULT_IGNORE_FILES;
|
|
258
|
+
|
|
259
|
+
if (isIgnoredFile(root.source && root.source.input.file, ignoreFiles)) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
root.walkDecls((decl) => {
|
|
264
|
+
if (decl.prop.startsWith("--") && RGB_CHANNELS_PATTERN.test(decl.value)) {
|
|
265
|
+
report(decl, result, messages.rejected(decl.value.trim()), decl.value.trim());
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
checkValue(decl.value, decl, result);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
root.walkAtRules((atRule) => {
|
|
272
|
+
if (!atRule.params) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
checkValue(atRule.params, atRule, result);
|
|
277
|
+
});
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
// Scan a CSS/SCSS value for inline colors
|
|
282
|
+
function checkValue(value, node, result) {
|
|
283
|
+
const skipRanges = getSkipRanges(value);
|
|
284
|
+
|
|
285
|
+
findMatches(value, SOURCE_VARIABLE_PATTERN, skipRanges).forEach((color) => {
|
|
286
|
+
report(node, result, messages.rejectedSource(color), color);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
findMatches(value, HEX_COLOR_PATTERN, skipRanges).forEach((color) => {
|
|
290
|
+
report(node, result, messages.rejected(color), color);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
findMatches(value, NAMED_COLOR_PATTERN, skipRanges).forEach((color) => {
|
|
294
|
+
report(node, result, messages.rejected(color), color);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
forEachFunction(value, (name, args) => {
|
|
298
|
+
if (isSkippedIndex(skipRanges, args.index)) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (SASS_COLOR_FUNCTIONS.has(name)) {
|
|
303
|
+
// `invert()`, `grayscale()` and `saturate()` are also CSS filters
|
|
304
|
+
if (CSS_FILTER_FUNCTIONS.has(name) && isNumericFunctionArgs(args.value)) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
report(node, result, messages.rejected(`${name}()`), name);
|
|
309
|
+
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// `rgb(var(--token))` and `rgb($color)` wrap a variable, they are not inline colors
|
|
314
|
+
if (!COLOR_FUNCTIONS.has(name) || hasVarFunction(args.value) || hasSassVariable(args.value)) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Hex and named colors inside the function are reported separately
|
|
319
|
+
if (hasPattern(HEX_COLOR_PATTERN, args.value) || hasPattern(NAMED_COLOR_PATTERN, args.value)) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
report(node, result, messages.rejected(`${name}(${args.value.trim()})`), name);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Report a violation
|
|
328
|
+
function report(node, result, message, word) {
|
|
329
|
+
stylelint.utils.report({
|
|
330
|
+
ruleName,
|
|
331
|
+
result,
|
|
332
|
+
node,
|
|
333
|
+
message,
|
|
334
|
+
word
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Ranges that should not be scanned (url() and quoted strings)
|
|
339
|
+
function getSkipRanges(value) {
|
|
340
|
+
const ranges = [];
|
|
341
|
+
let match;
|
|
342
|
+
|
|
343
|
+
while ((match = SKIP_VALUE_PATTERN.exec(value)) !== null) {
|
|
344
|
+
ranges.push([match.index, match.index + match[0].length]);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
SKIP_VALUE_PATTERN.lastIndex = 0;
|
|
348
|
+
|
|
349
|
+
return ranges;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Collect regex matches outside skipped ranges
|
|
353
|
+
function findMatches(value, pattern, skipRanges) {
|
|
354
|
+
const matches = [];
|
|
355
|
+
let match;
|
|
356
|
+
|
|
357
|
+
pattern.lastIndex = 0;
|
|
358
|
+
|
|
359
|
+
while ((match = pattern.exec(value)) !== null) {
|
|
360
|
+
if (!isSkippedIndex(skipRanges, match.index)) {
|
|
361
|
+
matches.push(match[0]);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
pattern.lastIndex = 0;
|
|
366
|
+
|
|
367
|
+
return matches;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// Walk function calls in a value (including nested ones)
|
|
371
|
+
function forEachFunction(value, callback) {
|
|
372
|
+
for (let index = 0; index < value.length; index++) {
|
|
373
|
+
if (value[index] !== "(") {
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
let nameStart = index - 1;
|
|
378
|
+
|
|
379
|
+
while (nameStart >= 0 && FUNCTION_NAME_CHAR_PATTERN.test(value[nameStart])) {
|
|
380
|
+
nameStart--;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
nameStart++;
|
|
384
|
+
|
|
385
|
+
const name = value.slice(nameStart, index).toLowerCase();
|
|
386
|
+
|
|
387
|
+
if (!name || !FUNCTION_NAME_PATTERN.test(name)) {
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
let depth = 1;
|
|
392
|
+
let end = index + 1;
|
|
393
|
+
|
|
394
|
+
while (end < value.length && depth > 0) {
|
|
395
|
+
if (value[end] === "(") {
|
|
396
|
+
depth++;
|
|
397
|
+
} else if (value[end] === ")") {
|
|
398
|
+
depth--;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
end++;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
callback(name, {
|
|
405
|
+
value: value.slice(index + 1, end - 1),
|
|
406
|
+
index: index + 1
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Check if a file name is ignored
|
|
412
|
+
function isIgnoredFile(filePath, ignoreFiles) {
|
|
413
|
+
if (!filePath) {
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const fileName = path.basename(filePath);
|
|
418
|
+
|
|
419
|
+
return ignoreFiles.includes(fileName);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function hasVarFunction(value) {
|
|
423
|
+
return VAR_FUNCTION_PATTERN.test(value);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function hasSassVariable(value) {
|
|
427
|
+
return SASS_VARIABLE_PATTERN.test(value);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function isNumericFunctionArgs(value) {
|
|
431
|
+
const first = value.trim().split(",")[0].trim();
|
|
432
|
+
|
|
433
|
+
return NUMERIC_ARGS_PATTERN.test(first);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function hasPattern(pattern, value) {
|
|
437
|
+
pattern.lastIndex = 0;
|
|
438
|
+
|
|
439
|
+
const matches = pattern.test(value);
|
|
440
|
+
|
|
441
|
+
pattern.lastIndex = 0;
|
|
442
|
+
|
|
443
|
+
return matches;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function isSkippedIndex(ranges, index) {
|
|
447
|
+
return ranges.some(([start, end]) => {
|
|
448
|
+
return index >= start && index < end;
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function isString(value) {
|
|
453
|
+
return typeof value === "string";
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
module.exports = stylelint.createPlugin(ruleName, ruleFunction);
|