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
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,48 @@ declare_oxc_lint!(
/// Disallow extra non-null assertions.
///
/// ### Why is this bad?
/// The `!` non-null assertion operator in TypeScript is used to assert that a value's type does not include null or undefined. Using the operator any more than once on a single value does nothing.
///
/// ### Example
/// The `!` non-null assertion operator in TypeScript is used to assert that a value's type
/// does not include null or undefined. Using the operator any more than once on a single value
/// does nothing.
///
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
/// ```ts
/// const foo: { bar: number } | null = null;
/// const bar = foo!!!.bar;
/// ```
///
/// ```ts
/// function foo(bar: number | undefined) {
/// const bar: number = bar!!!;
/// }
/// ```
///
/// ```ts
/// function foo(bar?: { n: number }) {
/// return bar!?.n;
/// }
/// ```
///
/// Examples of **correct** code for this rule:
/// ```ts
/// const foo: { bar: number } | null = null;
/// const bar = foo!.bar;
/// ```
///
/// ```ts
/// function foo(bar: number | undefined) {
/// const bar: number = bar!;
/// }
/// ```
///
/// ```ts
/// function foo(bar?: { n: number }) {
/// return bar?.n;
/// }
/// ```
NoExtraNonNullAssertion,
typescript,
correctness
Expand Down Expand Up @@ -86,6 +121,7 @@ fn test() {
];

let fail = vec![
"const foo: { bar: number } | null = null; const bar = foo!!!.bar;",
"const foo: { bar: number } | null = null; const bar = foo!!.bar; ",
"function foo(bar: number | undefined) { const a: number = bar!!; }",
"function foo(bar?: { n: number }) { return bar!?.n; }",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
---
source: crates/oxc_linter/src/tester.rs
---
⚠ typescript-eslint(no-extra-non-null-assertion): extra non-null assertion
╭─[no_extra_non_null_assertion.tsx:1:59]
1 │ const foo: { bar: number } | null = null; const bar = foo!!!.bar;
· ▲
╰────

⚠ typescript-eslint(no-extra-non-null-assertion): extra non-null assertion
╭─[no_extra_non_null_assertion.tsx:1:58]
1 │ const foo: { bar: number } | null = null; const bar = foo!!!.bar;
· ▲
╰────

⚠ typescript-eslint(no-extra-non-null-assertion): extra non-null assertion
╭─[no_extra_non_null_assertion.tsx:1:58]
1 │ const foo: { bar: number } | null = null; const bar = foo!!.bar;
Expand Down