@alfalab/stylelint-core-vars 1.5.0 → 1.6.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 +6 -1
- package/index.js +12 -6
- package/index.test.js +59 -0
- package/package.json +1 -1
- package/utils.js +7 -1
package/README.md
CHANGED
|
@@ -26,7 +26,12 @@ npm install --save-dev stylelint @alfalab/stylelint-core-vars
|
|
|
26
26
|
"@alfalab/stylelint-core-vars"
|
|
27
27
|
],
|
|
28
28
|
"rules": {
|
|
29
|
-
"stylelint-core-vars/use-vars":
|
|
29
|
+
"stylelint-core-vars/use-vars": [
|
|
30
|
+
true,
|
|
31
|
+
{
|
|
32
|
+
"allowNumericValues": true
|
|
33
|
+
}
|
|
34
|
+
],
|
|
30
35
|
"stylelint-core-vars/use-mixins": true,
|
|
31
36
|
"stylelint-core-vars/use-one-of-vars": [
|
|
32
37
|
true,
|
package/index.js
CHANGED
|
@@ -61,7 +61,7 @@ const messages = {
|
|
|
61
61
|
}),
|
|
62
62
|
};
|
|
63
63
|
|
|
64
|
-
const checkVars = (decl, result, context, ruleName, matcher, shouldReport) => {
|
|
64
|
+
const checkVars = (decl, result, context, ruleName, matcher, shouldReport, options = {}) => {
|
|
65
65
|
const { prop, raws } = decl;
|
|
66
66
|
|
|
67
67
|
let value = toOneLine(decl.value);
|
|
@@ -69,7 +69,7 @@ const checkVars = (decl, result, context, ruleName, matcher, shouldReport) => {
|
|
|
69
69
|
|
|
70
70
|
const previousValues = [];
|
|
71
71
|
|
|
72
|
-
while ((substitution = matcher(value, prop))) {
|
|
72
|
+
while ((substitution = matcher(value, prop, options))) {
|
|
73
73
|
let fixed = false;
|
|
74
74
|
|
|
75
75
|
value = value.replace(substitution.value, substitution.fixedValue);
|
|
@@ -175,11 +175,13 @@ const checkDarkColorsUsage = (decl, result, context, ruleName) => {
|
|
|
175
175
|
};
|
|
176
176
|
|
|
177
177
|
module.exports = [
|
|
178
|
-
stylelint.createPlugin(RULE_USE_VARS, (enabled,
|
|
178
|
+
stylelint.createPlugin(RULE_USE_VARS, (enabled, config, context) => {
|
|
179
179
|
if (!enabled || !VARS_AVAILABLE) {
|
|
180
180
|
return () => {};
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
+
const allowNumericValues = config?.allowNumericValues || false;
|
|
184
|
+
|
|
183
185
|
return (root, result) => {
|
|
184
186
|
root.walkDecls((decl) => {
|
|
185
187
|
checkVars(
|
|
@@ -188,16 +190,19 @@ module.exports = [
|
|
|
188
190
|
context,
|
|
189
191
|
RULE_USE_VARS,
|
|
190
192
|
findVars,
|
|
191
|
-
(fixable, fixed) => fixable && !fixed
|
|
193
|
+
(fixable, fixed) => fixable && !fixed,
|
|
194
|
+
{ allowNumericValues }
|
|
192
195
|
);
|
|
193
196
|
});
|
|
194
197
|
};
|
|
195
198
|
}),
|
|
196
|
-
stylelint.createPlugin(RULE_USE_ONE_OF_VARS, (enabled,
|
|
199
|
+
stylelint.createPlugin(RULE_USE_ONE_OF_VARS, (enabled, config, context) => {
|
|
197
200
|
if (!enabled || !VARS_AVAILABLE) {
|
|
198
201
|
return () => {};
|
|
199
202
|
}
|
|
200
203
|
|
|
204
|
+
const allowNumericValues = config?.allowNumericValues || false;
|
|
205
|
+
|
|
201
206
|
return (root, result) => {
|
|
202
207
|
root.walkDecls((decl) => {
|
|
203
208
|
checkVars(
|
|
@@ -206,7 +211,8 @@ module.exports = [
|
|
|
206
211
|
context,
|
|
207
212
|
RULE_USE_ONE_OF_VARS,
|
|
208
213
|
findVars,
|
|
209
|
-
(fixable, fixed) => !fixable && !fixed
|
|
214
|
+
(fixable, fixed) => !fixable && !fixed,
|
|
215
|
+
{ allowNumericValues }
|
|
210
216
|
);
|
|
211
217
|
});
|
|
212
218
|
};
|
package/index.test.js
CHANGED
|
@@ -22,6 +22,13 @@ testRule({
|
|
|
22
22
|
}`,
|
|
23
23
|
description: 'singleline rule with single value',
|
|
24
24
|
},
|
|
25
|
+
{
|
|
26
|
+
code: `.class {
|
|
27
|
+
/* stylelint-disable-next-line */
|
|
28
|
+
padding-left: 8px;
|
|
29
|
+
}`,
|
|
30
|
+
description: 'singleline rule with single value',
|
|
31
|
+
},
|
|
25
32
|
{
|
|
26
33
|
code: `.class {
|
|
27
34
|
padding: var(--gap-xs) 0 var(--gap-m) 0;
|
|
@@ -282,6 +289,43 @@ testRule({
|
|
|
282
289
|
],
|
|
283
290
|
});
|
|
284
291
|
|
|
292
|
+
testRule({
|
|
293
|
+
plugins: [RULE_USE_VARS],
|
|
294
|
+
ruleName: RULE_USE_VARS,
|
|
295
|
+
config: [true, { allowNumericValues: true }],
|
|
296
|
+
fix: true,
|
|
297
|
+
accept: [
|
|
298
|
+
{
|
|
299
|
+
code: `.class {
|
|
300
|
+
padding-left: 4px;
|
|
301
|
+
}`,
|
|
302
|
+
description: 'singleline rule with single value',
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
code: `.class {
|
|
306
|
+
padding: 8px 0 16px 0;
|
|
307
|
+
}`,
|
|
308
|
+
description: 'singleline rule with multiple values',
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
code: `.class {
|
|
312
|
+
padding: 8px 0 16px 0;
|
|
313
|
+
margin: var(--gap-l);
|
|
314
|
+
}`,
|
|
315
|
+
description: 'multiline rule',
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
code: `.class {
|
|
319
|
+
.inner {
|
|
320
|
+
padding: 8px 0 16px 0;
|
|
321
|
+
}
|
|
322
|
+
}`,
|
|
323
|
+
description: 'nested rule',
|
|
324
|
+
},
|
|
325
|
+
],
|
|
326
|
+
reject: [],
|
|
327
|
+
});
|
|
328
|
+
|
|
285
329
|
testRule({
|
|
286
330
|
plugins: [RULE_USE_ONE_OF_VARS],
|
|
287
331
|
ruleName: RULE_USE_ONE_OF_VARS,
|
|
@@ -575,6 +619,21 @@ testRule({
|
|
|
575
619
|
}`,
|
|
576
620
|
description: 'typography nested rule',
|
|
577
621
|
},
|
|
622
|
+
{
|
|
623
|
+
code: `
|
|
624
|
+
/* stylelint-disable ${RULE_USE_MIXINS} */
|
|
625
|
+
.class {
|
|
626
|
+
background-color: var(--color-light-bg-primary);
|
|
627
|
+
font-size: 48px;
|
|
628
|
+
line-height: 64px;
|
|
629
|
+
background: var(--color-light-bg-primary);
|
|
630
|
+
font-weight: 500;
|
|
631
|
+
color: var(--color-light-text-primary);
|
|
632
|
+
}
|
|
633
|
+
/* stylelint-enable ${RULE_USE_MIXINS} */
|
|
634
|
+
`,
|
|
635
|
+
description: 'typography',
|
|
636
|
+
},
|
|
578
637
|
],
|
|
579
638
|
reject: [
|
|
580
639
|
{
|
package/package.json
CHANGED
package/utils.js
CHANGED
|
@@ -202,12 +202,18 @@ function choiceVars(variables, prop, group) {
|
|
|
202
202
|
return variables;
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
function findVars(cssValue, prop) {
|
|
205
|
+
function findVars(cssValue, prop, options) {
|
|
206
206
|
const vars = varsByProperties[prop];
|
|
207
207
|
if (!vars) return;
|
|
208
208
|
|
|
209
209
|
const group = getVarsGroup(vars);
|
|
210
210
|
|
|
211
|
+
if (group === 'gaps' || group === 'borderRadiuses') {
|
|
212
|
+
if (options.allowNumericValues) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
211
217
|
for (const [value, variables] of Object.entries(vars)) {
|
|
212
218
|
const chosen = choiceVars(variables, prop, group);
|
|
213
219
|
|