Skip to content

Commit 5d2ce7d

Browse files
committed
Port autofix for template-no-redundant-fn
1 parent c97528a commit 5d2ce7d

4 files changed

Lines changed: 23 additions & 10 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ rules in templates can be disabled with eslint directives with mustache or html
258258
| [template-no-passed-in-event-handlers](docs/rules/template-no-passed-in-event-handlers.md) | disallow passing event handlers directly as component arguments | | | |
259259
| [template-no-positional-data-test-selectors](docs/rules/template-no-positional-data-test-selectors.md) | disallow positional data-test-* params in curly invocations | | | |
260260
| [template-no-potential-path-strings](docs/rules/template-no-potential-path-strings.md) | disallow potential path strings in attribute values | | | |
261-
| [template-no-redundant-fn](docs/rules/template-no-redundant-fn.md) | disallow unnecessary usage of (fn) helper | | | |
261+
| [template-no-redundant-fn](docs/rules/template-no-redundant-fn.md) | disallow unnecessary usage of (fn) helper | | 🔧 | |
262262
| [template-no-restricted-invocations](docs/rules/template-no-restricted-invocations.md) | disallow certain components, helpers or modifiers from being used | | | |
263263
| [template-no-splattributes-with-class](docs/rules/template-no-splattributes-with-class.md) | disallow splattributes with class attribute | | | |
264264
| [template-no-this-in-template-only-components](docs/rules/template-no-this-in-template-only-components.md) | disallow this in template-only components (gjs/gts) | | 🔧 | |

docs/rules/template-no-redundant-fn.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ember/template-no-redundant-fn
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

57
The `fn` helper can be used to bind arguments to another function. Using it without any arguments is redundant because then the inner function could just be used directly.

lib/rules/template-no-redundant-fn.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-redundant-fn.md',
99
templateMode: 'both',
1010
},
11-
fixable: null,
11+
fixable: 'code',
1212
schema: [],
1313
messages: {
1414
redundant:
@@ -66,6 +66,13 @@ module.exports = {
6666
data: {
6767
suggestion: paramText,
6868
},
69+
fix(fixer) {
70+
if (node.type === 'GlimmerMustacheStatement') {
71+
return fixer.replaceTextRange(node.range, `{{${paramText}}}`);
72+
}
73+
// GlimmerSubExpression: (fn this.handleClick) → this.handleClick
74+
return fixer.replaceTextRange(node.range, paramText);
75+
},
6976
});
7077
}
7178
}

tests/lib/rules/template-no-redundant-fn.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ ruleTester.run('template-no-redundant-fn', rule, {
5151
code: `<template>
5252
<button {{on "click" (fn this.handleClick)}}>Click</button>
5353
</template>`,
54-
output: null,
54+
output: `<template>
55+
<button {{on "click" this.handleClick}}>Click</button>
56+
</template>`,
5557
errors: [
5658
{
5759
message:
@@ -64,7 +66,9 @@ ruleTester.run('template-no-redundant-fn', rule, {
6466
code: `<template>
6567
<Component @action={{fn this.save}} />
6668
</template>`,
67-
output: null,
69+
output: `<template>
70+
<Component @action={{this.save}} />
71+
</template>`,
6872
errors: [
6973
{
7074
message: 'Unnecessary use of (fn) helper. Pass the function directly instead: this.save',
@@ -75,7 +79,7 @@ ruleTester.run('template-no-redundant-fn', rule, {
7579

7680
{
7781
code: '<template><button {{on "click" (fn this.handleClick)}}>Click Me</button></template>',
78-
output: null,
82+
output: '<template><button {{on "click" this.handleClick}}>Click Me</button></template>',
7983
errors: [
8084
{
8185
message:
@@ -85,7 +89,7 @@ ruleTester.run('template-no-redundant-fn', rule, {
8589
},
8690
{
8791
code: '<template><SomeComponent @onClick={{fn this.handleClick}} /></template>',
88-
output: null,
92+
output: '<template><SomeComponent @onClick={{this.handleClick}} /></template>',
8993
errors: [
9094
{
9195
message:
@@ -95,7 +99,7 @@ ruleTester.run('template-no-redundant-fn', rule, {
9599
},
96100
{
97101
code: '<template>{{foo bar=(fn this.handleClick)}}></template>',
98-
output: null,
102+
output: '<template>{{foo bar=this.handleClick}}></template>',
99103
errors: [
100104
{
101105
message:
@@ -126,7 +130,7 @@ hbsRuleTester.run('template-no-redundant-fn', rule, {
126130
invalid: [
127131
{
128132
code: '<button {{on "click" (fn this.handleClick)}}>Click Me</button>',
129-
output: null,
133+
output: '<button {{on "click" this.handleClick}}>Click Me</button>',
130134
errors: [
131135
{
132136
message:
@@ -136,7 +140,7 @@ hbsRuleTester.run('template-no-redundant-fn', rule, {
136140
},
137141
{
138142
code: '<SomeComponent @onClick={{fn this.handleClick}} />',
139-
output: null,
143+
output: '<SomeComponent @onClick={{this.handleClick}} />',
140144
errors: [
141145
{
142146
message:
@@ -146,7 +150,7 @@ hbsRuleTester.run('template-no-redundant-fn', rule, {
146150
},
147151
{
148152
code: '{{foo bar=(fn this.handleClick)}}>',
149-
output: null,
153+
output: '{{foo bar=this.handleClick}}>',
150154
errors: [
151155
{
152156
message:

0 commit comments

Comments
 (0)