stylelint-plugin-defensive-css 0.1.0 → 0.3.0

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
@@ -123,7 +123,7 @@ div {
123
123
  "rules": {
124
124
  "plugin/use-defensive-css": [
125
125
  true,
126
- { "custom-property-fallbacks": [true, { ignore: [/hel-/, 'theme-']}] }
126
+ { "custom-property-fallbacks": [true, { "ignore": [/hel-/, "theme-"] }] }
127
127
  ]
128
128
  }
129
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stylelint-plugin-defensive-css",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "A Stylelint plugin to enforce defensive CSS best practices.",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -7,6 +7,7 @@ const {
7
7
  findShorthandBackgroundRepeat,
8
8
  } = require('../../utils/findShorthandBackgroundRepeat');
9
9
  const { findVendorPrefixes } = require('../../utils/findVendorPrefixes');
10
+ const { findCustomProperties } = require('../../utils/findCustomProperties');
10
11
 
11
12
  const defaultBackgroundRepeatProps = {
12
13
  hasBackgroundImage: false,
@@ -71,14 +72,20 @@ const ruleFunction = (_, options) => {
71
72
 
72
73
  /* CUSTOM PROPERTY FALLBACKS */
73
74
  if (options?.['custom-property-fallbacks']) {
74
- if (decl.value.includes('var(--') && !decl.value.includes(',')) {
75
+ const propertiesWithoutFallback = findCustomProperties(decl.value);
76
+
77
+ if (propertiesWithoutFallback.length) {
75
78
  if (Array.isArray(options?.['custom-property-fallbacks'])) {
76
79
  if (options['custom-property-fallbacks'][0]) {
77
80
  const patterns = options['custom-property-fallbacks'][1].ignore;
78
- const patternMatched = patterns.some((pattern) =>
79
- typeof pattern === 'string'
80
- ? new RegExp(pattern).test(decl.value.slice(4, -1))
81
- : pattern.test(decl.value.slice(4, -1)),
81
+ const patternMatched = propertiesWithoutFallback.some(
82
+ (property) => {
83
+ return patterns.some((pattern) =>
84
+ typeof pattern === 'string'
85
+ ? new RegExp(pattern).test(property)
86
+ : pattern.test(property),
87
+ );
88
+ },
82
89
  );
83
90
 
84
91
  if (patternMatched) {
@@ -0,0 +1,14 @@
1
+ const expression = /var\(.+?\)/g;
2
+
3
+ function findCustomProperties(value) {
4
+ if (!value) return false;
5
+
6
+ let propertiesFound = [...value.trim().matchAll(expression)];
7
+ return propertiesFound
8
+ .map(([property]) => (property.includes(',') ? undefined : property))
9
+ .filter((value) => value);
10
+ }
11
+
12
+ module.exports = {
13
+ findCustomProperties,
14
+ };
@@ -1,9 +1,9 @@
1
- const expression = /-[-moz-|-ms-|-o-|-webkit-|]+-/g;
1
+ const expression = /-[moz|ms|o|webkit|]+-/g;
2
2
 
3
3
  function findVendorPrefixes(selector) {
4
4
  if (!selector) return false;
5
5
 
6
- let prefixesFound = [...selector.matchAll(expression)];
6
+ let prefixesFound = [...selector.trim().matchAll(expression)];
7
7
  return prefixesFound.length > 1;
8
8
  }
9
9