-
-
Notifications
You must be signed in to change notification settings - Fork 385
/
Copy pathno-null.js
154 lines (140 loc) · 3.47 KB
/
no-null.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const {
isMethodCall,
isCallExpression,
isLiteral,
} = require('./ast/index.js');
const ERROR_MESSAGE_ID = 'error';
const SUGGESTION_REPLACE_MESSAGE_ID = 'replace';
const SUGGESTION_REMOVE_MESSAGE_ID = 'remove';
const messages = {
[ERROR_MESSAGE_ID]: 'Use `undefined` instead of `null`.',
[SUGGESTION_REPLACE_MESSAGE_ID]: 'Replace `null` with `undefined`.',
[SUGGESTION_REMOVE_MESSAGE_ID]: 'Remove `null`.',
};
const isLooseEqual = node => node.type === 'BinaryExpression' && ['==', '!='].includes(node.operator);
const isStrictEqual = node => node.type === 'BinaryExpression' && ['===', '!=='].includes(node.operator);
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const {checkStrictEquality} = {
checkStrictEquality: false,
...context.options[0],
};
return {
Literal(node) {
if (
// eslint-disable-next-line unicorn/no-null
!isLiteral(node, null)
|| (!checkStrictEquality && isStrictEqual(node.parent))
// `Object.create(null)`, `Object.create(null, foo)`
|| (
isMethodCall(node.parent, {
object: 'Object',
method: 'create',
minimumArguments: 1,
maximumArguments: 2,
optionalCall: false,
optionalMember: false,
})
&& node.parent.arguments[0] === node
)
// `useRef(null)`
|| (
isCallExpression(node.parent, {
name: 'useRef',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
&& node.parent.arguments[0] === node
)
// `React.useRef(null)`
|| (
isMethodCall(node.parent, {
object: 'React',
method: 'useRef',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
&& node.parent.arguments[0] === node
)
// `foo.insertBefore(bar, null)`
|| (
isMethodCall(node.parent, {
method: 'insertBefore',
argumentsLength: 2,
optionalCall: false,
optionalMember: false,
})
&& node.parent.arguments[1] === node
)
) {
return;
}
const {parent} = node;
const problem = {
node,
messageId: ERROR_MESSAGE_ID,
};
const useUndefinedFix = fixer => fixer.replaceText(node, 'undefined');
if (isLooseEqual(parent)) {
problem.fix = useUndefinedFix;
return problem;
}
const useUndefinedSuggestion = {
messageId: SUGGESTION_REPLACE_MESSAGE_ID,
fix: useUndefinedFix,
};
if (parent.type === 'ReturnStatement' && parent.argument === node) {
problem.suggest = [
{
messageId: SUGGESTION_REMOVE_MESSAGE_ID,
fix: fixer => fixer.remove(node),
},
useUndefinedSuggestion,
];
return problem;
}
if (parent.type === 'VariableDeclarator' && parent.init === node && parent.parent.kind !== 'const') {
problem.suggest = [
{
messageId: SUGGESTION_REMOVE_MESSAGE_ID,
fix: fixer => fixer.removeRange([parent.id.range[1], node.range[1]]),
},
useUndefinedSuggestion,
];
return problem;
}
problem.suggest = [useUndefinedSuggestion];
return problem;
},
};
};
const schema = [
{
type: 'object',
additionalProperties: false,
properties: {
checkStrictEquality: {
type: 'boolean',
default: false,
},
},
},
];
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow the use of the `null` literal.',
recommended: true,
},
fixable: 'code',
hasSuggestions: true,
schema,
messages,
},
};