|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Requirements |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +const rule = require('../../../lib/rules/template-no-unnecessary-curly-parens'); |
| 6 | +const RuleTester = require('eslint').RuleTester; |
| 7 | + |
| 8 | +const ruleTester = new RuleTester({ |
| 9 | + parser: require.resolve('ember-eslint-parser'), |
| 10 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 11 | +}); |
| 12 | + |
| 13 | +ruleTester.run('template-no-unnecessary-curly-parens', rule, { |
| 14 | + valid: [ |
| 15 | + '<template>{{helper param}}</template>', |
| 16 | + '<template>{{#if condition}}text{{/if}}</template>', |
| 17 | + '<template>{{this.property}}</template>', |
| 18 | + |
| 19 | + '<template>{{foo}}</template>', |
| 20 | + '<template>{{this.foo}}</template>', |
| 21 | + '<template>{{(helper)}}</template>', |
| 22 | + '<template>{{(this.helper)}}</template>', |
| 23 | + '<template>{{concat "a" "b"}}</template>', |
| 24 | + '<template>{{concat (capitalize "foo") "-bar"}}</template>', |
| 25 | + ], |
| 26 | + invalid: [ |
| 27 | + { |
| 28 | + code: '<template>{{(helper value)}}</template>', |
| 29 | + output: '<template>{{helper value}}</template>', |
| 30 | + errors: [{ messageId: 'noUnnecessaryCurlyParens' }], |
| 31 | + }, |
| 32 | + { |
| 33 | + code: '<template>{{(concat "a" "b")}}</template>', |
| 34 | + output: '<template>{{concat "a" "b"}}</template>', |
| 35 | + errors: [{ messageId: 'noUnnecessaryCurlyParens' }], |
| 36 | + }, |
| 37 | + { |
| 38 | + code: '<template>{{(if condition "yes" "no")}}</template>', |
| 39 | + output: '<template>{{if condition "yes" "no"}}</template>', |
| 40 | + errors: [{ messageId: 'noUnnecessaryCurlyParens' }], |
| 41 | + }, |
| 42 | + |
| 43 | + { |
| 44 | + code: '<template><FooBar @x="{{index}}X{{(someHelper foo)}}" /></template>', |
| 45 | + output: '<template><FooBar @x="{{index}}X{{someHelper foo}}" /></template>', |
| 46 | + errors: [{ messageId: 'noUnnecessaryCurlyParens' }], |
| 47 | + }, |
| 48 | + { |
| 49 | + code: '<template><FooBar @x="{{index}}XY{{(someHelper foo)}}" /></template>', |
| 50 | + output: '<template><FooBar @x="{{index}}XY{{someHelper foo}}" /></template>', |
| 51 | + errors: [{ messageId: 'noUnnecessaryCurlyParens' }], |
| 52 | + }, |
| 53 | + { |
| 54 | + code: '<template><FooBar @x="{{index}}--{{(someHelper foo)}}" /></template>', |
| 55 | + output: '<template><FooBar @x="{{index}}--{{someHelper foo}}" /></template>', |
| 56 | + errors: [{ messageId: 'noUnnecessaryCurlyParens' }], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: '<template>{{(helper a="b")}}</template>', |
| 60 | + output: '<template>{{helper a="b"}}</template>', |
| 61 | + errors: [{ messageId: 'noUnnecessaryCurlyParens' }], |
| 62 | + }, |
| 63 | + ], |
| 64 | +}); |
| 65 | + |
| 66 | +const hbsRuleTester = new RuleTester({ |
| 67 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 68 | + parserOptions: { |
| 69 | + ecmaVersion: 2022, |
| 70 | + sourceType: 'module', |
| 71 | + }, |
| 72 | +}); |
| 73 | + |
| 74 | +hbsRuleTester.run('template-no-unnecessary-curly-parens', rule, { |
| 75 | + valid: [ |
| 76 | + '{{foo}}', |
| 77 | + '{{this.foo}}', |
| 78 | + '{{(helper)}}', |
| 79 | + '{{(this.helper)}}', |
| 80 | + '{{concat "a" "b"}}', |
| 81 | + '{{concat (capitalize "foo") "-bar"}}', |
| 82 | + ], |
| 83 | + invalid: [ |
| 84 | + { |
| 85 | + code: '<FooBar @x="{{index}}X{{(someHelper foo)}}" />', |
| 86 | + output: '<FooBar @x="{{index}}X{{someHelper foo}}" />', |
| 87 | + errors: [{ message: 'Unnecessary parentheses enclosing statement.' }], |
| 88 | + }, |
| 89 | + { |
| 90 | + code: '<FooBar @x="{{index}}XY{{(someHelper foo)}}" />', |
| 91 | + output: '<FooBar @x="{{index}}XY{{someHelper foo}}" />', |
| 92 | + errors: [{ message: 'Unnecessary parentheses enclosing statement.' }], |
| 93 | + }, |
| 94 | + { |
| 95 | + code: '<FooBar @x="{{index}}--{{(someHelper foo)}}" />', |
| 96 | + output: '<FooBar @x="{{index}}--{{someHelper foo}}" />', |
| 97 | + errors: [{ message: 'Unnecessary parentheses enclosing statement.' }], |
| 98 | + }, |
| 99 | + { |
| 100 | + code: '{{(concat "a" "b")}}', |
| 101 | + output: '{{concat "a" "b"}}', |
| 102 | + errors: [{ message: 'Unnecessary parentheses enclosing statement.' }], |
| 103 | + }, |
| 104 | + { |
| 105 | + code: '{{(helper a="b")}}', |
| 106 | + output: '{{helper a="b"}}', |
| 107 | + errors: [{ message: 'Unnecessary parentheses enclosing statement.' }], |
| 108 | + }, |
| 109 | + ], |
| 110 | +}); |
0 commit comments