Skip to content

Commit f81409d

Browse files
committed
First approach and tests for no-await-sync-query
1 parent b552bbc commit f81409d

File tree

4 files changed

+41
-45
lines changed

4 files changed

+41
-45
lines changed

docs/rules/no-await-sync-query.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Disallow unnecessary `await` for sync queries (no-await-sync-query)
22

3-
Please describe the origin of the rule here.
3+
TODO: Please describe the origin of the rule here.
44

55

66
## Rule Details
@@ -11,26 +11,26 @@ Examples of **incorrect** code for this rule:
1111

1212
```js
1313

14-
// fill me in
14+
// TODO: fill me in
1515

1616
```
1717

1818
Examples of **correct** code for this rule:
1919

2020
```js
2121

22-
// fill me in
22+
// TODO: fill me in
2323

2424
```
2525

2626
### Options
2727

28-
If there are any options, describe them here. Otherwise, delete this section.
28+
TODO: If there are any options, describe them here. Otherwise, delete this section.
2929

3030
## When Not To Use It
3131

32-
Give a short description of when it would be appropriate to turn off this rule.
32+
TODO: Give a short description of when it would be appropriate to turn off this rule.
3333

3434
## Further Reading
3535

36-
If there are other links that describe the issue this rule addresses, please include them here in a bulleted list.
36+
- [Testing Library queries cheatsheet](https://testing-library.com/docs/dom-testing-library/cheatsheet#queries)

lib/rules/await-async-query.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const VALID_PARENTS = [
66
'ReturnStatement',
77
];
88

9+
const ASYNC_QUERIES_REGEXP = /^find(All)?By(LabelText|PlaceholderText|Text|AltText|Title|DisplayValue|Role|TestId)$/;
10+
11+
const getError = nodeName => `\`${nodeName}\` must have \`await\` operator`;
12+
913
module.exports = {
1014
meta: {
1115
type: 'problem',
@@ -22,9 +26,7 @@ module.exports = {
2226

2327
create: function(context) {
2428
return {
25-
'CallExpression Identifier[name=/^find(All)?By(LabelText|PlaceholderText|Text|AltText|Title|DisplayValue|Role|TestId)$/]'(
26-
node
27-
) {
29+
[`CallExpression Identifier[name=${ASYNC_QUERIES_REGEXP}]`](node) {
2830
let hasError = true;
2931
try {
3032
if (VALID_PARENTS.includes(node.parent.parent.type)) {
@@ -37,7 +39,7 @@ module.exports = {
3739
if (hasError) {
3840
context.report({
3941
node,
40-
message: `\`${node.name}\` must have \`await\` operator`,
42+
message: getError(node.name),
4143
});
4244
}
4345
},

lib/rules/no-await-sync-query.js

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
1-
/**
2-
* @fileoverview Disallow unnecessary `await` for sync queries
3-
* @author Mario
4-
*/
51
'use strict';
62

7-
// ------------------------------------------------------------------------------
8-
// Rule Definition
9-
// ------------------------------------------------------------------------------
3+
const SYNC_QUERIES_REGEXP = /^(get|query)(All)?By(LabelText|PlaceholderText|Text|AltText|Title|DisplayValue|Role|TestId)$/;
4+
5+
const getError = nodeName => `\`${nodeName}\` does not need \`await\` operator`;
106

117
module.exports = {
128
meta: {
9+
type: 'problem',
1310
docs: {
1411
description: 'Disallow unnecessary `await` for sync queries',
15-
category: 'Fill me in',
16-
recommended: false,
12+
category: 'Best Practices',
13+
recommended: true,
14+
url: 'TODO',
1715
},
18-
fixable: null, // or "code" or "whitespace"
19-
schema: [
20-
// fill in your schema
21-
],
16+
fixable: null,
17+
schema: [],
2218
},
2319

2420
create: function(context) {
25-
// variables should be defined here
26-
27-
// ----------------------------------------------------------------------
28-
// Helpers
29-
// ----------------------------------------------------------------------
30-
31-
// any helper functions should go here or else delete this section
32-
33-
// ----------------------------------------------------------------------
34-
// Public
35-
// ----------------------------------------------------------------------
36-
3721
return {
38-
// give me methods
22+
[`AwaitExpression > CallExpression > Identifier[name=${SYNC_QUERIES_REGEXP}]`](
23+
node
24+
) {
25+
context.report({
26+
node,
27+
message: getError(node.name),
28+
});
29+
},
3930
};
4031
},
4132
};

tests/lib/rules/no-await-sync-query.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* @fileoverview Disallow unnecessary `await` for sync queries
3-
* @author Mario
4-
*/
51
'use strict';
62

73
// ------------------------------------------------------------------------------
@@ -15,19 +11,26 @@ const RuleTester = require('eslint').RuleTester;
1511
// Tests
1612
// ------------------------------------------------------------------------------
1713

18-
const ruleTester = new RuleTester();
14+
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018 } });
1915
ruleTester.run('no-await-sync-query', rule, {
2016
valid: [
21-
// give me some code that won't trigger a warning
17+
{
18+
code: `async () => {
19+
getByText('foo')
20+
}
21+
`,
22+
},
2223
],
2324

2425
invalid: [
2526
{
26-
code: "await getByText('foo')",
27+
code: `async () => {
28+
await getByText('foo')
29+
}
30+
`,
2731
errors: [
2832
{
29-
message: 'Fill me in.',
30-
type: 'Me too',
33+
message: '`getByText` does not need `await` operator',
3134
},
3235
],
3336
},

0 commit comments

Comments
 (0)