@alfalab/stylelint-core-vars 1.0.0 → 1.4.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
@@ -39,6 +39,12 @@ npm install --save-dev stylelint stylelint-core-vars
39
39
  {
40
40
  "severity": "warning"
41
41
  }
42
+ ],
43
+ "stylelint-core-vars/do-not-use-dark-colors": [
44
+ true,
45
+ {
46
+ "severity": "warning"
47
+ }
42
48
  ]
43
49
  },
44
50
  }
package/index.js CHANGED
@@ -1,28 +1,26 @@
1
1
  const stylelint = require('stylelint');
2
2
  const toOneLine = require('./utils').toOneLine;
3
3
  const formatVar = require('./utils').formatVar;
4
- const findVar = require('./utils').findVar;
5
- const findTypographyMixin = require('./utils').findTypographyMixin;
4
+ const findVars = require('./utils').findVars;
5
+ const colorsSet = require('./utils').colorsSet;
6
+ const findTypographyMixins = require('./utils').findTypographyMixins;
6
7
  const VARS_AVAILABLE = require('./utils').VARS_AVAILABLE;
7
8
 
8
9
  const RULE_USE_VARS = 'stylelint-core-vars/use-vars';
9
10
  const RULE_USE_ONE_OF_VARS = 'stylelint-core-vars/use-one-of-vars';
10
11
  const RULE_USE_MIXINS = 'stylelint-core-vars/use-mixins';
11
12
  const RULE_USE_ONE_OF_MIXINS = 'stylelint-core-vars/use-one-of-mixins';
13
+ const RULE_DO_NOT_USE_DARK_COLORS = 'stylelint-core-vars/do-not-use-dark-colors';
12
14
 
13
15
  const messages = {
14
16
  [RULE_USE_VARS]: stylelint.utils.ruleMessages(RULE_USE_VARS, {
15
17
  expected: (variable, value) => {
16
- return `Use variable '${variable}' instead of plain value '${value}'`;
18
+ return `Use variable 'var(${variable})' instead of plain value '${value}'`;
17
19
  },
18
20
  }),
19
21
  [RULE_USE_ONE_OF_VARS]: stylelint.utils.ruleMessages(RULE_USE_ONE_OF_VARS, {
20
- expected: (variableOrVariables, value) => {
21
- const variables = Array.isArray(variableOrVariables)
22
- ? variableOrVariables
23
- : [variableOrVariables];
24
-
25
- const variablesPart = variables.map((v) => ` - ${v}`).join('\n');
22
+ expected: (variables, value) => {
23
+ const variablesPart = variables.map((v) => `var(${v})`).join('\n');
26
24
 
27
25
  return `Use variables instead of plain value '${value}':\n${variablesPart}\n`;
28
26
  },
@@ -33,15 +31,18 @@ const messages = {
33
31
  },
34
32
  }),
35
33
  [RULE_USE_ONE_OF_MIXINS]: stylelint.utils.ruleMessages(RULE_USE_ONE_OF_MIXINS, {
36
- expected: (mixinOrMixins) => {
37
- const mixins = Array.isArray(mixinOrMixins) ? mixinOrMixins : [mixinOrMixins];
38
-
34
+ expected: (mixins) => {
39
35
  const mixinsPart = mixins
40
- .map(({ name, props }) => ` - ${name} (${Object.values(props).join('|')})`)
36
+ .map(({ name, props }) => `${name} (${Object.values(props).join('|')})`)
41
37
  .join('\n');
42
38
  return `Use mixins instead of plain values:\n${mixinsPart}\n`;
43
39
  },
44
40
  }),
41
+ [RULE_DO_NOT_USE_DARK_COLORS]: stylelint.utils.ruleMessages(RULE_DO_NOT_USE_DARK_COLORS, {
42
+ expected: () => {
43
+ return 'Do not use dark colors directly. Only light and static colors are allowed';
44
+ },
45
+ }),
45
46
  };
46
47
 
47
48
  const checkVars = (decl, result, context, ruleName) => {
@@ -52,11 +53,11 @@ const checkVars = (decl, result, context, ruleName) => {
52
53
  let substitution;
53
54
  const previousValues = [];
54
55
 
55
- while ((substitution = findVar(value, prop))) {
56
+ while ((substitution = findVars(value, prop))) {
56
57
  let fixed = false;
57
58
 
58
- const exactVar = Array.isArray(substitution.variable) === false;
59
- const fixedValue = formatVar(exactVar ? substitution.variable : substitution.variable[0]);
59
+ const exactVar = substitution.variables.length === 1;
60
+ const fixedValue = formatVar(substitution.variables[0]);
60
61
 
61
62
  value = value.replace(substitution.value, fixedValue);
62
63
 
@@ -70,13 +71,16 @@ const checkVars = (decl, result, context, ruleName) => {
70
71
  substitution.index
71
72
  );
72
73
 
73
- const shouldReport = !fixed && (ruleName === RULE_USE_ONE_OF_VARS || exactVar);
74
+ const shouldReport =
75
+ !fixed &&
76
+ ((ruleName === RULE_USE_ONE_OF_VARS && !exactVar) ||
77
+ (ruleName === RULE_USE_VARS && exactVar));
74
78
 
75
79
  if (shouldReport) {
76
80
  stylelint.utils.report({
77
81
  result,
78
82
  ruleName,
79
- message: messages[ruleName].expected(substitution.variable, substitution.value),
83
+ message: messages[ruleName].expected(substitution.variables, substitution.value),
80
84
  node: decl,
81
85
  word: value,
82
86
  index: originalValueIndex + prop.length + raws.between.length,
@@ -101,16 +105,16 @@ const checkTypography = (rule, result, context, ruleName) => {
101
105
  const hasTypography = 'font-size' in typographyProps;
102
106
  if (!hasTypography) return;
103
107
 
104
- const mixin = findTypographyMixin(typographyProps);
108
+ const mixins = findTypographyMixins(typographyProps);
105
109
 
106
- if (!mixin) return;
110
+ if (!mixins || !mixins.length) return;
107
111
 
108
- const exactMixin = Array.isArray(mixin) === false;
112
+ const exactMixin = mixins.length === 1;
109
113
 
110
114
  let fixed = false;
111
115
  if (context.fix && exactMixin) {
112
116
  fixed = true;
113
- const { name, props } = mixin;
117
+ const { name, props } = mixins[0];
114
118
 
115
119
  const before = rule.nodes[0].raws.before;
116
120
  rule.walkDecls((decl) => {
@@ -124,13 +128,14 @@ const checkTypography = (rule, result, context, ruleName) => {
124
128
 
125
129
  const shouldReport =
126
130
  !fixed &&
127
- (ruleName === RULE_USE_ONE_OF_MIXINS || (ruleName === RULE_USE_MIXINS && exactMixin));
131
+ ((ruleName === RULE_USE_ONE_OF_MIXINS && !exactMixin) ||
132
+ (ruleName === RULE_USE_MIXINS && exactMixin));
128
133
 
129
134
  if (shouldReport) {
130
135
  stylelint.utils.report({
131
136
  result,
132
137
  ruleName,
133
- message: messages[ruleName].expected(mixin),
138
+ message: messages[ruleName].expected(mixins),
134
139
  node: rule,
135
140
  word: 'font-size',
136
141
  index: 0,
@@ -138,6 +143,25 @@ const checkTypography = (rule, result, context, ruleName) => {
138
143
  }
139
144
  };
140
145
 
146
+ const checkDarkColorsUsage = (decl, result, context, ruleName) => {
147
+ const { prop, raws } = decl;
148
+
149
+ let value = toOneLine(decl.value);
150
+
151
+ const matches = /--color-dark-[\w-]+/.exec(value);
152
+
153
+ if (matches && colorsSet.has(matches[0])) {
154
+ stylelint.utils.report({
155
+ result,
156
+ ruleName: RULE_DO_NOT_USE_DARK_COLORS,
157
+ message: messages[RULE_DO_NOT_USE_DARK_COLORS].expected(),
158
+ node: decl,
159
+ word: value,
160
+ index: prop.length + raws.between.length + decl.value.indexOf(matches[0]),
161
+ });
162
+ }
163
+ };
164
+
141
165
  module.exports = [
142
166
  stylelint.createPlugin(RULE_USE_VARS, (enabled, _, context) => {
143
167
  if (!enabled || !VARS_AVAILABLE) {
@@ -145,10 +169,8 @@ module.exports = [
145
169
  }
146
170
 
147
171
  return (root, result) => {
148
- root.walkRules((rule) => {
149
- rule.walkDecls((decl) => {
150
- checkVars(decl, result, context, RULE_USE_VARS);
151
- });
172
+ root.walkDecls((decl) => {
173
+ checkVars(decl, result, context, RULE_USE_VARS);
152
174
  });
153
175
  };
154
176
  }),
@@ -158,10 +180,8 @@ module.exports = [
158
180
  }
159
181
 
160
182
  return (root, result) => {
161
- root.walkRules((rule) => {
162
- rule.walkDecls((decl) => {
163
- checkVars(decl, result, context, RULE_USE_ONE_OF_VARS);
164
- });
183
+ root.walkDecls((decl) => {
184
+ checkVars(decl, result, context, RULE_USE_ONE_OF_VARS);
165
185
  });
166
186
  };
167
187
  }),
@@ -187,6 +207,17 @@ module.exports = [
187
207
  });
188
208
  };
189
209
  }),
210
+ stylelint.createPlugin(RULE_DO_NOT_USE_DARK_COLORS, (enabled, _, context) => {
211
+ if (!enabled || !VARS_AVAILABLE) {
212
+ return () => {};
213
+ }
214
+
215
+ return (root, result) => {
216
+ root.walkDecls((decl) => {
217
+ checkDarkColorsUsage(decl, result, context);
218
+ });
219
+ };
220
+ }),
190
221
  ];
191
222
 
192
223
  module.exports.messages = messages;
@@ -194,3 +225,4 @@ module.exports.RULE_USE_VARS = RULE_USE_VARS;
194
225
  module.exports.RULE_USE_ONE_OF_VARS = RULE_USE_ONE_OF_VARS;
195
226
  module.exports.RULE_USE_MIXINS = RULE_USE_MIXINS;
196
227
  module.exports.RULE_USE_ONE_OF_MIXINS = RULE_USE_ONE_OF_MIXINS;
228
+ module.exports.RULE_DO_NOT_USE_DARK_COLORS = RULE_DO_NOT_USE_DARK_COLORS;
package/index.test.js CHANGED
@@ -6,6 +6,7 @@ const {
6
6
  RULE_USE_MIXINS,
7
7
  RULE_USE_ONE_OF_MIXINS,
8
8
  messages,
9
+ RULE_DO_NOT_USE_DARK_COLORS,
9
10
  } = require('.');
10
11
 
11
12
  testRule({
@@ -40,6 +41,15 @@ testRule({
40
41
  }`,
41
42
  description: 'multiline rule',
42
43
  },
44
+ {
45
+ code: `.class {
46
+ .inner {
47
+ padding: var(--gap-xs) 0 var(--gap-m) 0;
48
+ margin: var(--gap-l);
49
+ }
50
+ }`,
51
+ description: 'nested rule',
52
+ },
43
53
  {
44
54
  code: `.class {
45
55
  padding-left: 3px;
@@ -70,6 +80,10 @@ testRule({
70
80
  border-radius: var(--border-radius-xl);
71
81
  border-radius: var(--border-radius-circle);
72
82
  border-radius: var(--border-radius-pill);
83
+ border-top-left-radius: var(--border-radius-s);
84
+ border-top-right-radius: var(--border-radius-s);
85
+ border-bottom-left-radius: var(--border-radius-s);
86
+ border-bottom-right-radius: var(--border-radius-s);
73
87
  }`,
74
88
  description: 'border-radius',
75
89
  },
@@ -87,7 +101,7 @@ testRule({
87
101
  code: `.class {\n padding-top: 8px;\n}`,
88
102
  fixed: `.class {\n padding-top: var(--gap-xs);\n}`,
89
103
  description: 'hardcode single gap',
90
- message: messages[RULE_USE_VARS].expected('--gap-xs', '8px'),
104
+ message: messages[RULE_USE_VARS].expected(['--gap-xs'], '8px'),
91
105
  line: 2,
92
106
  column: 18,
93
107
  },
@@ -97,33 +111,68 @@ testRule({
97
111
  description: 'hardcode multiple gaps',
98
112
  warnings: [
99
113
  {
100
- message: messages[RULE_USE_VARS].expected('--gap-2xs', '4px'),
114
+ message: messages[RULE_USE_VARS].expected(['--gap-2xs'], '4px'),
101
115
  line: 2,
102
116
  column: 23,
103
117
  },
104
118
  {
105
- message: messages[RULE_USE_VARS].expected('--gap-xs', '8px'),
119
+ message: messages[RULE_USE_VARS].expected(['--gap-xs'], '8px'),
106
120
  line: 2,
107
121
  column: 14,
108
122
  },
109
123
  {
110
- message: messages[RULE_USE_VARS].expected('--gap-s', '12px'),
124
+ message: messages[RULE_USE_VARS].expected(['--gap-s'], '12px'),
111
125
  line: 2,
112
126
  column: 18,
113
127
  },
114
128
  {
115
- message: messages[RULE_USE_VARS].expected('--gap-m', '16px'),
129
+ message: messages[RULE_USE_VARS].expected(['--gap-m'], '16px'),
116
130
  line: 2,
117
131
  column: 27,
118
132
  },
119
133
  ],
120
134
  },
135
+ {
136
+ code: `.class {
137
+ .inner {
138
+ padding: 8px 12px 4px 16px;
139
+ }
140
+ }`,
141
+ fixed: `.class {
142
+ .inner {
143
+ padding: var(--gap-xs) var(--gap-s) var(--gap-2xs) var(--gap-m);
144
+ }
145
+ }`,
146
+ description: 'hardcode nested rule',
147
+ warnings: [
148
+ {
149
+ message: messages[RULE_USE_VARS].expected(['--gap-2xs'], '4px'),
150
+ line: 3,
151
+ column: 39,
152
+ },
153
+ {
154
+ message: messages[RULE_USE_VARS].expected(['--gap-xs'], '8px'),
155
+ line: 3,
156
+ column: 30,
157
+ },
158
+ {
159
+ message: messages[RULE_USE_VARS].expected(['--gap-s'], '12px'),
160
+ line: 3,
161
+ column: 34,
162
+ },
163
+ {
164
+ message: messages[RULE_USE_VARS].expected(['--gap-m'], '16px'),
165
+ line: 3,
166
+ column: 43,
167
+ },
168
+ ],
169
+ },
121
170
  {
122
171
  code: `.class {\n box-shadow: 0 0 4px rgba(11, 31, 53, 0.02), 0 2px 4px rgba(11, 31, 53, 0.04);\n}`,
123
172
  fixed: `.class {\n box-shadow: var(--shadow-xs);\n}`,
124
173
  description: 'hardcode singleline shadow',
125
174
  message: messages[RULE_USE_VARS].expected(
126
- '--shadow-xs',
175
+ ['--shadow-xs'],
127
176
  '0 0 4px rgba(11, 31, 53, 0.02), 0 2px 4px rgba(11, 31, 53, 0.04)'
128
177
  ),
129
178
  line: 2,
@@ -135,7 +184,7 @@ testRule({
135
184
  fixed: `.class {\n box-shadow: var(--shadow-xs-hard);\n}`,
136
185
  description: 'hardcode multiline shadow',
137
186
  message: messages[RULE_USE_VARS].expected(
138
- '--shadow-xs-hard',
187
+ ['--shadow-xs-hard'],
139
188
  '0 0 4px rgba(11, 31, 53, 0.02), 0 2px 4px rgba(11, 31, 53, 0.04), 0 2px 4px rgba(11, 31, 53, 0.16)'
140
189
  ),
141
190
  line: 2,
@@ -145,7 +194,7 @@ testRule({
145
194
  code: `.class {\n color: #0b1f35;\n}`,
146
195
  fixed: `.class {\n color: var(--color-light-text-primary);\n}`,
147
196
  description: 'hardcode single color',
148
- message: messages[RULE_USE_VARS].expected('--color-light-text-primary', '#0b1f35'),
197
+ message: messages[RULE_USE_VARS].expected(['--color-light-text-primary'], '#0b1f35'),
149
198
  line: 2,
150
199
  column: 12,
151
200
  },
@@ -155,13 +204,13 @@ testRule({
155
204
  description: 'hardcode multiple props',
156
205
  warnings: [
157
206
  {
158
- message: messages[RULE_USE_VARS].expected('--gap-xs', '8px'),
207
+ message: messages[RULE_USE_VARS].expected(['--gap-xs'], '8px'),
159
208
  line: 2,
160
209
  column: 18,
161
210
  },
162
211
  {
163
212
  message: messages[RULE_USE_VARS].expected(
164
- '--shadow-xs',
213
+ ['--shadow-xs'],
165
214
  '0 0 4px rgba(11, 31, 53, 0.02), 0 2px 4px rgba(11, 31, 53, 0.04)'
166
215
  ),
167
216
  line: 3,
@@ -170,12 +219,21 @@ testRule({
170
219
  ],
171
220
  },
172
221
  {
173
- code: `.class {\n border-radius: 8px;\n}`,
174
- fixed: `.class {\n border-radius: var(--border-radius-m);\n}`,
222
+ code: `.class {\n border-radius: 8px;\n border-top-left-radius: 4px;\n}`,
223
+ fixed: `.class {\n border-radius: var(--border-radius-m);\n border-top-left-radius: var(--border-radius-s);\n}`,
175
224
  description: 'hardcode border-radius',
176
- message: messages[RULE_USE_VARS].expected('--border-radius-m', '8px'),
177
- line: 2,
178
- column: 20,
225
+ warnings: [
226
+ {
227
+ message: messages[RULE_USE_VARS].expected(['--border-radius-m'], '8px'),
228
+ line: 2,
229
+ column: 20,
230
+ },
231
+ {
232
+ message: messages[RULE_USE_VARS].expected(['--border-radius-s'], '4px'),
233
+ line: 3,
234
+ column: 29,
235
+ }
236
+ ]
179
237
  },
180
238
  ...Object.entries(vars.gaps).map(([value, vars]) => {
181
239
  const gapVar = vars[0];
@@ -202,7 +260,7 @@ testRule({
202
260
  }`,
203
261
  description: `should use ${gapVar}`,
204
262
  warnings: props.map((_) => ({
205
- message: messages[RULE_USE_VARS].expected(gapVar, value),
263
+ message: messages[RULE_USE_VARS].expected([gapVar], value),
206
264
  })),
207
265
  };
208
266
  }),
@@ -217,7 +275,7 @@ testRule({
217
275
  box-shadow: var(${shadowVar});
218
276
  }`,
219
277
  description: `should use ${shadowVar}`,
220
- message: messages[RULE_USE_VARS].expected(shadowVar, value),
278
+ message: messages[RULE_USE_VARS].expected([shadowVar], value),
221
279
  };
222
280
  }),
223
281
  ],
@@ -255,6 +313,15 @@ testRule({
255
313
  }`,
256
314
  description: 'multiline rule',
257
315
  },
316
+ {
317
+ code: `.class {
318
+ .inner {
319
+ padding: var(--gap-xs) 0 var(--gap-m) 0;
320
+ margin: var(--gap-l);
321
+ }
322
+ }`,
323
+ description: 'multiline nested rule',
324
+ },
258
325
  {
259
326
  code: `.class {
260
327
  padding-left: 3px;
@@ -285,6 +352,10 @@ testRule({
285
352
  border-radius: var(--border-radius-xl);
286
353
  border-radius: var(--border-radius-circle);
287
354
  border-radius: var(--border-radius-pill);
355
+ border-top-left-radius: var(--border-radius-s);
356
+ border-top-right-radius: var(--border-radius-s);
357
+ border-bottom-left-radius: var(--border-radius-s);
358
+ border-bottom-right-radius: var(--border-radius-s);
288
359
  }`,
289
360
  description: 'border-radius',
290
361
  },
@@ -343,12 +414,60 @@ testRule({
343
414
  ],
344
415
  },
345
416
  {
346
- code: `.class {\n padding-top: 8px;\n}`,
347
- fixed: `.class {\n padding-top: var(--gap-xs);\n}`,
348
- description: 'hardcode single gap',
349
- message: messages[RULE_USE_ONE_OF_VARS].expected(['--gap-xs'], '8px'),
350
- line: 2,
351
- column: 18,
417
+ code: `.class {
418
+ .inner {
419
+ background-color: #fff;
420
+ background: #fff;
421
+ border: 1px solid #0b1f35;
422
+ }
423
+ }`,
424
+ fixed: `.class {
425
+ .inner {
426
+ background-color: #fff;
427
+ background: #fff;
428
+ border: 1px solid #0b1f35;
429
+ }
430
+ }`,
431
+ unfixable: true,
432
+ description: 'hardcode colors in nested rule',
433
+ warnings: [
434
+ {
435
+ column: 39,
436
+ line: 3,
437
+ message: messages[RULE_USE_ONE_OF_VARS].expected(
438
+ [
439
+ '--color-light-bg-primary',
440
+ '--color-light-specialbg-secondary-grouped',
441
+ '--color-light-graphic-primary-inverted',
442
+ ],
443
+ '#fff'
444
+ ),
445
+ },
446
+ {
447
+ column: 33,
448
+ line: 4,
449
+ message: messages[RULE_USE_ONE_OF_VARS].expected(
450
+ [
451
+ '--color-light-bg-primary',
452
+ '--color-light-specialbg-secondary-grouped',
453
+ '--color-light-graphic-primary-inverted',
454
+ ],
455
+ '#fff'
456
+ ),
457
+ },
458
+ {
459
+ column: 39,
460
+ line: 5,
461
+ message: messages[RULE_USE_ONE_OF_VARS].expected(
462
+ [
463
+ '--color-light-border-key',
464
+ '--color-light-graphic-primary',
465
+ '--color-light-bg-primary-inverted',
466
+ ],
467
+ '#0b1f35'
468
+ ),
469
+ },
470
+ ],
352
471
  },
353
472
  ],
354
473
  });
@@ -371,6 +490,14 @@ testRule({
371
490
  }`,
372
491
  description: 'not exact match',
373
492
  },
493
+ {
494
+ code: `.class {
495
+ .inner {
496
+ @mixin headline_xlarge;
497
+ }
498
+ }`,
499
+ description: 'typography nested rule',
500
+ },
374
501
  ],
375
502
  reject: [
376
503
  {
@@ -389,7 +516,7 @@ testRule({
389
516
  color: var(--color-light-text-primary);
390
517
  }`,
391
518
  description: 'typography',
392
- message: messages[RULE_USE_MIXINS].expected({ name: 'headline_xlarge' }),
519
+ message: messages[RULE_USE_MIXINS].expected([{ name: 'headline_xlarge' }]),
393
520
  line: 3,
394
521
  column: 17,
395
522
  },
@@ -403,7 +530,7 @@ testRule({
403
530
  @mixin headline_xlarge;
404
531
  }`,
405
532
  description: 'typography',
406
- message: messages[RULE_USE_MIXINS].expected({ name: 'headline_xlarge' }),
533
+ message: messages[RULE_USE_MIXINS].expected([{ name: 'headline_xlarge' }]),
407
534
  line: 2,
408
535
  column: 17,
409
536
  },
@@ -417,10 +544,28 @@ testRule({
417
544
  @mixin paragraph_primary_small;
418
545
  }`,
419
546
  description: 'typography',
420
- message: messages[RULE_USE_MIXINS].expected({ name: 'paragraph_primary_small' }),
547
+ message: messages[RULE_USE_MIXINS].expected([{ name: 'paragraph_primary_small' }]),
421
548
  line: 2,
422
549
  column: 17,
423
550
  },
551
+ {
552
+ code: `.class {
553
+ .inner {
554
+ font-size: 48px;
555
+ line-height: 64px;
556
+ font-weight: 500;
557
+ }
558
+ }`,
559
+ fixed: `.class {
560
+ .inner {
561
+ @mixin headline_xlarge;
562
+ }
563
+ }`,
564
+ description: 'typography nested rule',
565
+ message: messages[RULE_USE_MIXINS].expected([{ name: 'headline_xlarge' }]),
566
+ line: 3,
567
+ column: 21,
568
+ },
424
569
  ],
425
570
  });
426
571
 
@@ -482,26 +627,75 @@ testRule({
482
627
  },
483
628
  {
484
629
  code: `.class {
485
- background-color: var(--color-light-bg-primary);
486
- font-size: 48px;
487
- line-height: 64px;
488
- background: var(--color-light-bg-primary);
489
- font-weight: 500;
490
- color: var(--color-light-text-primary);
630
+ .inner {
631
+ font-size: 14px;
632
+ }
491
633
  }`,
492
634
  fixed: `.class {
493
- @mixin headline_xlarge;
494
- background-color: var(--color-light-bg-primary);
495
- background: var(--color-light-bg-primary);
496
- color: var(--color-light-text-primary);
635
+ .inner {
636
+ font-size: 14px;
637
+ }
497
638
  }`,
498
- description: 'typography',
499
- message: messages[RULE_USE_ONE_OF_MIXINS].expected({
500
- name: 'headline_xlarge',
501
- props: mixins.typography['headline_xlarge'],
502
- }),
639
+ unfixable: true,
640
+ description: 'typography nested rule',
641
+ message: messages[RULE_USE_ONE_OF_MIXINS].expected([
642
+ {
643
+ name: 'paragraph_primary_small',
644
+ props: mixins.typography['paragraph_primary_small'],
645
+ },
646
+ { name: 'accent_primary_small', props: mixins.typography['accent_primary_small'] },
647
+ { name: 'action_primary_small', props: mixins.typography['action_primary_small'] },
648
+ ]),
503
649
  line: 3,
504
- column: 17,
650
+ column: 21,
651
+ },
652
+ ],
653
+ });
654
+
655
+ testRule({
656
+ plugins: [RULE_DO_NOT_USE_DARK_COLORS],
657
+ ruleName: RULE_DO_NOT_USE_DARK_COLORS,
658
+ config: true,
659
+ fix: true,
660
+ accept: [
661
+ {
662
+ code: `.class {
663
+ color: var(--color-light-text-primary);
664
+ border: 1px solid var(--color-light-border-primary);
665
+ }`,
666
+ description: 'allowed colors',
667
+ },
668
+ {
669
+ code: `.class {
670
+ color: var(--color-dark-indigo);
671
+ }`,
672
+ description: 'old colors',
673
+ },
674
+ ],
675
+ reject: [
676
+ {
677
+ code: `.class {
678
+ color: var(--color-dark-text-primary);
679
+ border: 1px solid var(--color-dark-border-primary);
680
+ }`,
681
+ fixed: `.class {
682
+ color: var(--color-dark-text-primary);
683
+ border: 1px solid var(--color-dark-border-primary);
684
+ }`,
685
+ unfixable: true,
686
+ description: 'dark colors',
687
+ warnings: [
688
+ {
689
+ column: 28,
690
+ line: 2,
691
+ message: messages[RULE_DO_NOT_USE_DARK_COLORS].expected(),
692
+ },
693
+ {
694
+ column: 39,
695
+ line: 3,
696
+ message: messages[RULE_DO_NOT_USE_DARK_COLORS].expected(),
697
+ },
698
+ ],
505
699
  },
506
700
  ],
507
701
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alfalab/stylelint-core-vars",
3
- "version": "1.0.0",
3
+ "version": "1.4.0",
4
4
  "description": "core-components css linter",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  "author": "reme3d2y",
10
10
  "license": "ISC",
11
11
  "devDependencies": {
12
- "@alfalab/core-components": "^15.2.1",
12
+ "@alfalab/core-components-vars": "^3.0.1",
13
13
  "jest": "^26.6.3",
14
14
  "jest-preset-stylelint": "^3.0.0",
15
15
  "stylelint": "^13.8.0"
package/utils.js CHANGED
@@ -2,6 +2,7 @@ const fs = require('fs');
2
2
  const path = require('path');
3
3
 
4
4
  const CORE_COMPONENTS_PACKAGE = '@alfalab/core-components';
5
+ const CORE_COMPONENTS_VARS_PACKAGE = '@alfalab/core-components-vars';
5
6
  const VAR_RE = /(?:^|\n)\s+(--[-\w]+):\s*([\s\S]+?);/gm;
6
7
  const MIXIN_RE = /@define-mixin (.*?) {(.*?)}/g;
7
8
  const BORDERS = {
@@ -18,6 +19,12 @@ const vars = {
18
19
  borderRadiuses: loadVars('border-radius.css'),
19
20
  };
20
21
 
22
+ const colorsSet = Object.values(vars.colors).reduce((acc, vars) => {
23
+ vars.forEach(colorVar => acc.add(colorVar));
24
+
25
+ return acc;
26
+ }, new Set());
27
+
21
28
  const mixins = {
22
29
  typography: loadMixins('typography.css'),
23
30
  };
@@ -43,16 +50,25 @@ const varsByProperties = {
43
50
  'border-bottom': vars.colors,
44
51
  'border-left': vars.colors,
45
52
  'border-radius': vars.borderRadiuses,
53
+ 'border-top-left-radius': vars.borderRadiuses,
54
+ 'border-top-right-radius': vars.borderRadiuses,
55
+ 'border-bottom-left-radius': vars.borderRadiuses,
56
+ 'border-bottom-right-radius': vars.borderRadiuses,
46
57
  };
47
58
 
48
- const VARS_AVAILABLE = coreComponentsInstalled() || runInsideCoreComponents();
59
+ const VARS_AVAILABLE = Boolean(getInstalledVarsPackage()) || runInsideCoreComponents();
49
60
 
50
- function coreComponentsInstalled() {
61
+ function getInstalledVarsPackage() {
51
62
  try {
52
63
  require.resolve(`${CORE_COMPONENTS_PACKAGE}/package.json`);
53
- return true;
64
+ return CORE_COMPONENTS_PACKAGE;
54
65
  } catch (e) {
55
- return false;
66
+ try {
67
+ require.resolve(`${CORE_COMPONENTS_VARS_PACKAGE}/package.json`);
68
+ return CORE_COMPONENTS_VARS_PACKAGE;
69
+ } catch (e) {
70
+ return false;
71
+ }
56
72
  }
57
73
  }
58
74
 
@@ -67,8 +83,14 @@ function runInsideCoreComponents() {
67
83
  }
68
84
 
69
85
  function resolveVarsFile(file) {
70
- if (coreComponentsInstalled()) {
71
- return fs.readFileSync(require.resolve(`${CORE_COMPONENTS_PACKAGE}/vars/${file}`));
86
+ const packageName = getInstalledVarsPackage();
87
+ if (packageName) {
88
+ const fullPath =
89
+ packageName === CORE_COMPONENTS_PACKAGE
90
+ ? `${packageName}/vars/${file}`
91
+ : `${packageName}/${file}`;
92
+
93
+ return fs.readFileSync(require.resolve(fullPath));
72
94
  } else {
73
95
  return fs.readFileSync(path.resolve(__dirname, `../../../packages/vars/src/${file}`));
74
96
  }
@@ -147,7 +169,7 @@ function findInValue(haystack, needle, fromIndex) {
147
169
  );
148
170
  }
149
171
 
150
- function choiceVar(variables, prop, group) {
172
+ function choiceVars(variables, prop, group) {
151
173
  if (group === 'colors') {
152
174
  const colorVariants = () => {
153
175
  switch (prop) {
@@ -172,37 +194,36 @@ function choiceVar(variables, prop, group) {
172
194
  const condition = (variable) =>
173
195
  variants.some((variant) => variable.startsWith(`--color-light-${variant}`));
174
196
 
175
- const result = sortVarsByUsage(variables, variants).filter(condition);
176
-
177
- return result.length <= 1 ? result[0] : result;
197
+ return sortVarsByUsage(variables, variants).filter(condition);
178
198
  }
179
199
 
180
- return variables[0];
200
+ return variables;
181
201
  }
182
202
 
183
- function findVar(cssValue, prop) {
203
+ function findVars(cssValue, prop) {
184
204
  const vars = varsByProperties[prop];
185
205
  if (!vars) return;
186
206
 
187
207
  const group = getVarsGroup(vars);
188
208
 
189
209
  for (const [value, variables] of Object.entries(vars)) {
190
- const variable = choiceVar(variables, prop, group);
210
+ const chosen = choiceVars(variables, prop, group);
191
211
 
192
- if (!variable) continue;
212
+ if (!chosen || !chosen.length) continue;
193
213
 
194
214
  const index = findInValue(cssValue, value);
215
+
195
216
  if (index !== false) {
196
217
  return {
197
218
  index,
198
219
  value,
199
- variable,
220
+ variables: chosen,
200
221
  };
201
222
  }
202
223
  }
203
224
  }
204
225
 
205
- function findTypographyMixin(ruleProps) {
226
+ function findTypographyMixins(ruleProps) {
206
227
  const findMixin = (exact) => {
207
228
  return Object.entries(mixins.typography)
208
229
  .filter(([_, mixinProps]) => {
@@ -219,7 +240,7 @@ function findTypographyMixin(ruleProps) {
219
240
 
220
241
  const exact = findMixin(true);
221
242
  if (exact.length) {
222
- return exact[0];
243
+ return exact;
223
244
  } else {
224
245
  const mixins = findMixin(false);
225
246
  return mixins.length > 0 ? mixins : null;
@@ -247,8 +268,9 @@ function sortVarsByUsage(arr, sortingArr) {
247
268
 
248
269
  module.exports.VARS_AVAILABLE = VARS_AVAILABLE;
249
270
  module.exports.vars = vars;
271
+ module.exports.colorsSet = colorsSet;
250
272
  module.exports.mixins = mixins;
251
- module.exports.findVar = findVar;
273
+ module.exports.findVars = findVars;
252
274
  module.exports.formatVar = formatVar;
253
275
  module.exports.toOneLine = toOneLine;
254
- module.exports.findTypographyMixin = findTypographyMixin;
276
+ module.exports.findTypographyMixins = findTypographyMixins;