Skip to content

Commit 29180c5

Browse files
committed
Extract rule: template-no-extra-mut-helper-argument
1 parent ee24ac8 commit 29180c5

4 files changed

Lines changed: 178 additions & 3 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,10 @@ rules in templates can be disabled with eslint directives with mustache or html
344344

345345
### Possible Errors
346346

347-
| Name | Description | 💼 | 🔧 | 💡 |
348-
| :--------------------------------------------------------------------- | :-------------------------------------- | :- | :- | :- |
349-
| [template-no-jsx-attributes](docs/rules/template-no-jsx-attributes.md) | disallow JSX-style camelCase attributes | | 🔧 | |
347+
| Name | Description | 💼 | 🔧 | 💡 |
348+
| :------------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- | :- | :- |
349+
| [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 | | | |
350+
| [template-no-jsx-attributes](docs/rules/template-no-jsx-attributes.md) | disallow JSX-style camelCase attributes | | 🔧 | |
350351

351352
### Routes
352353

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ember/template-no-extra-mut-helper-argument
2+
3+
> **HBS Only**: This rule applies to classic `.hbs` template files only (loose mode). It is not relevant for `gjs`/`gts` files (strict mode), where these patterns cannot occur.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
Disallows passing more than one argument to the `mut` helper.
8+
9+
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`.
10+
11+
## Examples
12+
13+
This rule **forbids** the following:
14+
15+
```hbs
16+
{{my-component click=(action (mut isClicked true))}}
17+
```
18+
19+
This rule **allows** the following:
20+
21+
```hbs
22+
{{my-component click=(action (mut isClicked) true)}}
23+
```
24+
25+
## Related Rules
26+
27+
- [template-no-mut-helper](template-no-mut-helper.md)
28+
29+
## References
30+
31+
- 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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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: false,
9+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-extra-mut-helper-argument.md',
10+
templateMode: 'loose',
11+
},
12+
fixable: null,
13+
schema: [],
14+
messages: {},
15+
originallyFrom: {
16+
name: 'ember-template-lint',
17+
rule: 'lib/rules/no-extra-mut-helper-argument.js',
18+
docs: 'docs/rule/no-extra-mut-helper-argument.md',
19+
tests: 'test/unit/rules/no-extra-mut-helper-argument-test.js',
20+
},
21+
},
22+
23+
create(context) {
24+
const ERROR_MESSAGE =
25+
'The handlebars `mut(attr)` helper should only have one argument passed to it. To pass a value, use: `(action (mut attr) value)`.';
26+
27+
return {
28+
GlimmerSubExpression(node) {
29+
if (
30+
node.path &&
31+
node.path.type === 'GlimmerPathExpression' &&
32+
node.path.original === 'mut'
33+
) {
34+
if (node.params && node.params.length > 1) {
35+
context.report({
36+
node,
37+
message: ERROR_MESSAGE,
38+
});
39+
}
40+
}
41+
},
42+
};
43+
},
44+
};
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
});
50+
51+
const hbsRuleTester = new RuleTester({
52+
parser: require.resolve('ember-eslint-parser/hbs'),
53+
parserOptions: {
54+
ecmaVersion: 2022,
55+
sourceType: 'module',
56+
},
57+
});
58+
59+
hbsRuleTester.run('template-no-extra-mut-helper-argument', rule, {
60+
valid: [
61+
'{{my-component click=(action (mut isClicked))}}',
62+
'{{my-component click=(action (mut isClicked) true)}}',
63+
'{{my-component isClickedMutable=(mut isClicked)}}',
64+
'<button {{action (mut isClicked)}}></button>',
65+
'<button {{action (mut isClicked) true}}></button>',
66+
],
67+
invalid: [
68+
{
69+
code: '{{my-component click=(action (mut isClicked true))}}',
70+
output: null,
71+
errors: [
72+
{
73+
message:
74+
'The handlebars `mut(attr)` helper should only have one argument passed to it. To pass a value, use: `(action (mut attr) value)`.',
75+
},
76+
],
77+
},
78+
{
79+
code: '{{my-component isClickedMutable=(mut isClicked true)}}',
80+
output: null,
81+
errors: [
82+
{
83+
message:
84+
'The handlebars `mut(attr)` helper should only have one argument passed to it. To pass a value, use: `(action (mut attr) value)`.',
85+
},
86+
],
87+
},
88+
{
89+
code: '<button {{action (mut isClicked true)}}></button>',
90+
output: null,
91+
errors: [
92+
{
93+
message:
94+
'The handlebars `mut(attr)` helper should only have one argument passed to it. To pass a value, use: `(action (mut attr) value)`.',
95+
},
96+
],
97+
},
98+
],
99+
});

0 commit comments

Comments
 (0)