-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a54493
commit 4ac8d66
Showing
2 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
tests/lib/rules/no-unsafe-this-access-in-async-function.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-unsafe-this-access-in-async-function'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module', | ||
}, | ||
parser: require.resolve('@babel/eslint-parser'), | ||
}); | ||
|
||
function eachDefaultExport(builder, rest = {}) { | ||
let paths = [...rule.FRAMEWORK_EXTENDABLES, ...rule.KNOWN_DESTROYABLES].map(x => x.importPath); | ||
let results = []; | ||
|
||
for (let importPath of paths) { | ||
let specifier = 'X'; | ||
let testCase = { | ||
...rest, | ||
code: `import ${specifier} from '${importPath}'\n\n` + builder(specifier), | ||
}; | ||
|
||
results.push(testCase); | ||
} | ||
|
||
return results; | ||
} | ||
|
||
|
||
ruleTester.run('no-unsafe-this-access-in-async-function', rule, { | ||
valid: [ | ||
`class { | ||
async foo() { | ||
await Promise.resolve(); | ||
this.foo; | ||
} | ||
}`, | ||
eachDefaultExport((parentClass) => ` | ||
class extends ${parentClass} { | ||
async foo() { | ||
await Promise.resolve(); | ||
if (this.isDestroying || this.isDestroyed) return; | ||
this.hello(); | ||
} | ||
} | ||
`), | ||
eachDefaultExport((parentClass) => ` | ||
import { isDestroying, isDestroyed } from '@ember/destroyable'; | ||
class extends ${parentClass} { | ||
async foo() { | ||
await Promise.resolve(); | ||
if (isDestroying(this) || isDestroyed(this)) return; | ||
this.hello(); | ||
} | ||
} | ||
`), | ||
eachDefaultExport((parentClass) => ` | ||
import { isDestroying as A, isDestroyed as B } from '@ember/destroyable'; | ||
class extends ${parentClass} { | ||
async foo() { | ||
await Promise.resolve(); | ||
if (B(this) || A(this)) return; | ||
this.hello(); | ||
} | ||
} | ||
`), | ||
], | ||
invalild: [ | ||
{ | ||
code: ` | ||
import Component from '@glimmer/component'; | ||
class extends Component { | ||
async foo() { | ||
await Promise.resolve(); | ||
this.foo; | ||
} | ||
} | ||
`, | ||
output: ` | ||
import Component from '@glimmer/component'; | ||
class extends Component { | ||
async foo() { | ||
await Promise.resolve(); | ||
if (this.isDestroyed || this.isDestroying) return; | ||
this.foo; | ||
} | ||
} | ||
`, | ||
} | ||
] | ||
}) |