1
- import {
2
- ESLintUtils ,
3
- TSESTree ,
4
- ASTUtils ,
5
- } from '@typescript-eslint/experimental-utils' ;
6
- import {
7
- getDocsUrl ,
8
- ALL_QUERIES_METHODS ,
9
- PRESENCE_MATCHERS ,
10
- ABSENCE_MATCHERS ,
11
- } from '../utils' ;
1
+ import { TSESTree , ASTUtils } from '@typescript-eslint/experimental-utils' ;
2
+ import { PRESENCE_MATCHERS , ABSENCE_MATCHERS } from '../utils' ;
12
3
import { findClosestCallNode , isMemberExpression } from '../node-utils' ;
13
4
5
+ import { createTestingLibraryRule } from '../create-testing-library-rule' ;
6
+
14
7
export const RULE_NAME = 'prefer-explicit-assert' ;
15
8
export type MessageIds =
16
9
| 'preferExplicitAssert'
17
10
| 'preferExplicitAssertAssertion' ;
18
11
type Options = [
19
12
{
20
13
assertion ?: string ;
21
- customQueryNames ?: string [ ] ;
22
14
}
23
15
] ;
24
16
25
- const ALL_GET_BY_QUERIES = ALL_QUERIES_METHODS . map (
26
- ( queryMethod ) => `get${ queryMethod } `
27
- ) ;
28
-
29
- const isValidQuery = ( node : TSESTree . Identifier , customQueryNames : string [ ] ) =>
30
- ALL_GET_BY_QUERIES . includes ( node . name ) ||
31
- customQueryNames . includes ( node . name ) ;
32
-
33
17
const isAtTopLevel = ( node : TSESTree . Node ) =>
34
18
node . parent . parent . type === 'ExpressionStatement' ;
35
19
36
- export default ESLintUtils . RuleCreator ( getDocsUrl ) < Options , MessageIds > ( {
20
+ export default createTestingLibraryRule < Options , MessageIds > ( {
37
21
name : RULE_NAME ,
38
22
meta : {
39
23
type : 'suggestion' ,
@@ -59,26 +43,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
59
43
type : 'string' ,
60
44
enum : PRESENCE_MATCHERS ,
61
45
} ,
62
- customQueryNames : {
63
- type : 'array' ,
64
- } ,
65
46
} ,
66
47
} ,
67
48
] ,
68
49
} ,
69
- defaultOptions : [
70
- {
71
- customQueryNames : [ ] ,
72
- } ,
73
- ] ,
74
-
75
- create : function ( context , [ options ] ) {
76
- const { customQueryNames, assertion } = options ;
50
+ defaultOptions : [ { } ] ,
51
+ create ( context , [ options ] , helpers ) {
52
+ const { assertion } = options ;
77
53
const getQueryCalls : TSESTree . Identifier [ ] = [ ] ;
78
54
79
55
return {
80
56
'CallExpression Identifier' ( node : TSESTree . Identifier ) {
81
- if ( isValidQuery ( node , customQueryNames ) ) {
57
+ if ( helpers . isGetByQuery ( node ) ) {
82
58
getQueryCalls . push ( node ) ;
83
59
}
84
60
} ,
@@ -93,7 +69,9 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
93
69
node : queryCall ,
94
70
messageId : 'preferExplicitAssert' ,
95
71
} ) ;
96
- } else if ( assertion ) {
72
+ }
73
+
74
+ if ( assertion ) {
97
75
const expectCallNode = findClosestCallNode ( node , 'expect' ) ;
98
76
if ( ! expectCallNode ) return ;
99
77
0 commit comments