stylelint-config-crisp 1.0.26 → 1.0.28

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 CHANGED
@@ -85,6 +85,7 @@ This is the list of configured syntaxes, plugins and rules used by Stylelint Con
85
85
 
86
86
  #### Crisp rules
87
87
  - [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`)
88
+ - [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)
88
89
  - [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
89
90
  - [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)
90
91
 
package/index.js CHANGED
@@ -13,6 +13,7 @@ module.exports = {
13
13
 
14
14
  plugins: [
15
15
  "./rules/at-rule-conditional-parentheses",
16
+ "./rules/filename-class-match",
16
17
  "./rules/rule-empty-line-before",
17
18
  "./rules/selector-class-interpolation",
18
19
 
@@ -121,6 +122,7 @@ module.exports = {
121
122
 
122
123
  // Crisp rules
123
124
  "crisp/at-rule-conditional-parentheses": true,
125
+ "crisp/filename-class-match": true,
124
126
  "crisp/rule-empty-line-before": true,
125
127
  "crisp/selector-class-interpolation": true
126
128
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-config-crisp",
3
- "version": "1.0.26",
3
+ "version": "1.0.28",
4
4
  "description": "Custom Stylelint Rules for Crisp",
5
5
  "author": "Crisp IM SAS",
6
6
  "main": "index.js",
@@ -2,7 +2,7 @@ const stylelint = require("stylelint");
2
2
 
3
3
  const ruleName = "crisp/at-rule-conditional-parentheses";
4
4
  const messages = stylelint.utils.ruleMessages(ruleName, {
5
- expected: "Expected parentheses in \"@if\", \"@else-if\" and \"@while\" statements",
5
+ expected: (rule) => `Expected parentheses in \"@${rule}\" statement`
6
6
  });
7
7
 
8
8
  const ruleFunction = (primaryOption, secondaryOptions, context) => {
@@ -17,10 +17,12 @@ const ruleFunction = (primaryOption, secondaryOptions, context) => {
17
17
  }
18
18
 
19
19
  root.walkAtRules((rule) => {
20
+ let actualRule = rule.name;
20
21
  let elseIf = false;
21
22
 
22
- // An `@else if` is a regular `@else` with `if` in its params
23
+ // An `@else if` is a regular `@else` with `if ` in its params
23
24
  if (rule.name === "else" && rule.params.startsWith("if ")) {
25
+ actualRule = "else-if";
24
26
  elseIf = true;
25
27
  }
26
28
 
@@ -36,7 +38,7 @@ const ruleFunction = (primaryOption, secondaryOptions, context) => {
36
38
  ruleName,
37
39
  result,
38
40
  node: rule,
39
- message: messages.expected
41
+ message: messages.expected(actualRule)
40
42
  });
41
43
  }
42
44
  }
@@ -0,0 +1,81 @@
1
+ const stylelint = require("stylelint");
2
+ const path = require("path");
3
+
4
+ const ruleName = "crisp/filename-class-match";
5
+ const messages = stylelint.utils.ruleMessages(ruleName, {
6
+ expected: (className, fileName) => `Expected top-level class '${className}' to match file name '${fileName}'`
7
+ });
8
+
9
+ const ruleFunction = (primaryOption, secondaryOptions = {}, context) => {
10
+ return (root, result) => {
11
+ // Validate options for the rule
12
+ const validOptions = stylelint.utils.validateOptions(result, ruleName, {
13
+ actual: primaryOption,
14
+ possible: [true]
15
+ }, {
16
+ actual: secondaryOptions,
17
+ possible: {
18
+ prefix: [isString],
19
+ suffix: [isString]
20
+ },
21
+ optional: true
22
+ });
23
+
24
+ if (!validOptions) {
25
+ return;
26
+ }
27
+
28
+ // Skip non-Vue files
29
+ if (!root.source.input.file.endsWith(".vue")) {
30
+ return;
31
+ }
32
+
33
+ let fileScopeClass;
34
+
35
+ // Identify the parent selector
36
+ root.walkDecls(decl => {
37
+ if (decl.prop === "$c") {
38
+ // Remove leading `.` and `"`
39
+ fileScopeClass = decl.value.replace(/^["']?\./, "").replace(/["']?$/, "");
40
+
41
+ return;
42
+ }
43
+ });
44
+
45
+ // No parent selector found, this will be caught by
46
+ // `selector-class-interpolation`
47
+ if (!fileScopeClass) {
48
+ return;
49
+ }
50
+
51
+ const fileName = path.basename(root.source.input.file, ".vue");
52
+ const { prefix = "", suffix = "" } = secondaryOptions;
53
+ const expectedClassName = `${prefix}${toKebabCase(fileName)}${suffix}`;
54
+
55
+ // Skip `index` and `[[*]]` special filenames
56
+ if (fileName === "index" || fileName.startsWith("[[")) {
57
+ return;
58
+ }
59
+
60
+ if (fileScopeClass !== expectedClassName) {
61
+ stylelint.utils.report({
62
+ ruleName,
63
+ result,
64
+ node: root,
65
+ message: messages.expected(fileScopeClass, expectedClassName)
66
+ });
67
+ }
68
+ };
69
+ };
70
+
71
+ // Convert a string to kebab-case
72
+ function toKebabCase(str) {
73
+ return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/\s+/g, "-").toLowerCase();
74
+ }
75
+
76
+ // Check if a value is a string
77
+ function isString(value) {
78
+ return typeof value === "string";
79
+ }
80
+
81
+ module.exports = stylelint.createPlugin(ruleName, ruleFunction);
@@ -146,9 +146,9 @@ function isSelectorIgnoredOptions(rule, ignoreSelectors = []) {
146
146
  return isIgnored;
147
147
  }
148
148
 
149
- // Check if selector is ignored (out of file scope)
149
+ // Check if selector is ignored (this skip classes elements outside of current \
150
+ // file scope, for example of CSS overrides)
150
151
  function isSelectorIgnored(rule, fileScopeClass) {
151
- // Skip classes elements outside of CSS file scope
152
152
  if (fileScopeClass) {
153
153
  if (rule.selector.startsWith(".")) {
154
154
  // Extract base part from BEM selector