stylelint-config-crisp 1.0.40 → 1.0.42

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,9 @@ 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. Ignored anywhere inside `@mixin` definitions |
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. Ignored anywhere inside `@mixin` definitions |
105
+ | [mixins-comment](https://github.com/crisp-oss/stylelint-config-crisp/blob/master/rules/mixins-comment.js) | Requires `// --> MIXINS <--` and an empty line before the first `@mixin` definition in a block, at the same indentation |
104
106
  | [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
107
  | [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
108
  | [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,7 +14,9 @@ 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",
19
+ "./rules/mixins-comment",
18
20
  "./rules/no-inline-colors",
19
21
  "./rules/rule-empty-line-before",
20
22
  "./rules/selector-class-interpolation",
@@ -132,7 +134,9 @@ module.exports = {
132
134
  // Crisp rules
133
135
  "crisp/at-rule-conditional-parentheses": true,
134
136
  "crisp/filename-class-match": true,
137
+ "crisp/keyframes-comment": true,
135
138
  "crisp/media-queries-comment": true,
139
+ "crisp/mixins-comment": true,
136
140
  "crisp/no-inline-colors": true,
137
141
  "crisp/rule-empty-line-before": true,
138
142
  "crisp/selector-class-interpolation": 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.42",
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,8 @@
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
+ ["mixin"]
8
+ );
@@ -1,189 +1,29 @@
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
+ ["mixin"]
29
+ );
@@ -0,0 +1,7 @@
1
+ const sectionCommentRule = require("./utils/section-comment-rule");
2
+
3
+ module.exports = sectionCommentRule(
4
+ "crisp/mixins-comment",
5
+ "MIXINS",
6
+ (atRule) => /^mixin$/i.test(atRule.name)
7
+ );
@@ -0,0 +1,157 @@
1
+ const stylelint = require("stylelint");
2
+ const path = require("node:path");
3
+ const { existsSync } = require("node:fs");
4
+
5
+ // Require one section heading before the first matching at-rule in each block.
6
+ module.exports = (ruleName, heading, isSectionAtRule, ignoredAncestorAtRules = []) => {
7
+ const text = `--> ${heading} <--`;
8
+ const commentLine = `// ${text}`;
9
+ const looseHeading = heading.replace(/-/g, "[-\\s]");
10
+ const looseCommentPattern = new RegExp(`^\\s*(?:-->\\s*)?${looseHeading}(?:\\s*<--)?\\s*$`, "i");
11
+ const messages = stylelint.utils.ruleMessages(ruleName, {
12
+ expected: `Expected "${commentLine}" comment before the first section at-rule`,
13
+ expectedIndent: `Expected "${commentLine}" comment to match at-rule indentation`,
14
+ expectedEmptyLine: `Expected an empty line after "${commentLine}"`
15
+ });
16
+
17
+ const ruleFunction = (primaryOption, secondaryOptions = {}) => {
18
+ return (root, result) => {
19
+ if (!stylelint.utils.validateOptions(result, ruleName, {
20
+ actual: primaryOption,
21
+ possible: [true]
22
+ }, {
23
+ actual: secondaryOptions,
24
+ possible: {
25
+ ignoreFiles: (value) => Array.isArray(secondaryOptions.ignoreFiles) && typeof value === "string"
26
+ },
27
+ optional: true
28
+ })) {
29
+ return;
30
+ }
31
+
32
+ const filePath = root.source && root.source.input.file;
33
+
34
+ if (filePath && isIgnoredFile(filePath, secondaryOptions.ignoreFiles || [])) {
35
+ return;
36
+ }
37
+
38
+ root.walkAtRules((atRule) => {
39
+ if (!isSectionAtRule(atRule) || hasPreviousSectionAtRule(atRule, isSectionAtRule)) {
40
+ return;
41
+ }
42
+
43
+ // Nested sections inherit their heading; ignored ancestors need none.
44
+ let parent = atRule.parent;
45
+
46
+ while (parent) {
47
+ if (parent.type === "atrule" &&
48
+ (isSectionAtRule(parent) || ignoredAncestorAtRules.includes(parent.name.toLowerCase()))) {
49
+ return;
50
+ }
51
+
52
+ parent = parent.parent;
53
+ }
54
+
55
+ const indent = getIndent(atRule);
56
+ const previousNode = atRule.prev();
57
+ const isComment = previousNode && previousNode.type === "comment";
58
+ const isExactComment = isComment &&
59
+ previousNode.text === text &&
60
+ previousNode.raws.inline === true &&
61
+ previousNode.raws.left === " " &&
62
+ !previousNode.raws.right;
63
+ const newline = root.source.input.css.includes("\r\n") ? "\r\n" : "\n";
64
+ const report = (message, fix, node = atRule) => {
65
+ stylelint.utils.report({ ruleName, result, node, message, fix });
66
+ };
67
+
68
+ if (!isExactComment) {
69
+ report(messages.expected, () => {
70
+ const raws = { inline: true, left: " ", right: "", text };
71
+
72
+ if (isComment && looseCommentPattern.test(previousNode.text)) {
73
+ previousNode.text = text;
74
+ Object.assign(previousNode.raws, raws);
75
+ setIndent(previousNode, indent, newline);
76
+ } else {
77
+ atRule.parent.insertBefore(atRule, {
78
+ text,
79
+ source: atRule.source,
80
+ raws: { ...raws, before: atRule.raws.before || "" }
81
+ });
82
+ }
83
+
84
+ atRule.raws.before = `${newline}${newline}${indent}`;
85
+ });
86
+
87
+ return;
88
+ }
89
+
90
+ if (getIndent(previousNode) !== indent) {
91
+ report(messages.expectedIndent, () => {
92
+ setIndent(previousNode, indent, newline);
93
+ }, previousNode);
94
+ }
95
+
96
+ if (!/^(?:\r?\n)[ \t]*(?:\r?\n)[ \t]*$/.test(atRule.raws.before || "")) {
97
+ report(messages.expectedEmptyLine, () => {
98
+ atRule.raws.before = `${newline}${newline}${indent}`;
99
+ });
100
+ }
101
+ });
102
+ };
103
+ };
104
+
105
+ ruleFunction.ruleName = ruleName;
106
+ ruleFunction.messages = messages;
107
+ ruleFunction.meta = { fixable: true };
108
+
109
+ return stylelint.createPlugin(ruleName, ruleFunction);
110
+ };
111
+
112
+ function isIgnoredFile(filePath, ignoreFiles) {
113
+ if (ignoreFiles.length === 0) {
114
+ return false;
115
+ }
116
+
117
+ let directory = path.dirname(filePath);
118
+
119
+ while (!existsSync(path.join(directory, ".git"))) {
120
+ const parent = path.dirname(directory);
121
+
122
+ if (parent === directory) {
123
+ directory = process.cwd();
124
+ break;
125
+ }
126
+
127
+ directory = parent;
128
+ }
129
+
130
+ return ignoreFiles.some((file) => path.resolve(directory, file) === path.resolve(filePath));
131
+ }
132
+
133
+ function hasPreviousSectionAtRule(atRule, isSectionAtRule) {
134
+ let previousNode = atRule.prev();
135
+
136
+ while (previousNode) {
137
+ if (previousNode.type === "atrule" && isSectionAtRule(previousNode)) {
138
+ return true;
139
+ }
140
+
141
+ previousNode = previousNode.prev();
142
+ }
143
+
144
+ return false;
145
+ }
146
+
147
+ function getIndent(node) {
148
+ return (node.raws.before || "").match(/(?:^|\r?\n)([ \t]*)$/)?.[1] || "";
149
+ }
150
+
151
+ function setIndent(node, indent, newline) {
152
+ const before = node.raws.before || "";
153
+
154
+ node.raws.before = /(?:^|\n)[ \t]*$/.test(before)
155
+ ? before.replace(/[ \t]*$/, indent)
156
+ : `${newline}${indent}`;
157
+ }