Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Jul 2, 2022
1 parent d3d7b4d commit 6722a00
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/rules/no-unsafe-this-access-in-async-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const FRAMEWORK_EXTENDABLES = [
},
{
importPath: '@ember/component/helper',
},
}
{
importPath: '@ember/routing/route',
},
Expand All @@ -32,6 +32,16 @@ const FRAMEWORK_EXTENDABLES = [
},
];

/**
* These objects don't have their own destroyable APIs but are
* wired in to the framework via associateDestroyableChild
*/
const KNOWN_DESTROYABLES = [
{
importPath: 'ember-modifier'
}
]

// if already has protection, also early return
// two forms:
// - isDestroying(this) || isDestroyed(this) // on any destroyable object
Expand Down Expand Up @@ -61,6 +71,8 @@ function isProtection(node) {
//------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
KNOWN_DESTROYABLES,
FRAMEWORK_EXTENDABLES,
meta: {
type: 'suggestion',
docs: {
Expand Down Expand Up @@ -158,3 +170,4 @@ module.exports = {
};
},
};

109 changes: 109 additions & 0 deletions tests/lib/rules/no-unsafe-this-access-in-async-function.js
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;
}
}
`,
}
]
})

0 comments on commit 6722a00

Please sign in to comment.