|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Requirements |
| 3 | +//------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +const rule = require('../../../lib/rules/no-unsafe-this-access-in-async-function'); |
| 6 | +const RuleTester = require('eslint').RuleTester; |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | +// Tests |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const ruleTester = new RuleTester({ |
| 13 | + parserOptions: { |
| 14 | + ecmaVersion: 2020, |
| 15 | + sourceType: 'module', |
| 16 | + }, |
| 17 | + parser: require.resolve('@babel/eslint-parser'), |
| 18 | +}); |
| 19 | + |
| 20 | +function eachDefaultExport(builder, rest = {}) { |
| 21 | + let paths = [...rule.FRAMEWORK_EXTENDABLES, ...rule.KNOWN_DESTROYABLES].map(x => x.importPath); |
| 22 | + let results = []; |
| 23 | + |
| 24 | + for (let importPath of paths) { |
| 25 | + let specifier = 'X'; |
| 26 | + let testCase = { |
| 27 | + ...rest, |
| 28 | + code: `import ${specifier} from '${importPath}'\n\n` + builder(specifier), |
| 29 | + }; |
| 30 | + |
| 31 | + results.push(testCase); |
| 32 | + } |
| 33 | + |
| 34 | + return results; |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +ruleTester.run('no-unsafe-this-access-in-async-function', rule, { |
| 39 | + valid: [ |
| 40 | + `class { |
| 41 | + async foo() { |
| 42 | + await Promise.resolve(); |
| 43 | + this.foo; |
| 44 | + } |
| 45 | + }`, |
| 46 | + eachDefaultExport((parentClass) => ` |
| 47 | + class extends ${parentClass} { |
| 48 | + async foo() { |
| 49 | + await Promise.resolve(); |
| 50 | +
|
| 51 | + if (this.isDestroying || this.isDestroyed) return; |
| 52 | +
|
| 53 | + this.hello(); |
| 54 | + } |
| 55 | + } |
| 56 | + `), |
| 57 | + eachDefaultExport((parentClass) => ` |
| 58 | + import { isDestroying, isDestroyed } from '@ember/destroyable'; |
| 59 | +
|
| 60 | + class extends ${parentClass} { |
| 61 | + async foo() { |
| 62 | + await Promise.resolve(); |
| 63 | +
|
| 64 | + if (isDestroying(this) || isDestroyed(this)) return; |
| 65 | +
|
| 66 | + this.hello(); |
| 67 | + } |
| 68 | + } |
| 69 | + `), |
| 70 | + eachDefaultExport((parentClass) => ` |
| 71 | + import { isDestroying as A, isDestroyed as B } from '@ember/destroyable'; |
| 72 | +
|
| 73 | + class extends ${parentClass} { |
| 74 | + async foo() { |
| 75 | + await Promise.resolve(); |
| 76 | +
|
| 77 | + if (B(this) || A(this)) return; |
| 78 | +
|
| 79 | + this.hello(); |
| 80 | + } |
| 81 | + } |
| 82 | + `), |
| 83 | + ], |
| 84 | + invalild: [ |
| 85 | + { |
| 86 | + code: ` |
| 87 | + import Component from '@glimmer/component'; |
| 88 | +
|
| 89 | + class extends Component { |
| 90 | + async foo() { |
| 91 | + await Promise.resolve(); |
| 92 | + this.foo; |
| 93 | + } |
| 94 | + } |
| 95 | + `, |
| 96 | + output: ` |
| 97 | + import Component from '@glimmer/component'; |
| 98 | +
|
| 99 | + class extends Component { |
| 100 | + async foo() { |
| 101 | + await Promise.resolve(); |
| 102 | + if (this.isDestroyed || this.isDestroying) return; |
| 103 | + this.foo; |
| 104 | + } |
| 105 | + } |
| 106 | + `, |
| 107 | + } |
| 108 | + ] |
| 109 | +}) |
0 commit comments