Skip to content

Commit e058a16

Browse files
authored
no-null: Use simple selector (#2109)
1 parent e7f4697 commit e058a16

File tree

1 file changed

+55
-22
lines changed

1 file changed

+55
-22
lines changed

rules/no-null.js

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22
const {
3-
not,
4-
methodCallSelector,
5-
callExpressionSelector,
6-
} = require('./selectors/index.js');
3+
isMethodCall,
4+
isCallExpression,
5+
isLiteral,
6+
} = require('./ast/index.js');
77

88
const ERROR_MESSAGE_ID = 'error';
99
const SUGGESTION_REPLACE_MESSAGE_ID = 'replace';
@@ -14,21 +14,6 @@ const messages = {
1414
[SUGGESTION_REMOVE_MESSAGE_ID]: 'Remove `null`.',
1515
};
1616

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-
3217
const isLooseEqual = node => node.type === 'BinaryExpression' && ['==', '!='].includes(node.operator);
3318
const isStrictEqual = node => node.type === 'BinaryExpression' && ['===', '!=='].includes(node.operator);
3419

@@ -40,12 +25,60 @@ const create = context => {
4025
};
4126

4227
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+
) {
4677
return;
4778
}
4879

80+
const {parent} = node;
81+
4982
const problem = {
5083
node,
5184
messageId: ERROR_MESSAGE_ID,

0 commit comments

Comments
 (0)