@@ -25,11 +25,19 @@ fn no_null_diagnostic(null: Span) -> OxcDiagnostic {
2525 OxcDiagnostic :: warn ( "Do not use `null` literals" ) . with_label ( null)
2626}
2727
28- #[ derive( Debug , Default , Clone , JsonSchema , Deserialize ) ]
28+ #[ derive( Debug , Clone , JsonSchema , Deserialize ) ]
2929#[ serde( rename_all = "camelCase" , default , deny_unknown_fields) ]
3030pub struct NoNull {
3131 /// When set to `true`, the rule will also check strict equality/inequality comparisons (`===` and `!==`) against `null`.
3232 check_strict_equality : bool ,
33+ /// When set to `true`, disallow the use of `null` as a direct function call or constructor argument.
34+ check_arguments : bool ,
35+ }
36+
37+ impl Default for NoNull {
38+ fn default ( ) -> Self {
39+ Self { check_strict_equality : false , check_arguments : true }
40+ }
3341}
3442
3543declare_oxc_lint ! (
@@ -75,6 +83,13 @@ fn match_null_arg(call_expr: &CallExpression, index: usize, span: Span) -> bool
7583 }
7684}
7785
86+ fn match_direct_null_arg ( arguments : & [ Argument ] , span : Span ) -> bool {
87+ arguments
88+ . iter ( )
89+ . filter_map ( Argument :: as_expression)
90+ . any ( |expr| matches ! ( expr. get_inner_expression( ) , Expression :: NullLiteral ( null_lit) if span. contains_inclusive( null_lit. span) ) )
91+ }
92+
7893impl NoNull {
7994 fn diagnose_binary_expression (
8095 & self ,
@@ -198,6 +213,18 @@ impl Rule for NoNull {
198213 {
199214 // no violation
200215 }
216+ ( AstKind :: CallExpression ( call_expr) , _)
217+ if !self . check_arguments
218+ && match_direct_null_arg ( & call_expr. arguments , null_literal. span ) =>
219+ {
220+ // no violation
221+ }
222+ ( AstKind :: NewExpression ( new_expr) , _)
223+ if !self . check_arguments
224+ && match_direct_null_arg ( & new_expr. arguments , null_literal. span ) =>
225+ {
226+ // no violation
227+ }
201228 ( AstKind :: BinaryExpression ( binary_expr) , _) => {
202229 self . diagnose_binary_expression ( ctx, null_literal, binary_expr) ;
203230 }
@@ -264,6 +291,17 @@ fn test() {
264291 "checkStrictEquality" : option,
265292 } ] )
266293 }
294+ fn check_arguments ( option : bool ) -> serde_json:: Value {
295+ serde_json:: json!( [ {
296+ "checkArguments" : option,
297+ } ] )
298+ }
299+ fn check_arguments_and_strict_equality ( ) -> serde_json:: Value {
300+ serde_json:: json!( [ {
301+ "checkArguments" : false ,
302+ "checkStrictEquality" : true ,
303+ } ] )
304+ }
267305
268306 let pass = vec ! [
269307 ( "let foo" , None ) ,
@@ -290,6 +328,14 @@ fn test() {
290328 ( "if (foo !== null) {}" , Some ( check_strict_equality( false ) ) ) ,
291329 ( "if (null !== foo) {}" , Some ( check_strict_equality( false ) ) ) ,
292330 ( "if (foo === null || foo === undefined) {}" , None ) ,
331+ ( "foo(null)" , Some ( check_arguments( false ) ) ) ,
332+ ( "foo(bar, null)" , Some ( check_arguments( false ) ) ) ,
333+ ( "drawingManager.setMap(null)" , Some ( check_arguments( false ) ) ) ,
334+ ( "markers[index].setMap(null)" , Some ( check_arguments( false ) ) ) ,
335+ ( "object?.method?.(null)" , Some ( check_arguments( false ) ) ) ,
336+ ( "foo?.(null)" , Some ( check_arguments( false ) ) ) ,
337+ ( "new HttpResponse(null)" , Some ( check_arguments( false ) ) ) ,
338+ ( "new HttpResponse(body, null)" , Some ( check_arguments( false ) ) ) ,
293339 ] ;
294340
295341 let fail = vec ! [
@@ -312,6 +358,18 @@ fn test() {
312358 ( "var foo = null;" , None ) ,
313359 ( "var foo = 1, bar = null, baz = 2;" , None ) ,
314360 ( "const foo = null;" , None ) ,
361+ ( "const foo = null;" , Some ( check_arguments( false ) ) ) ,
362+ (
363+ "function foo() {
364+ return null;
365+ }" ,
366+ Some ( check_arguments( false ) ) ,
367+ ) ,
368+ ( "if (foo === null) {}" , Some ( check_arguments_and_strict_equality( ) ) ) ,
369+ ( "foo([null])" , Some ( check_arguments( false ) ) ) ,
370+ ( "foo(bar ?? null)" , Some ( check_arguments( false ) ) ) ,
371+ ( "foo(...[null])" , Some ( check_arguments( false ) ) ) ,
372+ ( "new HttpResponse([null])" , Some ( check_arguments( false ) ) ) ,
315373 // `checkStrictEquality`
316374 ( "if (foo === null) {}" , Some ( check_strict_equality( true ) ) ) ,
317375 ( "if (null === foo) {}" , Some ( check_strict_equality( true ) ) ) ,
0 commit comments