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 ||
44 node . arguments [ 1 ] . value === true ||
55 node . arguments [ 1 ] . value === "" ) &&
6- ! negated
6+ ! negated
77 ? preferred
88 : negatedPreferred ;
9- }
109
1110 const isBannedArg = ( node ) =>
1211 attributes . some ( ( attr ) => attr === node . arguments [ 0 ] . value ) ;
@@ -15,100 +14,100 @@ module.exports = ({ preferred, negatedPreferred, attributes }) => (context) => {
1514 [ `CallExpression[callee.property.name=/${ preferred } |${ negatedPreferred } /][callee.object.property.name='not'][callee.object.object.callee.name='expect']` ] (
1615 node
1716 ) {
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 ;
3319 }
34- } ,
3520
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']` ] (
3736 node
3837 ) {
3938 const {
4039 arguments : [ { property, property : { name } = { } } ] ,
4140 } = node . callee . object ;
4241 const matcher = node . callee . property . name ;
4342 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 ;
6645 }
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+ } ) ;
6765 } ,
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']` ] (
6967 node
7068 ) {
7169 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 ;
8672 }
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+ } ) ;
8786 } ,
88- " CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]" (
87+ [ ` CallExpression[callee.object.callee.name='expect'][callee.property.name=/toHaveProperty|toHaveAttribute/]` ] (
8988 node
9089 ) {
91- if ( isBannedArg ( node ) ) {
92- const correctFunction = getCorrectFunctionFor ( node ) ;
90+ if ( ! isBannedArg ( node ) ) {
91+ return ;
92+ }
9393
94- const incorrectFunction = node . callee . property . name ;
94+ const correctFunction = getCorrectFunctionFor ( node ) ;
9595
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+ } ) ;
112111 } ,
113112 } ;
114113} ;
0 commit comments