@alfalab/stylelint-core-vars 1.6.0 → 1.7.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 +26 -0
- package/index.js +1 -279
- package/lib/index.js +279 -0
- package/package.json +33 -8
- package/.prettierrc.js +0 -7
- package/index.test.js +0 -861
- /package/{old-new-map.js → lib/old-new-map.js} +0 -0
- /package/{utils.js → lib/utils.js} +0 -0
package/README.md
CHANGED
|
@@ -60,3 +60,29 @@ npm install --save-dev stylelint @alfalab/stylelint-core-vars
|
|
|
60
60
|
},
|
|
61
61
|
}
|
|
62
62
|
```
|
|
63
|
+
|
|
64
|
+
## Лицензия
|
|
65
|
+
|
|
66
|
+
```
|
|
67
|
+
The MIT License (MIT)
|
|
68
|
+
|
|
69
|
+
Copyright (c) 2024 core-ds contributors
|
|
70
|
+
|
|
71
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
72
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
73
|
+
in the Software without restriction, including without limitation the rights
|
|
74
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
75
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
76
|
+
furnished to do so, subject to the following conditions:
|
|
77
|
+
|
|
78
|
+
The above copyright notice and this permission notice shall be included in
|
|
79
|
+
all copies or substantial portions of the Software.
|
|
80
|
+
|
|
81
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
82
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
83
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
84
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
85
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
86
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
87
|
+
THE SOFTWARE.
|
|
88
|
+
```
|
package/index.js
CHANGED
|
@@ -1,279 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const { findOldVars } = require('./utils');
|
|
3
|
-
const toOneLine = require('./utils').toOneLine;
|
|
4
|
-
const findVars = require('./utils').findVars;
|
|
5
|
-
const colorsSet = require('./utils').colorsSet;
|
|
6
|
-
const findTypographyMixins = require('./utils').findTypographyMixins;
|
|
7
|
-
const VARS_AVAILABLE = require('./utils').VARS_AVAILABLE;
|
|
8
|
-
|
|
9
|
-
const RULE_USE_VARS = 'stylelint-core-vars/use-vars';
|
|
10
|
-
const RULE_USE_ONE_OF_VARS = 'stylelint-core-vars/use-one-of-vars';
|
|
11
|
-
const RULE_USE_MIXINS = 'stylelint-core-vars/use-mixins';
|
|
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';
|
|
14
|
-
const RULE_DO_NOT_USE_OLD_VARS = 'stylelint-core-vars/do-not-use-old-vars';
|
|
15
|
-
|
|
16
|
-
const messages = {
|
|
17
|
-
[RULE_USE_VARS]: stylelint.utils.ruleMessages(RULE_USE_VARS, {
|
|
18
|
-
expected: (variable, value) => {
|
|
19
|
-
return `Use variable 'var(${variable})' instead of plain value '${value}'`;
|
|
20
|
-
},
|
|
21
|
-
}),
|
|
22
|
-
[RULE_USE_ONE_OF_VARS]: stylelint.utils.ruleMessages(RULE_USE_ONE_OF_VARS, {
|
|
23
|
-
expected: (variables, value) => {
|
|
24
|
-
const variablesPart = variables.map((v) => `var(${v})`).join('\n');
|
|
25
|
-
|
|
26
|
-
return `Use variables instead of plain value '${value}':\n${variablesPart}\n`;
|
|
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
|
-
}),
|
|
44
|
-
[RULE_USE_MIXINS]: stylelint.utils.ruleMessages(RULE_USE_MIXINS, {
|
|
45
|
-
expected: (mixin) => {
|
|
46
|
-
return `Use mixin '${mixin.name}' instead of plain values`;
|
|
47
|
-
},
|
|
48
|
-
}),
|
|
49
|
-
[RULE_USE_ONE_OF_MIXINS]: stylelint.utils.ruleMessages(RULE_USE_ONE_OF_MIXINS, {
|
|
50
|
-
expected: (mixins) => {
|
|
51
|
-
const mixinsPart = mixins
|
|
52
|
-
.map(({ name, props }) => `${name} (${Object.values(props).join('|')})`)
|
|
53
|
-
.join('\n');
|
|
54
|
-
return `Use mixins instead of plain values:\n${mixinsPart}\n`;
|
|
55
|
-
},
|
|
56
|
-
}),
|
|
57
|
-
[RULE_DO_NOT_USE_DARK_COLORS]: stylelint.utils.ruleMessages(RULE_DO_NOT_USE_DARK_COLORS, {
|
|
58
|
-
expected: () => {
|
|
59
|
-
return 'Do not use dark colors directly. Only light and static colors are allowed';
|
|
60
|
-
},
|
|
61
|
-
}),
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const checkVars = (decl, result, context, ruleName, matcher, shouldReport, options = {}) => {
|
|
65
|
-
const { prop, raws } = decl;
|
|
66
|
-
|
|
67
|
-
let value = toOneLine(decl.value);
|
|
68
|
-
let substitution;
|
|
69
|
-
|
|
70
|
-
const previousValues = [];
|
|
71
|
-
|
|
72
|
-
while ((substitution = matcher(value, prop, options))) {
|
|
73
|
-
let fixed = false;
|
|
74
|
-
|
|
75
|
-
value = value.replace(substitution.value, substitution.fixedValue);
|
|
76
|
-
|
|
77
|
-
if (context.fix && substitution.fixable) {
|
|
78
|
-
decl.value = value;
|
|
79
|
-
fixed = true;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const originalValueIndex = previousValues.reduce(
|
|
83
|
-
(acc, sub) => (acc > sub.index + sub.diff ? acc - sub.diff : acc),
|
|
84
|
-
substitution.index
|
|
85
|
-
);
|
|
86
|
-
|
|
87
|
-
if (shouldReport(substitution.fixable, fixed)) {
|
|
88
|
-
stylelint.utils.report({
|
|
89
|
-
result,
|
|
90
|
-
ruleName,
|
|
91
|
-
message: messages[ruleName].expected(
|
|
92
|
-
substitution.variables,
|
|
93
|
-
substitution.value,
|
|
94
|
-
substitution.fixable
|
|
95
|
-
),
|
|
96
|
-
node: decl,
|
|
97
|
-
word: value,
|
|
98
|
-
index: originalValueIndex + prop.length + raws.between.length,
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
previousValues.unshift({
|
|
103
|
-
...substitution,
|
|
104
|
-
diff: substitution.fixedValue.length - substitution.value.length,
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const checkTypography = (rule, result, context, ruleName) => {
|
|
110
|
-
const typographyProps = rule.nodes.reduce((acc, node) => {
|
|
111
|
-
if (['font-size', 'line-height', 'font-weight'].includes(node.prop)) {
|
|
112
|
-
acc[node.prop] = node.value;
|
|
113
|
-
}
|
|
114
|
-
return acc;
|
|
115
|
-
}, {});
|
|
116
|
-
|
|
117
|
-
const hasTypography = 'font-size' in typographyProps;
|
|
118
|
-
if (!hasTypography) return;
|
|
119
|
-
|
|
120
|
-
const mixins = findTypographyMixins(typographyProps);
|
|
121
|
-
|
|
122
|
-
if (!mixins || !mixins.length) return;
|
|
123
|
-
|
|
124
|
-
const exactMixin = mixins.length === 1;
|
|
125
|
-
|
|
126
|
-
let fixed = false;
|
|
127
|
-
if (context.fix && exactMixin) {
|
|
128
|
-
fixed = true;
|
|
129
|
-
const { name, props } = mixins[0];
|
|
130
|
-
|
|
131
|
-
const before = rule.nodes[0].raws.before;
|
|
132
|
-
rule.walkDecls((decl) => {
|
|
133
|
-
if (decl.prop in props) {
|
|
134
|
-
decl.remove();
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
rule.prepend(`${before}@mixin ${name};\n`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const shouldReport =
|
|
142
|
-
!fixed &&
|
|
143
|
-
((ruleName === RULE_USE_ONE_OF_MIXINS && !exactMixin) ||
|
|
144
|
-
(ruleName === RULE_USE_MIXINS && exactMixin));
|
|
145
|
-
|
|
146
|
-
if (shouldReport) {
|
|
147
|
-
stylelint.utils.report({
|
|
148
|
-
result,
|
|
149
|
-
ruleName,
|
|
150
|
-
message: messages[ruleName].expected(mixins),
|
|
151
|
-
node: rule,
|
|
152
|
-
word: 'font-size',
|
|
153
|
-
index: 0,
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
const checkDarkColorsUsage = (decl, result, context, ruleName) => {
|
|
159
|
-
const { prop, raws } = decl;
|
|
160
|
-
|
|
161
|
-
let value = toOneLine(decl.value);
|
|
162
|
-
|
|
163
|
-
const matches = /--color-dark-[\w-]+/.exec(value);
|
|
164
|
-
|
|
165
|
-
if (matches && colorsSet.has(matches[0])) {
|
|
166
|
-
stylelint.utils.report({
|
|
167
|
-
result,
|
|
168
|
-
ruleName: RULE_DO_NOT_USE_DARK_COLORS,
|
|
169
|
-
message: messages[RULE_DO_NOT_USE_DARK_COLORS].expected(),
|
|
170
|
-
node: decl,
|
|
171
|
-
word: value,
|
|
172
|
-
index: prop.length + raws.between.length + decl.value.indexOf(matches[0]),
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
module.exports = [
|
|
178
|
-
stylelint.createPlugin(RULE_USE_VARS, (enabled, config, context) => {
|
|
179
|
-
if (!enabled || !VARS_AVAILABLE) {
|
|
180
|
-
return () => {};
|
|
181
|
-
}
|
|
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
|
-
|
|
206
|
-
return (root, result) => {
|
|
207
|
-
root.walkDecls((decl) => {
|
|
208
|
-
checkVars(
|
|
209
|
-
decl,
|
|
210
|
-
result,
|
|
211
|
-
context,
|
|
212
|
-
RULE_USE_ONE_OF_VARS,
|
|
213
|
-
findVars,
|
|
214
|
-
(fixable, fixed) => !fixable && !fixed,
|
|
215
|
-
{ allowNumericValues }
|
|
216
|
-
);
|
|
217
|
-
});
|
|
218
|
-
};
|
|
219
|
-
}),
|
|
220
|
-
stylelint.createPlugin(RULE_DO_NOT_USE_OLD_VARS, (enabled, _, context) => {
|
|
221
|
-
if (!enabled || !VARS_AVAILABLE) {
|
|
222
|
-
return () => {};
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
return (root, result) => {
|
|
226
|
-
root.walkDecls((decl) => {
|
|
227
|
-
checkVars(
|
|
228
|
-
decl,
|
|
229
|
-
result,
|
|
230
|
-
context,
|
|
231
|
-
RULE_DO_NOT_USE_OLD_VARS,
|
|
232
|
-
findOldVars,
|
|
233
|
-
(_, fixed) => !fixed
|
|
234
|
-
);
|
|
235
|
-
});
|
|
236
|
-
};
|
|
237
|
-
}),
|
|
238
|
-
stylelint.createPlugin(RULE_USE_MIXINS, (enabled, _, context) => {
|
|
239
|
-
if (!enabled || !VARS_AVAILABLE) {
|
|
240
|
-
return () => {};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return (root, result) => {
|
|
244
|
-
root.walkRules((rule) => {
|
|
245
|
-
checkTypography(rule, result, context, RULE_USE_MIXINS);
|
|
246
|
-
});
|
|
247
|
-
};
|
|
248
|
-
}),
|
|
249
|
-
stylelint.createPlugin(RULE_USE_ONE_OF_MIXINS, (enabled, _, context) => {
|
|
250
|
-
if (!enabled || !VARS_AVAILABLE) {
|
|
251
|
-
return () => {};
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
return (root, result) => {
|
|
255
|
-
root.walkRules((rule) => {
|
|
256
|
-
checkTypography(rule, result, context, RULE_USE_ONE_OF_MIXINS);
|
|
257
|
-
});
|
|
258
|
-
};
|
|
259
|
-
}),
|
|
260
|
-
stylelint.createPlugin(RULE_DO_NOT_USE_DARK_COLORS, (enabled, _, context) => {
|
|
261
|
-
if (!enabled || !VARS_AVAILABLE) {
|
|
262
|
-
return () => {};
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
return (root, result) => {
|
|
266
|
-
root.walkDecls((decl) => {
|
|
267
|
-
checkDarkColorsUsage(decl, result, context);
|
|
268
|
-
});
|
|
269
|
-
};
|
|
270
|
-
}),
|
|
271
|
-
];
|
|
272
|
-
|
|
273
|
-
module.exports.messages = messages;
|
|
274
|
-
module.exports.RULE_USE_VARS = RULE_USE_VARS;
|
|
275
|
-
module.exports.RULE_USE_ONE_OF_VARS = RULE_USE_ONE_OF_VARS;
|
|
276
|
-
module.exports.RULE_USE_MIXINS = RULE_USE_MIXINS;
|
|
277
|
-
module.exports.RULE_USE_ONE_OF_MIXINS = RULE_USE_ONE_OF_MIXINS;
|
|
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;
|
|
1
|
+
module.exports = require('./lib');
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
const stylelint = require('stylelint');
|
|
2
|
+
const { findOldVars } = require('./utils');
|
|
3
|
+
const toOneLine = require('./utils').toOneLine;
|
|
4
|
+
const findVars = require('./utils').findVars;
|
|
5
|
+
const colorsSet = require('./utils').colorsSet;
|
|
6
|
+
const findTypographyMixins = require('./utils').findTypographyMixins;
|
|
7
|
+
const VARS_AVAILABLE = require('./utils').VARS_AVAILABLE;
|
|
8
|
+
|
|
9
|
+
const RULE_USE_VARS = 'stylelint-core-vars/use-vars';
|
|
10
|
+
const RULE_USE_ONE_OF_VARS = 'stylelint-core-vars/use-one-of-vars';
|
|
11
|
+
const RULE_USE_MIXINS = 'stylelint-core-vars/use-mixins';
|
|
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';
|
|
14
|
+
const RULE_DO_NOT_USE_OLD_VARS = 'stylelint-core-vars/do-not-use-old-vars';
|
|
15
|
+
|
|
16
|
+
const messages = {
|
|
17
|
+
[RULE_USE_VARS]: stylelint.utils.ruleMessages(RULE_USE_VARS, {
|
|
18
|
+
expected: (variable, value) => {
|
|
19
|
+
return `Use variable 'var(${variable})' instead of plain value '${value}'`;
|
|
20
|
+
},
|
|
21
|
+
}),
|
|
22
|
+
[RULE_USE_ONE_OF_VARS]: stylelint.utils.ruleMessages(RULE_USE_ONE_OF_VARS, {
|
|
23
|
+
expected: (variables, value) => {
|
|
24
|
+
const variablesPart = variables.map((v) => `var(${v})`).join('\n');
|
|
25
|
+
|
|
26
|
+
return `Use variables instead of plain value '${value}':\n${variablesPart}\n`;
|
|
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
|
+
}),
|
|
44
|
+
[RULE_USE_MIXINS]: stylelint.utils.ruleMessages(RULE_USE_MIXINS, {
|
|
45
|
+
expected: (mixin) => {
|
|
46
|
+
return `Use mixin '${mixin.name}' instead of plain values`;
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
[RULE_USE_ONE_OF_MIXINS]: stylelint.utils.ruleMessages(RULE_USE_ONE_OF_MIXINS, {
|
|
50
|
+
expected: (mixins) => {
|
|
51
|
+
const mixinsPart = mixins
|
|
52
|
+
.map(({ name, props }) => `${name} (${Object.values(props).join('|')})`)
|
|
53
|
+
.join('\n');
|
|
54
|
+
return `Use mixins instead of plain values:\n${mixinsPart}\n`;
|
|
55
|
+
},
|
|
56
|
+
}),
|
|
57
|
+
[RULE_DO_NOT_USE_DARK_COLORS]: stylelint.utils.ruleMessages(RULE_DO_NOT_USE_DARK_COLORS, {
|
|
58
|
+
expected: () => {
|
|
59
|
+
return 'Do not use dark colors directly. Only light and static colors are allowed';
|
|
60
|
+
},
|
|
61
|
+
}),
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const checkVars = (decl, result, context, ruleName, matcher, shouldReport, options = {}) => {
|
|
65
|
+
const { prop, raws } = decl;
|
|
66
|
+
|
|
67
|
+
let value = toOneLine(decl.value);
|
|
68
|
+
let substitution;
|
|
69
|
+
|
|
70
|
+
const previousValues = [];
|
|
71
|
+
|
|
72
|
+
while ((substitution = matcher(value, prop, options))) {
|
|
73
|
+
let fixed = false;
|
|
74
|
+
|
|
75
|
+
value = value.replace(substitution.value, substitution.fixedValue);
|
|
76
|
+
|
|
77
|
+
if (context.fix && substitution.fixable) {
|
|
78
|
+
decl.value = value;
|
|
79
|
+
fixed = true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const originalValueIndex = previousValues.reduce(
|
|
83
|
+
(acc, sub) => (acc > sub.index + sub.diff ? acc - sub.diff : acc),
|
|
84
|
+
substitution.index
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
if (shouldReport(substitution.fixable, fixed)) {
|
|
88
|
+
stylelint.utils.report({
|
|
89
|
+
result,
|
|
90
|
+
ruleName,
|
|
91
|
+
message: messages[ruleName].expected(
|
|
92
|
+
substitution.variables,
|
|
93
|
+
substitution.value,
|
|
94
|
+
substitution.fixable
|
|
95
|
+
),
|
|
96
|
+
node: decl,
|
|
97
|
+
word: value,
|
|
98
|
+
index: originalValueIndex + prop.length + raws.between.length,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
previousValues.unshift({
|
|
103
|
+
...substitution,
|
|
104
|
+
diff: substitution.fixedValue.length - substitution.value.length,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const checkTypography = (rule, result, context, ruleName) => {
|
|
110
|
+
const typographyProps = rule.nodes.reduce((acc, node) => {
|
|
111
|
+
if (['font-size', 'line-height', 'font-weight'].includes(node.prop)) {
|
|
112
|
+
acc[node.prop] = node.value;
|
|
113
|
+
}
|
|
114
|
+
return acc;
|
|
115
|
+
}, {});
|
|
116
|
+
|
|
117
|
+
const hasTypography = 'font-size' in typographyProps;
|
|
118
|
+
if (!hasTypography) return;
|
|
119
|
+
|
|
120
|
+
const mixins = findTypographyMixins(typographyProps);
|
|
121
|
+
|
|
122
|
+
if (!mixins || !mixins.length) return;
|
|
123
|
+
|
|
124
|
+
const exactMixin = mixins.length === 1;
|
|
125
|
+
|
|
126
|
+
let fixed = false;
|
|
127
|
+
if (context.fix && exactMixin) {
|
|
128
|
+
fixed = true;
|
|
129
|
+
const { name, props } = mixins[0];
|
|
130
|
+
|
|
131
|
+
const before = rule.nodes[0].raws.before;
|
|
132
|
+
rule.walkDecls((decl) => {
|
|
133
|
+
if (decl.prop in props) {
|
|
134
|
+
decl.remove();
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
rule.prepend(`${before}@mixin ${name};\n`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const shouldReport =
|
|
142
|
+
!fixed &&
|
|
143
|
+
((ruleName === RULE_USE_ONE_OF_MIXINS && !exactMixin) ||
|
|
144
|
+
(ruleName === RULE_USE_MIXINS && exactMixin));
|
|
145
|
+
|
|
146
|
+
if (shouldReport) {
|
|
147
|
+
stylelint.utils.report({
|
|
148
|
+
result,
|
|
149
|
+
ruleName,
|
|
150
|
+
message: messages[ruleName].expected(mixins),
|
|
151
|
+
node: rule,
|
|
152
|
+
word: 'font-size',
|
|
153
|
+
index: 0,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const checkDarkColorsUsage = (decl, result, context, ruleName) => {
|
|
159
|
+
const { prop, raws } = decl;
|
|
160
|
+
|
|
161
|
+
let value = toOneLine(decl.value);
|
|
162
|
+
|
|
163
|
+
const matches = /--color-dark-[\w-]+/.exec(value);
|
|
164
|
+
|
|
165
|
+
if (matches && colorsSet.has(matches[0])) {
|
|
166
|
+
stylelint.utils.report({
|
|
167
|
+
result,
|
|
168
|
+
ruleName: RULE_DO_NOT_USE_DARK_COLORS,
|
|
169
|
+
message: messages[RULE_DO_NOT_USE_DARK_COLORS].expected(),
|
|
170
|
+
node: decl,
|
|
171
|
+
word: value,
|
|
172
|
+
index: prop.length + raws.between.length + decl.value.indexOf(matches[0]),
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
module.exports = [
|
|
178
|
+
stylelint.createPlugin(RULE_USE_VARS, (enabled, config, context) => {
|
|
179
|
+
if (!enabled || !VARS_AVAILABLE) {
|
|
180
|
+
return () => {};
|
|
181
|
+
}
|
|
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
|
+
|
|
206
|
+
return (root, result) => {
|
|
207
|
+
root.walkDecls((decl) => {
|
|
208
|
+
checkVars(
|
|
209
|
+
decl,
|
|
210
|
+
result,
|
|
211
|
+
context,
|
|
212
|
+
RULE_USE_ONE_OF_VARS,
|
|
213
|
+
findVars,
|
|
214
|
+
(fixable, fixed) => !fixable && !fixed,
|
|
215
|
+
{ allowNumericValues }
|
|
216
|
+
);
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
}),
|
|
220
|
+
stylelint.createPlugin(RULE_DO_NOT_USE_OLD_VARS, (enabled, _, context) => {
|
|
221
|
+
if (!enabled || !VARS_AVAILABLE) {
|
|
222
|
+
return () => {};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return (root, result) => {
|
|
226
|
+
root.walkDecls((decl) => {
|
|
227
|
+
checkVars(
|
|
228
|
+
decl,
|
|
229
|
+
result,
|
|
230
|
+
context,
|
|
231
|
+
RULE_DO_NOT_USE_OLD_VARS,
|
|
232
|
+
findOldVars,
|
|
233
|
+
(_, fixed) => !fixed
|
|
234
|
+
);
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
}),
|
|
238
|
+
stylelint.createPlugin(RULE_USE_MIXINS, (enabled, _, context) => {
|
|
239
|
+
if (!enabled || !VARS_AVAILABLE) {
|
|
240
|
+
return () => {};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return (root, result) => {
|
|
244
|
+
root.walkRules((rule) => {
|
|
245
|
+
checkTypography(rule, result, context, RULE_USE_MIXINS);
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
}),
|
|
249
|
+
stylelint.createPlugin(RULE_USE_ONE_OF_MIXINS, (enabled, _, context) => {
|
|
250
|
+
if (!enabled || !VARS_AVAILABLE) {
|
|
251
|
+
return () => {};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return (root, result) => {
|
|
255
|
+
root.walkRules((rule) => {
|
|
256
|
+
checkTypography(rule, result, context, RULE_USE_ONE_OF_MIXINS);
|
|
257
|
+
});
|
|
258
|
+
};
|
|
259
|
+
}),
|
|
260
|
+
stylelint.createPlugin(RULE_DO_NOT_USE_DARK_COLORS, (enabled, _, context) => {
|
|
261
|
+
if (!enabled || !VARS_AVAILABLE) {
|
|
262
|
+
return () => {};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return (root, result) => {
|
|
266
|
+
root.walkDecls((decl) => {
|
|
267
|
+
checkDarkColorsUsage(decl, result, context);
|
|
268
|
+
});
|
|
269
|
+
};
|
|
270
|
+
}),
|
|
271
|
+
];
|
|
272
|
+
|
|
273
|
+
module.exports.messages = messages;
|
|
274
|
+
module.exports.RULE_USE_VARS = RULE_USE_VARS;
|
|
275
|
+
module.exports.RULE_USE_ONE_OF_VARS = RULE_USE_ONE_OF_VARS;
|
|
276
|
+
module.exports.RULE_USE_MIXINS = RULE_USE_MIXINS;
|
|
277
|
+
module.exports.RULE_USE_ONE_OF_MIXINS = RULE_USE_ONE_OF_MIXINS;
|
|
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/package.json
CHANGED
|
@@ -1,23 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alfalab/stylelint-core-vars",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "core-components css linter",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "jest"
|
|
8
8
|
},
|
|
9
|
-
"author": "
|
|
10
|
-
"license": "
|
|
9
|
+
"author": "core-ds contributors",
|
|
10
|
+
"license": "MIT",
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@alfalab/core-components-vars": "
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
12
|
+
"@alfalab/core-components-vars": "7.0.0",
|
|
13
|
+
"@semantic-release/changelog": "6.0.3",
|
|
14
|
+
"@semantic-release/commit-analyzer": "11.1.0",
|
|
15
|
+
"@semantic-release/git": "10.0.1",
|
|
16
|
+
"@semantic-release/github": "9.2.6",
|
|
17
|
+
"@semantic-release/npm": "11.0.2",
|
|
18
|
+
"@semantic-release/release-notes-generator": "12.1.0",
|
|
19
|
+
"arui-presets-lint": "7.3.0",
|
|
20
|
+
"jest": "29.7.0",
|
|
21
|
+
"jest-preset-stylelint": "6.3.2",
|
|
22
|
+
"lint-staged": "15.2.7",
|
|
23
|
+
"prettier": "3.3.2",
|
|
24
|
+
"semantic-release": "22.0.12",
|
|
25
|
+
"simple-git-hooks": "2.11.1",
|
|
26
|
+
"stylelint": "13.13.1"
|
|
16
27
|
},
|
|
17
28
|
"jest": {
|
|
18
29
|
"preset": "jest-preset-stylelint",
|
|
19
30
|
"setupFiles": [
|
|
20
31
|
"./jest.setup.js"
|
|
21
32
|
]
|
|
22
|
-
}
|
|
33
|
+
},
|
|
34
|
+
"commitlint": {
|
|
35
|
+
"extends": "./node_modules/arui-presets-lint/commitlint"
|
|
36
|
+
},
|
|
37
|
+
"lint-staged": {
|
|
38
|
+
"*.{js,jsx,ts,tsx,json}": [
|
|
39
|
+
"prettier --write"
|
|
40
|
+
]
|
|
41
|
+
},
|
|
42
|
+
"simple-git-hooks": {
|
|
43
|
+
"pre-commit": "./node_modules/.bin/lint-staged",
|
|
44
|
+
"commit-msg": "./node_modules/.bin/commitlint --edit $1"
|
|
45
|
+
},
|
|
46
|
+
"prettier": "arui-presets-lint/prettier",
|
|
47
|
+
"packageManager": "yarn@4.3.1"
|
|
23
48
|
}
|