-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathbutton-has-type.ts
134 lines (124 loc) · 3.01 KB
/
button-has-type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import type { AST } from 'svelte-eslint-parser';
import { createRule } from '../utils';
import {
findAttribute,
findShorthandAttribute,
findBindDirective,
getStaticAttributeValue
} from '../utils/ast-utils';
type Options = {
button: boolean;
submit: boolean;
reset: boolean;
};
export default createRule('button-has-type', {
meta: {
docs: {
description: 'disallow usage of button without an explicit type attribute',
category: 'Best Practices',
recommended: false
},
schema: [
{
type: 'object',
properties: {
button: {
type: 'boolean'
},
submit: {
type: 'boolean'
},
reset: {
type: 'boolean'
}
},
additionalProperties: false
}
],
messages: {
missingTypeAttribute: 'Missing an explicit type attribute for button.',
invalidTypeAttribute: '{{value}} is an invalid value for button type attribute.',
forbiddenTypeAttribute: '{{value}} is a forbidden value for button type attribute.',
emptyTypeAttribute: 'A value must be set for button type attribute.'
},
type: 'suggestion' // "problem",
},
create(context) {
const configuration: Options = {
button: true,
submit: true,
reset: true,
...(context.options[0] ?? {})
};
/**
* Checks whether given text is known button type
*/
function isButtonType(type: string): type is 'button' | 'submit' | 'reset' {
return type === 'button' || type === 'submit' || type === 'reset';
}
/**
* Report
*/
function report(
node: AST.SvelteAttribute | AST.SvelteBindingDirective | AST.SvelteStartTag,
messageId: string,
data: Record<string, string> = {}
) {
context.report({
node,
messageId,
data
});
}
/**
* Validate attribute
*/
function validateAttribute(attribute: AST.SvelteAttribute) {
if (attribute.value.length === 0) {
report(attribute, 'emptyTypeAttribute');
return;
}
const strValue = getStaticAttributeValue(attribute);
if (strValue == null) {
return;
}
if (!isButtonType(strValue)) {
report(attribute, 'invalidTypeAttribute', { value: strValue });
} else if (!configuration[strValue]) {
report(attribute, 'forbiddenTypeAttribute', { value: strValue });
}
}
/**
* @param {VDirective} directive
*/
function validateDirective(directive: AST.SvelteBindingDirective) {
if (!directive.expression) {
report(directive, 'emptyTypeAttribute');
}
}
return {
"SvelteElement[name.name='button'] > SvelteStartTag"(node: AST.SvelteStartTag) {
const typeAttr = findAttribute(node, 'type');
if (typeAttr) {
validateAttribute(typeAttr);
return;
}
const typeDir = findBindDirective(node, 'type');
if (typeDir) {
validateDirective(typeDir);
return;
}
const typeShortAttr = findShorthandAttribute(node, 'type');
if (typeShortAttr) {
return;
}
for (const attr of node.attributes) {
if (attr.type === 'SvelteSpreadAttribute') {
return;
}
}
report(node, 'missingTypeAttribute');
}
};
}
});