mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
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
This commit is contained in:
parent
dc9e1e233a
commit
b629e160db
2 changed files with 81 additions and 89 deletions
|
|
@ -17,16 +17,8 @@ use crate::{
|
|||
AstNode,
|
||||
};
|
||||
|
||||
fn replace_null_diagnostic(span0: Span) -> OxcDiagnostic {
|
||||
OxcDiagnostic::warn("Disallow the use of the `null` literal")
|
||||
.with_help("Replace the `null` literal with `undefined`.")
|
||||
.with_label(span0)
|
||||
}
|
||||
|
||||
fn remove_null_diagnostic(span0: Span) -> OxcDiagnostic {
|
||||
OxcDiagnostic::warn("Disallow the use of the `null` literal")
|
||||
.with_help("Remove the `null` literal.")
|
||||
.with_label(span0)
|
||||
fn no_null_diagnostic(null: Span) -> OxcDiagnostic {
|
||||
OxcDiagnostic::warn("Do not use `null` literals").with_label(null)
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
|
|
@ -56,7 +48,7 @@ declare_oxc_lint!(
|
|||
/// ```
|
||||
NoNull,
|
||||
style,
|
||||
fix
|
||||
conditional_fix
|
||||
);
|
||||
|
||||
fn match_null_arg(call_expr: &CallExpression, index: usize, span: Span) -> bool {
|
||||
|
|
@ -87,7 +79,7 @@ fn diagnose_binary_expression(
|
|||
|
||||
// `if (foo != null) {}`
|
||||
if matches!(binary_expr.operator, BinaryOperator::Equality | BinaryOperator::Inequality) {
|
||||
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
|
||||
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
|
||||
fix_null(fixer, null_literal)
|
||||
});
|
||||
|
||||
|
|
@ -95,7 +87,7 @@ fn diagnose_binary_expression(
|
|||
}
|
||||
|
||||
// checkStrictEquality=true && `if (foo !== null) {}`
|
||||
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
|
||||
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
|
||||
fix_null(fixer, null_literal)
|
||||
});
|
||||
}
|
||||
|
|
@ -110,7 +102,7 @@ fn diagnose_variable_declarator(
|
|||
if matches!(&variable_declarator.init, Some(Expression::NullLiteral(expr)) if expr.span == null_literal.span)
|
||||
&& matches!(parent_kind, Some(AstKind::VariableDeclaration(var_declaration)) if !var_declaration.kind.is_const() )
|
||||
{
|
||||
ctx.diagnostic_with_fix(remove_null_diagnostic(null_literal.span), |fixer| {
|
||||
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
|
||||
fixer.delete_range(Span::new(variable_declarator.id.span().end, null_literal.span.end))
|
||||
});
|
||||
|
||||
|
|
@ -118,7 +110,7 @@ fn diagnose_variable_declarator(
|
|||
}
|
||||
|
||||
// `const foo = null`
|
||||
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
|
||||
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
|
||||
fix_null(fixer, null_literal)
|
||||
});
|
||||
}
|
||||
|
|
@ -207,7 +199,7 @@ impl Rule for NoNull {
|
|||
|
||||
// `function foo() { return null; }`,
|
||||
if matches!(parent_node.kind(), AstKind::ReturnStatement(_)) {
|
||||
ctx.diagnostic_with_fix(remove_null_diagnostic(null_literal.span), |fixer| {
|
||||
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
|
||||
fixer.delete_range(null_literal.span)
|
||||
});
|
||||
|
||||
|
|
@ -215,7 +207,7 @@ impl Rule for NoNull {
|
|||
}
|
||||
}
|
||||
|
||||
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
|
||||
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
|
||||
fix_null(fixer, null_literal)
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,256 +1,256 @@
|
|||
---
|
||||
source: crates/oxc_linter/src/tester.rs
|
||||
---
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:13]
|
||||
1 │ const foo = null
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:5]
|
||||
1 │ foo(null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:12]
|
||||
1 │ if (foo == null) {}
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:12]
|
||||
1 │ if (foo != null) {}
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:5]
|
||||
1 │ if (null == foo) {}
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:5]
|
||||
1 │ if (null != foo) {}
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:2:20]
|
||||
1 │ function foo() {
|
||||
2 │ return null;
|
||||
· ────
|
||||
3 │ }
|
||||
╰────
|
||||
help: Remove the `null` literal.
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:11]
|
||||
1 │ let foo = null;
|
||||
· ────
|
||||
╰────
|
||||
help: Remove the `null` literal.
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:11]
|
||||
1 │ var foo = null;
|
||||
· ────
|
||||
╰────
|
||||
help: Remove the `null` literal.
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:20]
|
||||
1 │ var foo = 1, bar = null, baz = 2;
|
||||
· ────
|
||||
╰────
|
||||
help: Remove the `null` literal.
|
||||
help: Delete this code.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:13]
|
||||
1 │ const foo = null;
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:13]
|
||||
1 │ if (foo === null) {}
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:5]
|
||||
1 │ if (null === foo) {}
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:13]
|
||||
1 │ if (foo !== null) {}
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:5]
|
||||
1 │ if (null !== foo) {}
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:19]
|
||||
1 │ new Object.create(null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:27]
|
||||
1 │ new foo.insertBefore(bar, null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:8]
|
||||
1 │ create(null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:19]
|
||||
1 │ insertBefore(bar, null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:18]
|
||||
1 │ Object['create'](null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:26]
|
||||
1 │ foo['insertBefore'](bar, null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:16]
|
||||
1 │ Object[create](null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:24]
|
||||
1 │ foo[insertBefore](bar, null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:8]
|
||||
1 │ Object[null](null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:14]
|
||||
1 │ Object[null](null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:18]
|
||||
1 │ Object.notCreate(null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:26]
|
||||
1 │ foo.notInsertBefore(foo, null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:18]
|
||||
1 │ NotObject.create(null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:19]
|
||||
1 │ lib.Object.create(null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:19]
|
||||
1 │ Object.create(...[null])
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:15]
|
||||
1 │ Object.create(null, bar, extraArgument)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:18]
|
||||
1 │ foo.insertBefore(null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:23]
|
||||
1 │ foo.insertBefore(foo, null, bar)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:28]
|
||||
1 │ foo.insertBefore(...[foo], null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:18]
|
||||
1 │ foo.insertBefore(null, bar)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
||||
⚠ eslint-plugin-unicorn(no-null): Disallow the use of the `null` literal
|
||||
⚠ eslint-plugin-unicorn(no-null): Do not use `null` literals
|
||||
╭─[no_null.tsx:1:20]
|
||||
1 │ Object.create(bar, null)
|
||||
· ────
|
||||
╰────
|
||||
help: Replace the `null` literal with `undefined`.
|
||||
help: Replace `null` with `undefined`.
|
||||
|
|
|
|||
Loading…
Reference in a new issue