Skip to content

Commit 1ebfdf1

Browse files
committed
Sync with template-lint
1 parent f9b47db commit 1ebfdf1

4 files changed

Lines changed: 139 additions & 234 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ rules in templates can be disabled with eslint directives with mustache or html
253253
| [template-no-obsolete-elements](docs/rules/template-no-obsolete-elements.md) | disallow obsolete HTML elements | | | |
254254
| [template-no-outlet-outside-routes](docs/rules/template-no-outlet-outside-routes.md) | disallow {{outlet}} outside of route templates | | | |
255255
| [template-no-page-title-component](docs/rules/template-no-page-title-component.md) | disallow usage of ember-page-title component | | | |
256-
| [template-require-form-method](docs/rules/template-require-form-method.md) | require form method attribute | | | |
256+
| [template-require-form-method](docs/rules/template-require-form-method.md) | require form method attribute | | 🔧 | |
257257
| [template-require-has-block-helper](docs/rules/template-require-has-block-helper.md) | require (has-block) helper usage instead of hasBlock property | | 🔧 | |
258258
| [template-require-iframe-src-attribute](docs/rules/template-require-iframe-src-attribute.md) | require iframe elements to have src attribute | | 🔧 | |
259259
| [template-require-splattributes](docs/rules/template-require-splattributes.md) | require splattributes usage in component templates | | | |
Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,55 @@
11
# ember/template-require-form-method
22

3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
35
<!-- end auto-generated rule header -->
46

5-
Require form elements to have a method attribute.
7+
This rule requires all `<form>` elements to have `method` attribute with `POST`, `GET` or `DIALOG` value.
68

7-
Form elements should explicitly specify the HTTP method they use. This improves code clarity and helps catch potential issues.
9+
By default `form` elements without `method` attribute are submitted as `GET` requests.
10+
In usual applications `submit` event listeners are attached to `form` elements and `event.preventDefault()` is called to avoid form submission.
811

9-
## Examples
12+
However in case of failure to prevent default action, form submission as `GET` request can leak sensitive end-user information.
1013

11-
This rule **forbids** the following:
14+
Example uses of `GET` requests:
1215

13-
```gjs
14-
<template>
15-
<form></form>
16-
</template>
17-
```
16+
- non-secure data
17+
- bookmarking the submission result
18+
- data search query strings
1819

19-
```gjs
20-
<template>
21-
<form method='DELETE'></form>
22-
</template>
23-
```
20+
**Caution** - this rules does not check for `formmethod` attribute on `form` elements themselves.
2421

25-
This rule **allows** the following:
22+
## Examples
2623

27-
```gjs
28-
<template>
29-
<form method='POST'></form>
30-
</template>
31-
```
24+
This rule **forbids** the following:
3225

3326
```gjs
3427
<template>
35-
<form method='GET'></form>
28+
<form>Hello world!</form>
29+
<form method=''></form>
30+
<form method='random'>Hello world!</form>
3631
</template>
3732
```
3833

39-
```gjs
40-
<template>
41-
<form method='DIALOG'></form>
42-
</template>
43-
```
34+
This rule **allows** the following:
4435

4536
```gjs
4637
<template>
47-
<form method='{{dynamicMethod}}'></form>
38+
<form method='post'>Hello world!</form>
39+
<form method='get'>Hello world!</form>
40+
<form method='dialog'>Hello world!</form>
4841
</template>
4942
```
5043

5144
## Configuration
5245

53-
- `allowedMethods` (default: `['POST', 'GET', 'DIALOG']`) - Array of allowed form method values
54-
55-
```js
56-
// .eslintrc.js
57-
module.exports = {
58-
rules: {
59-
'ember/template-require-form-method': [
60-
'error',
61-
{
62-
allowedMethods: ['POST', 'GET'],
63-
},
64-
],
65-
},
66-
};
67-
```
46+
The following values are valid configuration:
47+
48+
- boolean - `true` to enable / `false` to disable
49+
- object -- An object with the following keys:
50+
- `allowedMethods` -- An array of allowed form `method` attribute values, default: `['POST', 'GET', 'DIALOG']`
6851

6952
## References
7053

71-
- [HTML Spec - Form Method Attribute](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method)
54+
- [MDN - form method attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-method)
55+
- [HTML spec - form method attribute](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method)

lib/rules/template-require-form-method.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,21 @@ function makeErrorMessage(methods) {
3333
return `All \`<form>\` elements should have \`method\` attribute with value of \`${methods.join(',')}\``;
3434
}
3535

36+
function getFixedMethod(config) {
37+
return config.allowedMethods[0];
38+
}
39+
3640
/** @type {import('eslint').Rule.RuleModule} */
3741
module.exports = {
3842
meta: {
3943
type: 'suggestion',
4044
docs: {
4145
description: 'require form method attribute',
4246
category: 'Best Practices',
43-
recommended: false,
4447
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-require-form-method.md',
4548
templateMode: 'both',
4649
},
47-
fixable: null,
50+
fixable: 'code',
4851
schema: [
4952
{
5053
oneOf: [
@@ -63,7 +66,9 @@ module.exports = {
6366
],
6467
},
6568
],
66-
messages: {},
69+
messages: {
70+
invalidMethod: '{{message}}',
71+
},
6772
originallyFrom: {
6873
name: 'ember-template-lint',
6974
rule: 'lib/rules/require-form-method.js',
@@ -92,7 +97,16 @@ module.exports = {
9297
if (!methodAttribute) {
9398
context.report({
9499
node,
95-
message: makeErrorMessage(config.allowedMethods),
100+
messageId: 'invalidMethod',
101+
data: {
102+
message: makeErrorMessage(config.allowedMethods),
103+
},
104+
fix(fixer) {
105+
return fixer.insertTextAfterRange(
106+
[node.parts.at(-1).range[1], node.parts.at(-1).range[1]],
107+
` method="${getFixedMethod(config)}"`
108+
);
109+
},
96110
});
97111
return;
98112
}
@@ -104,7 +118,16 @@ module.exports = {
104118
if (!config.allowedMethods.includes(methodValue)) {
105119
context.report({
106120
node,
107-
message: makeErrorMessage(config.allowedMethods),
121+
messageId: 'invalidMethod',
122+
data: {
123+
message: makeErrorMessage(config.allowedMethods),
124+
},
125+
fix(fixer) {
126+
return fixer.replaceTextRange(
127+
methodAttribute.value.range,
128+
`"${getFixedMethod(config)}"`
129+
);
130+
},
108131
});
109132
}
110133
}

0 commit comments

Comments
 (0)