@maltjoy/eslint-rules 0.5.0 → 0.6.1

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 (44) 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 +47 -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 +59 -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/accessible-label.js +103 -0
  41. package/rules/utils/forbidden-attribute.js +1 -1
  42. package/rules/utils/forbidden-element.js +36 -0
  43. package/rules/utils/require-ancestor.js +47 -0
  44. package/rules/utils/style-inspection.js +439 -0
@@ -0,0 +1,168 @@
1
+ import { createTemplateBodyVisitor } from '../utils/vue-ast.js';
2
+ import {
3
+ collectStringLiteralsFromExpression,
4
+ isBoundStyleAttribute,
5
+ isStaticStyleAttribute,
6
+ iterateStyleBlockContents,
7
+ objectExpressionHasColorProperty,
8
+ objectExpressionHasFontSizeProperty,
9
+ reportArbitraryFontSizesInText,
10
+ reportHexColorsInText,
11
+ reportNamedColorsInText,
12
+ reportRgbColorsInText,
13
+ styleStringHasColorDeclaration,
14
+ } from '../utils/style-inspection.js';
15
+
16
+ function visitStyleAttribute(context, attribute, handlers) {
17
+ if (isStaticStyleAttribute(attribute)) {
18
+ handlers.onStyleText?.(attribute.value.value, attribute.value.range[0]);
19
+ return;
20
+ }
21
+
22
+ if (!isBoundStyleAttribute(attribute)) {
23
+ return;
24
+ }
25
+
26
+ const expression = attribute.value?.expression;
27
+ handlers.onStyleExpression?.(expression, attribute);
28
+
29
+ collectStringLiteralsFromExpression(expression).forEach((literal) => {
30
+ if (typeof literal.value === 'string') {
31
+ handlers.onStyleText?.(literal.value, literal.range[0]);
32
+ }
33
+ });
34
+ }
35
+
36
+ export function createVueStyleInspectionRule({
37
+ description,
38
+ details,
39
+ examples,
40
+ messageId,
41
+ message,
42
+ inspectStyleText,
43
+ inspectStyleExpression,
44
+ includeStyleBlocks = true,
45
+ }) {
46
+ return {
47
+ meta: {
48
+ type: 'suggestion',
49
+ docs: {
50
+ description,
51
+ details,
52
+ examples,
53
+ recommended: true,
54
+ category: 'tokens',
55
+ },
56
+ schema: [],
57
+ messages: {
58
+ [messageId]: message,
59
+ },
60
+ },
61
+ create(context) {
62
+ const handlers = {
63
+ onStyleText(text, startIndex) {
64
+ inspectStyleText(context, text, startIndex, messageId);
65
+ },
66
+ onStyleExpression(expression, attribute) {
67
+ inspectStyleExpression?.(context, expression, attribute, messageId);
68
+ },
69
+ };
70
+
71
+ const templateVisitor = createTemplateBodyVisitor((node) => {
72
+ if (!Array.isArray(node.startTag?.attributes)) {
73
+ return;
74
+ }
75
+
76
+ node.startTag.attributes.forEach((attribute) => {
77
+ visitStyleAttribute(context, attribute, handlers);
78
+ });
79
+ });
80
+
81
+ return {
82
+ Program(node) {
83
+ if (includeStyleBlocks) {
84
+ for (const block of iterateStyleBlockContents(context.sourceCode)) {
85
+ inspectStyleText(context, block.content, block.startIndex, messageId);
86
+ }
87
+ }
88
+
89
+ templateVisitor.Program(node);
90
+ },
91
+ };
92
+ },
93
+ };
94
+ }
95
+
96
+ export function inspectStyleTextForHexColors(context, text, startIndex, messageId) {
97
+ reportHexColorsInText(context, text, startIndex, messageId);
98
+ }
99
+
100
+ export function inspectStyleTextForRgbColors(context, text, startIndex, messageId) {
101
+ reportRgbColorsInText(context, text, startIndex, messageId);
102
+ }
103
+
104
+ export function inspectStyleTextForNamedColors(context, text, startIndex, messageId) {
105
+ reportNamedColorsInText(context, text, startIndex, messageId);
106
+ }
107
+
108
+ export function inspectStyleTextForInlineColors(context, text, startIndex, messageId) {
109
+ if (styleStringHasColorDeclaration(text)) {
110
+ context.report({
111
+ loc: {
112
+ start: context.sourceCode.getLocFromIndex(startIndex),
113
+ end: context.sourceCode.getLocFromIndex(startIndex + text.length),
114
+ },
115
+ messageId,
116
+ });
117
+ }
118
+ }
119
+
120
+ export function inspectStyleTextForArbitraryFontSize(context, text, startIndex, messageId) {
121
+ reportArbitraryFontSizesInText(context, text, startIndex, messageId);
122
+ }
123
+
124
+ export function inspectStyleExpressionForInlineColors(context, expression, attribute, messageId) {
125
+ if (objectExpressionHasColorProperty(expression)) {
126
+ context.report({
127
+ node: attribute,
128
+ messageId,
129
+ });
130
+ }
131
+ }
132
+
133
+ export function inspectStyleExpressionForFontSize(context, expression, attribute, messageId) {
134
+ if (objectExpressionHasFontSizeProperty(expression)) {
135
+ context.report({
136
+ node: attribute,
137
+ messageId,
138
+ });
139
+ }
140
+
141
+ if (expression?.type === 'ObjectExpression') {
142
+ expression.properties.forEach((property) => {
143
+ if (property.type !== 'Property') {
144
+ return;
145
+ }
146
+
147
+ const propertyName = property.key?.type === 'Identifier'
148
+ ? property.key.name
149
+ : property.key?.value;
150
+
151
+ if (propertyName !== 'fontSize' && propertyName !== 'font-size') {
152
+ return;
153
+ }
154
+
155
+ if (
156
+ property.value?.type === 'Literal' &&
157
+ typeof property.value.value === 'string'
158
+ ) {
159
+ reportArbitraryFontSizesInText(
160
+ context,
161
+ `font-size: ${property.value.value}`,
162
+ property.value.range[0],
163
+ messageId,
164
+ );
165
+ }
166
+ });
167
+ }
168
+ }
@@ -0,0 +1,103 @@
1
+ import { getStaticName } from './vue-ast.js';
2
+
3
+ function isNonemptyString(value) {
4
+ return typeof value === 'string' && value.trim().length > 0;
5
+ }
6
+
7
+ function isLiteralStringExpression(expr) {
8
+ if (!expr) {
9
+ return false;
10
+ }
11
+
12
+ if (expr.type === 'Literal' && isNonemptyString(expr.value)) {
13
+ return true;
14
+ }
15
+
16
+ if (
17
+ expr.type === 'TemplateLiteral' &&
18
+ expr.expressions.length === 0 &&
19
+ expr.quasis.length === 1
20
+ ) {
21
+ return isNonemptyString(expr.quasis[0].value.cooked);
22
+ }
23
+
24
+ return false;
25
+ }
26
+
27
+ export function isNonemptyLabelAttribute(node) {
28
+ if (node?.type !== 'VAttribute' || !node.key) {
29
+ return false;
30
+ }
31
+
32
+ if (!node.directive) {
33
+ if (node.key.name !== 'label') {
34
+ return false;
35
+ }
36
+
37
+ if (node.value?.type === 'VLiteral') {
38
+ return isNonemptyString(node.value.value);
39
+ }
40
+
41
+ return Boolean(node.value);
42
+ }
43
+
44
+ if (node.key.name?.name !== 'bind') {
45
+ return false;
46
+ }
47
+
48
+ if (getStaticName(node.key.argument) !== 'label') {
49
+ return false;
50
+ }
51
+
52
+ if (node.value?.type !== 'VExpressionContainer') {
53
+ return false;
54
+ }
55
+
56
+ const { expression } = node.value;
57
+
58
+ if (expression?.type === 'Literal' || expression?.type === 'TemplateLiteral') {
59
+ return isLiteralStringExpression(expression);
60
+ }
61
+
62
+ return Boolean(expression);
63
+ }
64
+
65
+ export function hasNonemptyLabelProp(node) {
66
+ if (!Array.isArray(node.startTag?.attributes)) {
67
+ return false;
68
+ }
69
+
70
+ return node.startTag.attributes.some(isNonemptyLabelAttribute);
71
+ }
72
+
73
+ function isMeaningfulTemplateNode(node) {
74
+ if (!node) {
75
+ return false;
76
+ }
77
+
78
+ if (node.type === 'VText' || node.type === 'VTextContainer') {
79
+ return isNonemptyString(node.value);
80
+ }
81
+
82
+ if (node.type === 'VExpressionContainer') {
83
+ return Boolean(node.expression);
84
+ }
85
+
86
+ if (node.type === 'VElement') {
87
+ if (!Array.isArray(node.children) || node.children.length === 0) {
88
+ return false;
89
+ }
90
+
91
+ return node.children.some((child) => isMeaningfulTemplateNode(child));
92
+ }
93
+
94
+ return false;
95
+ }
96
+
97
+ export function hasMeaningfulDefaultSlotContent(node) {
98
+ if (!Array.isArray(node.children)) {
99
+ return false;
100
+ }
101
+
102
+ return node.children.some((child) => isMeaningfulTemplateNode(child));
103
+ }
@@ -1,6 +1,6 @@
1
1
  import { createTemplateBodyVisitor, isElementNamed } from './vue-ast.js';
2
2
 
3
- function isStaticOrBoundAttributeNamed(node, attributeName) {
3
+ export function isStaticOrBoundAttributeNamed(node, attributeName) {
4
4
  if (node?.type !== 'VAttribute') {
5
5
  return false;
6
6
  }
@@ -0,0 +1,36 @@
1
+ import { createTemplateBodyVisitor, isElementNamed } from './vue-ast.js';
2
+
3
+ export function createForbiddenElementRule({
4
+ elementNames,
5
+ description,
6
+ details,
7
+ examples,
8
+ message,
9
+ messageId,
10
+ }) {
11
+ return {
12
+ meta: {
13
+ type: 'suggestion',
14
+ docs: {
15
+ description,
16
+ details,
17
+ examples,
18
+ recommended: true,
19
+ },
20
+ schema: [],
21
+ messages: {
22
+ [messageId]: message,
23
+ },
24
+ },
25
+ create(context) {
26
+ return createTemplateBodyVisitor((node) => {
27
+ if (isElementNamed(node, elementNames)) {
28
+ context.report({
29
+ node,
30
+ messageId,
31
+ });
32
+ }
33
+ });
34
+ },
35
+ };
36
+ }
@@ -0,0 +1,47 @@
1
+ import { createTemplateBodyVisitor, isElementNamed } from './vue-ast.js';
2
+
3
+ export function createRequireAncestorRule({
4
+ childElementNames,
5
+ parentElementNames,
6
+ description,
7
+ details,
8
+ examples,
9
+ message,
10
+ messageId,
11
+ category = 'components',
12
+ }) {
13
+ return {
14
+ meta: {
15
+ type: 'problem',
16
+ docs: {
17
+ description,
18
+ details,
19
+ examples,
20
+ recommended: true,
21
+ category,
22
+ },
23
+ schema: [],
24
+ messages: {
25
+ [messageId]: message,
26
+ },
27
+ },
28
+ create(context) {
29
+ return createTemplateBodyVisitor((node, _parentElement, ancestors) => {
30
+ if (!isElementNamed(node, childElementNames)) {
31
+ return;
32
+ }
33
+
34
+ const hasRequiredAncestor = ancestors.some((ancestor) =>
35
+ isElementNamed(ancestor, parentElementNames),
36
+ );
37
+
38
+ if (!hasRequiredAncestor) {
39
+ context.report({
40
+ node,
41
+ messageId,
42
+ });
43
+ }
44
+ });
45
+ },
46
+ };
47
+ }