|
| 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 | + // Rule is HBS-only: hyphenated identifiers are not valid JS, so camelCase |
| 11 | + // modifier names in .gjs/.gts files are intentional and must not be flagged. |
| 12 | + valid: [ |
| 13 | + '<template><div {{did-insert}}></div></template>', |
| 14 | + '<template><div {{did-update}}></div></template>', |
| 15 | + '<template><div {{on-click}}></div></template>', |
| 16 | + '<template><div {{(modifier "did-insert")}}></div></template>', |
| 17 | + '<template><div {{(modifier "on-click")}}></div></template>', |
| 18 | + '<template><div {{did-insert "something"}}></div></template>', |
| 19 | + '<template><div {{did-insert action=something}}></div></template>', |
| 20 | + '<template><button {{on "click" somethingAmazing}}></button></template>', |
| 21 | + '<template><button onclick={{do-a-thing "foo"}}></button></template>', |
| 22 | + '<template><button onclick={{doAThing "foo"}}></button></template>', |
| 23 | + '<template><a href="#" onclick={{amazingActionThing "foo"}} {{did-insert}}></a></template>', |
| 24 | + '<template><div didInsert></div></template>', |
| 25 | + '<template><div {{(modifier "foo-bar")}}></div></template>', |
| 26 | + '<template><div {{(if this.foo (modifier "foo-bar"))}}></div></template>', |
| 27 | + '<template><div {{(modifier this.fooBar)}}></div></template>', |
| 28 | + // camelCase modifiers in GJS are not flagged — hyphenated names are invalid JS identifiers |
| 29 | + '<template><div {{didInsert}}></div></template>', |
| 30 | + '<template><div {{doSomething}}></div></template>', |
| 31 | + '<template><div {{fooBar}}></div></template>', |
| 32 | + '<template><div {{FooBar}}></div></template>', |
| 33 | + '<template><div {{(modifier "fooBar")}}></div></template>', |
| 34 | + ], |
| 35 | + invalid: [], |
| 36 | +}); |
| 37 | + |
| 38 | +const hbsRuleTester = new RuleTester({ |
| 39 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 40 | + parserOptions: { |
| 41 | + ecmaVersion: 2022, |
| 42 | + sourceType: 'module', |
| 43 | + }, |
| 44 | +}); |
| 45 | + |
| 46 | +hbsRuleTester.run('template-modifier-name-case', rule, { |
| 47 | + valid: [ |
| 48 | + { filename: 'test.hbs', code: '<div {{did-insert}}></div>' }, |
| 49 | + { filename: 'test.hbs', code: '<div {{did-insert "something"}}></div>' }, |
| 50 | + { filename: 'test.hbs', code: '<div {{did-insert action=something}}></div>' }, |
| 51 | + { filename: 'test.hbs', code: '<button {{on "click" somethingAmazing}}></button>' }, |
| 52 | + { filename: 'test.hbs', code: '<button onclick={{do-a-thing "foo"}}></button>' }, |
| 53 | + { filename: 'test.hbs', code: '<button onclick={{doAThing "foo"}}></button>' }, |
| 54 | + { |
| 55 | + filename: 'test.hbs', |
| 56 | + code: '<a href="#" onclick={{amazingActionThing "foo"}} {{did-insert}}></a>', |
| 57 | + }, |
| 58 | + { filename: 'test.hbs', code: '<div didInsert></div>' }, |
| 59 | + { filename: 'test.hbs', code: '<div {{(modifier "foo-bar")}}></div>' }, |
| 60 | + { filename: 'test.hbs', code: '<div {{(if this.foo (modifier "foo-bar"))}}></div>' }, |
| 61 | + { filename: 'test.hbs', code: '<div {{(modifier this.fooBar)}}></div>' }, |
| 62 | + ], |
| 63 | + invalid: [ |
| 64 | + { |
| 65 | + filename: 'test.hbs', |
| 66 | + code: '<div {{didInsert}}></div>', |
| 67 | + output: '<div {{did-insert}}></div>', |
| 68 | + errors: [{ messageId: 'dasherized' }], |
| 69 | + }, |
| 70 | + { |
| 71 | + filename: 'test.hbs', |
| 72 | + code: '<div class="monkey" {{didInsert "something" with="somethingElse"}}></div>', |
| 73 | + output: '<div class="monkey" {{did-insert "something" with="somethingElse"}}></div>', |
| 74 | + errors: [{ messageId: 'dasherized' }], |
| 75 | + }, |
| 76 | + // PascalCase: index-0 guard prevents leading dash |
| 77 | + { |
| 78 | + filename: 'test.hbs', |
| 79 | + code: '<div {{FooBar}}></div>', |
| 80 | + output: '<div {{foo-bar}}></div>', |
| 81 | + errors: [{ messageId: 'dasherized' }], |
| 82 | + }, |
| 83 | + { |
| 84 | + filename: 'test.hbs', |
| 85 | + code: '<a href="#" onclick={{amazingActionThing "foo"}} {{doSomething}}></a>', |
| 86 | + output: '<a href="#" onclick={{amazingActionThing "foo"}} {{do-something}}></a>', |
| 87 | + errors: [{ messageId: 'dasherized' }], |
| 88 | + }, |
| 89 | + { |
| 90 | + filename: 'test.hbs', |
| 91 | + code: '<div {{(modifier "fooBar")}}></div>', |
| 92 | + output: '<div {{(modifier "foo-bar")}}></div>', |
| 93 | + errors: [{ messageId: 'dasherized' }], |
| 94 | + }, |
| 95 | + { |
| 96 | + filename: 'test.hbs', |
| 97 | + code: '<div {{(if this.foo (modifier "fooBar"))}}></div>', |
| 98 | + output: '<div {{(if this.foo (modifier "foo-bar"))}}></div>', |
| 99 | + errors: [{ messageId: 'dasherized' }], |
| 100 | + }, |
| 101 | + ], |
| 102 | +}); |
0 commit comments