-
-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): implement @typescript-eslint/no-non-null-assertion (#3825)
Related issue: #2180 original implementation - code: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/rules/no-non-null-assertion.ts - test: https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/tests/rules/no-non-null-assertion.test.ts - doc: https://typescript-eslint.io/rules/no-non-null-assertion/ typescript-eslint has a feature that is manually fixable by editor suggestions on this rule, but oxc does not have one as far as I know, so the implementation is simple compared to typescript-eslint
- Loading branch information
Showing
3 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
crates/oxc_linter/src/rules/typescript/no_non_null_assertion.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use oxc_ast::AstKind; | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::Span; | ||
|
||
use crate::{context::LintContext, rule::Rule, AstNode}; | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoNonNullAssertion; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// Disallow non-null assertions using the ! postfix operator. | ||
/// | ||
/// ### Why is this bad? | ||
/// TypeScript's ! non-null assertion operator asserts to the type system that an expression is non-nullable, as in not null or undefined. Using assertions to tell the type system new information is often a sign that code is not fully type-safe. It's generally better to structure program logic so that TypeScript understands when values may be nullable. | ||
/// | ||
/// ### Example | ||
/// ```javascript | ||
/// x!; | ||
/// x!.y; | ||
/// x.y!; | ||
/// ``` | ||
NoNonNullAssertion, | ||
restriction, | ||
); | ||
|
||
fn no_non_null_assertion_diagnostic(span0: Span) -> OxcDiagnostic { | ||
OxcDiagnostic::warn("typescript-eslint(no-non-null-assertion): Forbidden non-null assertion.") | ||
.with_help("Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator.") | ||
.with_labels([span0.into()]) | ||
} | ||
|
||
impl Rule for NoNonNullAssertion { | ||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
let AstKind::TSNonNullExpression(expr) = node.kind() else { return }; | ||
ctx.diagnostic(no_non_null_assertion_diagnostic(expr.span)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec!["x;", "x.y;", "x.y.z;", "x?.y.z;", "x?.y?.z;", "!x;"]; | ||
|
||
let fail = vec![ | ||
"x!;", | ||
"x!.y;", | ||
"x.y!;", | ||
"!x!.y;", | ||
"x!.y?.z;", | ||
"x![y];", | ||
"x![y]?.z;", | ||
"x.y.z!();", | ||
"x.y?.z!();", | ||
"x!!!;", | ||
"x!!.y;", | ||
"x.y!!;", | ||
"x.y.z!!();", | ||
"x!?.[y].z;", | ||
"x!?.y.z;", | ||
"x.y.z!?.();", | ||
" | ||
x! | ||
.y | ||
", | ||
" | ||
x! | ||
// comment | ||
.y | ||
", | ||
" | ||
x! | ||
// comment | ||
. /* comment */ | ||
y | ||
", | ||
" | ||
x! | ||
// comment | ||
/* comment */ ['y'] | ||
", | ||
]; | ||
|
||
Tester::new(NoNonNullAssertion::NAME, pass, fail).test_and_snapshot(); | ||
} |
185 changes: 185 additions & 0 deletions
185
crates/oxc_linter/src/snapshots/no_non_null_assertion.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
--- | ||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x!; | ||
· ── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x!.y; | ||
· ── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x.y!; | ||
· ──── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:2] | ||
1 │ !x!.y; | ||
· ── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x!.y?.z; | ||
· ── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x![y]; | ||
· ── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x![y]?.z; | ||
· ── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x.y.z!(); | ||
· ────── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x.y?.z!(); | ||
· ─────── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x!!!; | ||
· ──── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x!!!; | ||
· ─── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x!!!; | ||
· ── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x!!.y; | ||
· ─── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x!!.y; | ||
· ── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x.y!!; | ||
· ───── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x.y!!; | ||
· ──── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x.y.z!!(); | ||
· ─────── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x.y.z!!(); | ||
· ────── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x!?.[y].z; | ||
· ── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x!?.y.z; | ||
· ── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:1:1] | ||
1 │ x.y.z!?.(); | ||
· ────── | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:2:10] | ||
1 │ | ||
2 │ x! | ||
· ── | ||
3 │ .y | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:2:10] | ||
1 │ | ||
2 │ x! | ||
· ── | ||
3 │ // comment | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:2:10] | ||
1 │ | ||
2 │ x! | ||
· ── | ||
3 │ // comment | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. | ||
|
||
⚠ typescript-eslint(no-non-null-assertion): Forbidden non-null assertion. | ||
╭─[no_non_null_assertion.tsx:2:10] | ||
1 │ | ||
2 │ x! | ||
· ── | ||
3 │ // comment | ||
╰──── | ||
help: Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator. |