1
- module . exports = ( { preferred, negatedPreferred, attributes } ) => ( context ) => {
2
- function getCorrectFunctionFor ( node , negated = false ) {
3
- return ( node . arguments . length === 1 ||
1
+ export default ( { preferred, negatedPreferred, attributes } ) => ( context ) => {
2
+ const getCorrectFunctionFor = ( node , negated = false ) =>
3
+ ( node . arguments . length === 1 ||
4
4
node . arguments [ 1 ] . value === true ||
5
5
node . arguments [ 1 ] . value === "" ) &&
6
- ! negated
6
+ ! negated
7
7
? preferred
8
8
: negatedPreferred ;
9
- }
10
9
11
10
const isBannedArg = ( node ) =>
12
11
attributes . some ( ( attr ) => attr === node . arguments [ 0 ] . value ) ;
@@ -15,100 +14,100 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => (context) => {
15
14
[ `CallExpression[callee.property.name=/${ preferred } |${ negatedPreferred } /][callee.object.property.name='not'][callee.object.object.callee.name='expect']` ] (
16
15
node
17
16
) {
18
- if ( negatedPreferred . startsWith ( "toBe" ) ) {
19
- const incorrectFunction = node . callee . property . name ;
20
-
21
- const correctFunction =
22
- incorrectFunction === preferred ? negatedPreferred : preferred ;
23
- context . report ( {
24
- message : `Use ${ correctFunction } () instead of not.${ incorrectFunction } ()` ,
25
- node,
26
- fix ( fixer ) {
27
- return fixer . replaceTextRange (
28
- [ node . callee . object . property . range [ 0 ] , node . range [ 1 ] ] ,
29
- `${ correctFunction } ()`
30
- ) ;
31
- } ,
32
- } ) ;
17
+ if ( ! negatedPreferred . startsWith ( "toBe" ) ) {
18
+ return ;
33
19
}
34
- } ,
35
20
36
- "CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']" (
21
+ const incorrectFunction = node . callee . property . name ;
22
+
23
+ const correctFunction =
24
+ incorrectFunction === preferred ? negatedPreferred : preferred ;
25
+ context . report ( {
26
+ message : `Use ${ correctFunction } () instead of not.${ incorrectFunction } ()` ,
27
+ node,
28
+ fix : ( fixer ) =>
29
+ fixer . replaceTextRange (
30
+ [ node . callee . object . property . range [ 0 ] , node . range [ 1 ] ] ,
31
+ `${ correctFunction } ()`
32
+ ) ,
33
+ } ) ;
34
+ } ,
35
+ [ `CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']` ] (
37
36
node
38
37
) {
39
38
const {
40
39
arguments : [ { property, property : { name } = { } } ] ,
41
40
} = node . callee . object ;
42
41
const matcher = node . callee . property . name ;
43
42
const matcherArg = node . arguments . length && node . arguments [ 0 ] . value ;
44
- if ( attributes . some ( ( attr ) => attr === name ) ) {
45
- const isNegated =
46
- matcher . endsWith ( "Falsy" ) ||
47
- ( ( matcher === "toBe" || matcher === "toEqual" ) &&
48
- matcherArg !== true ) ;
49
- const correctFunction = getCorrectFunctionFor (
50
- node . callee . object ,
51
- isNegated
52
- ) ;
53
- context . report ( {
54
- node,
55
- message : `Use ${ correctFunction } () instead of checking .${ name } directly` ,
56
- fix ( fixer ) {
57
- return [
58
- fixer . removeRange ( [ property . range [ 0 ] - 1 , property . range [ 1 ] ] ) ,
59
- fixer . replaceTextRange (
60
- [ node . callee . property . range [ 0 ] , node . range [ 1 ] ] ,
61
- `${ correctFunction } ()`
62
- ) ,
63
- ] ;
64
- } ,
65
- } ) ;
43
+ if ( ! attributes . some ( ( attr ) => attr === name ) ) {
44
+ return ;
66
45
}
46
+
47
+ const isNegated =
48
+ matcher . endsWith ( "Falsy" ) ||
49
+ ( ( matcher === "toBe" || matcher === "toEqual" ) && matcherArg !== true ) ;
50
+ const correctFunction = getCorrectFunctionFor (
51
+ node . callee . object ,
52
+ isNegated
53
+ ) ;
54
+ context . report ( {
55
+ node,
56
+ message : `Use ${ correctFunction } () instead of checking .${ name } directly` ,
57
+ fix : ( fixer ) => [
58
+ fixer . removeRange ( [ property . range [ 0 ] - 1 , property . range [ 1 ] ] ) ,
59
+ fixer . replaceTextRange (
60
+ [ node . callee . property . range [ 0 ] , node . range [ 1 ] ] ,
61
+ `${ correctFunction } ()`
62
+ ) ,
63
+ ] ,
64
+ } ) ;
67
65
} ,
68
- " CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']" (
66
+ [ ` CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']` ] (
69
67
node
70
68
) {
71
69
const arg = node . arguments [ 0 ] . value ;
72
- if ( isBannedArg ( node ) ) {
73
- const correctFunction = getCorrectFunctionFor ( node , true ) ;
74
-
75
- const incorrectFunction = node . callee . property . name ;
76
- context . report ( {
77
- message : `Use ${ correctFunction } () instead of not.${ incorrectFunction } ('${ arg } ')` ,
78
- node,
79
- fix ( fixer ) {
80
- return fixer . replaceTextRange (
81
- [ node . callee . object . property . range [ 0 ] , node . range [ 1 ] ] ,
82
- `${ correctFunction } ()`
83
- ) ;
84
- } ,
85
- } ) ;
70
+ if ( ! isBannedArg ( node ) ) {
71
+ return ;
86
72
}
73
+
74
+ const correctFunction = getCorrectFunctionFor ( node , true ) ;
75
+
76
+ const incorrectFunction = node . callee . property . name ;
77
+ context . report ( {
78
+ message : `Use ${ correctFunction } () instead of not.${ incorrectFunction } ('${ arg } ')` ,
79
+ node,
80
+ fix : ( fixer ) =>
81
+ fixer . replaceTextRange (
82
+ [ node . callee . object . property . range [ 0 ] , node . range [ 1 ] ] ,
83
+ `${ correctFunction } ()`
84
+ ) ,
85
+ } ) ;
87
86
} ,
88
- " CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]" (
87
+ [ ` CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]` ] (
89
88
node
90
89
) {
91
- if ( isBannedArg ( node ) ) {
92
- const correctFunction = getCorrectFunctionFor ( node ) ;
90
+ if ( ! isBannedArg ( node ) ) {
91
+ return ;
92
+ }
93
93
94
- const incorrectFunction = node . callee . property . name ;
94
+ const correctFunction = getCorrectFunctionFor ( node ) ;
95
95
96
- const message = `Use ${ correctFunction } () instead of ${ incorrectFunction } (${ node . arguments
97
- . map ( ( { raw } ) => raw )
98
- . join ( ", " ) } )`;
99
- context . report ( {
100
- node : node . callee . property ,
101
- message,
102
- fix ( fixer ) {
103
- return [
104
- fixer . replaceTextRange (
105
- [ node . callee . property . range [ 0 ] , node . range [ 1 ] ] ,
106
- `${ correctFunction } ()`
107
- ) ,
108
- ] ;
109
- } ,
110
- } ) ;
111
- }
96
+ const incorrectFunction = node . callee . property . name ;
97
+
98
+ const message = `Use ${ correctFunction } () instead of ${ incorrectFunction } (${ node . arguments
99
+ . map ( ( { raw } ) => raw )
100
+ . join ( ", " ) } )`;
101
+ context . report ( {
102
+ node : node . callee . property ,
103
+ message,
104
+ fix : ( fixer ) => [
105
+ fixer . replaceTextRange (
106
+ [ node . callee . property . range [ 0 ] , node . range [ 1 ] ] ,
107
+ `${ correctFunction } ()`
108
+ ) ,
109
+ ] ,
110
+ } ) ;
112
111
} ,
113
112
} ;
114
113
} ;
0 commit comments