stylelint-config-crisp 1.0.40 → 1.0.41

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.
@@ -20,7 +20,7 @@ jobs:
20
20
  - name: Install NodeJS
21
21
  uses: actions/setup-node@v1
22
22
  with:
23
- node-version: 24.x
23
+ node-version: 26.x
24
24
  registry-url: https://registry.npmjs.org
25
25
 
26
26
  - name: Verify versions
package/README.md CHANGED
@@ -100,7 +100,8 @@ 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
+ | [keyframes-comment](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/keyframes-comment.js) | Requires `// --> KEYFRAMES <--` and an empty line before the first `@keyframes` (including vendor-prefixed variants) in a block, at the same indentation |
104
+ | [media-queries-comment](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/media-queries-comment.js) | Requires `// --> MEDIA-QUERIES <--` and an empty line before the first query (`@media`, `@include mq`, size-based `@container`) in a block, at the same indentation |
104
105
  | [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 |
105
106
  | [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 |
106
107
  | [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/keyframes-comment",
17
18
  "./rules/media-queries-comment",
18
19
  "./rules/no-inline-colors",
19
20
  "./rules/rule-empty-line-before",
@@ -132,6 +133,7 @@ module.exports = {
132
133
  // Crisp rules
133
134
  "crisp/at-rule-conditional-parentheses": true,
134
135
  "crisp/filename-class-match": true,
136
+ "crisp/keyframes-comment": true,
135
137
  "crisp/media-queries-comment": true,
136
138
  "crisp/no-inline-colors": true,
137
139
  "crisp/rule-empty-line-before": true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-config-crisp",
3
- "version": "1.0.40",
3
+ "version": "1.0.41",
4
4
  "description": "Custom Stylelint Rules for Crisp",
5
5
  "author": "Crisp IM SAS",
6
6
  "main": "index.js",
@@ -12,6 +12,9 @@
12
12
  "bugs": {
13
13
  "url": "https://github.com/crisp-oss/stylelint-config-crisp/issues"
14
14
  },
15
+ "scripts": {
16
+ "test": "node --test"
17
+ },
15
18
  "dependencies": {
16
19
  "@stylistic/stylelint-config": "2.0.0",
17
20
  "postcss-html": "1.8.0",
@@ -0,0 +1,7 @@
1
+ const sectionCommentRule = require("./utils/section-comment-rule");
2
+
3
+ module.exports = sectionCommentRule(
4
+ "crisp/keyframes-comment",
5
+ "KEYFRAMES",
6
+ (atRule) => /^(?:-[a-z]+-)?keyframes$/i.test(atRule.name)
7
+ );
@@ -1,189 +1,28 @@
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}`;
1
+ const sectionCommentRule = require("./utils/section-comment-rule");
7
2
 
8
3
  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
4
 
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
5
+ // Include all media queries and retain support for size-based container queries.
89
6
  function isBreakpointAtRule(atRule) {
90
- if (atRule.name === "media") {
91
- return SIZE_QUERY_PATTERN.test(atRule.params);
7
+ const name = atRule.name.toLowerCase();
8
+
9
+ if (name === "media") {
10
+ return true;
92
11
  }
93
12
 
94
- if (atRule.name === "include") {
13
+ if (name === "include") {
95
14
  return /^mq\s*\(/.test(atRule.params.trim());
96
15
  }
97
16
 
98
- if (atRule.name === "container") {
17
+ if (name === "container") {
99
18
  return SIZE_QUERY_PATTERN.test(atRule.params);
100
19
  }
101
20
 
102
21
  return false;
103
22
  }
104
23
 
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);
24
+ module.exports = sectionCommentRule(
25
+ "crisp/media-queries-comment",
26
+ "MEDIA-QUERIES",
27
+ isBreakpointAtRule
28
+ );
@@ -0,0 +1,121 @@
1
+ const stylelint = require("stylelint");
2
+
3
+ // Require one section heading before the first matching at-rule in each block.
4
+ module.exports = (ruleName, heading, isSectionAtRule) => {
5
+ const text = `--> ${heading} <--`;
6
+ const commentLine = `// ${text}`;
7
+ const looseHeading = heading.replace(/-/g, "[-\\s]");
8
+ const looseCommentPattern = new RegExp(`^\\s*(?:-->\\s*)?${looseHeading}(?:\\s*<--)?\\s*$`, "i");
9
+ const messages = stylelint.utils.ruleMessages(ruleName, {
10
+ expected: `Expected "${commentLine}" comment before the first section at-rule`,
11
+ expectedIndent: `Expected "${commentLine}" comment to match at-rule indentation`,
12
+ expectedEmptyLine: `Expected an empty line after "${commentLine}"`
13
+ });
14
+
15
+ const ruleFunction = (primaryOption) => {
16
+ return (root, result) => {
17
+ if (!stylelint.utils.validateOptions(result, ruleName, {
18
+ actual: primaryOption,
19
+ possible: [true]
20
+ })) {
21
+ return;
22
+ }
23
+
24
+ root.walkAtRules((atRule) => {
25
+ if (!isSectionAtRule(atRule) || hasPreviousSectionAtRule(atRule, isSectionAtRule)) {
26
+ return;
27
+ }
28
+
29
+ // Nested queries inherit the heading of the containing section.
30
+ let parent = atRule.parent;
31
+
32
+ while (parent) {
33
+ if (parent.type === "atrule" && isSectionAtRule(parent)) {
34
+ return;
35
+ }
36
+
37
+ parent = parent.parent;
38
+ }
39
+
40
+ const indent = getIndent(atRule);
41
+ const previousNode = atRule.prev();
42
+ const isComment = previousNode && previousNode.type === "comment";
43
+ const isExactComment = isComment &&
44
+ previousNode.text === text &&
45
+ previousNode.raws.inline === true &&
46
+ previousNode.raws.left === " " &&
47
+ !previousNode.raws.right;
48
+ const newline = root.source.input.css.includes("\r\n") ? "\r\n" : "\n";
49
+ const report = (message, fix, node = atRule) => {
50
+ stylelint.utils.report({ ruleName, result, node, message, fix });
51
+ };
52
+
53
+ if (!isExactComment) {
54
+ report(messages.expected, () => {
55
+ const raws = { inline: true, left: " ", right: "", text };
56
+
57
+ if (isComment && looseCommentPattern.test(previousNode.text)) {
58
+ previousNode.text = text;
59
+ Object.assign(previousNode.raws, raws);
60
+ setIndent(previousNode, indent, newline);
61
+ } else {
62
+ atRule.parent.insertBefore(atRule, {
63
+ text,
64
+ source: atRule.source,
65
+ raws: { ...raws, before: atRule.raws.before || "" }
66
+ });
67
+ }
68
+
69
+ atRule.raws.before = `${newline}${newline}${indent}`;
70
+ });
71
+
72
+ return;
73
+ }
74
+
75
+ if (getIndent(previousNode) !== indent) {
76
+ report(messages.expectedIndent, () => {
77
+ setIndent(previousNode, indent, newline);
78
+ }, previousNode);
79
+ }
80
+
81
+ if (!/^(?:\r?\n)[ \t]*(?:\r?\n)[ \t]*$/.test(atRule.raws.before || "")) {
82
+ report(messages.expectedEmptyLine, () => {
83
+ atRule.raws.before = `${newline}${newline}${indent}`;
84
+ });
85
+ }
86
+ });
87
+ };
88
+ };
89
+
90
+ ruleFunction.ruleName = ruleName;
91
+ ruleFunction.messages = messages;
92
+ ruleFunction.meta = { fixable: true };
93
+
94
+ return stylelint.createPlugin(ruleName, ruleFunction);
95
+ };
96
+
97
+ function hasPreviousSectionAtRule(atRule, isSectionAtRule) {
98
+ let previousNode = atRule.prev();
99
+
100
+ while (previousNode) {
101
+ if (previousNode.type === "atrule" && isSectionAtRule(previousNode)) {
102
+ return true;
103
+ }
104
+
105
+ previousNode = previousNode.prev();
106
+ }
107
+
108
+ return false;
109
+ }
110
+
111
+ function getIndent(node) {
112
+ return (node.raws.before || "").match(/(?:^|\r?\n)([ \t]*)$/)?.[1] || "";
113
+ }
114
+
115
+ function setIndent(node, indent, newline) {
116
+ const before = node.raws.before || "";
117
+
118
+ node.raws.before = /(?:^|\n)[ \t]*$/.test(before)
119
+ ? before.replace(/[ \t]*$/, indent)
120
+ : `${newline}${indent}`;
121
+ }