File tree Expand file tree Collapse file tree 4 files changed +41
-45
lines changed Expand file tree Collapse file tree 4 files changed +41
-45
lines changed Original file line number Diff line number Diff line change 1
1
# Disallow unnecessary ` await ` for sync queries (no-await-sync-query)
2
2
3
- Please describe the origin of the rule here.
3
+ TODO: Please describe the origin of the rule here.
4
4
5
5
6
6
## Rule Details
@@ -11,26 +11,26 @@ Examples of **incorrect** code for this rule:
11
11
12
12
``` js
13
13
14
- // fill me in
14
+ // TODO: fill me in
15
15
16
16
```
17
17
18
18
Examples of ** correct** code for this rule:
19
19
20
20
``` js
21
21
22
- // fill me in
22
+ // TODO: fill me in
23
23
24
24
```
25
25
26
26
### Options
27
27
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.
29
29
30
30
## When Not To Use It
31
31
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.
33
33
34
34
## Further Reading
35
35
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 )
Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ const VALID_PARENTS = [
6
6
'ReturnStatement' ,
7
7
] ;
8
8
9
+ const ASYNC_QUERIES_REGEXP = / ^ f i n d ( A l l ) ? B y ( L a b e l T e x t | P l a c e h o l d e r T e x t | T e x t | A l t T e x t | T i t l e | D i s p l a y V a l u e | R o l e | T e s t I d ) $ / ;
10
+
11
+ const getError = nodeName => `\`${ nodeName } \` must have \`await\` operator` ;
12
+
9
13
module . exports = {
10
14
meta : {
11
15
type : 'problem' ,
@@ -22,9 +26,7 @@ module.exports = {
22
26
23
27
create : function ( context ) {
24
28
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 ) {
28
30
let hasError = true ;
29
31
try {
30
32
if ( VALID_PARENTS . includes ( node . parent . parent . type ) ) {
@@ -37,7 +39,7 @@ module.exports = {
37
39
if ( hasError ) {
38
40
context . report ( {
39
41
node,
40
- message : `\` ${ node . name } \` must have \`await\` operator` ,
42
+ message : getError ( node . name ) ,
41
43
} ) ;
42
44
}
43
45
} ,
Original file line number Diff line number Diff line change 1
- /**
2
- * @fileoverview Disallow unnecessary `await` for sync queries
3
- * @author Mario
4
- */
5
1
'use strict' ;
6
2
7
- // ------------------------------------------------------------------------------
8
- // Rule Definition
9
- // ------------------------------------------------------------------------------
3
+ const SYNC_QUERIES_REGEXP = / ^ ( g e t | q u e r y ) ( A l l ) ? B y ( L a b e l T e x t | P l a c e h o l d e r T e x t | T e x t | A l t T e x t | T i t l e | D i s p l a y V a l u e | R o l e | T e s t I d ) $ / ;
4
+
5
+ const getError = nodeName => `\` ${ nodeName } \` does not need \`await\` operator` ;
10
6
11
7
module . exports = {
12
8
meta : {
9
+ type : 'problem' ,
13
10
docs : {
14
11
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' ,
17
15
} ,
18
- fixable : null , // or "code" or "whitespace"
19
- schema : [
20
- // fill in your schema
21
- ] ,
16
+ fixable : null ,
17
+ schema : [ ] ,
22
18
} ,
23
19
24
20
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
-
37
21
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
+ } ,
39
30
} ;
40
31
} ,
41
32
} ;
Original file line number Diff line number Diff line change 1
- /**
2
- * @fileoverview Disallow unnecessary `await` for sync queries
3
- * @author Mario
4
- */
5
1
'use strict' ;
6
2
7
3
// ------------------------------------------------------------------------------
@@ -15,19 +11,26 @@ const RuleTester = require('eslint').RuleTester;
15
11
// Tests
16
12
// ------------------------------------------------------------------------------
17
13
18
- const ruleTester = new RuleTester ( ) ;
14
+ const ruleTester = new RuleTester ( { parserOptions : { ecmaVersion : 2018 } } ) ;
19
15
ruleTester . run ( 'no-await-sync-query' , rule , {
20
16
valid : [
21
- // give me some code that won't trigger a warning
17
+ {
18
+ code : `async () => {
19
+ getByText('foo')
20
+ }
21
+ ` ,
22
+ } ,
22
23
] ,
23
24
24
25
invalid : [
25
26
{
26
- code : "await getByText('foo')" ,
27
+ code : `async () => {
28
+ await getByText('foo')
29
+ }
30
+ ` ,
27
31
errors : [
28
32
{
29
- message : 'Fill me in.' ,
30
- type : 'Me too' ,
33
+ message : '`getByText` does not need `await` operator' ,
31
34
} ,
32
35
] ,
33
36
} ,
You can’t perform that action at this time.
0 commit comments