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..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 @@ -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 @@ -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; }", 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..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 @@ -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;