forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-require-form-method.js
More file actions
149 lines (132 loc) · 4.32 KB
/
template-require-form-method.js
File metadata and controls
149 lines (132 loc) · 4.32 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Form `method` attribute keywords:
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method
const VALID_FORM_METHODS = ['POST', 'GET', 'DIALOG'];
const DEFAULT_CONFIG = {
allowedMethods: VALID_FORM_METHODS,
};
function parseConfig(config) {
if (config === false) {
return false;
}
if (config === true || config === undefined) {
return DEFAULT_CONFIG;
}
if (typeof config === 'object' && Array.isArray(config.allowedMethods)) {
const allowedMethods = config.allowedMethods.map((m) => String(m).toUpperCase());
// Check if all methods are valid
const hasAllValid = allowedMethods.every((m) => VALID_FORM_METHODS.includes(m));
if (hasAllValid) {
return { allowedMethods };
}
}
throw new Error(
'template-require-form-method: invalid configuration. Expected one of:\n' +
' * boolean - `true` to enable / `false` to disable\n' +
' * object -- An object with the following keys:\n' +
` * \`allowedMethods\` -- An array of allowed form \`method\` attribute values of \`${VALID_FORM_METHODS}\`\n` +
`Received: ${JSON.stringify(config)}`
);
}
function makeErrorMessage(methods) {
return `All \`<form>\` elements should have \`method\` attribute with value of \`${methods.join(',')}\``;
}
function getFixedMethod(config) {
return config.allowedMethods[0];
}
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require form method attribute',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-form-method.md',
templateMode: 'both',
},
fixable: 'code',
schema: [
{
oneOf: [
{ type: 'boolean' },
{
type: 'object',
properties: {
allowedMethods: {
type: 'array',
items: {
// Accept any string so case-insensitive values like "get"
// still pass schema validation; parseConfig normalizes to
// upper-case and throws on values not in VALID_FORM_METHODS.
type: 'string',
},
},
},
additionalProperties: false,
},
],
},
],
messages: {
invalidMethod: '{{message}}',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/require-form-method.js',
docs: 'docs/rule/require-form-method.md',
tests: 'test/unit/rules/require-form-method-test.js',
},
},
create(context) {
// Default-enabled: parseConfig(undefined) returns DEFAULT_CONFIG.
// Pass `false` to explicitly disable the rule.
const rawOption = context.options[0];
const config = parseConfig(rawOption);
if (config === false) {
return {};
}
return {
GlimmerElementNode(node) {
if (node.tag !== 'form') {
return;
}
const methodAttribute = node.attributes.find((attr) => attr.name === 'method');
if (!methodAttribute) {
context.report({
node,
messageId: 'invalidMethod',
data: {
message: makeErrorMessage(config.allowedMethods),
},
fix(fixer) {
return fixer.insertTextAfterRange(
[node.parts.at(-1).range[1], node.parts.at(-1).range[1]],
` method="${getFixedMethod(config)}"`
);
},
});
return;
}
// Check if it's a text value
if (methodAttribute.value && methodAttribute.value.type === 'GlimmerTextNode') {
const methodValue = methodAttribute.value.chars.toUpperCase();
if (!config.allowedMethods.includes(methodValue)) {
context.report({
node,
messageId: 'invalidMethod',
data: {
message: makeErrorMessage(config.allowedMethods),
},
fix(fixer) {
return fixer.replaceTextRange(
methodAttribute.value.range,
`"${getFixedMethod(config)}"`
);
},
});
}
}
// If it's a dynamic value (like {{foo}}), don't report
},
};
},
};