stylelint-config-crisp 1.0.39 → 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 +1 -0
- package/index.js +2 -0
- package/package.json +1 -1
- package/rules/media-queries-comment.js +189 -0
package/README.md
CHANGED
|
@@ -100,6 +100,7 @@ This is the list of configured syntaxes, plugins and rules used by Stylelint Con
|
|
|
100
100
|
| :- | :- |
|
|
101
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`) |
|
|
102
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 |
|
|
103
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 |
|
|
104
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 |
|
|
105
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) |
|
package/index.js
CHANGED
|
@@ -14,6 +14,7 @@ module.exports = {
|
|
|
14
14
|
plugins: [
|
|
15
15
|
"./rules/at-rule-conditional-parentheses",
|
|
16
16
|
"./rules/filename-class-match",
|
|
17
|
+
"./rules/media-queries-comment",
|
|
17
18
|
"./rules/no-inline-colors",
|
|
18
19
|
"./rules/rule-empty-line-before",
|
|
19
20
|
"./rules/selector-class-interpolation",
|
|
@@ -131,6 +132,7 @@ module.exports = {
|
|
|
131
132
|
// Crisp rules
|
|
132
133
|
"crisp/at-rule-conditional-parentheses": true,
|
|
133
134
|
"crisp/filename-class-match": true,
|
|
135
|
+
"crisp/media-queries-comment": true,
|
|
134
136
|
"crisp/no-inline-colors": true,
|
|
135
137
|
"crisp/rule-empty-line-before": true,
|
|
136
138
|
"crisp/selector-class-interpolation": true
|
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);
|