Skip to content

Commit fb14ff6

Browse files
committed
Extract rule: template-no-extra-mut-helpers
1 parent 0149ef1 commit fb14ff6

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ rules in templates can be disabled with eslint directives with mustache or html
199199
| [template-no-capital-arguments](docs/rules/template-no-capital-arguments.md) | disallow capital arguments (use lowercase @arg instead of @Arg) | | | |
200200
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
201201
| [template-no-element-event-actions](docs/rules/template-no-element-event-actions.md) | disallow element event actions (use {{on}} modifier instead) | | | |
202+
| [template-no-extra-mut-helpers](docs/rules/template-no-extra-mut-helpers.md) | disallow unnecessary mut helpers | | | |
202203
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
203204

204205
### Components
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ember/template-no-extra-mut-helpers
2+
3+
<!-- end auto-generated rule header -->
4+
5+
Disallows unnecessary `mut` helpers.
6+
7+
## Rule Details
8+
9+
The `mut` helper is often unnecessary when passing simple values or properties. It should only be used when explicitly creating a mutable reference is required.
10+
11+
## Examples
12+
13+
Examples of **incorrect** code for this rule:
14+
15+
```gjs
16+
<template>
17+
<MyComponent @onChange={{mut this.value}} />
18+
</template>
19+
```
20+
21+
```gjs
22+
<template>
23+
<Input @value={{mut this.text}} />
24+
</template>
25+
```
26+
27+
Examples of **correct** code for this rule:
28+
29+
```gjs
30+
<template>
31+
<MyComponent @value={{this.value}} />
32+
</template>
33+
```
34+
35+
```gjs
36+
<template>
37+
<Input @value={{this.text}} />
38+
</template>
39+
```
40+
41+
## References
42+
43+
- [eslint-plugin-ember template-no-extra-mut-helper-argument](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-extra-mut-helper-argument.md)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'disallow unnecessary mut helpers',
7+
category: 'Best Practices',
8+
strictGjs: true,
9+
strictGts: true,
10+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-extra-mut-helpers.md',
11+
},
12+
fixable: null,
13+
schema: [],
14+
messages: {
15+
unnecessaryMut: 'Unnecessary mut helper. Remove it.',
16+
},
17+
},
18+
19+
create(context) {
20+
return {
21+
GlimmerSubExpression(node) {
22+
if (
23+
node.path &&
24+
node.path.type === 'GlimmerPathExpression' &&
25+
node.path.original === 'mut' &&
26+
node.params &&
27+
node.params.length === 1
28+
) {
29+
const param = node.params[0];
30+
// mut is unnecessary when wrapping a simple path expression
31+
if (param.type === 'GlimmerPathExpression') {
32+
context.report({
33+
node,
34+
messageId: 'unnecessaryMut',
35+
});
36+
}
37+
}
38+
},
39+
};
40+
},
41+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
const rule = require('../../../lib/rules/template-no-extra-mut-helpers');
6+
const RuleTester = require('eslint').RuleTester;
7+
8+
//------------------------------------------------------------------------------
9+
// Tests
10+
//------------------------------------------------------------------------------
11+
12+
const ruleTester = new RuleTester({
13+
parser: require.resolve('ember-eslint-parser'),
14+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
15+
});
16+
17+
ruleTester.run('template-no-extra-mut-helpers', rule, {
18+
valid: [
19+
`<template>
20+
<MyComponent @value={{this.value}} />
21+
</template>`,
22+
`<template>
23+
<input value={{this.text}} />
24+
</template>`,
25+
`<template>
26+
<div></div>
27+
</template>`,
28+
],
29+
30+
invalid: [
31+
{
32+
code: `<template>
33+
{{my-component onChange=(mut this.value)}}
34+
</template>`,
35+
output: null,
36+
errors: [
37+
{
38+
message: 'Unnecessary mut helper. Remove it.',
39+
type: 'GlimmerSubExpression',
40+
},
41+
],
42+
},
43+
{
44+
code: `<template>
45+
{{input value=(mut this.text)}}
46+
</template>`,
47+
output: null,
48+
errors: [
49+
{
50+
message: 'Unnecessary mut helper. Remove it.',
51+
type: 'GlimmerSubExpression',
52+
},
53+
],
54+
},
55+
{
56+
code: `<template>
57+
{{my-component value=(mut this.data)}}
58+
</template>`,
59+
output: null,
60+
errors: [
61+
{
62+
message: 'Unnecessary mut helper. Remove it.',
63+
type: 'GlimmerSubExpression',
64+
},
65+
],
66+
},
67+
],
68+
});

0 commit comments

Comments
 (0)