@maltjoy/eslint-rules 0.5.0 → 0.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.
Files changed (43) hide show
  1. package/README.md +59 -2
  2. package/index.js +83 -11
  3. package/package.json +1 -1
  4. package/rules/accessibility/require-vjoyiconbutton-label.js +92 -0
  5. package/rules/{require-vjoytemplate-template-title-heading.js → accessibility/require-vjoytemplate-template-title-heading.js} +2 -1
  6. package/rules/{no-vjoyavatar-fullname-totalnumber.js → components/no-vjoyavatar-fullname-totalnumber.js} +1 -1
  7. package/rules/{no-vjoyavatar-photourl-totalnumber.js → components/no-vjoyavatar-photourl-totalnumber.js} +1 -1
  8. package/rules/{no-vjoybutton-circle.js → components/no-vjoybutton-circle.js} +1 -1
  9. package/rules/components/no-vjoybutton-icon-only.js +87 -0
  10. package/rules/components/no-vjoybutton-white-variant.js +94 -0
  11. package/rules/components/no-vjoycollapse-single-item.js +55 -0
  12. package/rules/components/no-vjoycollapseitem-outside-collapse.js +15 -0
  13. package/rules/components/no-vjoycompanyavatar.js +16 -0
  14. package/rules/components/no-vjoyformerror-no-html-error-text.js +66 -0
  15. package/rules/components/no-vjoyformerror-visible-prop.js +18 -0
  16. package/rules/{no-vjoyformgroup-vjoyradio.js → components/no-vjoyformgroup-vjoyradio.js} +1 -1
  17. package/rules/components/no-vjoyfunnelfooter-slot.js +89 -0
  18. package/rules/{no-vjoyinput-type-password.js → components/no-vjoyinput-type-password.js} +1 -1
  19. package/rules/{no-vjoyinput-unit.js → components/no-vjoyinput-unit.js} +1 -1
  20. package/rules/components/no-vjoymenu-items-prop.js +19 -0
  21. package/rules/{no-vjoymenu-vjoypanel.js → components/no-vjoymenu-vjoypanel.js} +1 -1
  22. package/rules/components/no-vjoymenuitem-outside-menu.js +15 -0
  23. package/rules/components/no-vjoyradio-outside-group.js +16 -0
  24. package/rules/components/no-vjoyselectableitem-group.js +17 -0
  25. package/rules/components/no-vjoyselectableitem.js +55 -0
  26. package/rules/components/no-vjoysnackbar-direct.js +18 -0
  27. package/rules/components/no-vjoystep-outside-stepper.js +15 -0
  28. package/rules/components/no-vjoytab-outside-tabs.js +15 -0
  29. package/rules/components/no-vjoytooltip-enable-click-in-content.js +18 -0
  30. package/rules/{no-vjoyusercard-rating-stars-slot.js → components/no-vjoyusercard-rating-stars-slot.js} +1 -1
  31. package/rules/{require-vjoymenu-template-sidebar.js → components/require-vjoymenu-template-sidebar.js} +1 -1
  32. package/rules/{require-vjoytemplate-template-title.js → components/require-vjoytemplate-template-title.js} +1 -1
  33. package/rules/tokens/no-arbitrary-font-size.js +24 -0
  34. package/rules/tokens/no-hardcoded-color-names.js +22 -0
  35. package/rules/tokens/no-hardcoded-hex-colors-in-vue.js +22 -0
  36. package/rules/tokens/no-hardcoded-rgb-colors-in-vue.js +22 -0
  37. package/rules/tokens/no-inline-style-colors.js +23 -0
  38. package/rules/tokens/prefer-joy-color-classes.js +128 -0
  39. package/rules/tokens/vue-style-visitors.js +168 -0
  40. package/rules/utils/forbidden-attribute.js +1 -1
  41. package/rules/utils/forbidden-element.js +36 -0
  42. package/rules/utils/require-ancestor.js +47 -0
  43. package/rules/utils/style-inspection.js +439 -0
@@ -0,0 +1,439 @@
1
+ const HEX_COLOR_PATTERN = /#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\b/g;
2
+ const RGB_COLOR_PATTERN = /\brgba?\(\s*[^)]+\)/gi;
3
+ const JOY_COLOR_VAR_PATTERN = /var\(\s*(--joy-color-[\w-]+)\s*\)/i;
4
+ const COLOR_DECLARATION_PATTERN = /([^{}:;]+)\s*:\s*([^;}\n]+)/g;
5
+
6
+ const NON_NAMED_COLOR_VALUES = new Set([
7
+ 'inherit',
8
+ 'initial',
9
+ 'unset',
10
+ 'revert',
11
+ 'revert-layer',
12
+ 'transparent',
13
+ 'currentcolor',
14
+ 'none',
15
+ 'auto',
16
+ 'canvas',
17
+ ]);
18
+
19
+ const COLOR_PROPERTY_NAMES = new Set([
20
+ 'color',
21
+ 'background',
22
+ 'background-color',
23
+ 'backgroundcolor',
24
+ 'border-color',
25
+ 'bordercolor',
26
+ 'border-top-color',
27
+ 'bordertopcolor',
28
+ 'border-right-color',
29
+ 'borderrightcolor',
30
+ 'border-bottom-color',
31
+ 'borderbottomcolor',
32
+ 'border-left-color',
33
+ 'borderleftcolor',
34
+ 'outline-color',
35
+ 'outlinecolor',
36
+ 'caret-color',
37
+ 'caretcolor',
38
+ 'text-decoration-color',
39
+ 'textdecorationcolor',
40
+ 'fill',
41
+ 'stroke',
42
+ 'accent-color',
43
+ 'accentcolor',
44
+ ]);
45
+
46
+ const FONT_SIZE_KEYWORDS = new Set([
47
+ 'inherit',
48
+ 'initial',
49
+ 'unset',
50
+ 'revert',
51
+ 'revert-layer',
52
+ 'medium',
53
+ 'smaller',
54
+ 'larger',
55
+ ]);
56
+
57
+ const FONT_SIZE_DECLARATION_PATTERN = /font-size\s*:\s*([^;}\n]+)/gi;
58
+ const STYLE_BLOCK_PATTERN = /<style(?:\s[^>]*)?>([\s\S]*?)<\/style>/gi;
59
+ const JOY_FONT_SIZE_VAR_PATTERN = /^var\(\s*--joy-font-size-(?:primary|secondary)-\d+/i;
60
+
61
+ function normalizePropertyName(name) {
62
+ return name.replace(/[\s_-]+/g, '').toLowerCase();
63
+ }
64
+
65
+ export function isColorPropertyName(name) {
66
+ if (typeof name !== 'string' || !name.trim()) {
67
+ return false;
68
+ }
69
+
70
+ return COLOR_PROPERTY_NAMES.has(normalizePropertyName(name));
71
+ }
72
+
73
+ export function styleStringHasColorDeclaration(styleValue) {
74
+ if (typeof styleValue !== 'string' || !styleValue.trim()) {
75
+ return false;
76
+ }
77
+
78
+ return styleValue.split(';').some((declaration) => {
79
+ const separatorIndex = declaration.indexOf(':');
80
+ if (separatorIndex === -1) {
81
+ return false;
82
+ }
83
+
84
+ const propertyName = declaration.slice(0, separatorIndex).trim();
85
+ return isColorPropertyName(propertyName);
86
+ });
87
+ }
88
+
89
+ export function isArbitraryFontSizeValue(value) {
90
+ const trimmed = value.trim();
91
+ if (!trimmed) {
92
+ return false;
93
+ }
94
+
95
+ if (JOY_FONT_SIZE_VAR_PATTERN.test(trimmed)) {
96
+ return false;
97
+ }
98
+
99
+ if (FONT_SIZE_KEYWORDS.has(trimmed.toLowerCase())) {
100
+ return false;
101
+ }
102
+
103
+ return true;
104
+ }
105
+
106
+ export function* iterateHexColors(text) {
107
+ if (typeof text !== 'string' || !text) {
108
+ return;
109
+ }
110
+
111
+ const pattern = new RegExp(HEX_COLOR_PATTERN.source, 'g');
112
+ let match = pattern.exec(text);
113
+
114
+ while (match) {
115
+ yield {
116
+ value: match[0],
117
+ index: match.index,
118
+ };
119
+ match = pattern.exec(text);
120
+ }
121
+ }
122
+
123
+ export function* iterateRgbColors(text) {
124
+ if (typeof text !== 'string' || !text) {
125
+ return;
126
+ }
127
+
128
+ const pattern = new RegExp(RGB_COLOR_PATTERN.source, 'gi');
129
+ let match = pattern.exec(text);
130
+
131
+ while (match) {
132
+ yield {
133
+ value: match[0],
134
+ index: match.index,
135
+ };
136
+ match = pattern.exec(text);
137
+ }
138
+ }
139
+
140
+ export function isHardcodedNamedColorValue(value) {
141
+ const trimmed = value.trim().toLowerCase();
142
+
143
+ if (!/^[a-z]+$/.test(trimmed)) {
144
+ return false;
145
+ }
146
+
147
+ return !NON_NAMED_COLOR_VALUES.has(trimmed);
148
+ }
149
+
150
+ export function* iterateNamedColorDeclarations(text) {
151
+ if (typeof text !== 'string' || !text) {
152
+ return;
153
+ }
154
+
155
+ const pattern = new RegExp(COLOR_DECLARATION_PATTERN.source, 'g');
156
+ let match = pattern.exec(text);
157
+
158
+ while (match) {
159
+ const propertyName = match[1]?.trim() ?? '';
160
+ const value = match[2]?.trim() ?? '';
161
+
162
+ if (isColorPropertyName(propertyName) && isHardcodedNamedColorValue(value)) {
163
+ yield {
164
+ value,
165
+ index: match.index + match[0].indexOf(value),
166
+ length: value.length,
167
+ data: { value },
168
+ };
169
+ }
170
+
171
+ match = pattern.exec(text);
172
+ }
173
+ }
174
+
175
+ export function extractJoyColorVarName(value) {
176
+ const match = value.trim().match(JOY_COLOR_VAR_PATTERN);
177
+ return match?.[1];
178
+ }
179
+
180
+ export function getSuggestedJoyColorClass(propertyName, cssVarName) {
181
+ const token = cssVarName.replace(/^--joy-color-/, '');
182
+
183
+ if (normalizePropertyName(propertyName) === 'color') {
184
+ return `joy-color-${token}`;
185
+ }
186
+
187
+ if (
188
+ normalizePropertyName(propertyName) === 'backgroundcolor' ||
189
+ normalizePropertyName(propertyName) === 'background'
190
+ ) {
191
+ return `joy-bgcolor-${token}`;
192
+ }
193
+
194
+ return undefined;
195
+ }
196
+
197
+ export function* iterateJoyColorVarDeclarations(text) {
198
+ if (typeof text !== 'string' || !text) {
199
+ return;
200
+ }
201
+
202
+ const pattern = new RegExp(COLOR_DECLARATION_PATTERN.source, 'g');
203
+ let match = pattern.exec(text);
204
+
205
+ while (match) {
206
+ const propertyName = match[1]?.trim() ?? '';
207
+ const value = match[2]?.trim() ?? '';
208
+ const cssVarName = extractJoyColorVarName(value);
209
+ const suggestedClass = cssVarName
210
+ ? getSuggestedJoyColorClass(propertyName, cssVarName)
211
+ : undefined;
212
+
213
+ if (isColorPropertyName(propertyName) && suggestedClass) {
214
+ yield {
215
+ value,
216
+ index: match.index,
217
+ length: match[0].length,
218
+ data: { suggestedClass, value },
219
+ };
220
+ }
221
+
222
+ match = pattern.exec(text);
223
+ }
224
+ }
225
+
226
+ export function elementHasClassName(node, className) {
227
+ if (!Array.isArray(node.startTag?.attributes)) {
228
+ return false;
229
+ }
230
+
231
+ return node.startTag.attributes.some((attribute) => {
232
+ if (attribute?.type !== 'VAttribute' || attribute.directive) {
233
+ return false;
234
+ }
235
+
236
+ if (attribute.key?.name !== 'class' || attribute.value?.type !== 'VLiteral') {
237
+ return false;
238
+ }
239
+
240
+ return attribute.value.value
241
+ .split(/\s+/)
242
+ .filter(Boolean)
243
+ .includes(className);
244
+ });
245
+ }
246
+
247
+ export function* iterateArbitraryFontSizeDeclarations(text) {
248
+ if (typeof text !== 'string' || !text) {
249
+ return;
250
+ }
251
+
252
+ const pattern = new RegExp(FONT_SIZE_DECLARATION_PATTERN.source, 'gi');
253
+ let match = pattern.exec(text);
254
+
255
+ while (match) {
256
+ const value = match[1]?.trim() ?? '';
257
+
258
+ if (isArbitraryFontSizeValue(value)) {
259
+ yield {
260
+ value,
261
+ index: match.index,
262
+ length: match[0].length,
263
+ };
264
+ }
265
+
266
+ match = pattern.exec(text);
267
+ }
268
+ }
269
+
270
+ export function* iterateStyleBlockContents(sourceCode) {
271
+ const text = sourceCode.getText();
272
+ const pattern = new RegExp(STYLE_BLOCK_PATTERN.source, 'gi');
273
+ let match = pattern.exec(text);
274
+
275
+ while (match) {
276
+ const content = match[1];
277
+ const contentStart = match.index + match[0].indexOf(content);
278
+
279
+ yield {
280
+ content,
281
+ startIndex: contentStart,
282
+ };
283
+
284
+ match = pattern.exec(text);
285
+ }
286
+ }
287
+
288
+ export function getPropertyName(propertyNode) {
289
+ if (propertyNode?.type !== 'Property') {
290
+ return undefined;
291
+ }
292
+
293
+ if (propertyNode.key?.type === 'Identifier') {
294
+ return propertyNode.key.name;
295
+ }
296
+
297
+ if (propertyNode.key?.type === 'Literal' && typeof propertyNode.key.value === 'string') {
298
+ return propertyNode.key.value;
299
+ }
300
+
301
+ return undefined;
302
+ }
303
+
304
+ export function isStaticStyleAttribute(attribute) {
305
+ return (
306
+ attribute?.type === 'VAttribute' &&
307
+ !attribute.directive &&
308
+ attribute.key?.name === 'style' &&
309
+ attribute.value?.type === 'VLiteral'
310
+ );
311
+ }
312
+
313
+ export function isBoundStyleAttribute(attribute) {
314
+ return (
315
+ attribute?.type === 'VAttribute' &&
316
+ attribute.directive &&
317
+ attribute.key?.name?.name === 'bind' &&
318
+ attribute.key?.argument?.name === 'style'
319
+ );
320
+ }
321
+
322
+ export function reportMatchesInText(context, text, startIndex, messageId, matches) {
323
+ for (const match of matches) {
324
+ context.report({
325
+ loc: {
326
+ start: context.sourceCode.getLocFromIndex(startIndex + match.index),
327
+ end: context.sourceCode.getLocFromIndex(startIndex + match.index + match.length),
328
+ },
329
+ messageId,
330
+ data: match.data,
331
+ });
332
+ }
333
+ }
334
+
335
+ export function reportRgbColorsInText(context, text, startIndex, messageId) {
336
+ reportMatchesInText(
337
+ context,
338
+ text,
339
+ startIndex,
340
+ messageId,
341
+ [...iterateRgbColors(text)].map((match) => ({
342
+ ...match,
343
+ length: match.value.length,
344
+ data: { value: match.value },
345
+ })),
346
+ );
347
+ }
348
+
349
+ export function reportNamedColorsInText(context, text, startIndex, messageId) {
350
+ reportMatchesInText(context, text, startIndex, messageId, [...iterateNamedColorDeclarations(text)]);
351
+ }
352
+
353
+ export function reportHexColorsInText(context, text, startIndex, messageId) {
354
+ reportMatchesInText(
355
+ context,
356
+ text,
357
+ startIndex,
358
+ messageId,
359
+ [...iterateHexColors(text)].map((match) => ({
360
+ ...match,
361
+ length: match.value.length,
362
+ data: { value: match.value },
363
+ })),
364
+ );
365
+ }
366
+
367
+ export function reportArbitraryFontSizesInText(context, text, startIndex, messageId) {
368
+ reportMatchesInText(
369
+ context,
370
+ text,
371
+ startIndex,
372
+ messageId,
373
+ [...iterateArbitraryFontSizeDeclarations(text)].map((match) => ({
374
+ ...match,
375
+ data: { value: match.value },
376
+ })),
377
+ );
378
+ }
379
+
380
+ export function collectStringLiteralsFromExpression(expression, literals = []) {
381
+ if (!expression) {
382
+ return literals;
383
+ }
384
+
385
+ if (expression.type === 'Literal' && typeof expression.value === 'string') {
386
+ literals.push(expression);
387
+ return literals;
388
+ }
389
+
390
+ if (
391
+ expression.type === 'TemplateLiteral' &&
392
+ expression.expressions.length === 0 &&
393
+ expression.quasis.length === 1
394
+ ) {
395
+ literals.push({
396
+ value: expression.quasis[0].value.cooked ?? '',
397
+ range: expression.range,
398
+ });
399
+ return literals;
400
+ }
401
+
402
+ if (expression.type === 'ObjectExpression') {
403
+ expression.properties.forEach((property) => {
404
+ if (property.type === 'Property') {
405
+ collectStringLiteralsFromExpression(property.value, literals);
406
+ }
407
+ });
408
+ }
409
+
410
+ return literals;
411
+ }
412
+
413
+ export function objectExpressionHasColorProperty(expression) {
414
+ if (expression?.type !== 'ObjectExpression') {
415
+ return false;
416
+ }
417
+
418
+ return expression.properties.some((property) => {
419
+ if (property.type !== 'Property') {
420
+ return false;
421
+ }
422
+
423
+ return isColorPropertyName(getPropertyName(property));
424
+ });
425
+ }
426
+
427
+ export function objectExpressionHasFontSizeProperty(expression) {
428
+ if (expression?.type !== 'ObjectExpression') {
429
+ return false;
430
+ }
431
+
432
+ return expression.properties.some((property) => {
433
+ if (property.type !== 'Property') {
434
+ return false;
435
+ }
436
+
437
+ return normalizePropertyName(getPropertyName(property) ?? '') === 'fontsize';
438
+ });
439
+ }