1
1
use oxc_ast:: {
2
2
ast:: {
3
- Argument , BinaryExpression , CallExpression , Expression , NullLiteral , VariableDeclarator ,
3
+ Argument , BinaryExpression , CallExpression , Expression , NullLiteral , SwitchStatement ,
4
+ VariableDeclarator ,
4
5
} ,
5
6
AstKind ,
6
7
} ;
@@ -201,6 +202,11 @@ impl Rule for NoNull {
201
202
fixer. delete ( null_literal)
202
203
} ) ;
203
204
}
205
+ ( AstKind :: SwitchCase ( _) , Some ( AstKind :: SwitchStatement ( switch) ) ) => {
206
+ ctx. diagnostic_with_fix ( no_null_diagnostic ( null_literal. span ) , |fixer| {
207
+ try_fix_case ( fixer, null_literal, switch)
208
+ } ) ;
209
+ }
204
210
_ => {
205
211
ctx. diagnostic_with_fix ( no_null_diagnostic ( null_literal. span ) , |fixer| {
206
212
fix_null ( fixer, null_literal)
@@ -214,6 +220,23 @@ fn fix_null<'a>(fixer: RuleFixer<'_, 'a>, null: &NullLiteral) -> RuleFix<'a> {
214
220
fixer. replace ( null. span , "undefined" )
215
221
}
216
222
223
+ fn try_fix_case < ' a > (
224
+ fixer : RuleFixer < ' _ , ' a > ,
225
+ null : & NullLiteral ,
226
+ switch : & SwitchStatement < ' a > ,
227
+ ) -> RuleFix < ' a > {
228
+ let also_has_undefined = switch
229
+ . cases
230
+ . iter ( )
231
+ . filter_map ( |case| case. test . as_ref ( ) )
232
+ . any ( |test| test. get_inner_expression ( ) . is_undefined ( ) ) ;
233
+ if also_has_undefined {
234
+ fixer. noop ( )
235
+ } else {
236
+ fixer. replace ( null. span , "undefined" )
237
+ }
238
+ }
239
+
217
240
#[ test]
218
241
fn test ( ) {
219
242
use crate :: tester:: Tester ;
@@ -313,12 +336,6 @@ fn test() {
313
336
( "if (foo == null) {}" , "if (foo == undefined) {}" , None ) ,
314
337
( "if (foo != null) {}" , "if (foo != undefined) {}" , None ) ,
315
338
( "if (foo == null) {}" , "if (foo == undefined) {}" , Some ( check_strict_equality( true ) ) ) ,
316
- // FIXME
317
- (
318
- "if (foo === null || foo === undefined) {}" ,
319
- "if (foo === undefined || foo === undefined) {}" ,
320
- Some ( check_strict_equality( true ) ) ,
321
- ) ,
322
339
(
323
340
"
324
341
let isNullish;
@@ -335,7 +352,7 @@ fn test() {
335
352
"
336
353
let isNullish;
337
354
switch (foo) {
338
- case undefined :
355
+ case null :
339
356
case undefined:
340
357
isNullish = true;
341
358
break;
@@ -346,6 +363,12 @@ fn test() {
346
363
" ,
347
364
None ,
348
365
) ,
366
+ // FIXME
367
+ (
368
+ "if (foo === null || foo === undefined) {}" ,
369
+ "if (foo === undefined || foo === undefined) {}" ,
370
+ Some ( check_strict_equality( true ) ) ,
371
+ ) ,
349
372
] ;
350
373
Tester :: new ( NoNull :: NAME , pass, fail) . expect_fix ( fix) . test_and_snapshot ( ) ;
351
374
}
0 commit comments