Skip to content

Commit 0c50c92

Browse files
committed
Extract rule: template-no-extra-mut-helper-argument
1 parent 3e664f5 commit 0c50c92

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ rules in templates can be disabled with eslint directives with mustache or html
309309
| [no-runloop](docs/rules/no-runloop.md) | disallow usage of `@ember/runloop` functions || | |
310310
| [require-fetch-import](docs/rules/require-fetch-import.md) | enforce explicit import for `fetch()` | | | |
311311

312+
### Possible Errors
313+
314+
| Name | Description | 💼 | 🔧 | 💡 |
315+
| :------------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- | :- | :- |
316+
| [template-no-extra-mut-helper-argument](docs/rules/template-no-extra-mut-helper-argument.md) | disallow passing more than one argument to the mut helper | | | |
317+
312318
### Routes
313319

314320
| Name                             | Description | 💼 | 🔧 | 💡 |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ember/template-no-extra-mut-helper-argument
2+
3+
<!-- end auto-generated rule header -->
4+
5+
Disallows passing more than one argument to the `mut` helper.
6+
7+
A common mistake when using the Ember handlebars template `mut(attr)` helper is to pass an extra `value` parameter to it when only `attr` should be passed. Instead, the `value` should be passed outside of `mut`.
8+
9+
## Examples
10+
11+
This rule **forbids** the following:
12+
13+
```hbs
14+
{{my-component click=(action (mut isClicked true))}}
15+
```
16+
17+
This rule **allows** the following:
18+
19+
```hbs
20+
{{my-component click=(action (mut isClicked) true)}}
21+
```
22+
23+
## Related Rules
24+
25+
- [template-no-mut-helper](template-no-mut-helper.md)
26+
27+
## References
28+
29+
- See the [documentation](https://emberjs.com/api/ember/release/classes/Ember.Templates.helpers/methods/mut?anchor=mut) for the Ember handlebars template `mut` helper
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'problem',
5+
docs: {
6+
description: 'disallow passing more than one argument to the mut helper',
7+
category: 'Possible Errors',
8+
recommended: true,
9+
strictGjs: true,
10+
strictGts: true,
11+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-extra-mut-helper-argument.md',
12+
},
13+
fixable: null,
14+
schema: [],
15+
messages: {},
16+
},
17+
18+
create(context) {
19+
const ERROR_MESSAGE =
20+
'The handlebars `mut(attr)` helper should only have one argument passed to it. To pass a value, use: `(action (mut attr) value)`.';
21+
22+
return {
23+
GlimmerSubExpression(node) {
24+
if (
25+
node.path &&
26+
node.path.type === 'GlimmerPathExpression' &&
27+
node.path.original === 'mut'
28+
) {
29+
if (node.params && node.params.length > 1) {
30+
context.report({
31+
node,
32+
message: ERROR_MESSAGE,
33+
});
34+
}
35+
}
36+
},
37+
};
38+
},
39+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const rule = require('../../../lib/rules/template-no-extra-mut-helper-argument');
2+
const RuleTester = require('eslint').RuleTester;
3+
4+
const ruleTester = new RuleTester({
5+
parser: require.resolve('ember-eslint-parser'),
6+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
7+
});
8+
9+
ruleTester.run('template-no-extra-mut-helper-argument', rule, {
10+
valid: [
11+
'<template>{{my-component click=(action (mut isClicked))}}</template>',
12+
'<template>{{my-component click=(action (mut isClicked) true)}}</template>',
13+
'<template>{{my-component isClickedMutable=(mut isClicked)}}</template>',
14+
'<template><button {{action (mut isClicked)}}></button></template>',
15+
'<template><button {{action (mut isClicked) true}}></button></template>',
16+
],
17+
invalid: [
18+
{
19+
code: '<template>{{my-component click=(action (mut isClicked true))}}</template>',
20+
output: null,
21+
errors: [
22+
{
23+
message:
24+
'The handlebars `mut(attr)` helper should only have one argument passed to it. To pass a value, use: `(action (mut attr) value)`.',
25+
},
26+
],
27+
},
28+
{
29+
code: '<template>{{my-component isClickedMutable=(mut isClicked true)}}</template>',
30+
output: null,
31+
errors: [
32+
{
33+
message:
34+
'The handlebars `mut(attr)` helper should only have one argument passed to it. To pass a value, use: `(action (mut attr) value)`.',
35+
},
36+
],
37+
},
38+
{
39+
code: '<template><button {{action (mut isClicked true)}}></button></template>',
40+
output: null,
41+
errors: [
42+
{
43+
message:
44+
'The handlebars `mut(attr)` helper should only have one argument passed to it. To pass a value, use: `(action (mut attr) value)`.',
45+
},
46+
],
47+
},
48+
],
49+
});

0 commit comments

Comments
 (0)