stylelint-plugin-defensive-css 0.0.7 → 0.1.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 +77 -51
- package/package.json +1 -1
- package/src/rules/use-defensive-css/index.js +17 -0
package/README.md
CHANGED
|
@@ -30,10 +30,7 @@ configuration.
|
|
|
30
30
|
{
|
|
31
31
|
"plugins": ["stylelint-plugin-defensive-css"],
|
|
32
32
|
"rules": {
|
|
33
|
-
"plugin/use-defensive-css": [
|
|
34
|
-
true,
|
|
35
|
-
{ "severity": "warning" }
|
|
36
|
-
]
|
|
33
|
+
"plugin/use-defensive-css": [true, { "severity": "warning" }]
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
36
|
```
|
|
@@ -46,17 +43,16 @@ The plugin provides multiple rules that can be toggled on and off as needed.
|
|
|
46
43
|
|
|
47
44
|
> [Read more about this pattern in Defensive CSS](https://defensivecss.dev/tip/bg-repeat/)
|
|
48
45
|
|
|
49
|
-
Oftentimes, when using a large image as a background, we tend to forget to
|
|
46
|
+
Oftentimes, when using a large image as a background, we tend to forget to
|
|
47
|
+
account for the case when the design is viewed on a large screen. That
|
|
48
|
+
background will repeat by default.
|
|
50
49
|
|
|
51
50
|
Enable this rule in order to prevent unintentional repeating background.
|
|
52
51
|
|
|
53
52
|
```json
|
|
54
53
|
{
|
|
55
54
|
"rules": {
|
|
56
|
-
"plugin/use-defensive-css": [
|
|
57
|
-
true,
|
|
58
|
-
{ "background-repeat": true }
|
|
59
|
-
]
|
|
55
|
+
"plugin/use-defensive-css": [true, { "background-repeat": true }]
|
|
60
56
|
}
|
|
61
57
|
}
|
|
62
58
|
```
|
|
@@ -65,41 +61,39 @@ Enable this rule in order to prevent unintentional repeating background.
|
|
|
65
61
|
|
|
66
62
|
```css
|
|
67
63
|
div {
|
|
68
|
-
|
|
64
|
+
background: url('some-image.jpg') repeat black top center;
|
|
69
65
|
}
|
|
70
66
|
div {
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
background: url('some-image.jpg') black top center;
|
|
68
|
+
background-repeat: no-repeat;
|
|
73
69
|
}
|
|
74
|
-
|
|
70
|
+
```
|
|
75
71
|
|
|
76
72
|
#### ❌ Failing Examples
|
|
77
73
|
|
|
78
74
|
```css
|
|
79
75
|
div {
|
|
80
|
-
|
|
76
|
+
background: url('some-image.jpg') black top center;
|
|
81
77
|
}
|
|
82
|
-
div {
|
|
83
|
-
|
|
78
|
+
div {
|
|
79
|
+
background-image: url('some-image.jpg');
|
|
84
80
|
}
|
|
85
81
|
```
|
|
86
82
|
|
|
87
|
-
|
|
88
83
|
### Custom Property Fallbacks
|
|
89
84
|
|
|
90
85
|
> [Read more about this pattern in Defensive CSS](https://defensivecss.dev/tip/css-variable-fallback/)
|
|
91
86
|
|
|
92
|
-
CSS variables are gaining more and more usage in web design. There is a method
|
|
87
|
+
CSS variables are gaining more and more usage in web design. There is a method
|
|
88
|
+
that we can apply to use them in a way that doesn’t break the experience, in
|
|
89
|
+
case the CSS variable value was empty for some reason.
|
|
93
90
|
|
|
94
91
|
Enable this rule in order to require fallbacks values for custom properties.
|
|
95
92
|
|
|
96
93
|
```json
|
|
97
94
|
{
|
|
98
95
|
"rules": {
|
|
99
|
-
"plugin/use-defensive-css": [
|
|
100
|
-
true,
|
|
101
|
-
{ "custom-property-fallbacks": true }
|
|
102
|
-
]
|
|
96
|
+
"plugin/use-defensive-css": [true, { "custom-property-fallbacks": true }]
|
|
103
97
|
}
|
|
104
98
|
}
|
|
105
99
|
```
|
|
@@ -108,15 +102,46 @@ Enable this rule in order to require fallbacks values for custom properties.
|
|
|
108
102
|
|
|
109
103
|
```css
|
|
110
104
|
div {
|
|
111
|
-
|
|
105
|
+
color: var(--color-primary, #000);
|
|
112
106
|
}
|
|
113
|
-
|
|
107
|
+
```
|
|
114
108
|
|
|
115
109
|
#### ❌ Failing Examples
|
|
116
110
|
|
|
117
111
|
```css
|
|
118
112
|
div {
|
|
119
|
-
|
|
113
|
+
color: var(--color-primary);
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
| Option | Description |
|
|
118
|
+
| ------ | ------------------------------------------------------------------------------------------------- |
|
|
119
|
+
| ignore | Pass an array of regular expressions and/or strings to ignore linting specific custom properties. |
|
|
120
|
+
|
|
121
|
+
```json
|
|
122
|
+
{
|
|
123
|
+
"rules": {
|
|
124
|
+
"plugin/use-defensive-css": [
|
|
125
|
+
true,
|
|
126
|
+
{ "custom-property-fallbacks": [true, { ignore: [/hel-/, 'theme-']}] }
|
|
127
|
+
]
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The `ignore` array can support regular expressions and strings. If a string is
|
|
133
|
+
provided, it will be translated into a RegExp like `new RegExp(string)` before
|
|
134
|
+
testing the custom property name.
|
|
135
|
+
|
|
136
|
+
#### ✅ Passing Examples
|
|
137
|
+
|
|
138
|
+
```css
|
|
139
|
+
div {
|
|
140
|
+
/* properties with theme- are ignored */
|
|
141
|
+
color: var(--theme-color-primary);
|
|
142
|
+
|
|
143
|
+
/* properties with hel- are ignored */
|
|
144
|
+
padding: var(--hel-spacing-200);
|
|
120
145
|
}
|
|
121
146
|
```
|
|
122
147
|
|
|
@@ -124,17 +149,18 @@ div {
|
|
|
124
149
|
|
|
125
150
|
> [Read more about this pattern in Defensive CSS](https://defensivecss.dev/tip/flexbox-wrapping/)
|
|
126
151
|
|
|
127
|
-
CSS flexbox is one of the most useful CSS layout features nowadays. It’s
|
|
152
|
+
CSS flexbox is one of the most useful CSS layout features nowadays. It’s
|
|
153
|
+
tempting to add `display: flex` to a wrapper and have the child items ordered
|
|
154
|
+
next to each other. The thing is when there is not enough space, those child
|
|
155
|
+
items won’t wrap into a new line by default. We need to change that behavior
|
|
156
|
+
with `flex-wrap: wrap`.
|
|
128
157
|
|
|
129
158
|
Enable this rule in order to require all flex rows to have a flex-wrap value.
|
|
130
159
|
|
|
131
160
|
```json
|
|
132
161
|
{
|
|
133
162
|
"rules": {
|
|
134
|
-
"plugin/use-defensive-css": [
|
|
135
|
-
true,
|
|
136
|
-
{ "flex-wrapping": true }
|
|
137
|
-
]
|
|
163
|
+
"plugin/use-defensive-css": [true, { "flex-wrapping": true }]
|
|
138
164
|
}
|
|
139
165
|
}
|
|
140
166
|
```
|
|
@@ -143,25 +169,25 @@ Enable this rule in order to require all flex rows to have a flex-wrap value.
|
|
|
143
169
|
|
|
144
170
|
```css
|
|
145
171
|
div {
|
|
146
|
-
|
|
147
|
-
|
|
172
|
+
display: flex;
|
|
173
|
+
flex-wrap: wrap;
|
|
148
174
|
}
|
|
149
175
|
div {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
176
|
+
display: flex;
|
|
177
|
+
flex-direction: row-reverse;
|
|
178
|
+
flex-wrap: wrap-reverse;
|
|
153
179
|
}
|
|
154
|
-
|
|
180
|
+
```
|
|
155
181
|
|
|
156
182
|
#### ❌ Failing Examples
|
|
157
183
|
|
|
158
184
|
```css
|
|
159
185
|
div {
|
|
160
|
-
|
|
186
|
+
display: flex;
|
|
161
187
|
}
|
|
162
188
|
div {
|
|
163
|
-
|
|
164
|
-
|
|
189
|
+
display: flex;
|
|
190
|
+
flex-direction: row;
|
|
165
191
|
}
|
|
166
192
|
```
|
|
167
193
|
|
|
@@ -169,38 +195,38 @@ div {
|
|
|
169
195
|
|
|
170
196
|
> [Read more about this pattern in Defensive CSS](https://defensivecss.dev/tip/grouping-selectors/)
|
|
171
197
|
|
|
172
|
-
It's not recommended to group selectors that are meant to work with different
|
|
198
|
+
It's not recommended to group selectors that are meant to work with different
|
|
199
|
+
browsers. For example, styling an input's placeholder needs multiple selectors
|
|
200
|
+
per the browser. If we group the selectors, the entire rule will be invalid,
|
|
201
|
+
according to [w3c](https://www.w3.org/TR/selectors/#grouping).
|
|
173
202
|
|
|
174
|
-
Enable this rule in order to require all vendor-prefixed selectors to be split
|
|
203
|
+
Enable this rule in order to require all vendor-prefixed selectors to be split
|
|
204
|
+
into their own rules.
|
|
175
205
|
|
|
176
206
|
```json
|
|
177
207
|
{
|
|
178
208
|
"rules": {
|
|
179
|
-
"plugin/use-defensive-css": [
|
|
180
|
-
true,
|
|
181
|
-
{ "vendor-prefix-grouping": true }
|
|
182
|
-
]
|
|
209
|
+
"plugin/use-defensive-css": [true, { "vendor-prefix-grouping": true }]
|
|
183
210
|
}
|
|
184
211
|
}
|
|
185
212
|
```
|
|
186
213
|
|
|
187
|
-
|
|
188
214
|
#### ✅ Passing Examples
|
|
189
215
|
|
|
190
216
|
```css
|
|
191
217
|
input::-webkit-input-placeholder {
|
|
192
|
-
|
|
218
|
+
color: #222;
|
|
193
219
|
}
|
|
194
220
|
input::-moz-placeholder {
|
|
195
|
-
|
|
221
|
+
color: #222;
|
|
196
222
|
}
|
|
197
|
-
|
|
223
|
+
```
|
|
198
224
|
|
|
199
225
|
#### ❌ Failing Examples
|
|
200
226
|
|
|
201
227
|
```css
|
|
202
228
|
input::-webkit-input-placeholder,
|
|
203
229
|
input::-moz-placeholder {
|
|
204
|
-
|
|
230
|
+
color: #222;
|
|
205
231
|
}
|
|
206
|
-
```
|
|
232
|
+
```
|
package/package.json
CHANGED
|
@@ -72,6 +72,23 @@ const ruleFunction = (_, options) => {
|
|
|
72
72
|
/* CUSTOM PROPERTY FALLBACKS */
|
|
73
73
|
if (options?.['custom-property-fallbacks']) {
|
|
74
74
|
if (decl.value.includes('var(--') && !decl.value.includes(',')) {
|
|
75
|
+
if (Array.isArray(options?.['custom-property-fallbacks'])) {
|
|
76
|
+
if (options['custom-property-fallbacks'][0]) {
|
|
77
|
+
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)),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
if (patternMatched) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
75
92
|
stylelint.utils.report({
|
|
76
93
|
message: ruleMessages.customPropertyFallbacks(),
|
|
77
94
|
node: decl,
|