stylelint-plugin-defensive-css 0.0.1 → 0.0.4

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
@@ -55,7 +55,7 @@ Enable this rule in order to prevent unintentional repeating background.
55
55
  "rules": {
56
56
  "plugin/use-defensive-css": [
57
57
  true,
58
- { ..., "background-repeat": true }
58
+ { "background-repeat": true }
59
59
  ]
60
60
  }
61
61
  }
@@ -98,7 +98,7 @@ Enable this rule in order to require fallbacks values for custom properties.
98
98
  "rules": {
99
99
  "plugin/use-defensive-css": [
100
100
  true,
101
- { ..., "custom-property-fallbacks": true }
101
+ { "custom-property-fallbacks": true }
102
102
  ]
103
103
  }
104
104
  }
@@ -133,7 +133,7 @@ Enable this rule in order to require all flex rows to have a flex-wrap value.
133
133
  "rules": {
134
134
  "plugin/use-defensive-css": [
135
135
  true,
136
- { ..., "flex-wrapping": true }
136
+ { "flex-wrapping": true }
137
137
  ]
138
138
  }
139
139
  }
@@ -178,7 +178,7 @@ Enable this rule in order to require all vendor-prefixed selectors to be split i
178
178
  "rules": {
179
179
  "plugin/use-defensive-css": [
180
180
  true,
181
- { ..., "vendor-prefix-grouping": true }
181
+ { "vendor-prefix-grouping": true }
182
182
  ]
183
183
  }
184
184
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-plugin-defensive-css",
3
- "version": "0.0.1",
3
+ "version": "0.0.4",
4
4
  "description": "A Stylelint plugin to enforce defensive CSS best practices.",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -15,7 +15,7 @@ const ruleMessages = stylelint.utils.ruleMessages(ruleName, {
15
15
  return 'Flex rows must have a `flex-wrap: wrap;` or `flex-wrap: wrap-reverse` declaration.';
16
16
  },
17
17
  vendorPrefixWGrouping() {
18
- return `It's not recommended to group selectors that are meant to work with different browsers. Instead, split them to separate rules.`;
18
+ return `Separate different vendor prefixes into their own rules.`;
19
19
  },
20
20
  });
21
21
 
@@ -8,20 +8,23 @@ const {
8
8
  } = require('../../utils/findShorthandBackgroundRepeat');
9
9
  const { findVendorPrefixes } = require('../../utils/findVendorPrefixes');
10
10
 
11
- const ruleFunction = (_, options) => {
12
- let isLastStyleDeclaration = false;
13
- const backgroundRepeatProps = {
14
- hasBackgroundImage: false,
15
- isMissingBackgroundRepeat: true,
16
- nodeToReport: undefined,
17
- };
18
- const flexWrappingProps = {
19
- isDisplayFlex: false,
20
- isFlexRow: true,
21
- isMissingFlexWrap: true,
22
- nodeToReport: undefined,
23
- };
11
+ const defaultBackgroundRepeatProps = {
12
+ hasBackgroundImage: false,
13
+ isMissingBackgroundRepeat: true,
14
+ nodeToReport: undefined,
15
+ };
16
+ const defaultFlexWrappingProps = {
17
+ isDisplayFlex: false,
18
+ isFlexRow: true,
19
+ isMissingFlexWrap: true,
20
+ nodeToReport: undefined,
21
+ };
24
22
 
23
+ let backgroundRepeatProps = { ...defaultBackgroundRepeatProps };
24
+ let flexWrappingProps = { ...defaultFlexWrappingProps };
25
+ let isLastStyleDeclaration = false;
26
+
27
+ const ruleFunction = (_, options) => {
25
28
  return (root, result) => {
26
29
  const validOptions = stylelint.utils.validateOptions(result, ruleName);
27
30
 
@@ -30,12 +33,9 @@ const ruleFunction = (_, options) => {
30
33
  }
31
34
 
32
35
  root.walkDecls((decl) => {
33
- if (
36
+ isLastStyleDeclaration =
34
37
  JSON.stringify(decl) ===
35
- JSON.stringify(decl.parent.nodes[decl.parent.nodes.length - 1])
36
- ) {
37
- isLastStyleDeclaration = true;
38
- }
38
+ JSON.stringify(decl.parent.nodes[decl.parent.nodes.length - 1]);
39
39
 
40
40
  /* BACKGROUND REPEAT */
41
41
  if (options?.['background-repeat']) {
@@ -55,23 +55,26 @@ const ruleFunction = (_, options) => {
55
55
  backgroundRepeatProps.isMissingBackgroundRepeat = false;
56
56
  }
57
57
 
58
- if (
59
- isLastStyleDeclaration &&
60
- Object.values(backgroundRepeatProps).every((prop) => prop)
61
- ) {
62
- stylelint.utils.report({
63
- message: ruleMessages.backgroundRepeat(),
64
- node: decl.parent,
65
- result,
66
- ruleName,
67
- });
58
+ if (isLastStyleDeclaration) {
59
+ if (Object.values(backgroundRepeatProps).every((prop) => prop)) {
60
+ stylelint.utils.report({
61
+ message: ruleMessages.backgroundRepeat(),
62
+ node: decl.parent,
63
+ result,
64
+ ruleName,
65
+ });
66
+ }
67
+
68
+ backgroundRepeatProps = { ...defaultBackgroundRepeatProps };
68
69
  }
70
+
71
+ return;
69
72
  }
70
73
 
71
74
  /* CUSTOM PROPERTY FALLBACKS */
72
75
  if (options?.['custom-property-fallbacks']) {
73
76
  if (decl.value.includes('var(--') && !decl.value.includes(',')) {
74
- stylelint.utils.report({
77
+ return stylelint.utils.report({
75
78
  message: ruleMessages.customPropertyFallbacks(),
76
79
  node: decl,
77
80
  result,
@@ -87,7 +90,7 @@ const ruleFunction = (_, options) => {
87
90
  flexWrappingProps.nodeToReport = decl;
88
91
  }
89
92
 
90
- if (decl.prop === 'flex-direction' && decl.value.startsWith('column')) {
93
+ if (decl.prop === 'flex-direction' && decl.value.includes('column')) {
91
94
  flexWrappingProps.isFlexRow = false;
92
95
  }
93
96
 
@@ -95,17 +98,20 @@ const ruleFunction = (_, options) => {
95
98
  flexWrappingProps.isMissingFlexWrap = false;
96
99
  }
97
100
 
98
- if (
99
- isLastStyleDeclaration &&
100
- Object.values(flexWrappingProps).every((prop) => prop)
101
- ) {
102
- stylelint.utils.report({
103
- message: ruleMessages.flexWrapping(),
104
- node: flexWrappingProps.nodeToReport,
105
- result,
106
- ruleName,
107
- });
101
+ if (isLastStyleDeclaration) {
102
+ if (Object.values(flexWrappingProps).every((prop) => prop)) {
103
+ stylelint.utils.report({
104
+ message: ruleMessages.flexWrapping(),
105
+ node: flexWrappingProps.nodeToReport,
106
+ result,
107
+ ruleName,
108
+ });
109
+ }
110
+
111
+ flexWrappingProps = { ...defaultFlexWrappingProps };
108
112
  }
113
+
114
+ return;
109
115
  }
110
116
 
111
117
  /* GROUPING VENDOR PREFIXES */
@@ -113,7 +119,7 @@ const ruleFunction = (_, options) => {
113
119
  const hasMultiplePrefixes = findVendorPrefixes(decl.parent.selector);
114
120
 
115
121
  if (hasMultiplePrefixes) {
116
- stylelint.utils.report({
122
+ return stylelint.utils.report({
117
123
  message: ruleMessages.vendorPrefixWGrouping(),
118
124
  node: decl.parent,
119
125
  result,