|
| 1 | +const rule = require('../../../lib/rules/template-no-this-in-template-only-components'); |
| 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-this-in-template-only-components', rule, { |
| 9 | + valid: [ |
| 10 | + '<template>{{@foo}}</template>', |
| 11 | + '<template>{{welcome-page}}</template>', |
| 12 | + '<template><WelcomePage /></template>', |
| 13 | + '<template><MyComponent @prop={{can "edit" @model}} /></template>', |
| 14 | + '<template>{{my-component model=model}}</template>', |
| 15 | + // Class components should not be flagged (not template-only) |
| 16 | + 'class MyComponent extends Component { <template>{{this.foo}}</template> }', |
| 17 | + 'class MyComponent extends Component { <template>{{this.bar}} {{this.baz}}</template> }', |
| 18 | + ], |
| 19 | + invalid: [ |
| 20 | + { |
| 21 | + code: '<template>{{this.foo}}</template>', |
| 22 | + output: '<template>{{@foo}}</template>', |
| 23 | + errors: [{ messageId: 'noThis' }], |
| 24 | + }, |
| 25 | + |
| 26 | + { |
| 27 | + code: '<template>{{my-component model=this.model}}</template>', |
| 28 | + output: '<template>{{my-component model=@model}}</template>', |
| 29 | + errors: [{ messageId: 'noThis' }], |
| 30 | + }, |
| 31 | + { |
| 32 | + code: '<template>{{my-component action=(action this.myAction)}}</template>', |
| 33 | + output: '<template>{{my-component action=(action @myAction)}}</template>', |
| 34 | + errors: [{ messageId: 'noThis' }], |
| 35 | + }, |
| 36 | + { |
| 37 | + code: '<template><MyComponent @prop={{can "edit" this.model}} /></template>', |
| 38 | + output: '<template><MyComponent @prop={{can "edit" @model}} /></template>', |
| 39 | + errors: [{ messageId: 'noThis' }], |
| 40 | + }, |
| 41 | + { |
| 42 | + code: '<template>{{input id=(concat this.elementId "-username")}}</template>', |
| 43 | + output: null, |
| 44 | + errors: [{ messageId: 'noThis' }], |
| 45 | + }, |
| 46 | + ], |
| 47 | +}); |
| 48 | + |
| 49 | +const hbsRuleTester = new RuleTester({ |
| 50 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 51 | + parserOptions: { |
| 52 | + ecmaVersion: 2022, |
| 53 | + sourceType: 'module', |
| 54 | + }, |
| 55 | +}); |
| 56 | + |
| 57 | +hbsRuleTester.run('template-no-this-in-template-only-components', rule, { |
| 58 | + valid: [ |
| 59 | + '{{welcome-page}}', |
| 60 | + '<WelcomePage />', |
| 61 | + '<MyComponent @prop={{can "edit" @model}} />', |
| 62 | + '{{my-component model=model}}', |
| 63 | + ], |
| 64 | + invalid: [ |
| 65 | + { |
| 66 | + code: '{{my-component model=this.model}}', |
| 67 | + output: '{{my-component model=@model}}', |
| 68 | + errors: [ |
| 69 | + { |
| 70 | + message: |
| 71 | + "Usage of 'this' in path 'this.model' is not allowed in a template-only component. Use '@model' if it is a named argument.", |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + { |
| 76 | + code: '{{my-component action=(action this.myAction)}}', |
| 77 | + output: '{{my-component action=(action @myAction)}}', |
| 78 | + errors: [ |
| 79 | + { |
| 80 | + message: |
| 81 | + "Usage of 'this' in path 'this.myAction' is not allowed in a template-only component. Use '@myAction' if it is a named argument.", |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + { |
| 86 | + code: '<MyComponent @prop={{can "edit" this.model}} />', |
| 87 | + output: '<MyComponent @prop={{can "edit" @model}} />', |
| 88 | + errors: [ |
| 89 | + { |
| 90 | + message: |
| 91 | + "Usage of 'this' in path 'this.model' is not allowed in a template-only component. Use '@model' if it is a named argument.", |
| 92 | + }, |
| 93 | + ], |
| 94 | + }, |
| 95 | + { |
| 96 | + code: '{{input id=(concat this.elementId "-username")}}', |
| 97 | + output: null, |
| 98 | + errors: [ |
| 99 | + { |
| 100 | + message: |
| 101 | + "Usage of 'this' in path 'this.elementId' is not allowed in a template-only component. Use '@elementId' if it is a named argument.", |
| 102 | + }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + ], |
| 106 | +}); |
0 commit comments