|
| 1 | +const rule = require('../../../lib/rules/template-no-args-paths'); |
| 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 | +ruleTester.run('template-no-args-paths', rule, { |
| 9 | + valid: [ |
| 10 | + '<template>{{@foo}}</template>', |
| 11 | + // @args.foo is a valid named argument, not a path violation |
| 12 | + '<template>{{@args.foo}}</template>', |
| 13 | + '<template><div @foo={{cleanup this.args}}></div></template>', |
| 14 | + '<template>{{foo (name this.args)}}</template>', |
| 15 | + '<template>{{foo name=this.args}}</template>', |
| 16 | + '<template>{{foo name=(extract this.args)}}</template>', |
| 17 | + '<template><Foo @params={{this.args}} /></template>', |
| 18 | + '<template><Foo {{mod this.args}} /></template>', |
| 19 | + '<template><Foo {{mod items=this.args}} /></template>', |
| 20 | + '<template><Foo {{mod items=(extract this.args)}} /></template>', |
| 21 | + // args as a block param is not flagged |
| 22 | + '<template>{{#each items as |args|}}{{args.name}}{{/each}}</template>', |
| 23 | + ], |
| 24 | + invalid: [ |
| 25 | + { |
| 26 | + code: '<template>{{hello (format value=args.foo)}}</template>', |
| 27 | + output: '<template>{{hello (format value=@foo)}}</template>', |
| 28 | + errors: [{ messageId: 'argsPath' }], |
| 29 | + }, |
| 30 | + { |
| 31 | + code: '<template>{{hello value=args.foo}}</template>', |
| 32 | + output: '<template>{{hello value=@foo}}</template>', |
| 33 | + errors: [{ messageId: 'argsPath' }], |
| 34 | + }, |
| 35 | + { |
| 36 | + code: '<template>{{hello (format args.foo.bar)}}</template>', |
| 37 | + output: '<template>{{hello (format @foo.bar)}}</template>', |
| 38 | + errors: [{ messageId: 'argsPath' }], |
| 39 | + }, |
| 40 | + { |
| 41 | + code: '<template><br {{hello args.foo.bar}}></template>', |
| 42 | + output: '<template><br {{hello @foo.bar}}></template>', |
| 43 | + errors: [{ messageId: 'argsPath' }], |
| 44 | + }, |
| 45 | + { |
| 46 | + code: '<template>{{hello args.foo.bar}}</template>', |
| 47 | + output: '<template>{{hello @foo.bar}}</template>', |
| 48 | + errors: [{ messageId: 'argsPath' }], |
| 49 | + }, |
| 50 | + { |
| 51 | + code: '<template>{{args.foo.bar}}</template>', |
| 52 | + output: '<template>{{@foo.bar}}</template>', |
| 53 | + errors: [{ messageId: 'argsPath' }], |
| 54 | + }, |
| 55 | + { |
| 56 | + code: '<template>{{args.foo}}</template>', |
| 57 | + output: '<template>{{@foo}}</template>', |
| 58 | + errors: [{ messageId: 'argsPath' }], |
| 59 | + }, |
| 60 | + { |
| 61 | + code: '<template>{{this.args.foo}}</template>', |
| 62 | + output: '<template>{{@foo}}</template>', |
| 63 | + errors: [{ messageId: 'argsPath' }], |
| 64 | + }, |
| 65 | + ], |
| 66 | +}); |
| 67 | + |
| 68 | +const hbsRuleTester = new RuleTester({ |
| 69 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 70 | + parserOptions: { |
| 71 | + ecmaVersion: 2022, |
| 72 | + sourceType: 'module', |
| 73 | + }, |
| 74 | +}); |
| 75 | + |
| 76 | +hbsRuleTester.run('template-no-args-paths', rule, { |
| 77 | + valid: [ |
| 78 | + // @args.foo is a valid named argument |
| 79 | + '{{@args.foo}}', |
| 80 | + '<div @foo={{cleanup this.args}}></div>', |
| 81 | + '{{foo (name this.args)}}', |
| 82 | + '{{foo name=this.args}}', |
| 83 | + '{{foo name=(extract this.args)}}', |
| 84 | + '<Foo @params={{this.args}} />', |
| 85 | + '<Foo {{mod this.args}} />', |
| 86 | + '<Foo {{mod items=this.args}} />', |
| 87 | + '<Foo {{mod items=(extract this.args)}} />', |
| 88 | + // args as a block param is not flagged |
| 89 | + '{{#each items as |args|}}{{args.name}}{{/each}}', |
| 90 | + ], |
| 91 | + invalid: [ |
| 92 | + { |
| 93 | + code: '{{hello (format value=args.foo)}}', |
| 94 | + output: '{{hello (format value=@foo)}}', |
| 95 | + errors: [{ messageId: 'argsPath' }], |
| 96 | + }, |
| 97 | + { |
| 98 | + code: '{{hello value=args.foo}}', |
| 99 | + output: '{{hello value=@foo}}', |
| 100 | + errors: [{ messageId: 'argsPath' }], |
| 101 | + }, |
| 102 | + { |
| 103 | + code: '{{hello (format args.foo.bar)}}', |
| 104 | + output: '{{hello (format @foo.bar)}}', |
| 105 | + errors: [{ messageId: 'argsPath' }], |
| 106 | + }, |
| 107 | + { |
| 108 | + code: '<br {{hello args.foo.bar}}>', |
| 109 | + output: '<br {{hello @foo.bar}}>', |
| 110 | + errors: [{ messageId: 'argsPath' }], |
| 111 | + }, |
| 112 | + { |
| 113 | + code: '{{hello args.foo.bar}}', |
| 114 | + output: '{{hello @foo.bar}}', |
| 115 | + errors: [{ messageId: 'argsPath' }], |
| 116 | + }, |
| 117 | + { |
| 118 | + code: '{{args.foo.bar}}', |
| 119 | + output: '{{@foo.bar}}', |
| 120 | + errors: [{ messageId: 'argsPath' }], |
| 121 | + }, |
| 122 | + { |
| 123 | + code: '{{args.foo}}', |
| 124 | + output: '{{@foo}}', |
| 125 | + errors: [{ messageId: 'argsPath' }], |
| 126 | + }, |
| 127 | + { |
| 128 | + code: '{{this.args.foo}}', |
| 129 | + output: '{{@foo}}', |
| 130 | + errors: [{ messageId: 'argsPath' }], |
| 131 | + }, |
| 132 | + ], |
| 133 | +}); |
0 commit comments