forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-no-action-on-submit-button.js
More file actions
62 lines (57 loc) · 1.96 KB
/
template-no-action-on-submit-button.js
File metadata and controls
62 lines (57 loc) · 1.96 KB
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
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow action attribute on submit buttons',
category: 'Best Practices',
strictGjs: true,
strictGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-action-on-submit-button.md',
},
fixable: null,
schema: [],
messages: {
noActionOnSubmitButton:
'Do not use action attribute on submit buttons. Use on modifier instead or handle form submission.',
},
},
create(context) {
return {
GlimmerElementNode(node) {
// Check if this is a button element
if (node.tag !== 'button' && node.tag !== 'input') {
return;
}
let hasActionAttribute = false;
let isSubmitButton = false;
let hasNonSubmitType = false;
for (const attr of node.attributes) {
if (attr.type === 'GlimmerAttrNode') {
// Check for action attribute
if (attr.name === 'action') {
hasActionAttribute = true;
}
// Check if type="submit" or no type (defaults to submit for button)
if (attr.name === 'type') {
const value = attr.value;
if (value.type === 'GlimmerTextNode' && value.chars === 'submit') {
isSubmitButton = true;
} else if (value.type === 'GlimmerTextNode' && value.chars !== 'submit') {
hasNonSubmitType = true;
}
}
}
}
// For buttons, default type is submit unless explicitly set otherwise
const isDefaultSubmitButton = node.tag === 'button' && !hasNonSubmitType && !isSubmitButton;
if (hasActionAttribute && (isSubmitButton || isDefaultSubmitButton)) {
context.report({
node,
messageId: 'noActionOnSubmitButton',
});
}
},
};
},
};