@alfalab/stylelint-core-vars 1.4.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 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
  ## Использование
@@ -26,7 +26,12 @@ npm install --save-dev stylelint stylelint-core-vars
26
26
  "@alfalab/stylelint-core-vars"
27
27
  ],
28
28
  "rules": {
29
- "stylelint-core-vars/use-vars": true,
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,
@@ -45,7 +50,13 @@ npm install --save-dev stylelint stylelint-core-vars
45
50
  {
46
51
  "severity": "warning"
47
52
  }
48
- ]
53
+ ],
54
+ "stylelint-core-vars/do-not-use-old-vars": [
55
+ true,
56
+ {
57
+ "severity": "warning"
58
+ }
59
+ ],
49
60
  },
50
61
  }
51
62
  ```
package/index.js CHANGED
@@ -1,6 +1,6 @@
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
5
  const colorsSet = require('./utils').colorsSet;
6
6
  const findTypographyMixins = require('./utils').findTypographyMixins;
@@ -11,6 +11,7 @@ const RULE_USE_ONE_OF_VARS = 'stylelint-core-vars/use-one-of-vars';
11
11
  const RULE_USE_MIXINS = 'stylelint-core-vars/use-mixins';
12
12
  const RULE_USE_ONE_OF_MIXINS = 'stylelint-core-vars/use-one-of-mixins';
13
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';
14
15
 
15
16
  const messages = {
16
17
  [RULE_USE_VARS]: stylelint.utils.ruleMessages(RULE_USE_VARS, {
@@ -25,6 +26,21 @@ const messages = {
25
26
  return `Use variables instead of plain value '${value}':\n${variablesPart}\n`;
26
27
  },
27
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
+ }),
28
44
  [RULE_USE_MIXINS]: stylelint.utils.ruleMessages(RULE_USE_MIXINS, {
29
45
  expected: (mixin) => {
30
46
  return `Use mixin '${mixin.name}' instead of plain values`;
@@ -45,23 +61,20 @@ const messages = {
45
61
  }),
46
62
  };
47
63
 
48
- const checkVars = (decl, result, context, ruleName) => {
64
+ const checkVars = (decl, result, context, ruleName, matcher, shouldReport, options = {}) => {
49
65
  const { prop, raws } = decl;
50
66
 
51
67
  let value = toOneLine(decl.value);
52
-
53
68
  let substitution;
69
+
54
70
  const previousValues = [];
55
71
 
56
- while ((substitution = findVars(value, prop))) {
72
+ while ((substitution = matcher(value, prop, options))) {
57
73
  let fixed = false;
58
74
 
59
- const exactVar = substitution.variables.length === 1;
60
- const fixedValue = formatVar(substitution.variables[0]);
61
-
62
- value = value.replace(substitution.value, fixedValue);
75
+ value = value.replace(substitution.value, substitution.fixedValue);
63
76
 
64
- if (context.fix && exactVar) {
77
+ if (context.fix && substitution.fixable) {
65
78
  decl.value = value;
66
79
  fixed = true;
67
80
  }
@@ -71,16 +84,15 @@ const checkVars = (decl, result, context, ruleName) => {
71
84
  substitution.index
72
85
  );
73
86
 
74
- const shouldReport =
75
- !fixed &&
76
- ((ruleName === RULE_USE_ONE_OF_VARS && !exactVar) ||
77
- (ruleName === RULE_USE_VARS && exactVar));
78
-
79
- if (shouldReport) {
87
+ if (shouldReport(substitution.fixable, fixed)) {
80
88
  stylelint.utils.report({
81
89
  result,
82
90
  ruleName,
83
- message: messages[ruleName].expected(substitution.variables, substitution.value),
91
+ message: messages[ruleName].expected(
92
+ substitution.variables,
93
+ substitution.value,
94
+ substitution.fixable
95
+ ),
84
96
  node: decl,
85
97
  word: value,
86
98
  index: originalValueIndex + prop.length + raws.between.length,
@@ -89,7 +101,7 @@ const checkVars = (decl, result, context, ruleName) => {
89
101
 
90
102
  previousValues.unshift({
91
103
  ...substitution,
92
- diff: fixedValue.length - substitution.value.length,
104
+ diff: substitution.fixedValue.length - substitution.value.length,
93
105
  });
94
106
  }
95
107
  };
@@ -163,25 +175,63 @@ const checkDarkColorsUsage = (decl, result, context, ruleName) => {
163
175
  };
164
176
 
165
177
  module.exports = [
166
- stylelint.createPlugin(RULE_USE_VARS, (enabled, _, context) => {
178
+ stylelint.createPlugin(RULE_USE_VARS, (enabled, config, context) => {
167
179
  if (!enabled || !VARS_AVAILABLE) {
168
180
  return () => {};
169
181
  }
170
182
 
183
+ const allowNumericValues = config?.allowNumericValues || false;
184
+
185
+ return (root, result) => {
186
+ root.walkDecls((decl) => {
187
+ checkVars(
188
+ decl,
189
+ result,
190
+ context,
191
+ RULE_USE_VARS,
192
+ findVars,
193
+ (fixable, fixed) => fixable && !fixed,
194
+ { allowNumericValues }
195
+ );
196
+ });
197
+ };
198
+ }),
199
+ stylelint.createPlugin(RULE_USE_ONE_OF_VARS, (enabled, config, context) => {
200
+ if (!enabled || !VARS_AVAILABLE) {
201
+ return () => {};
202
+ }
203
+
204
+ const allowNumericValues = config?.allowNumericValues || false;
205
+
171
206
  return (root, result) => {
172
207
  root.walkDecls((decl) => {
173
- checkVars(decl, result, context, RULE_USE_VARS);
208
+ checkVars(
209
+ decl,
210
+ result,
211
+ context,
212
+ RULE_USE_ONE_OF_VARS,
213
+ findVars,
214
+ (fixable, fixed) => !fixable && !fixed,
215
+ { allowNumericValues }
216
+ );
174
217
  });
175
218
  };
176
219
  }),
177
- stylelint.createPlugin(RULE_USE_ONE_OF_VARS, (enabled, _, context) => {
220
+ stylelint.createPlugin(RULE_DO_NOT_USE_OLD_VARS, (enabled, _, context) => {
178
221
  if (!enabled || !VARS_AVAILABLE) {
179
222
  return () => {};
180
223
  }
181
224
 
182
225
  return (root, result) => {
183
226
  root.walkDecls((decl) => {
184
- checkVars(decl, result, context, RULE_USE_ONE_OF_VARS);
227
+ checkVars(
228
+ decl,
229
+ result,
230
+ context,
231
+ RULE_DO_NOT_USE_OLD_VARS,
232
+ findOldVars,
233
+ (_, fixed) => !fixed
234
+ );
185
235
  });
186
236
  };
187
237
  }),
@@ -226,3 +276,4 @@ module.exports.RULE_USE_ONE_OF_VARS = RULE_USE_ONE_OF_VARS;
226
276
  module.exports.RULE_USE_MIXINS = RULE_USE_MIXINS;
227
277
  module.exports.RULE_USE_ONE_OF_MIXINS = RULE_USE_ONE_OF_MIXINS;
228
278
  module.exports.RULE_DO_NOT_USE_DARK_COLORS = RULE_DO_NOT_USE_DARK_COLORS;
279
+ 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({
@@ -21,6 +22,13 @@ testRule({
21
22
  }`,
22
23
  description: 'singleline rule with single value',
23
24
  },
25
+ {
26
+ code: `.class {
27
+ /* stylelint-disable-next-line */
28
+ padding-left: 8px;
29
+ }`,
30
+ description: 'singleline rule with single value',
31
+ },
24
32
  {
25
33
  code: `.class {
26
34
  padding: var(--gap-xs) 0 var(--gap-m) 0;
@@ -232,8 +240,8 @@ testRule({
232
240
  message: messages[RULE_USE_VARS].expected(['--border-radius-s'], '4px'),
233
241
  line: 3,
234
242
  column: 29,
235
- }
236
- ]
243
+ },
244
+ ],
237
245
  },
238
246
  ...Object.entries(vars.gaps).map(([value, vars]) => {
239
247
  const gapVar = vars[0];
@@ -281,6 +289,43 @@ testRule({
281
289
  ],
282
290
  });
283
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
+
284
329
  testRule({
285
330
  plugins: [RULE_USE_ONE_OF_VARS],
286
331
  ruleName: RULE_USE_ONE_OF_VARS,
@@ -472,6 +517,82 @@ testRule({
472
517
  ],
473
518
  });
474
519
 
520
+ testRule({
521
+ plugins: [RULE_DO_NOT_USE_OLD_VARS],
522
+ ruleName: RULE_DO_NOT_USE_OLD_VARS,
523
+ config: true,
524
+ fix: true,
525
+ accept: [
526
+ {
527
+ code: `.class {
528
+ color: #000;
529
+ }`,
530
+ description: 'custom value',
531
+ },
532
+ {
533
+ code: `.class {
534
+ background-color: var(--color-light-bg-primary);
535
+ background: var(--color-light-bg-primary);
536
+ color: var(--color-light-text-primary);
537
+ }`,
538
+ description: 'colors',
539
+ },
540
+ ],
541
+ reject: [
542
+ {
543
+ code: `.class {\n background-color: var(--color-dark-indigo-10-flat);\n}`,
544
+ fixed: `.class {\n background-color: var(--color-light-bg-tertiary);\n}`,
545
+ message: messages[RULE_DO_NOT_USE_OLD_VARS].expected(
546
+ ['--color-light-bg-tertiary'],
547
+ '--color-dark-indigo-10-flat'
548
+ ),
549
+ line: 2,
550
+ column: 27,
551
+ },
552
+ {
553
+ code: `.class {\n background-color: var(--color-red-brand-85);\n}`,
554
+ fixed: `.class {\n background-color: var(--color-red-brand-85);\n}`,
555
+ unfixable: true,
556
+ message: messages[RULE_DO_NOT_USE_OLD_VARS].expected([], ['--color-red-brand-85']),
557
+ line: 2,
558
+ column: 27,
559
+ },
560
+ {
561
+ code: `.class {
562
+ color: var(--color-red-brand-10-flat);
563
+ background: var(--color-red-dark);
564
+ fill: var(---color-black-10);
565
+ }`,
566
+ fixed: `.class {
567
+ color: var(--color-red-brand-10-flat);
568
+ background: var(--color-light-graphic-negative);
569
+ fill: var(---color-black-10);
570
+ }`,
571
+ description: 'multiple variants',
572
+ warnings: [
573
+ {
574
+ column: 28,
575
+ line: 2,
576
+ message: messages[RULE_DO_NOT_USE_OLD_VARS].expected(
577
+ ['--color-light-bg-negative-muted'],
578
+ '--color-red-brand-10-flat',
579
+ false
580
+ ),
581
+ },
582
+ {
583
+ column: 33,
584
+ line: 3,
585
+ message: messages[RULE_DO_NOT_USE_OLD_VARS].expected(
586
+ ['--color-light-graphic-negative'],
587
+ '--color-red-dark',
588
+ true
589
+ ),
590
+ },
591
+ ],
592
+ },
593
+ ],
594
+ });
595
+
475
596
  testRule({
476
597
  plugins: [RULE_USE_MIXINS],
477
598
  ruleName: RULE_USE_MIXINS,
@@ -498,6 +619,21 @@ testRule({
498
619
  }`,
499
620
  description: 'typography nested rule',
500
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
+ },
501
637
  ],
502
638
  reject: [
503
639
  {
@@ -619,8 +755,20 @@ testRule({
619
755
  name: 'paragraph_primary_small',
620
756
  props: mixins.typography['paragraph_primary_small'],
621
757
  },
758
+ {
759
+ name: 'paragraph_component_secondary',
760
+ props: mixins.typography['paragraph_component_secondary'],
761
+ },
622
762
  { name: 'accent_primary_small', props: mixins.typography['accent_primary_small'] },
763
+ {
764
+ name: 'accent_component_secondary',
765
+ props: mixins.typography['accent_component_secondary'],
766
+ },
623
767
  { name: 'action_primary_small', props: mixins.typography['action_primary_small'] },
768
+ {
769
+ name: 'action_component_secondary',
770
+ props: mixins.typography['action_component_secondary'],
771
+ },
624
772
  ]),
625
773
  line: 2,
626
774
  column: 17,
@@ -643,8 +791,20 @@ testRule({
643
791
  name: 'paragraph_primary_small',
644
792
  props: mixins.typography['paragraph_primary_small'],
645
793
  },
794
+ {
795
+ name: 'paragraph_component_secondary',
796
+ props: mixins.typography['paragraph_component_secondary'],
797
+ },
646
798
  { name: 'accent_primary_small', props: mixins.typography['accent_primary_small'] },
799
+ {
800
+ name: 'accent_component_secondary',
801
+ props: mixins.typography['accent_component_secondary'],
802
+ },
647
803
  { name: 'action_primary_small', props: mixins.typography['action_primary_small'] },
804
+ {
805
+ name: 'action_component_secondary',
806
+ props: mixins.typography['action_component_secondary'],
807
+ },
648
808
  ]),
649
809
  line: 3,
650
810
  column: 21,
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.4.0",
3
+ "version": "1.6.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';
@@ -20,7 +21,7 @@ const vars = {
20
21
  };
21
22
 
22
23
  const colorsSet = Object.values(vars.colors).reduce((acc, vars) => {
23
- vars.forEach(colorVar => acc.add(colorVar));
24
+ vars.forEach((colorVar) => acc.add(colorVar));
24
25
 
25
26
  return acc;
26
27
  }, new Set());
@@ -42,6 +43,7 @@ const varsByProperties = {
42
43
  'margin-left': vars.gaps,
43
44
  'box-shadow': vars.shadows,
44
45
  color: vars.colors,
46
+ fill: vars.colors,
45
47
  background: vars.colors,
46
48
  'background-color': vars.colors,
47
49
  border: vars.colors,
@@ -155,7 +157,7 @@ function loadMixins(file) {
155
157
  }
156
158
 
157
159
  function formatVar(variable) {
158
- return `var(${variable})`;
160
+ return variable.startsWith('var(') ? variable : `var(${variable})`;
159
161
  }
160
162
 
161
163
  function findInValue(haystack, needle, fromIndex) {
@@ -169,27 +171,27 @@ function findInValue(haystack, needle, fromIndex) {
169
171
  );
170
172
  }
171
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
+
172
192
  function choiceVars(variables, prop, group) {
173
193
  if (group === 'colors') {
174
- const colorVariants = () => {
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
-
192
- const variants = colorVariants();
194
+ const variants = getColorVariants(prop);
193
195
 
194
196
  const condition = (variable) =>
195
197
  variants.some((variant) => variable.startsWith(`--color-light-${variant}`));
@@ -200,12 +202,18 @@ function choiceVars(variables, prop, group) {
200
202
  return variables;
201
203
  }
202
204
 
203
- function findVars(cssValue, prop) {
205
+ function findVars(cssValue, prop, options) {
204
206
  const vars = varsByProperties[prop];
205
207
  if (!vars) return;
206
208
 
207
209
  const group = getVarsGroup(vars);
208
210
 
211
+ if (group === 'gaps' || group === 'borderRadiuses') {
212
+ if (options.allowNumericValues) {
213
+ return;
214
+ }
215
+ }
216
+
209
217
  for (const [value, variables] of Object.entries(vars)) {
210
218
  const chosen = choiceVars(variables, prop, group);
211
219
 
@@ -218,11 +226,40 @@ function findVars(cssValue, prop) {
218
226
  index,
219
227
  value,
220
228
  variables: chosen,
229
+ fixedValue: formatVar(chosen[0]),
230
+ fixable: chosen.length === 1,
221
231
  };
222
232
  }
223
233
  }
224
234
  }
225
235
 
236
+ function findOldVars(cssValue, prop) {
237
+ const matches = /var\(([^\),]+)/g.exec(cssValue);
238
+
239
+ if (matches) {
240
+ const oldVar = matches[1];
241
+ const replacements = oldNewMap[oldVar];
242
+ if (!replacements) return;
243
+
244
+ const variants = getColorVariants(prop);
245
+
246
+ let variables = replacements.filter((r) => variants.some((v) => r.includes(v)));
247
+ const fixable = variables.length === 1;
248
+
249
+ if (!variables.length) {
250
+ variables = replacements;
251
+ }
252
+
253
+ return {
254
+ index: cssValue.indexOf(oldVar),
255
+ value: oldVar,
256
+ variables,
257
+ fixedValue: variables.length ? variables[0] : 'NON_FIXABLE',
258
+ fixable,
259
+ };
260
+ }
261
+ }
262
+
226
263
  function findTypographyMixins(ruleProps) {
227
264
  const findMixin = (exact) => {
228
265
  return Object.entries(mixins.typography)
@@ -266,11 +303,14 @@ function sortVarsByUsage(arr, sortingArr) {
266
303
  });
267
304
  }
268
305
 
269
- module.exports.VARS_AVAILABLE = VARS_AVAILABLE;
270
- module.exports.vars = vars;
271
- module.exports.colorsSet = colorsSet;
272
- module.exports.mixins = mixins;
273
- module.exports.findVars = findVars;
274
- module.exports.formatVar = formatVar;
275
- module.exports.toOneLine = toOneLine;
276
- module.exports.findTypographyMixins = findTypographyMixins;
306
+ module.exports = {
307
+ VARS_AVAILABLE,
308
+ vars,
309
+ colorsSet,
310
+ mixins,
311
+ findVars,
312
+ formatVar,
313
+ toOneLine,
314
+ findTypographyMixins,
315
+ findOldVars,
316
+ };