1
1
'use strict' ;
2
2
const {
3
- not ,
4
- methodCallSelector ,
5
- callExpressionSelector ,
6
- } = require ( './selectors /index.js' ) ;
3
+ isMethodCall ,
4
+ isCallExpression ,
5
+ isLiteral ,
6
+ } = require ( './ast /index.js' ) ;
7
7
8
8
const ERROR_MESSAGE_ID = 'error' ;
9
9
const SUGGESTION_REPLACE_MESSAGE_ID = 'replace' ;
@@ -14,21 +14,6 @@ const messages = {
14
14
[ SUGGESTION_REMOVE_MESSAGE_ID ] : 'Remove `null`.' ,
15
15
} ;
16
16
17
- const selector = [
18
- 'Literal' ,
19
- '[raw="null"]' ,
20
- not ( [
21
- // `Object.create(null)`, `Object.create(null, foo)`
22
- `${ methodCallSelector ( { object : 'Object' , method : 'create' , minimumArguments : 1 , maximumArguments : 2 } ) } > .arguments:first-child` ,
23
- // `useRef(null)`
24
- `${ callExpressionSelector ( { name : 'useRef' , argumentsLength : 1 } ) } > .arguments:first-child` ,
25
- // `React.useRef(null)`
26
- `${ methodCallSelector ( { object : 'React' , method : 'useRef' , argumentsLength : 1 } ) } > .arguments:first-child` ,
27
- // `foo.insertBefore(bar, null)`
28
- `${ methodCallSelector ( { method : 'insertBefore' , argumentsLength : 2 } ) } [arguments.0.type!="SpreadElement"] > .arguments:nth-child(2)` ,
29
- ] ) ,
30
- ] . join ( '' ) ;
31
-
32
17
const isLooseEqual = node => node . type === 'BinaryExpression' && [ '==' , '!=' ] . includes ( node . operator ) ;
33
18
const isStrictEqual = node => node . type === 'BinaryExpression' && [ '===' , '!==' ] . includes ( node . operator ) ;
34
19
@@ -40,12 +25,60 @@ const create = context => {
40
25
} ;
41
26
42
27
return {
43
- [ selector ] ( node ) {
44
- const { parent} = node ;
45
- if ( ! checkStrictEquality && isStrictEqual ( parent ) ) {
28
+ Literal ( node ) {
29
+ if (
30
+ // eslint-disable-next-line unicorn/no-null
31
+ ! isLiteral ( node , null )
32
+ || ( ! checkStrictEquality && isStrictEqual ( node . parent ) )
33
+ // `Object.create(null)`, `Object.create(null, foo)`
34
+ || (
35
+ isMethodCall ( node . parent , {
36
+ object : 'Object' ,
37
+ method : 'create' ,
38
+ minimumArguments : 1 ,
39
+ maximumArguments : 2 ,
40
+ optionalCall : false ,
41
+ optionalMember : false ,
42
+ } )
43
+ && node . parent . arguments [ 0 ] === node
44
+ )
45
+ // `useRef(null)`
46
+ || (
47
+ isCallExpression ( node . parent , {
48
+ name : 'useRef' ,
49
+ argumentsLength : 1 ,
50
+ optionalCall : false ,
51
+ optionalMember : false ,
52
+ } )
53
+ && node . parent . arguments [ 0 ] === node
54
+ )
55
+ // `React.useRef(null)`
56
+ || (
57
+ isMethodCall ( node . parent , {
58
+ object : 'React' ,
59
+ method : 'useRef' ,
60
+ argumentsLength : 1 ,
61
+ optionalCall : false ,
62
+ optionalMember : false ,
63
+ } )
64
+ && node . parent . arguments [ 0 ] === node
65
+ )
66
+ // `foo.insertBefore(bar, null)`
67
+ || (
68
+ isMethodCall ( node . parent , {
69
+ method : 'insertBefore' ,
70
+ argumentsLength : 2 ,
71
+ optionalCall : false ,
72
+ optionalMember : false ,
73
+ } )
74
+ && node . parent . arguments [ 1 ] === node
75
+ )
76
+ ) {
46
77
return ;
47
78
}
48
79
80
+ const { parent} = node ;
81
+
49
82
const problem = {
50
83
node,
51
84
messageId : ERROR_MESSAGE_ID ,
0 commit comments