@maltjoy/eslint-rules 0.6.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.
package/package.json
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hasMeaningfulDefaultSlotContent,
|
|
3
|
+
hasNonemptyLabelProp,
|
|
4
|
+
} from '../utils/accessible-label.js';
|
|
1
5
|
import { createTemplateBodyVisitor, isElementNamed } from '../utils/vue-ast.js';
|
|
2
6
|
|
|
3
7
|
const ERROR_MESSAGE =
|
|
@@ -7,55 +11,6 @@ function isVJoyIconButtonElement(node) {
|
|
|
7
11
|
return isElementNamed(node, ['vjoyiconbutton', 'v-joy-icon-button']);
|
|
8
12
|
}
|
|
9
13
|
|
|
10
|
-
function isLabelAttribute(node) {
|
|
11
|
-
if (node?.type !== 'VAttribute') {
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (!node.directive) {
|
|
16
|
-
if (node.key?.name !== 'label') {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
if (node.value?.type === 'VLiteral') {
|
|
21
|
-
return node.value.value.trim().length > 0;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return Boolean(node.value);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return (
|
|
28
|
-
node.key?.name?.name === 'bind' &&
|
|
29
|
-
node.key?.argument?.name === 'label'
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function hasLabelProp(node) {
|
|
34
|
-
if (!Array.isArray(node.startTag?.attributes)) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return node.startTag.attributes.some(isLabelAttribute);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function hasDefaultSlotContent(node) {
|
|
42
|
-
if (!Array.isArray(node.children)) {
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return node.children.some((child) => {
|
|
47
|
-
if (child.type === 'VText') {
|
|
48
|
-
return child.value.trim().length > 0;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (child.type === 'VTextContainer') {
|
|
52
|
-
return child.value.trim().length > 0;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return child.type === 'VElement';
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
14
|
export default {
|
|
60
15
|
meta: {
|
|
61
16
|
type: 'suggestion',
|
|
@@ -79,8 +34,8 @@ export default {
|
|
|
79
34
|
return createTemplateBodyVisitor((node) => {
|
|
80
35
|
if (
|
|
81
36
|
isVJoyIconButtonElement(node) &&
|
|
82
|
-
!
|
|
83
|
-
!
|
|
37
|
+
!hasNonemptyLabelProp(node) &&
|
|
38
|
+
!hasMeaningfulDefaultSlotContent(node)
|
|
84
39
|
) {
|
|
85
40
|
context.report({
|
|
86
41
|
node,
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { isStaticOrBoundAttributeNamed } from '../utils/forbidden-attribute.js';
|
|
2
|
+
import {
|
|
3
|
+
hasMeaningfulDefaultSlotContent,
|
|
4
|
+
hasNonemptyLabelProp,
|
|
5
|
+
} from '../utils/accessible-label.js';
|
|
2
6
|
import { createTemplateBodyVisitor, isElementNamed } from '../utils/vue-ast.js';
|
|
3
7
|
|
|
4
8
|
const ERROR_MESSAGE =
|
|
@@ -18,45 +22,13 @@ function hasIconProp(node) {
|
|
|
18
22
|
);
|
|
19
23
|
}
|
|
20
24
|
|
|
21
|
-
function hasLabelProp(node) {
|
|
22
|
-
if (!Array.isArray(node.startTag?.attributes)) {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return node.startTag.attributes.some((attribute) => {
|
|
27
|
-
if (!isStaticOrBoundAttributeNamed(attribute, 'label')) {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (!attribute.directive && attribute.value?.type === 'VLiteral') {
|
|
32
|
-
return attribute.value.value.trim().length > 0;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return true;
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function hasMeaningfulDefaultSlotContent(node) {
|
|
40
|
-
if (!Array.isArray(node.children)) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return node.children.some((child) => {
|
|
45
|
-
if (child.type === 'VText' || child.type === 'VTextContainer') {
|
|
46
|
-
return child.value.trim().length > 0;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return child.type === 'VElement';
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
25
|
export default {
|
|
54
26
|
meta: {
|
|
55
27
|
type: 'problem',
|
|
56
28
|
docs: {
|
|
57
29
|
description: 'disallow icon-only usage of VJoyButton',
|
|
58
30
|
details:
|
|
59
|
-
'When a button only displays an icon, use VJoyIconButton instead of VJoyButton with an icon and no accessible label or slot content.',
|
|
31
|
+
'When a button only displays an icon, use VJoyIconButton instead of VJoyButton with an icon and no accessible label prop or default slot content.',
|
|
60
32
|
examples: {
|
|
61
33
|
incorrect: '<VJoyButton icon="edit" />',
|
|
62
34
|
correct: '<VJoyIconButton icon="edit" label="Edit" />',
|
|
@@ -74,7 +46,7 @@ export default {
|
|
|
74
46
|
if (
|
|
75
47
|
isVJoyButtonElement(node) &&
|
|
76
48
|
hasIconProp(node) &&
|
|
77
|
-
!
|
|
49
|
+
!hasNonemptyLabelProp(node) &&
|
|
78
50
|
!hasMeaningfulDefaultSlotContent(node)
|
|
79
51
|
) {
|
|
80
52
|
context.report({
|
|
@@ -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
|
+
}
|