Skip to content

Commit 6722a00

Browse files
committed
Add some tests
1 parent d3d7b4d commit 6722a00

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

lib/rules/no-unsafe-this-access-in-async-function.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const FRAMEWORK_EXTENDABLES = [
2323
},
2424
{
2525
importPath: '@ember/component/helper',
26-
},
26+
}
2727
{
2828
importPath: '@ember/routing/route',
2929
},
@@ -32,6 +32,16 @@ const FRAMEWORK_EXTENDABLES = [
3232
},
3333
];
3434

35+
/**
36+
* These objects don't have their own destroyable APIs but are
37+
* wired in to the framework via associateDestroyableChild
38+
*/
39+
const KNOWN_DESTROYABLES = [
40+
{
41+
importPath: 'ember-modifier'
42+
}
43+
]
44+
3545
// if already has protection, also early return
3646
// two forms:
3747
// - isDestroying(this) || isDestroyed(this) // on any destroyable object
@@ -61,6 +71,8 @@ function isProtection(node) {
6171
//------------------------------------------------------------------------------
6272
/** @type {import('eslint').Rule.RuleModule} */
6373
module.exports = {
74+
KNOWN_DESTROYABLES,
75+
FRAMEWORK_EXTENDABLES,
6476
meta: {
6577
type: 'suggestion',
6678
docs: {
@@ -158,3 +170,4 @@ module.exports = {
158170
};
159171
},
160172
};
173+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)