Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion crates/oxc_linter/src/rules/unicorn/no_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ fn test() {
("if (null === foo) {}", Some(check_strict_equality(false))),
("if (foo !== null) {}", Some(check_strict_equality(false))),
("if (null !== foo) {}", Some(check_strict_equality(false))),
("if (foo === null || foo === undefined) {}", None),
];

let fail = vec![
Expand All @@ -255,6 +256,7 @@ fn test() {
("if (foo != null) {}", None),
("if (null == foo) {}", None),
("if (null != foo) {}", None),
("let curr;\nwhile (curr != null) { curr = stack.pop() }", None),
// Suggestion `ReturnStatement`
(
"function foo() {
Expand Down Expand Up @@ -303,5 +305,45 @@ fn test() {
("Object.create(bar, null)", None),
];

Tester::new(NoNull::NAME, pass, fail).test_and_snapshot();
let fix = vec![
("let x = null;", "let x;", None),
("let x = null as any;", "let x = undefined as any;", None),
("if (foo == null) {}", "if (foo == undefined) {}", None),
("if (foo != null) {}", "if (foo != undefined) {}", None),
("if (foo == null) {}", "if (foo == undefined) {}", Some(check_strict_equality(true))),
// FIXME
(
"if (foo === null || foo === undefined) {}",
"if (foo === undefined || foo === undefined) {}",
Some(check_strict_equality(true)),
),
(
"
let isNullish;
switch (foo) {
case null:
case undefined:
isNullish = true;
break;
default:
isNullish = false;
break;
}
",
"
let isNullish;
switch (foo) {
case undefined:
case undefined:
isNullish = true;
break;
default:
isNullish = false;
break;
}
",
None,
),
];
Tester::new(NoNull::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}
8 changes: 8 additions & 0 deletions crates/oxc_linter/src/snapshots/no_null.snap
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ source: crates/oxc_linter/src/tester.rs
╰────
help: Replace `null` with `undefined`.

⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
╭─[no_null.tsx:2:16]
1 │ let curr;
2 │ while (curr != null) { curr = stack.pop() }
· ────
╰────
help: Replace `null` with `undefined`.

⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
╭─[no_null.tsx:2:20]
1 │ function foo() {
Expand Down