Skip to content

Commit 1c47f6b

Browse files
committed
First approach for await-async-queries implementation
1 parent 6b5f5f9 commit 1c47f6b

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

docs/rules/await-async-queries.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Enforce async queries (`findBy*`, `findAllBy*`) to have proper `await` (await-async-queries)
2+
3+
TODO: Please describe the origin of the rule here.
4+
5+
6+
## Rule Details
7+
8+
This rule aims to...
9+
10+
Examples of **incorrect** code for this rule:
11+
12+
```js
13+
14+
// fill me in
15+
16+
```
17+
18+
Examples of **correct** code for this rule:
19+
20+
```js
21+
22+
// fill me in
23+
24+
```
25+
26+
### Options
27+
28+
If there are any options, describe them here. Otherwise, delete this section.
29+
30+
## When Not To Use It
31+
32+
Give a short description of when it would be appropriate to turn off this rule.
33+
34+
## Further Reading
35+
36+
If there are other links that describe the issue this rule addresses, please include them here in a bulleted list.

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
testMatch: ['**/tests/**/*.js'],
3+
};

lib/rules/await-async-queries.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const VALID_PARENTS = [
4+
'AwaitExpression',
5+
'ArrowFunctionExpression',
6+
'ReturnStatement',
7+
];
8+
9+
module.exports = {
10+
meta: {
11+
type: 'problem',
12+
docs: {
13+
description:
14+
'Enforce async queries (`findBy*`, `findAllBy*`) to have proper `await`',
15+
category: 'Best Practices',
16+
recommended: true,
17+
url: 'TODO',
18+
},
19+
fixable: null,
20+
schema: [],
21+
},
22+
23+
create: function(context) {
24+
return {
25+
'CallExpression Identifier[name=/^find(All)?By.*/]'(node) {
26+
let hasError = true;
27+
try {
28+
if (VALID_PARENTS.includes(node.parent.parent.type)) {
29+
hasError = false;
30+
}
31+
} catch (e) {
32+
// not necessary to do anything
33+
}
34+
35+
if (hasError) {
36+
context.report({
37+
node,
38+
message: `\`${node.name}\` must have \`await\` operator`,
39+
});
40+
}
41+
},
42+
};
43+
},
44+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict';
2+
3+
// ------------------------------------------------------------------------------
4+
// Requirements
5+
// ------------------------------------------------------------------------------
6+
7+
const rule = require('../../../lib/rules/await-async-queries');
8+
const RuleTester = require('eslint').RuleTester;
9+
10+
// ------------------------------------------------------------------------------
11+
// Tests
12+
// ------------------------------------------------------------------------------
13+
14+
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
15+
ruleTester.run('await-async-queries', rule, {
16+
valid: [
17+
{
18+
code: `async () => {
19+
const foo = await findByText('foo')
20+
}
21+
`,
22+
},
23+
{
24+
code: `async () => {
25+
doSomething()
26+
const foo = await findByText('foo')
27+
}
28+
`,
29+
},
30+
{
31+
code: `async () => {
32+
doSomething()
33+
const foo = await findAllByText('foo')
34+
}
35+
`,
36+
},
37+
{
38+
code: `anArrowFunction = () => findByText('foo')`,
39+
},
40+
{
41+
code: `function foo() {return findByText('foo')}`,
42+
},
43+
{
44+
code: `function foo() {
45+
const promise = findByText('foo')
46+
return promise
47+
}`,
48+
},
49+
],
50+
51+
invalid: [
52+
{
53+
code: `async () => {
54+
const foo = findByText('foo')
55+
}
56+
`,
57+
errors: [
58+
{
59+
message: '`findByText` must have `await` operator',
60+
},
61+
],
62+
},
63+
{
64+
code: `async () => {
65+
doSomething()
66+
const foo = findByText('foo')
67+
}
68+
`,
69+
errors: [
70+
{
71+
message: '`findByText` must have `await` operator',
72+
},
73+
],
74+
},
75+
{
76+
code: `async () => {
77+
doSomething()
78+
const foo = findAllByText('foo')
79+
}
80+
`,
81+
errors: [
82+
{
83+
message: '`findAllByText` must have `await` operator',
84+
},
85+
],
86+
},
87+
],
88+
});

0 commit comments

Comments
 (0)