|
| 1 | +const rule = require('../../../lib/rules/template-no-unavailable-this'); |
| 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-no-unavailable-this', rule, { |
| 10 | + valid: [ |
| 11 | + // this inside a class body |
| 12 | + `import Component from '@glimmer/component'; |
| 13 | + class MyComponent extends Component { |
| 14 | + <template>{{this.name}}</template> |
| 15 | + }`, |
| 16 | + // this inside a function |
| 17 | + `function myComponent() { |
| 18 | + return <template>{{this.name}}</template>; |
| 19 | + }`, |
| 20 | + // this inside an arrow function that is inside a class (arrow inherits this from class) |
| 21 | + `import Component from '@glimmer/component'; |
| 22 | + class MyComponent extends Component { |
| 23 | + foo = () => { |
| 24 | + return <template>{{this.name}}</template>; |
| 25 | + } |
| 26 | + }`, |
| 27 | + // No this at all |
| 28 | + '<template>{{@value}}</template>', |
| 29 | + '<template><div>Content</div></template>', |
| 30 | + // yield this inside a class (this is valid, other rules handle yield specifics) |
| 31 | + `import Component from '@glimmer/component'; |
| 32 | + class MyComponent extends Component { |
| 33 | + <template>{{yield this}}</template> |
| 34 | + }`, |
| 35 | + ], |
| 36 | + invalid: [ |
| 37 | + // this.property at module level |
| 38 | + { |
| 39 | + code: '<template>{{this.name}}</template>', |
| 40 | + output: null, |
| 41 | + errors: [{ messageId: 'noUnavailableThis' }], |
| 42 | + }, |
| 43 | + // bare this at module level |
| 44 | + { |
| 45 | + code: '<template>{{this}}</template>', |
| 46 | + output: null, |
| 47 | + errors: [{ messageId: 'noUnavailableThis' }], |
| 48 | + }, |
| 49 | + // yield this at module level (this rule catches the `this`, yield rule catches yield semantics) |
| 50 | + { |
| 51 | + code: '<template>{{yield this}}</template>', |
| 52 | + output: null, |
| 53 | + errors: [{ messageId: 'noUnavailableThis' }], |
| 54 | + }, |
| 55 | + // multiple this references at module level |
| 56 | + { |
| 57 | + code: '<template>{{this.foo}} {{this.bar}}</template>', |
| 58 | + output: null, |
| 59 | + errors: [{ messageId: 'noUnavailableThis' }, { messageId: 'noUnavailableThis' }], |
| 60 | + }, |
| 61 | + // deeply nested this.property at module level |
| 62 | + { |
| 63 | + code: '<template>{{this.foo.bar.baz}}</template>', |
| 64 | + output: null, |
| 65 | + errors: [{ messageId: 'noUnavailableThis' }], |
| 66 | + }, |
| 67 | + // arrow function at module level (arrow functions don't have their own this) |
| 68 | + { |
| 69 | + code: `const myComponent = () => { |
| 70 | + return <template>{{this}}</template>; |
| 71 | + }`, |
| 72 | + output: null, |
| 73 | + errors: [{ messageId: 'noUnavailableThis' }], |
| 74 | + }, |
| 75 | + ], |
| 76 | +}); |
0 commit comments