Skip to content

Commit b629e16

Browse files
committedAug 24, 2024·
fix(linter/unicorn): improve diagnostic message for no-null (#5172)
This stack was made because of shortcomings I noticed while running `oxlint-eccosystem-ci` tests locally
1 parent dc9e1e2 commit b629e16

File tree

2 files changed

+81
-89
lines changed

2 files changed

+81
-89
lines changed
 

‎crates/oxc_linter/src/rules/unicorn/no_null.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,8 @@ use crate::{
1717
AstNode,
1818
};
1919

20-
fn replace_null_diagnostic(span0: Span) -> OxcDiagnostic {
21-
OxcDiagnostic::warn("Disallow the use of the `null` literal")
22-
.with_help("Replace the `null` literal with `undefined`.")
23-
.with_label(span0)
24-
}
25-
26-
fn remove_null_diagnostic(span0: Span) -> OxcDiagnostic {
27-
OxcDiagnostic::warn("Disallow the use of the `null` literal")
28-
.with_help("Remove the `null` literal.")
29-
.with_label(span0)
20+
fn no_null_diagnostic(null: Span) -> OxcDiagnostic {
21+
OxcDiagnostic::warn("Do not use `null` literals").with_label(null)
3022
}
3123

3224
#[derive(Debug, Default, Clone)]
@@ -56,7 +48,7 @@ declare_oxc_lint!(
5648
/// ```
5749
NoNull,
5850
style,
59-
fix
51+
conditional_fix
6052
);
6153

6254
fn match_null_arg(call_expr: &CallExpression, index: usize, span: Span) -> bool {
@@ -87,15 +79,15 @@ fn diagnose_binary_expression(
8779

8880
// `if (foo != null) {}`
8981
if matches!(binary_expr.operator, BinaryOperator::Equality | BinaryOperator::Inequality) {
90-
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
82+
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
9183
fix_null(fixer, null_literal)
9284
});
9385

9486
return;
9587
}
9688

9789
// checkStrictEquality=true && `if (foo !== null) {}`
98-
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
90+
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
9991
fix_null(fixer, null_literal)
10092
});
10193
}
@@ -110,15 +102,15 @@ fn diagnose_variable_declarator(
110102
if matches!(&variable_declarator.init, Some(Expression::NullLiteral(expr)) if expr.span == null_literal.span)
111103
&& matches!(parent_kind, Some(AstKind::VariableDeclaration(var_declaration)) if !var_declaration.kind.is_const() )
112104
{
113-
ctx.diagnostic_with_fix(remove_null_diagnostic(null_literal.span), |fixer| {
105+
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
114106
fixer.delete_range(Span::new(variable_declarator.id.span().end, null_literal.span.end))
115107
});
116108

117109
return;
118110
}
119111

120112
// `const foo = null`
121-
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
113+
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
122114
fix_null(fixer, null_literal)
123115
});
124116
}
@@ -207,15 +199,15 @@ impl Rule for NoNull {
207199

208200
// `function foo() { return null; }`,
209201
if matches!(parent_node.kind(), AstKind::ReturnStatement(_)) {
210-
ctx.diagnostic_with_fix(remove_null_diagnostic(null_literal.span), |fixer| {
202+
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
211203
fixer.delete_range(null_literal.span)
212204
});
213205

214206
return;
215207
}
216208
}
217209

218-
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
210+
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
219211
fix_null(fixer, null_literal)
220212
});
221213
}
+72-72
Original file line numberDiff line numberDiff line change
@@ -1,256 +1,256 @@
11
---
22
source: crates/oxc_linter/src/tester.rs
33
---
4-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
4+
eslint-plugin-unicorn(no-null): Do not use `null` literals
55
╭─[no_null.tsx:1:13]
66
1const foo = null
77
· ────
88
╰────
9-
help: Replace the `null` literal with `undefined`.
9+
help: Replace `null` with `undefined`.
1010

11-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
11+
eslint-plugin-unicorn(no-null): Do not use `null` literals
1212
╭─[no_null.tsx:1:5]
1313
1foo(null)
1414
· ────
1515
╰────
16-
help: Replace the `null` literal with `undefined`.
16+
help: Replace `null` with `undefined`.
1717

18-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
18+
eslint-plugin-unicorn(no-null): Do not use `null` literals
1919
╭─[no_null.tsx:1:12]
2020
1if (foo == null) {}
2121
· ────
2222
╰────
23-
help: Replace the `null` literal with `undefined`.
23+
help: Replace `null` with `undefined`.
2424

25-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
25+
eslint-plugin-unicorn(no-null): Do not use `null` literals
2626
╭─[no_null.tsx:1:12]
2727
1if (foo != null) {}
2828
· ────
2929
╰────
30-
help: Replace the `null` literal with `undefined`.
30+
help: Replace `null` with `undefined`.
3131

32-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
32+
eslint-plugin-unicorn(no-null): Do not use `null` literals
3333
╭─[no_null.tsx:1:5]
3434
1if (null == foo) {}
3535
· ────
3636
╰────
37-
help: Replace the `null` literal with `undefined`.
37+
help: Replace `null` with `undefined`.
3838

39-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
39+
eslint-plugin-unicorn(no-null): Do not use `null` literals
4040
╭─[no_null.tsx:1:5]
4141
1if (null != foo) {}
4242
· ────
4343
╰────
44-
help: Replace the `null` literal with `undefined`.
44+
help: Replace `null` with `undefined`.
4545

46-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
46+
eslint-plugin-unicorn(no-null): Do not use `null` literals
4747
╭─[no_null.tsx:2:20]
4848
1function foo() {
4949
2return null;
5050
· ────
5151
3 │ }
5252
╰────
53-
help: Remove the `null` literal.
53+
help: Delete this code.
5454

55-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
55+
eslint-plugin-unicorn(no-null): Do not use `null` literals
5656
╭─[no_null.tsx:1:11]
5757
1let foo = null;
5858
· ────
5959
╰────
60-
help: Remove the `null` literal.
60+
help: Delete this code.
6161

62-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
62+
eslint-plugin-unicorn(no-null): Do not use `null` literals
6363
╭─[no_null.tsx:1:11]
6464
1var foo = null;
6565
· ────
6666
╰────
67-
help: Remove the `null` literal.
67+
help: Delete this code.
6868

69-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
69+
eslint-plugin-unicorn(no-null): Do not use `null` literals
7070
╭─[no_null.tsx:1:20]
7171
1var foo = 1, bar = null, baz = 2;
7272
· ────
7373
╰────
74-
help: Remove the `null` literal.
74+
help: Delete this code.
7575

76-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
76+
eslint-plugin-unicorn(no-null): Do not use `null` literals
7777
╭─[no_null.tsx:1:13]
7878
1const foo = null;
7979
· ────
8080
╰────
81-
help: Replace the `null` literal with `undefined`.
81+
help: Replace `null` with `undefined`.
8282

83-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
83+
eslint-plugin-unicorn(no-null): Do not use `null` literals
8484
╭─[no_null.tsx:1:13]
8585
1if (foo === null) {}
8686
· ────
8787
╰────
88-
help: Replace the `null` literal with `undefined`.
88+
help: Replace `null` with `undefined`.
8989

90-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
90+
eslint-plugin-unicorn(no-null): Do not use `null` literals
9191
╭─[no_null.tsx:1:5]
9292
1if (null === foo) {}
9393
· ────
9494
╰────
95-
help: Replace the `null` literal with `undefined`.
95+
help: Replace `null` with `undefined`.
9696

97-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
97+
eslint-plugin-unicorn(no-null): Do not use `null` literals
9898
╭─[no_null.tsx:1:13]
9999
1if (foo !== null) {}
100100
· ────
101101
╰────
102-
help: Replace the `null` literal with `undefined`.
102+
help: Replace `null` with `undefined`.
103103

104-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
104+
eslint-plugin-unicorn(no-null): Do not use `null` literals
105105
╭─[no_null.tsx:1:5]
106106
1if (null !== foo) {}
107107
· ────
108108
╰────
109-
help: Replace the `null` literal with `undefined`.
109+
help: Replace `null` with `undefined`.
110110

111-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
111+
eslint-plugin-unicorn(no-null): Do not use `null` literals
112112
╭─[no_null.tsx:1:19]
113113
1new Object.create(null)
114114
· ────
115115
╰────
116-
help: Replace the `null` literal with `undefined`.
116+
help: Replace `null` with `undefined`.
117117

118-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
118+
eslint-plugin-unicorn(no-null): Do not use `null` literals
119119
╭─[no_null.tsx:1:27]
120120
1new foo.insertBefore(bar, null)
121121
· ────
122122
╰────
123-
help: Replace the `null` literal with `undefined`.
123+
help: Replace `null` with `undefined`.
124124

125-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
125+
eslint-plugin-unicorn(no-null): Do not use `null` literals
126126
╭─[no_null.tsx:1:8]
127127
1create(null)
128128
· ────
129129
╰────
130-
help: Replace the `null` literal with `undefined`.
130+
help: Replace `null` with `undefined`.
131131

132-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
132+
eslint-plugin-unicorn(no-null): Do not use `null` literals
133133
╭─[no_null.tsx:1:19]
134134
1insertBefore(bar, null)
135135
· ────
136136
╰────
137-
help: Replace the `null` literal with `undefined`.
137+
help: Replace `null` with `undefined`.
138138

139-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
139+
eslint-plugin-unicorn(no-null): Do not use `null` literals
140140
╭─[no_null.tsx:1:18]
141141
1Object['create'](null)
142142
· ────
143143
╰────
144-
help: Replace the `null` literal with `undefined`.
144+
help: Replace `null` with `undefined`.
145145

146-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
146+
eslint-plugin-unicorn(no-null): Do not use `null` literals
147147
╭─[no_null.tsx:1:26]
148148
1foo['insertBefore'](bar, null)
149149
· ────
150150
╰────
151-
help: Replace the `null` literal with `undefined`.
151+
help: Replace `null` with `undefined`.
152152

153-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
153+
eslint-plugin-unicorn(no-null): Do not use `null` literals
154154
╭─[no_null.tsx:1:16]
155155
1Object[create](null)
156156
· ────
157157
╰────
158-
help: Replace the `null` literal with `undefined`.
158+
help: Replace `null` with `undefined`.
159159

160-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
160+
eslint-plugin-unicorn(no-null): Do not use `null` literals
161161
╭─[no_null.tsx:1:24]
162162
1foo[insertBefore](bar, null)
163163
· ────
164164
╰────
165-
help: Replace the `null` literal with `undefined`.
165+
help: Replace `null` with `undefined`.
166166

167-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
167+
eslint-plugin-unicorn(no-null): Do not use `null` literals
168168
╭─[no_null.tsx:1:8]
169169
1Object[null](null)
170170
· ────
171171
╰────
172-
help: Replace the `null` literal with `undefined`.
172+
help: Replace `null` with `undefined`.
173173

174-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
174+
eslint-plugin-unicorn(no-null): Do not use `null` literals
175175
╭─[no_null.tsx:1:14]
176176
1Object[null](null)
177177
· ────
178178
╰────
179-
help: Replace the `null` literal with `undefined`.
179+
help: Replace `null` with `undefined`.
180180

181-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
181+
eslint-plugin-unicorn(no-null): Do not use `null` literals
182182
╭─[no_null.tsx:1:18]
183183
1Object.notCreate(null)
184184
· ────
185185
╰────
186-
help: Replace the `null` literal with `undefined`.
186+
help: Replace `null` with `undefined`.
187187

188-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
188+
eslint-plugin-unicorn(no-null): Do not use `null` literals
189189
╭─[no_null.tsx:1:26]
190190
1foo.notInsertBefore(foo, null)
191191
· ────
192192
╰────
193-
help: Replace the `null` literal with `undefined`.
193+
help: Replace `null` with `undefined`.
194194

195-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
195+
eslint-plugin-unicorn(no-null): Do not use `null` literals
196196
╭─[no_null.tsx:1:18]
197197
1NotObject.create(null)
198198
· ────
199199
╰────
200-
help: Replace the `null` literal with `undefined`.
200+
help: Replace `null` with `undefined`.
201201

202-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
202+
eslint-plugin-unicorn(no-null): Do not use `null` literals
203203
╭─[no_null.tsx:1:19]
204204
1lib.Object.create(null)
205205
· ────
206206
╰────
207-
help: Replace the `null` literal with `undefined`.
207+
help: Replace `null` with `undefined`.
208208

209-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
209+
eslint-plugin-unicorn(no-null): Do not use `null` literals
210210
╭─[no_null.tsx:1:19]
211211
1Object.create(...[null])
212212
· ────
213213
╰────
214-
help: Replace the `null` literal with `undefined`.
214+
help: Replace `null` with `undefined`.
215215

216-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
216+
eslint-plugin-unicorn(no-null): Do not use `null` literals
217217
╭─[no_null.tsx:1:15]
218218
1Object.create(null, bar, extraArgument)
219219
· ────
220220
╰────
221-
help: Replace the `null` literal with `undefined`.
221+
help: Replace `null` with `undefined`.
222222

223-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
223+
eslint-plugin-unicorn(no-null): Do not use `null` literals
224224
╭─[no_null.tsx:1:18]
225225
1foo.insertBefore(null)
226226
· ────
227227
╰────
228-
help: Replace the `null` literal with `undefined`.
228+
help: Replace `null` with `undefined`.
229229

230-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
230+
eslint-plugin-unicorn(no-null): Do not use `null` literals
231231
╭─[no_null.tsx:1:23]
232232
1foo.insertBefore(foo, null, bar)
233233
· ────
234234
╰────
235-
help: Replace the `null` literal with `undefined`.
235+
help: Replace `null` with `undefined`.
236236

237-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
237+
eslint-plugin-unicorn(no-null): Do not use `null` literals
238238
╭─[no_null.tsx:1:28]
239239
1foo.insertBefore(...[foo], null)
240240
· ────
241241
╰────
242-
help: Replace the `null` literal with `undefined`.
242+
help: Replace `null` with `undefined`.
243243

244-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
244+
eslint-plugin-unicorn(no-null): Do not use `null` literals
245245
╭─[no_null.tsx:1:18]
246246
1foo.insertBefore(null, bar)
247247
· ────
248248
╰────
249-
help: Replace the `null` literal with `undefined`.
249+
help: Replace `null` with `undefined`.
250250

251-
eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
251+
eslint-plugin-unicorn(no-null): Do not use `null` literals
252252
╭─[no_null.tsx:1:20]
253253
1Object.create(bar, null)
254254
· ────
255255
╰────
256-
help: Replace the `null` literal with `undefined`.
256+
help: Replace `null` with `undefined`.

0 commit comments

Comments
 (0)
Please sign in to comment.