From 7f64f2e144dd343ee0ad9eecfe98562b8fa2bfbc Mon Sep 17 00:00:00 2001 From: therewillbecode Date: Sat, 8 Mar 2025 13:13:29 +0000 Subject: [PATCH 1/3] Improve docs for ts no-extra-non-null-assertion --- .../typescript/no_extra_non_null_assertion.rs | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs b/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs index 6e19940fa18fa..ec91736d6c7da 100644 --- a/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs +++ b/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs @@ -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 From 76a514c3c3d7742ab12db7a827b5757299be0132 Mon Sep 17 00:00:00 2001 From: therewillbecode Date: Sat, 8 Mar 2025 13:18:37 +0000 Subject: [PATCH 2/3] Add missing test cases --- .../typescript/no_extra_non_null_assertion.rs | 2 ++ ...ypescript_no_extra_non_null_assertion.snap | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs b/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs index ec91736d6c7da..21a41abd578fa 100644 --- a/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs +++ b/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs @@ -121,6 +121,8 @@ fn test() { ]; let fail = vec![ + "const foo: { bar: number } | null = null; const bar = foo!!!.bar;", + "function foo(bar: number | undefined) { const bar: number = 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; }", diff --git a/crates/oxc_linter/src/snapshots/typescript_no_extra_non_null_assertion.snap b/crates/oxc_linter/src/snapshots/typescript_no_extra_non_null_assertion.snap index 4d49735a332ff..a0e6561ec1e40 100644 --- a/crates/oxc_linter/src/snapshots/typescript_no_extra_non_null_assertion.snap +++ b/crates/oxc_linter/src/snapshots/typescript_no_extra_non_null_assertion.snap @@ -1,6 +1,26 @@ --- 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; + · ▲ + ╰──── + + × Identifier `bar` has already been declared + ╭─[no_extra_non_null_assertion.tsx:1:14] + 1 │ function foo(bar: number | undefined) { const bar: number = bar!!!; } + · ───────────┬─────────── ─────┬───── + · │ ╰── It can not be redeclared here + · ╰── `bar` has already been declared here + ╰──── + ⚠ 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; From 50a21823aff717af401042961d955b566beb6127 Mon Sep 17 00:00:00 2001 From: therewillbecode Date: Sat, 8 Mar 2025 13:21:15 +0000 Subject: [PATCH 3/3] Remove conflicting test case --- .../src/rules/typescript/no_extra_non_null_assertion.rs | 1 - .../snapshots/typescript_no_extra_non_null_assertion.snap | 8 -------- 2 files changed, 9 deletions(-) diff --git a/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs b/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs index 21a41abd578fa..959475144f299 100644 --- a/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs +++ b/crates/oxc_linter/src/rules/typescript/no_extra_non_null_assertion.rs @@ -122,7 +122,6 @@ fn test() { let fail = vec![ "const foo: { bar: number } | null = null; const bar = foo!!!.bar;", - "function foo(bar: number | undefined) { const bar: number = 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; }", diff --git a/crates/oxc_linter/src/snapshots/typescript_no_extra_non_null_assertion.snap b/crates/oxc_linter/src/snapshots/typescript_no_extra_non_null_assertion.snap index a0e6561ec1e40..71d24b5368d15 100644 --- a/crates/oxc_linter/src/snapshots/typescript_no_extra_non_null_assertion.snap +++ b/crates/oxc_linter/src/snapshots/typescript_no_extra_non_null_assertion.snap @@ -13,14 +13,6 @@ source: crates/oxc_linter/src/tester.rs · ▲ ╰──── - × Identifier `bar` has already been declared - ╭─[no_extra_non_null_assertion.tsx:1:14] - 1 │ function foo(bar: number | undefined) { const bar: number = bar!!!; } - · ───────────┬─────────── ─────┬───── - · │ ╰── It can not be redeclared here - · ╰── `bar` has already been declared here - ╰──── - ⚠ 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;