@alfalab/stylelint-core-vars 1.3.0 → 1.5.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
@@ -1,19 +1,19 @@
1
1
  # stylelint-core-vars
2
2
 
3
- [![NPM Version](https://img.shields.io/npm/v/stylelint-core-vars.svg)](https://www.npmjs.com/package/stylelint-core-vars)
3
+ [![NPM Version](https://img.shields.io/npm/v/@alfalab/stylelint-core-vars.svg)](https://www.npmjs.com/package/@alfalab/stylelint-core-vars)
4
4
 
5
5
  [Stylelint](http://stylelint.io) плагин, проверяющий использование [дизайн-токенов](https://github.com/alfa-laboratory/core-components/tree/master/packages/vars/src)
6
6
 
7
7
  ## Установка
8
8
 
9
9
  ```
10
- yarn add --dev stylelint stylelint-core-vars
10
+ yarn add --dev stylelint @alfalab/stylelint-core-vars
11
11
  ```
12
12
 
13
13
  или
14
14
 
15
15
  ```
16
- npm install --save-dev stylelint stylelint-core-vars
16
+ npm install --save-dev stylelint @alfalab/stylelint-core-vars
17
17
  ```
18
18
 
19
19
  ## Использование
@@ -45,7 +45,13 @@ npm install --save-dev stylelint stylelint-core-vars
45
45
  {
46
46
  "severity": "warning"
47
47
  }
48
- ]
48
+ ],
49
+ "stylelint-core-vars/do-not-use-old-vars": [
50
+ true,
51
+ {
52
+ "severity": "warning"
53
+ }
54
+ ],
49
55
  },
50
56
  }
51
57
  ```
package/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  const stylelint = require('stylelint');
2
+ const { findOldVars } = require('./utils');
2
3
  const toOneLine = require('./utils').toOneLine;
3
- const formatVar = require('./utils').formatVar;
4
4
  const findVars = require('./utils').findVars;
5
+ const colorsSet = require('./utils').colorsSet;
5
6
  const findTypographyMixins = require('./utils').findTypographyMixins;
6
7
  const VARS_AVAILABLE = require('./utils').VARS_AVAILABLE;
7
8
 
@@ -10,20 +11,36 @@ 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';
12
13
  const RULE_DO_NOT_USE_DARK_COLORS = 'stylelint-core-vars/do-not-use-dark-colors';
14
+ const RULE_DO_NOT_USE_OLD_VARS = 'stylelint-core-vars/do-not-use-old-vars';
13
15
 
14
16
  const messages = {
15
17
  [RULE_USE_VARS]: stylelint.utils.ruleMessages(RULE_USE_VARS, {
16
18
  expected: (variable, value) => {
17
- return `Use variable '${variable}' instead of plain value '${value}'`;
19
+ return `Use variable 'var(${variable})' instead of plain value '${value}'`;
18
20
  },
19
21
  }),
20
22
  [RULE_USE_ONE_OF_VARS]: stylelint.utils.ruleMessages(RULE_USE_ONE_OF_VARS, {
21
23
  expected: (variables, value) => {
22
- const variablesPart = variables.map((v) => `${v}`).join('\n');
24
+ const variablesPart = variables.map((v) => `var(${v})`).join('\n');
23
25
 
24
26
  return `Use variables instead of plain value '${value}':\n${variablesPart}\n`;
25
27
  },
26
28
  }),
29
+ [RULE_DO_NOT_USE_OLD_VARS]: stylelint.utils.ruleMessages(RULE_DO_NOT_USE_OLD_VARS, {
30
+ expected: (variables, value, fixable = true) => {
31
+ if (!variables.length) return `Ask you designer how to replace old token '${value}'`;
32
+
33
+ const startMsg = fixable ? 'Use' : 'Cant find proper token, but you can use';
34
+
35
+ if (variables.length === 1) {
36
+ return `${startMsg} variable 'var(${variables[0]})' instead of old '${value}'`;
37
+ }
38
+
39
+ const variablesPart = variables.map((v) => `var(${v})`).join('\n');
40
+
41
+ return `${startMsg} one of new variables instead of old '${value}':\n${variablesPart}\n`;
42
+ },
43
+ }),
27
44
  [RULE_USE_MIXINS]: stylelint.utils.ruleMessages(RULE_USE_MIXINS, {
28
45
  expected: (mixin) => {
29
46
  return `Use mixin '${mixin.name}' instead of plain values`;
@@ -44,23 +61,20 @@ const messages = {
44
61
  }),
45
62
  };
46
63
 
47
- const checkVars = (decl, result, context, ruleName) => {
64
+ const checkVars = (decl, result, context, ruleName, matcher, shouldReport) => {
48
65
  const { prop, raws } = decl;
49
66
 
50
67
  let value = toOneLine(decl.value);
51
-
52
68
  let substitution;
69
+
53
70
  const previousValues = [];
54
71
 
55
- while ((substitution = findVars(value, prop))) {
72
+ while ((substitution = matcher(value, prop))) {
56
73
  let fixed = false;
57
74
 
58
- const exactVar = substitution.variables.length === 1;
59
- const fixedValue = formatVar(substitution.variables[0]);
60
-
61
- value = value.replace(substitution.value, fixedValue);
75
+ value = value.replace(substitution.value, substitution.fixedValue);
62
76
 
63
- if (context.fix && exactVar) {
77
+ if (context.fix && substitution.fixable) {
64
78
  decl.value = value;
65
79
  fixed = true;
66
80
  }
@@ -70,16 +84,15 @@ const checkVars = (decl, result, context, ruleName) => {
70
84
  substitution.index
71
85
  );
72
86
 
73
- const shouldReport =
74
- !fixed &&
75
- ((ruleName === RULE_USE_ONE_OF_VARS && !exactVar) ||
76
- (ruleName === RULE_USE_VARS && exactVar));
77
-
78
- if (shouldReport) {
87
+ if (shouldReport(substitution.fixable, fixed)) {
79
88
  stylelint.utils.report({
80
89
  result,
81
90
  ruleName,
82
- message: messages[ruleName].expected(substitution.variables, substitution.value),
91
+ message: messages[ruleName].expected(
92
+ substitution.variables,
93
+ substitution.value,
94
+ substitution.fixable
95
+ ),
83
96
  node: decl,
84
97
  word: value,
85
98
  index: originalValueIndex + prop.length + raws.between.length,
@@ -88,7 +101,7 @@ const checkVars = (decl, result, context, ruleName) => {
88
101
 
89
102
  previousValues.unshift({
90
103
  ...substitution,
91
- diff: fixedValue.length - substitution.value.length,
104
+ diff: substitution.fixedValue.length - substitution.value.length,
92
105
  });
93
106
  }
94
107
  };
@@ -149,7 +162,7 @@ const checkDarkColorsUsage = (decl, result, context, ruleName) => {
149
162
 
150
163
  const matches = /--color-dark-[\w-]+/.exec(value);
151
164
 
152
- if (matches) {
165
+ if (matches && colorsSet.has(matches[0])) {
153
166
  stylelint.utils.report({
154
167
  result,
155
168
  ruleName: RULE_DO_NOT_USE_DARK_COLORS,
@@ -169,7 +182,14 @@ module.exports = [
169
182
 
170
183
  return (root, result) => {
171
184
  root.walkDecls((decl) => {
172
- checkVars(decl, result, context, RULE_USE_VARS);
185
+ checkVars(
186
+ decl,
187
+ result,
188
+ context,
189
+ RULE_USE_VARS,
190
+ findVars,
191
+ (fixable, fixed) => fixable && !fixed
192
+ );
173
193
  });
174
194
  };
175
195
  }),
@@ -180,7 +200,32 @@ module.exports = [
180
200
 
181
201
  return (root, result) => {
182
202
  root.walkDecls((decl) => {
183
- checkVars(decl, result, context, RULE_USE_ONE_OF_VARS);
203
+ checkVars(
204
+ decl,
205
+ result,
206
+ context,
207
+ RULE_USE_ONE_OF_VARS,
208
+ findVars,
209
+ (fixable, fixed) => !fixable && !fixed
210
+ );
211
+ });
212
+ };
213
+ }),
214
+ stylelint.createPlugin(RULE_DO_NOT_USE_OLD_VARS, (enabled, _, context) => {
215
+ if (!enabled || !VARS_AVAILABLE) {
216
+ return () => {};
217
+ }
218
+
219
+ return (root, result) => {
220
+ root.walkDecls((decl) => {
221
+ checkVars(
222
+ decl,
223
+ result,
224
+ context,
225
+ RULE_DO_NOT_USE_OLD_VARS,
226
+ findOldVars,
227
+ (_, fixed) => !fixed
228
+ );
184
229
  });
185
230
  };
186
231
  }),
@@ -225,3 +270,4 @@ module.exports.RULE_USE_ONE_OF_VARS = RULE_USE_ONE_OF_VARS;
225
270
  module.exports.RULE_USE_MIXINS = RULE_USE_MIXINS;
226
271
  module.exports.RULE_USE_ONE_OF_MIXINS = RULE_USE_ONE_OF_MIXINS;
227
272
  module.exports.RULE_DO_NOT_USE_DARK_COLORS = RULE_DO_NOT_USE_DARK_COLORS;
273
+ module.exports.RULE_DO_NOT_USE_OLD_VARS = RULE_DO_NOT_USE_OLD_VARS;
package/index.test.js CHANGED
@@ -5,8 +5,9 @@ const {
5
5
  RULE_USE_ONE_OF_VARS,
6
6
  RULE_USE_MIXINS,
7
7
  RULE_USE_ONE_OF_MIXINS,
8
- messages,
9
8
  RULE_DO_NOT_USE_DARK_COLORS,
9
+ RULE_DO_NOT_USE_OLD_VARS,
10
+ messages,
10
11
  } = require('.');
11
12
 
12
13
  testRule({
@@ -80,6 +81,10 @@ testRule({
80
81
  border-radius: var(--border-radius-xl);
81
82
  border-radius: var(--border-radius-circle);
82
83
  border-radius: var(--border-radius-pill);
84
+ border-top-left-radius: var(--border-radius-s);
85
+ border-top-right-radius: var(--border-radius-s);
86
+ border-bottom-left-radius: var(--border-radius-s);
87
+ border-bottom-right-radius: var(--border-radius-s);
83
88
  }`,
84
89
  description: 'border-radius',
85
90
  },
@@ -215,12 +220,21 @@ testRule({
215
220
  ],
216
221
  },
217
222
  {
218
- code: `.class {\n border-radius: 8px;\n}`,
219
- fixed: `.class {\n border-radius: var(--border-radius-m);\n}`,
223
+ code: `.class {\n border-radius: 8px;\n border-top-left-radius: 4px;\n}`,
224
+ fixed: `.class {\n border-radius: var(--border-radius-m);\n border-top-left-radius: var(--border-radius-s);\n}`,
220
225
  description: 'hardcode border-radius',
221
- message: messages[RULE_USE_VARS].expected(['--border-radius-m'], '8px'),
222
- line: 2,
223
- column: 20,
226
+ warnings: [
227
+ {
228
+ message: messages[RULE_USE_VARS].expected(['--border-radius-m'], '8px'),
229
+ line: 2,
230
+ column: 20,
231
+ },
232
+ {
233
+ message: messages[RULE_USE_VARS].expected(['--border-radius-s'], '4px'),
234
+ line: 3,
235
+ column: 29,
236
+ },
237
+ ],
224
238
  },
225
239
  ...Object.entries(vars.gaps).map(([value, vars]) => {
226
240
  const gapVar = vars[0];
@@ -339,6 +353,10 @@ testRule({
339
353
  border-radius: var(--border-radius-xl);
340
354
  border-radius: var(--border-radius-circle);
341
355
  border-radius: var(--border-radius-pill);
356
+ border-top-left-radius: var(--border-radius-s);
357
+ border-top-right-radius: var(--border-radius-s);
358
+ border-bottom-left-radius: var(--border-radius-s);
359
+ border-bottom-right-radius: var(--border-radius-s);
342
360
  }`,
343
361
  description: 'border-radius',
344
362
  },
@@ -455,6 +473,82 @@ testRule({
455
473
  ],
456
474
  });
457
475
 
476
+ testRule({
477
+ plugins: [RULE_DO_NOT_USE_OLD_VARS],
478
+ ruleName: RULE_DO_NOT_USE_OLD_VARS,
479
+ config: true,
480
+ fix: true,
481
+ accept: [
482
+ {
483
+ code: `.class {
484
+ color: #000;
485
+ }`,
486
+ description: 'custom value',
487
+ },
488
+ {
489
+ code: `.class {
490
+ background-color: var(--color-light-bg-primary);
491
+ background: var(--color-light-bg-primary);
492
+ color: var(--color-light-text-primary);
493
+ }`,
494
+ description: 'colors',
495
+ },
496
+ ],
497
+ reject: [
498
+ {
499
+ code: `.class {\n background-color: var(--color-dark-indigo-10-flat);\n}`,
500
+ fixed: `.class {\n background-color: var(--color-light-bg-tertiary);\n}`,
501
+ message: messages[RULE_DO_NOT_USE_OLD_VARS].expected(
502
+ ['--color-light-bg-tertiary'],
503
+ '--color-dark-indigo-10-flat'
504
+ ),
505
+ line: 2,
506
+ column: 27,
507
+ },
508
+ {
509
+ code: `.class {\n background-color: var(--color-red-brand-85);\n}`,
510
+ fixed: `.class {\n background-color: var(--color-red-brand-85);\n}`,
511
+ unfixable: true,
512
+ message: messages[RULE_DO_NOT_USE_OLD_VARS].expected([], ['--color-red-brand-85']),
513
+ line: 2,
514
+ column: 27,
515
+ },
516
+ {
517
+ code: `.class {
518
+ color: var(--color-red-brand-10-flat);
519
+ background: var(--color-red-dark);
520
+ fill: var(---color-black-10);
521
+ }`,
522
+ fixed: `.class {
523
+ color: var(--color-red-brand-10-flat);
524
+ background: var(--color-light-graphic-negative);
525
+ fill: var(---color-black-10);
526
+ }`,
527
+ description: 'multiple variants',
528
+ warnings: [
529
+ {
530
+ column: 28,
531
+ line: 2,
532
+ message: messages[RULE_DO_NOT_USE_OLD_VARS].expected(
533
+ ['--color-light-bg-negative-muted'],
534
+ '--color-red-brand-10-flat',
535
+ false
536
+ ),
537
+ },
538
+ {
539
+ column: 33,
540
+ line: 3,
541
+ message: messages[RULE_DO_NOT_USE_OLD_VARS].expected(
542
+ ['--color-light-graphic-negative'],
543
+ '--color-red-dark',
544
+ true
545
+ ),
546
+ },
547
+ ],
548
+ },
549
+ ],
550
+ });
551
+
458
552
  testRule({
459
553
  plugins: [RULE_USE_MIXINS],
460
554
  ruleName: RULE_USE_MIXINS,
@@ -602,8 +696,20 @@ testRule({
602
696
  name: 'paragraph_primary_small',
603
697
  props: mixins.typography['paragraph_primary_small'],
604
698
  },
699
+ {
700
+ name: 'paragraph_component_secondary',
701
+ props: mixins.typography['paragraph_component_secondary'],
702
+ },
605
703
  { name: 'accent_primary_small', props: mixins.typography['accent_primary_small'] },
704
+ {
705
+ name: 'accent_component_secondary',
706
+ props: mixins.typography['accent_component_secondary'],
707
+ },
606
708
  { name: 'action_primary_small', props: mixins.typography['action_primary_small'] },
709
+ {
710
+ name: 'action_component_secondary',
711
+ props: mixins.typography['action_component_secondary'],
712
+ },
607
713
  ]),
608
714
  line: 2,
609
715
  column: 17,
@@ -626,8 +732,20 @@ testRule({
626
732
  name: 'paragraph_primary_small',
627
733
  props: mixins.typography['paragraph_primary_small'],
628
734
  },
735
+ {
736
+ name: 'paragraph_component_secondary',
737
+ props: mixins.typography['paragraph_component_secondary'],
738
+ },
629
739
  { name: 'accent_primary_small', props: mixins.typography['accent_primary_small'] },
740
+ {
741
+ name: 'accent_component_secondary',
742
+ props: mixins.typography['accent_component_secondary'],
743
+ },
630
744
  { name: 'action_primary_small', props: mixins.typography['action_primary_small'] },
745
+ {
746
+ name: 'action_component_secondary',
747
+ props: mixins.typography['action_component_secondary'],
748
+ },
631
749
  ]),
632
750
  line: 3,
633
751
  column: 21,
@@ -648,6 +766,12 @@ testRule({
648
766
  }`,
649
767
  description: 'allowed colors',
650
768
  },
769
+ {
770
+ code: `.class {
771
+ color: var(--color-dark-indigo);
772
+ }`,
773
+ description: 'old colors',
774
+ },
651
775
  ],
652
776
  reject: [
653
777
  {
package/old-new-map.js ADDED
@@ -0,0 +1,122 @@
1
+ module.exports = {
2
+ '--color-dark-indigo': [
3
+ '--color-light-text-primary',
4
+ '--color-light-bg-primary-inverted',
5
+ '--color-light-border-key',
6
+ '--color-light-graphic-primary',
7
+ ],
8
+ '--color-dark-indigo-60-flat': [
9
+ '--color-light-text-secondary',
10
+ '--color-light-border-tertiary-inverted',
11
+ '--color-light-graphic-secondary',
12
+ ],
13
+ '--color-dark-indigo-30-flat': [
14
+ '--color-light-text-tertiary',
15
+ '--color-light-border-secondary-inverted',
16
+ '--color-light-graphic-tertiary',
17
+ ],
18
+ '--color-dark-indigo-15-flat': [
19
+ '--color-light-text-disabled',
20
+ '--color-light-bg-quaternary',
21
+ '--color-light-border-primary',
22
+ '--color-light-graphic-quaternary',
23
+ ],
24
+ '--color-dark-indigo-10-flat': ['--color-light-bg-tertiary', '--color-light-border-secondary'],
25
+ '--color-dark-indigo-07-flat': ['--color-light-specialbg-component'],
26
+ '--color-dark-indigo-05-flat': [
27
+ '--color-light-bg-secondary',
28
+ '--color-light-border-tertiary',
29
+ '--color-light-graphic-primary-inverted',
30
+ ],
31
+ '--color-red-brand': [
32
+ '--color-light-text-accent',
33
+ '--color-light-bg-accent',
34
+ '--color-light-border-accent',
35
+ '--color-light-graphic-accent',
36
+ ],
37
+ '--color-red-brand-85-flat': [],
38
+ '--color-red-brand-70-flat': [],
39
+ '--color-red-brand-50-flat': [],
40
+ '--color-red-brand-30-flat': [],
41
+ '--color-red-brand-20-flat': [],
42
+ '--color-red-brand-10-flat': ['--color-light-bg-negative-muted'],
43
+ '--color-red-brand-05-flat': ['--color-light-bg-negative-muted'],
44
+ '--color-red-dark': ['--color-light-text-negative', '--color-light-graphic-negative'],
45
+ '--color-red-error': ['--color-light-text-negative', '--color-light-graphic-negative'],
46
+ '--color-green-text': ['--color-light-text-positive'],
47
+ '--color-green-text-60-flat': [],
48
+ '--color-green-done': ['--color-light-graphic-positive'],
49
+ '--color-green-done-20-flat': [],
50
+ '--color-green-done-10-flat': ['--color-light-bg-positive-muted'],
51
+ '--color-green-done-05-flat': ['--color-light-bg-positive-muted'],
52
+ '--color-blue-action': [],
53
+ '--color-blue-swipe-a': [],
54
+ '--color-blue-swipe-b': [],
55
+ '--color-blue-swipe-c': [],
56
+ '--color-blue-link-mobile': [
57
+ '--color-light-text-link',
58
+ '--color-light-graphic-link',
59
+ '--color-light-border-link',
60
+ ],
61
+ '--color-blue-link-mobile-10-flat': ['--color-light-bg-info'],
62
+ '--color-blue-link-web': [
63
+ '--color-light-text-link',
64
+ '--color-light-graphic-link',
65
+ '--color-light-border-link',
66
+ ],
67
+ '--color-sand': ['--color-static-graphic-sand'],
68
+ '--color-lavender': ['--color-static-graphic-lavender'],
69
+ '--color-amethyst': ['--color-static-graphic-amethyst'],
70
+ '--color-black': [],
71
+ '--color-black-60-flat': ['--color-light-bg-overlay'],
72
+ '--color-black-30-flat': [],
73
+ '--color-black-10-flat': [],
74
+ '--color-white': [
75
+ '--color-light-text-primary-inverted',
76
+ '--color-light-bg-primary',
77
+ '--color-light-border-key-inverted',
78
+ ],
79
+ '--color-bg-corporate': ['--color-static-corpbg-primary'],
80
+ '--color-bg-corporate-90-flat': ['--color-static-corpbg-secondary'],
81
+ '--color-bg-corporate-60-flat': ['--color-static-corpbg-tertiary'],
82
+ '--color-bg-corporate-30-flat': ['--color-static-corpbg-neutral'],
83
+ '--color-light-blue': ['--color-static-local-light-blue'],
84
+ '--color-dark-indigo-60': [
85
+ '--color-light-text-secondary-transparent',
86
+ '--color-light-border-tertiary-inverted',
87
+ '--color-light-graphic-secondary',
88
+ ],
89
+ '--color-dark-indigo-30': [
90
+ '--color-light-text-tertiary',
91
+ '--color-light-border-secondary-inverted',
92
+ '--color-light-graphic-tertiary',
93
+ ],
94
+ '--color-dark-indigo-15': ['--color-light-text-disabled'],
95
+ '--color-dark-indigo-10': ['--color-light-bg-tertiary', '--color-light-border-secondary'],
96
+ '--color-dark-indigo-07': ['--color-light-specialbg-component'],
97
+ '--color-dark-indigo-05': [
98
+ '--color-light-bg-secondary',
99
+ '--color-light-border-tertiary',
100
+ '--color-light-graphic-primary-inverted',
101
+ ],
102
+ '--color-red-brand-85': [],
103
+ '--color-red-brand-70': [],
104
+ '--color-red-brand-50': [],
105
+ '--color-red-brand-30': [],
106
+ '--color-red-brand-20': [],
107
+ '--color-red-brand-10': ['--color-light-bg-negative-muted'],
108
+ '--color-green-text-60': [],
109
+ '--color-green-done-20': [],
110
+ '--color-green-done-10': ['--color-light-bg-positive-muted'],
111
+ '--color-green-done-05': ['--color-light-bg-positive-muted'],
112
+ '--color-blue-link-mobile-10': ['--color-light-bg-info'],
113
+ '--color-black-60': ['--color-light-bg-overlay'],
114
+ '--color-black-30': [],
115
+ '--color-black-10': [],
116
+ '--color-white-60': ['--color-light-text-secondary-inverted', '--color-light-bg-opaque'],
117
+ '--color-white-30': ['--color-light-text-tertiary-inverted'],
118
+ '--color-white-10': [],
119
+ '--color-bg-corporate-90': ['--color-static-corpbg-secondary'],
120
+ '--color-bg-corporate-60': ['--color-static-corpbg-tertiary'],
121
+ '--color-bg-corporate-30': ['--color-static-corpbg-neutral'],
122
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alfalab/stylelint-core-vars",
3
- "version": "1.3.0",
3
+ "version": "1.5.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-vars": "^3.0.1",
12
+ "@alfalab/core-components-vars": "^7.0.0",
13
13
  "jest": "^26.6.3",
14
14
  "jest-preset-stylelint": "^3.0.0",
15
15
  "stylelint": "^13.8.0"
package/utils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
+ const oldNewMap = require('./old-new-map');
3
4
 
4
5
  const CORE_COMPONENTS_PACKAGE = '@alfalab/core-components';
5
6
  const CORE_COMPONENTS_VARS_PACKAGE = '@alfalab/core-components-vars';
@@ -19,6 +20,12 @@ const vars = {
19
20
  borderRadiuses: loadVars('border-radius.css'),
20
21
  };
21
22
 
23
+ const colorsSet = Object.values(vars.colors).reduce((acc, vars) => {
24
+ vars.forEach((colorVar) => acc.add(colorVar));
25
+
26
+ return acc;
27
+ }, new Set());
28
+
22
29
  const mixins = {
23
30
  typography: loadMixins('typography.css'),
24
31
  };
@@ -36,6 +43,7 @@ const varsByProperties = {
36
43
  'margin-left': vars.gaps,
37
44
  'box-shadow': vars.shadows,
38
45
  color: vars.colors,
46
+ fill: vars.colors,
39
47
  background: vars.colors,
40
48
  'background-color': vars.colors,
41
49
  border: vars.colors,
@@ -44,6 +52,10 @@ const varsByProperties = {
44
52
  'border-bottom': vars.colors,
45
53
  'border-left': vars.colors,
46
54
  'border-radius': vars.borderRadiuses,
55
+ 'border-top-left-radius': vars.borderRadiuses,
56
+ 'border-top-right-radius': vars.borderRadiuses,
57
+ 'border-bottom-left-radius': vars.borderRadiuses,
58
+ 'border-bottom-right-radius': vars.borderRadiuses,
47
59
  };
48
60
 
49
61
  const VARS_AVAILABLE = Boolean(getInstalledVarsPackage()) || runInsideCoreComponents();
@@ -145,7 +157,7 @@ function loadMixins(file) {
145
157
  }
146
158
 
147
159
  function formatVar(variable) {
148
- return `var(${variable})`;
160
+ return variable.startsWith('var(') ? variable : `var(${variable})`;
149
161
  }
150
162
 
151
163
  function findInValue(haystack, needle, fromIndex) {
@@ -159,27 +171,27 @@ function findInValue(haystack, needle, fromIndex) {
159
171
  );
160
172
  }
161
173
 
174
+ function getColorVariants(prop) {
175
+ switch (prop) {
176
+ case 'color':
177
+ return ['text'];
178
+ case 'background-color':
179
+ case 'background':
180
+ return ['bg', 'specialbg', 'graphic'];
181
+ case 'border':
182
+ case 'border-top':
183
+ case 'border-right':
184
+ case 'border-bottom':
185
+ case 'border-left':
186
+ return ['border', 'graphic', 'bg', 'specialbg'];
187
+ default:
188
+ return [];
189
+ }
190
+ }
191
+
162
192
  function choiceVars(variables, prop, group) {
163
193
  if (group === 'colors') {
164
- const colorVariants = () => {
165
- switch (prop) {
166
- case 'color':
167
- return ['text'];
168
- case 'background-color':
169
- case 'background':
170
- return ['bg', 'specialbg', 'graphic'];
171
- case 'border':
172
- case 'border-top':
173
- case 'border-right':
174
- case 'border-bottom':
175
- case 'border-left':
176
- return ['border', 'graphic', 'bg', 'specialbg'];
177
- default:
178
- return [];
179
- }
180
- };
181
-
182
- const variants = colorVariants();
194
+ const variants = getColorVariants(prop);
183
195
 
184
196
  const condition = (variable) =>
185
197
  variants.some((variant) => variable.startsWith(`--color-light-${variant}`));
@@ -208,11 +220,40 @@ function findVars(cssValue, prop) {
208
220
  index,
209
221
  value,
210
222
  variables: chosen,
223
+ fixedValue: formatVar(chosen[0]),
224
+ fixable: chosen.length === 1,
211
225
  };
212
226
  }
213
227
  }
214
228
  }
215
229
 
230
+ function findOldVars(cssValue, prop) {
231
+ const matches = /var\(([^\),]+)/g.exec(cssValue);
232
+
233
+ if (matches) {
234
+ const oldVar = matches[1];
235
+ const replacements = oldNewMap[oldVar];
236
+ if (!replacements) return;
237
+
238
+ const variants = getColorVariants(prop);
239
+
240
+ let variables = replacements.filter((r) => variants.some((v) => r.includes(v)));
241
+ const fixable = variables.length === 1;
242
+
243
+ if (!variables.length) {
244
+ variables = replacements;
245
+ }
246
+
247
+ return {
248
+ index: cssValue.indexOf(oldVar),
249
+ value: oldVar,
250
+ variables,
251
+ fixedValue: variables.length ? variables[0] : 'NON_FIXABLE',
252
+ fixable,
253
+ };
254
+ }
255
+ }
256
+
216
257
  function findTypographyMixins(ruleProps) {
217
258
  const findMixin = (exact) => {
218
259
  return Object.entries(mixins.typography)
@@ -256,10 +297,14 @@ function sortVarsByUsage(arr, sortingArr) {
256
297
  });
257
298
  }
258
299
 
259
- module.exports.VARS_AVAILABLE = VARS_AVAILABLE;
260
- module.exports.vars = vars;
261
- module.exports.mixins = mixins;
262
- module.exports.findVars = findVars;
263
- module.exports.formatVar = formatVar;
264
- module.exports.toOneLine = toOneLine;
265
- module.exports.findTypographyMixins = findTypographyMixins;
300
+ module.exports = {
301
+ VARS_AVAILABLE,
302
+ vars,
303
+ colorsSet,
304
+ mixins,
305
+ findVars,
306
+ formatVar,
307
+ toOneLine,
308
+ findTypographyMixins,
309
+ findOldVars,
310
+ };