|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Requirements |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +const rule = require('../../../lib/rules/template-no-redundant-fn'); |
| 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-redundant-fn', rule, { |
| 18 | + valid: [ |
| 19 | + `<template> |
| 20 | + <button {{on "click" this.handleClick}}>Click</button> |
| 21 | + </template>`, |
| 22 | + `<template> |
| 23 | + <button {{on "click" (fn this.handleClick arg)}}>Click</button> |
| 24 | + </template>`, |
| 25 | + `<template> |
| 26 | + <button {{on "click" (fn this.handleClick arg1 arg2)}}>Click</button> |
| 27 | + </template>`, |
| 28 | + `<template> |
| 29 | + <Component @action={{this.myAction}} /> |
| 30 | + </template>`, |
| 31 | + |
| 32 | + '<template><button {{on "click" this.handleClick}}>Click Me</button></template>', |
| 33 | + '<template><button {{on "click" (fn this.handleClick "foo")}}>Click Me</button></template>', |
| 34 | + '<template><SomeComponent @onClick={{this.handleClick}} /></template>', |
| 35 | + '<template><SomeComponent @onClick={{fn this.handleClick "foo"}} /></template>', |
| 36 | + '<template>{{foo bar=this.handleClick}}></template>', |
| 37 | + '<template>{{foo bar=(fn this.handleClick "foo")}}></template>', |
| 38 | + |
| 39 | + // `fn` is imported from a local module — not Ember's built-in helper. |
| 40 | + // Should NOT be flagged even with a single argument. |
| 41 | + ` |
| 42 | + import { fn } from './my-utils'; |
| 43 | + <template> |
| 44 | + <button {{on "click" (fn this.handleClick)}}>Click</button> |
| 45 | + </template> |
| 46 | + `, |
| 47 | + ], |
| 48 | + |
| 49 | + invalid: [ |
| 50 | + { |
| 51 | + code: `<template> |
| 52 | + <button {{on "click" (fn this.handleClick)}}>Click</button> |
| 53 | + </template>`, |
| 54 | + output: null, |
| 55 | + errors: [ |
| 56 | + { |
| 57 | + message: |
| 58 | + 'Unnecessary use of (fn) helper. Pass the function directly instead: this.handleClick', |
| 59 | + type: 'GlimmerSubExpression', |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + { |
| 64 | + code: `<template> |
| 65 | + <Component @action={{fn this.save}} /> |
| 66 | + </template>`, |
| 67 | + output: null, |
| 68 | + errors: [ |
| 69 | + { |
| 70 | + message: 'Unnecessary use of (fn) helper. Pass the function directly instead: this.save', |
| 71 | + type: 'GlimmerMustacheStatement', |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + |
| 76 | + { |
| 77 | + code: '<template><button {{on "click" (fn this.handleClick)}}>Click Me</button></template>', |
| 78 | + output: null, |
| 79 | + errors: [ |
| 80 | + { |
| 81 | + message: |
| 82 | + 'Unnecessary use of (fn) helper. Pass the function directly instead: this.handleClick', |
| 83 | + }, |
| 84 | + ], |
| 85 | + }, |
| 86 | + { |
| 87 | + code: '<template><SomeComponent @onClick={{fn this.handleClick}} /></template>', |
| 88 | + output: null, |
| 89 | + errors: [ |
| 90 | + { |
| 91 | + message: |
| 92 | + 'Unnecessary use of (fn) helper. Pass the function directly instead: this.handleClick', |
| 93 | + }, |
| 94 | + ], |
| 95 | + }, |
| 96 | + { |
| 97 | + code: '<template>{{foo bar=(fn this.handleClick)}}></template>', |
| 98 | + output: null, |
| 99 | + errors: [ |
| 100 | + { |
| 101 | + message: |
| 102 | + 'Unnecessary use of (fn) helper. Pass the function directly instead: this.handleClick', |
| 103 | + }, |
| 104 | + ], |
| 105 | + }, |
| 106 | + ], |
| 107 | +}); |
| 108 | + |
| 109 | +const hbsRuleTester = new RuleTester({ |
| 110 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 111 | + parserOptions: { |
| 112 | + ecmaVersion: 2022, |
| 113 | + sourceType: 'module', |
| 114 | + }, |
| 115 | +}); |
| 116 | + |
| 117 | +hbsRuleTester.run('template-no-redundant-fn', rule, { |
| 118 | + valid: [ |
| 119 | + '<button {{on "click" this.handleClick}}>Click Me</button>', |
| 120 | + '<button {{on "click" (fn this.handleClick "foo")}}>Click Me</button>', |
| 121 | + '<SomeComponent @onClick={{this.handleClick}} />', |
| 122 | + '<SomeComponent @onClick={{fn this.handleClick "foo"}} />', |
| 123 | + '{{foo bar=this.handleClick}}>', |
| 124 | + '{{foo bar=(fn this.handleClick "foo")}}>', |
| 125 | + ], |
| 126 | + invalid: [ |
| 127 | + { |
| 128 | + code: '<button {{on "click" (fn this.handleClick)}}>Click Me</button>', |
| 129 | + output: null, |
| 130 | + errors: [ |
| 131 | + { |
| 132 | + message: |
| 133 | + 'Unnecessary use of (fn) helper. Pass the function directly instead: this.handleClick', |
| 134 | + }, |
| 135 | + ], |
| 136 | + }, |
| 137 | + { |
| 138 | + code: '<SomeComponent @onClick={{fn this.handleClick}} />', |
| 139 | + output: null, |
| 140 | + errors: [ |
| 141 | + { |
| 142 | + message: |
| 143 | + 'Unnecessary use of (fn) helper. Pass the function directly instead: this.handleClick', |
| 144 | + }, |
| 145 | + ], |
| 146 | + }, |
| 147 | + { |
| 148 | + code: '{{foo bar=(fn this.handleClick)}}>', |
| 149 | + output: null, |
| 150 | + errors: [ |
| 151 | + { |
| 152 | + message: |
| 153 | + 'Unnecessary use of (fn) helper. Pass the function directly instead: this.handleClick', |
| 154 | + }, |
| 155 | + ], |
| 156 | + }, |
| 157 | + ], |
| 158 | +}); |
0 commit comments