@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.
- package/README.md +59 -2
- package/index.js +83 -11
- package/package.json +1 -1
- package/rules/accessibility/require-vjoyiconbutton-label.js +92 -0
- package/rules/{require-vjoytemplate-template-title-heading.js → accessibility/require-vjoytemplate-template-title-heading.js} +2 -1
- package/rules/{no-vjoyavatar-fullname-totalnumber.js → components/no-vjoyavatar-fullname-totalnumber.js} +1 -1
- package/rules/{no-vjoyavatar-photourl-totalnumber.js → components/no-vjoyavatar-photourl-totalnumber.js} +1 -1
- package/rules/{no-vjoybutton-circle.js → components/no-vjoybutton-circle.js} +1 -1
- package/rules/components/no-vjoybutton-icon-only.js +87 -0
- package/rules/components/no-vjoybutton-white-variant.js +94 -0
- package/rules/components/no-vjoycollapse-single-item.js +55 -0
- package/rules/components/no-vjoycollapseitem-outside-collapse.js +15 -0
- package/rules/components/no-vjoycompanyavatar.js +16 -0
- package/rules/components/no-vjoyformerror-no-html-error-text.js +66 -0
- package/rules/components/no-vjoyformerror-visible-prop.js +18 -0
- package/rules/{no-vjoyformgroup-vjoyradio.js → components/no-vjoyformgroup-vjoyradio.js} +1 -1
- package/rules/components/no-vjoyfunnelfooter-slot.js +89 -0
- package/rules/{no-vjoyinput-type-password.js → components/no-vjoyinput-type-password.js} +1 -1
- package/rules/{no-vjoyinput-unit.js → components/no-vjoyinput-unit.js} +1 -1
- package/rules/components/no-vjoymenu-items-prop.js +19 -0
- package/rules/{no-vjoymenu-vjoypanel.js → components/no-vjoymenu-vjoypanel.js} +1 -1
- package/rules/components/no-vjoymenuitem-outside-menu.js +15 -0
- package/rules/components/no-vjoyradio-outside-group.js +16 -0
- package/rules/components/no-vjoyselectableitem-group.js +17 -0
- package/rules/components/no-vjoyselectableitem.js +55 -0
- package/rules/components/no-vjoysnackbar-direct.js +18 -0
- package/rules/components/no-vjoystep-outside-stepper.js +15 -0
- package/rules/components/no-vjoytab-outside-tabs.js +15 -0
- package/rules/components/no-vjoytooltip-enable-click-in-content.js +18 -0
- package/rules/{no-vjoyusercard-rating-stars-slot.js → components/no-vjoyusercard-rating-stars-slot.js} +1 -1
- package/rules/{require-vjoymenu-template-sidebar.js → components/require-vjoymenu-template-sidebar.js} +1 -1
- package/rules/{require-vjoytemplate-template-title.js → components/require-vjoytemplate-template-title.js} +1 -1
- package/rules/tokens/no-arbitrary-font-size.js +24 -0
- package/rules/tokens/no-hardcoded-color-names.js +22 -0
- package/rules/tokens/no-hardcoded-hex-colors-in-vue.js +22 -0
- package/rules/tokens/no-hardcoded-rgb-colors-in-vue.js +22 -0
- package/rules/tokens/no-inline-style-colors.js +23 -0
- package/rules/tokens/prefer-joy-color-classes.js +128 -0
- package/rules/tokens/vue-style-visitors.js +168 -0
- package/rules/utils/forbidden-attribute.js +1 -1
- package/rules/utils/forbidden-element.js +36 -0
- package/rules/utils/require-ancestor.js +47 -0
- package/rules/utils/style-inspection.js +439 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { createTemplateBodyVisitor } from '../utils/vue-ast.js';
|
|
2
|
+
import {
|
|
3
|
+
elementHasClassName,
|
|
4
|
+
extractJoyColorVarName,
|
|
5
|
+
getPropertyName,
|
|
6
|
+
getSuggestedJoyColorClass,
|
|
7
|
+
isBoundStyleAttribute,
|
|
8
|
+
isColorPropertyName,
|
|
9
|
+
isStaticStyleAttribute,
|
|
10
|
+
iterateJoyColorVarDeclarations,
|
|
11
|
+
} from '../utils/style-inspection.js';
|
|
12
|
+
|
|
13
|
+
const WARNING_MESSAGE =
|
|
14
|
+
'Prefer Joy color utility classes such as {{ suggestedClass }} instead of inline var(--joy-color-*).';
|
|
15
|
+
|
|
16
|
+
function reportMissingJoyColorClass(context, node, declaration, messageId) {
|
|
17
|
+
const suggestedClass = declaration.data?.suggestedClass;
|
|
18
|
+
|
|
19
|
+
if (!suggestedClass || elementHasClassName(node, suggestedClass)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
context.report({
|
|
24
|
+
loc: {
|
|
25
|
+
start: context.sourceCode.getLocFromIndex(declaration.index),
|
|
26
|
+
end: context.sourceCode.getLocFromIndex(declaration.index + declaration.length),
|
|
27
|
+
},
|
|
28
|
+
messageId,
|
|
29
|
+
data: { suggestedClass },
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function inspectInlineStyleText(context, node, text, startIndex, messageId) {
|
|
34
|
+
for (const declaration of iterateJoyColorVarDeclarations(text)) {
|
|
35
|
+
reportMissingJoyColorClass(context, node, {
|
|
36
|
+
...declaration,
|
|
37
|
+
index: startIndex + declaration.index,
|
|
38
|
+
}, messageId);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function inspectStyleExpression(context, node, expression, attribute, messageId) {
|
|
43
|
+
if (expression?.type !== 'ObjectExpression') {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
expression.properties.forEach((property) => {
|
|
48
|
+
if (property.type !== 'Property') {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const propertyName = getPropertyName(property);
|
|
53
|
+
|
|
54
|
+
if (
|
|
55
|
+
!isColorPropertyName(propertyName) ||
|
|
56
|
+
property.value?.type !== 'Literal' ||
|
|
57
|
+
typeof property.value.value !== 'string'
|
|
58
|
+
) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const cssVarName = extractJoyColorVarName(property.value.value);
|
|
63
|
+
const suggestedClass = cssVarName
|
|
64
|
+
? getSuggestedJoyColorClass(propertyName, cssVarName)
|
|
65
|
+
: undefined;
|
|
66
|
+
|
|
67
|
+
if (!suggestedClass || elementHasClassName(node, suggestedClass)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
context.report({
|
|
72
|
+
node: attribute,
|
|
73
|
+
messageId,
|
|
74
|
+
data: { suggestedClass },
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export default {
|
|
80
|
+
meta: {
|
|
81
|
+
type: 'suggestion',
|
|
82
|
+
docs: {
|
|
83
|
+
description: 'prefer Joy color utility classes over inline var(--joy-color-*) styles',
|
|
84
|
+
details:
|
|
85
|
+
'When setting text or background colors inline with Joy CSS variables, prefer the matching utility classes from @maltjoy/themes such as joy-color-primary-30 or joy-bgcolor-neutral-0.',
|
|
86
|
+
examples: {
|
|
87
|
+
incorrect: '<div style="color: var(--joy-color-primary-30)" />',
|
|
88
|
+
correct: '<div class="joy-color-primary-30" />',
|
|
89
|
+
},
|
|
90
|
+
recommended: true,
|
|
91
|
+
category: 'tokens',
|
|
92
|
+
},
|
|
93
|
+
schema: [],
|
|
94
|
+
messages: {
|
|
95
|
+
preferJoyColorClass: WARNING_MESSAGE,
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
create(context) {
|
|
99
|
+
return createTemplateBodyVisitor((node) => {
|
|
100
|
+
if (!Array.isArray(node.startTag?.attributes)) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
node.startTag.attributes.forEach((attribute) => {
|
|
105
|
+
if (isStaticStyleAttribute(attribute)) {
|
|
106
|
+
inspectInlineStyleText(
|
|
107
|
+
context,
|
|
108
|
+
node,
|
|
109
|
+
attribute.value.value,
|
|
110
|
+
attribute.value.range[0],
|
|
111
|
+
'preferJoyColorClass',
|
|
112
|
+
);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (isBoundStyleAttribute(attribute)) {
|
|
117
|
+
inspectStyleExpression(
|
|
118
|
+
context,
|
|
119
|
+
node,
|
|
120
|
+
attribute.value?.expression,
|
|
121
|
+
attribute,
|
|
122
|
+
'preferJoyColorClass',
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
};
|
|
@@ -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,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
|
+
}
|