|
| 1 | +const rule = require('../../../lib/rules/template-modifier-name-case'); |
| 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-modifier-name-case', rule, { |
| 10 | + valid: [ |
| 11 | + '<template><div {{did-insert}}></div></template>', |
| 12 | + '<template><div {{did-update}}></div></template>', |
| 13 | + '<template><div {{on-click}}></div></template>', |
| 14 | + '<template><div {{(modifier "did-insert")}}></div></template>', |
| 15 | + '<template><div {{(modifier "on-click")}}></div></template>', |
| 16 | + |
| 17 | + '<template><div {{did-insert "something"}}></div></template>', |
| 18 | + '<template><div {{did-insert action=something}}></div></template>', |
| 19 | + '<template><button {{on "click" somethingAmazing}}></button></template>', |
| 20 | + '<template><button onclick={{do-a-thing "foo"}}></button></template>', |
| 21 | + '<template><button onclick={{doAThing "foo"}}></button></template>', |
| 22 | + '<template><a href="#" onclick={{amazingActionThing "foo"}} {{did-insert}}></a></template>', |
| 23 | + '<template><div didInsert></div></template>', |
| 24 | + '<template><div {{(modifier "foo-bar")}}></div></template>', |
| 25 | + '<template><div {{(if this.foo (modifier "foo-bar"))}}></div></template>', |
| 26 | + '<template><div {{(modifier this.fooBar)}}></div></template>', |
| 27 | + ], |
| 28 | + invalid: [ |
| 29 | + { |
| 30 | + code: '<template><div {{didInsert}}></div></template>', |
| 31 | + output: '<template><div {{did-insert}}></div></template>', |
| 32 | + errors: [ |
| 33 | + { |
| 34 | + message: |
| 35 | + 'Use dasherized names for modifier invocation. Please replace `didInsert` with `did-insert`.', |
| 36 | + }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + { |
| 40 | + code: '<template><div {{doSomething}}></div></template>', |
| 41 | + output: '<template><div {{do-something}}></div></template>', |
| 42 | + errors: [ |
| 43 | + { |
| 44 | + message: |
| 45 | + 'Use dasherized names for modifier invocation. Please replace `doSomething` with `do-something`.', |
| 46 | + }, |
| 47 | + ], |
| 48 | + }, |
| 49 | + { |
| 50 | + code: '<template><div {{fooBar}}></div></template>', |
| 51 | + output: '<template><div {{foo-bar}}></div></template>', |
| 52 | + errors: [ |
| 53 | + { |
| 54 | + message: |
| 55 | + 'Use dasherized names for modifier invocation. Please replace `fooBar` with `foo-bar`.', |
| 56 | + }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + { |
| 60 | + code: '<template><div {{(modifier "didInsert")}}></div></template>', |
| 61 | + output: '<template><div {{(modifier "did-insert")}}></div></template>', |
| 62 | + errors: [ |
| 63 | + { |
| 64 | + message: |
| 65 | + 'Use dasherized names for modifier invocation. Please replace `didInsert` with `did-insert`.', |
| 66 | + }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + |
| 70 | + { |
| 71 | + code: '<template><div class="monkey" {{didInsert "something" with="somethingElse"}}></div></template>', |
| 72 | + output: |
| 73 | + '<template><div class="monkey" {{did-insert "something" with="somethingElse"}}></div></template>', |
| 74 | + errors: [ |
| 75 | + { |
| 76 | + message: |
| 77 | + 'Use dasherized names for modifier invocation. Please replace `didInsert` with `did-insert`.', |
| 78 | + }, |
| 79 | + ], |
| 80 | + }, |
| 81 | + { |
| 82 | + code: '<template><a href="#" onclick={{amazingActionThing "foo"}} {{doSomething}}></a></template>', |
| 83 | + output: |
| 84 | + '<template><a href="#" onclick={{amazingActionThing "foo"}} {{do-something}}></a></template>', |
| 85 | + errors: [ |
| 86 | + { |
| 87 | + message: |
| 88 | + 'Use dasherized names for modifier invocation. Please replace `doSomething` with `do-something`.', |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + { |
| 93 | + code: '<template><div {{(modifier "fooBar")}}></div></template>', |
| 94 | + output: '<template><div {{(modifier "foo-bar")}}></div></template>', |
| 95 | + errors: [ |
| 96 | + { |
| 97 | + message: |
| 98 | + 'Use dasherized names for modifier invocation. Please replace `fooBar` with `foo-bar`.', |
| 99 | + }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + { |
| 103 | + code: '<template><div {{(if this.foo (modifier "fooBar"))}}></div></template>', |
| 104 | + output: '<template><div {{(if this.foo (modifier "foo-bar"))}}></div></template>', |
| 105 | + errors: [ |
| 106 | + { |
| 107 | + message: |
| 108 | + 'Use dasherized names for modifier invocation. Please replace `fooBar` with `foo-bar`.', |
| 109 | + }, |
| 110 | + ], |
| 111 | + }, |
| 112 | + ], |
| 113 | +}); |
| 114 | + |
| 115 | +const hbsRuleTester = new RuleTester({ |
| 116 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 117 | + parserOptions: { |
| 118 | + ecmaVersion: 2022, |
| 119 | + sourceType: 'module', |
| 120 | + }, |
| 121 | +}); |
| 122 | + |
| 123 | +hbsRuleTester.run('template-modifier-name-case', rule, { |
| 124 | + valid: [ |
| 125 | + '<div {{did-insert}}></div>', |
| 126 | + '<div {{did-insert "something"}}></div>', |
| 127 | + '<div {{did-insert action=something}}></div>', |
| 128 | + '<button {{on "click" somethingAmazing}}></button>', |
| 129 | + '<button onclick={{do-a-thing "foo"}}></button>', |
| 130 | + '<button onclick={{doAThing "foo"}}></button>', |
| 131 | + '<a href="#" onclick={{amazingActionThing "foo"}} {{did-insert}}></a>', |
| 132 | + '<div didInsert></div>', |
| 133 | + '<div {{(modifier "foo-bar")}}></div>', |
| 134 | + '<div {{(if this.foo (modifier "foo-bar"))}}></div>', |
| 135 | + '<div {{(modifier this.fooBar)}}></div>', |
| 136 | + ], |
| 137 | + invalid: [ |
| 138 | + { |
| 139 | + code: '<div {{didInsert}}></div>', |
| 140 | + output: '<div {{did-insert}}></div>', |
| 141 | + errors: [ |
| 142 | + { |
| 143 | + message: |
| 144 | + 'Use dasherized names for modifier invocation. Please replace `didInsert` with `did-insert`.', |
| 145 | + }, |
| 146 | + ], |
| 147 | + }, |
| 148 | + { |
| 149 | + code: '<div class="monkey" {{didInsert "something" with="somethingElse"}}></div>', |
| 150 | + output: '<div class="monkey" {{did-insert "something" with="somethingElse"}}></div>', |
| 151 | + errors: [ |
| 152 | + { |
| 153 | + message: |
| 154 | + 'Use dasherized names for modifier invocation. Please replace `didInsert` with `did-insert`.', |
| 155 | + }, |
| 156 | + ], |
| 157 | + }, |
| 158 | + { |
| 159 | + code: '<a href="#" onclick={{amazingActionThing "foo"}} {{doSomething}}></a>', |
| 160 | + output: '<a href="#" onclick={{amazingActionThing "foo"}} {{do-something}}></a>', |
| 161 | + errors: [ |
| 162 | + { |
| 163 | + message: |
| 164 | + 'Use dasherized names for modifier invocation. Please replace `doSomething` with `do-something`.', |
| 165 | + }, |
| 166 | + ], |
| 167 | + }, |
| 168 | + { |
| 169 | + code: '<div {{(modifier "fooBar")}}></div>', |
| 170 | + output: '<div {{(modifier "foo-bar")}}></div>', |
| 171 | + errors: [ |
| 172 | + { |
| 173 | + message: |
| 174 | + 'Use dasherized names for modifier invocation. Please replace `fooBar` with `foo-bar`.', |
| 175 | + }, |
| 176 | + ], |
| 177 | + }, |
| 178 | + { |
| 179 | + code: '<div {{(if this.foo (modifier "fooBar"))}}></div>', |
| 180 | + output: '<div {{(if this.foo (modifier "foo-bar"))}}></div>', |
| 181 | + errors: [ |
| 182 | + { |
| 183 | + message: |
| 184 | + 'Use dasherized names for modifier invocation. Please replace `fooBar` with `foo-bar`.', |
| 185 | + }, |
| 186 | + ], |
| 187 | + }, |
| 188 | + ], |
| 189 | +}); |
0 commit comments