Skip to content

Commit

Permalink
test(linter/unicorn): add fixer tests for no-null
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Aug 24, 2024
1 parent 35669aa commit d3a8b01
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
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]
1let curr;
2while (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]
1function foo() {
Expand Down

0 comments on commit d3a8b01

Please sign in to comment.