Skip to content

Commit c37c5ad

Browse files
Merge pull request #2692 from johanrd/day_fix/template-no-mut-helper
Post-merge-review: Fix template-no-mut-helper: templateMode 'both' and unwrap setterAlternative
2 parents b4a9afd + e4ab056 commit c37c5ad

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

docs/rules/template-no-mut-helper.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# ember/template-no-mut-helper
22

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-
53
<!-- end auto-generated rule header -->
64

75
Disallow usage of the `(mut)` helper.

lib/rules/template-no-mut-helper.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
description: 'disallow usage of (mut) helper',
77
category: 'Best Practices',
88
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-mut-helper.md',
9-
templateMode: 'loose',
9+
templateMode: 'both',
1010
},
1111
fixable: null,
1212
schema: [
@@ -31,7 +31,13 @@ module.exports = {
3131

3232
create(context) {
3333
const options = context.options[0] || {};
34-
const setterAlternative = options.setterAlternative;
34+
const rawSetterAlternative = options.setterAlternative;
35+
// If the user already wrapped their expression in `{{...}}`, strip it so we
36+
// don't end up with `{{{{...}}}}` in the error message.
37+
const setterAlternative =
38+
typeof rawSetterAlternative === 'string'
39+
? rawSetterAlternative.replace(/^{{([\S\s]*)}}$/, '$1').trim()
40+
: rawSetterAlternative;
3541
const message = setterAlternative
3642
? `Do not use the (mut) helper. Consider using a JS action or {{${setterAlternative}}} instead.`
3743
: 'Do not use the (mut) helper. Use regular setters or actions instead.';

tests/lib/rules/template-no-mut-helper.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,43 @@ ruleTester.run('template-no-mut-helper', rule, {
144144
output: null,
145145
errors: [{ message: 'Do not use the (mut) helper. Use regular setters or actions instead.' }],
146146
},
147+
// `mut` is a strict-mode ambient keyword — no import needed in .gjs strict
148+
// mode templates.
149+
{
150+
code: '<template>{{mut foo bar}}</template>',
151+
output: null,
152+
errors: [
153+
{
154+
message: 'Do not use the (mut) helper. Use regular setters or actions instead.',
155+
type: 'GlimmerMustacheStatement',
156+
},
157+
],
158+
},
159+
// Config: setterAlternative should produce exactly one `{{...}}` wrapping
160+
// around the user-supplied expression — even when the user accidentally
161+
// included the braces themselves.
162+
{
163+
code: '<template><MyComponent onchange={{action (mut this.val) value="target.value"}}/></template>',
164+
output: null,
165+
options: [{ setterAlternative: '(set this.foo)' }],
166+
errors: [
167+
{
168+
message:
169+
'Do not use the (mut) helper. Consider using a JS action or {{(set this.foo)}} instead.',
170+
},
171+
],
172+
},
173+
{
174+
code: '<template><MyComponent onchange={{action (mut this.val) value="target.value"}}/></template>',
175+
output: null,
176+
options: [{ setterAlternative: '{{(set this.foo)}}' }],
177+
errors: [
178+
{
179+
message:
180+
'Do not use the (mut) helper. Consider using a JS action or {{(set this.foo)}} instead.',
181+
},
182+
],
183+
},
147184
],
148185
});
149186

0 commit comments

Comments
 (0)