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
5 changes: 5 additions & 0 deletions .changeset/full-dancers-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed [#8475](https://github.com/biomejs/biome/issues/8475): fixed a regression in how `noExtraNonNullAssertion` flags extra non-null assertions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Add a code example and simplify wording.

The changeset guidelines require code examples for rule changes. Consider showing what's now correctly handled (e.g., blob.b!.d!.length). Also, "Fixed ... fixed" reads redundantly.

Apply this diff:

-Fixed [#8475](https://github.com/biomejs/biome/issues/8475): fixed a regression in how `noExtraNonNullAssertion` flags extra non-null assertions
+Fixed [#8475](https://github.com/biomejs/biome/issues/8475). The rule `noExtraNonNullAssertion` now correctly handles chained non-null assertions like `blob.b!.d!.length`.

Based on coding guidelines for changesets.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Fixed [#8475](https://github.com/biomejs/biome/issues/8475): fixed a regression in how `noExtraNonNullAssertion` flags extra non-null assertions
Fixed [#8475](https://github.com/biomejs/biome/issues/8475). The rule `noExtraNonNullAssertion` now correctly handles chained non-null assertions like `blob.b!.d!.length`.
🤖 Prompt for AI Agents
In .changeset/full-dancers-repeat.md around line 5, the changeset message is
redundant ("Fixed ... fixed") and lacks the required code example for the rule
change; update the text to remove the duplicate wording (e.g., start with "Fix:
regression in how `noExtraNonNullAssertion` flags extra non-null assertions")
and add a brief code example showing the corrected behavior (for example
illustrate allowed/flagged cases such as blob.b!.d!.length). Keep the wording
concise and follow changeset guidelines by including the example and a single
clear sentence describing the fix.

Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,16 @@ impl Rule for NoExtraNonNullAssertion {
AnyTsNonNullAssertion::TsNonNullAssertionExpression(_) => {
// First check if this is nested within another non-null assertion (always invalid)

let mut is_nested_in_non_null_assertion = false;
for ancestor in node.syntax().ancestors().skip(1) {
if JsParenthesizedExpression::can_cast(ancestor.kind()) {
continue;
}
if TsNonNullAssertionExpression::can_cast(ancestor.kind()) {
is_nested_in_non_null_assertion = true;
break;
}
}

if is_nested_in_non_null_assertion {
if let Some(parent) = node
.syntax()
.ancestors()
.skip(1)
.filter(|ancestor| !JsParenthesizedExpression::can_cast(ancestor.kind()))
// Consider only the first immediate ancestor that is not a parenthesized expression
.take(1)
.next()
&& TsNonNullAssertionExpression::can_cast(parent.kind())
{
return Some(());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ arr1[0]!! ^= arr1[1];

const arr2: number[] = [1, 2, 3];
arr2[0] ^= arr2[1]!!;

// Test case for issue #8475
const _d2 = (blob.b!)!.d!.length;
const _d2 = ((blob.b!))!.d!.length;
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
assertion_line: 151
expression: invalid.ts
---
# Input
Expand Down Expand Up @@ -61,6 +60,10 @@ arr1[0]!! ^= arr1[1];
const arr2: number[] = [1, 2, 3];
arr2[0] ^= arr2[1]!!;

// Test case for issue #8475
const _d2 = (blob.b!)!.d!.length;
const _d2 = ((blob.b!))!.d!.length;

```

# Diagnostics
Expand Down Expand Up @@ -407,10 +410,47 @@ invalid.ts:55:12 lint/suspicious/noExtraNonNullAssertion FIXABLE ━━━━
> 55 │ arr2[0] ^= arr2[1]!!;
│ ^^^^^^^^
56 │
57 │ // Test case for issue #8475

i Safe fix: Remove extra non-null assertion.

55 │ arr2[0]·^=·arr2[1]!!;
│ -

```

```
invalid.ts:58:14 lint/suspicious/noExtraNonNullAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Forbidden extra non-null assertion.

57 │ // Test case for issue #8475
> 58 │ const _d2 = (blob.b!)!.d!.length;
│ ^^^^^^^
59 │ const _d2 = ((blob.b!))!.d!.length;
60 │

i Safe fix: Remove extra non-null assertion.

58 │ const·_d2·=·(blob.b!)!.d!.length;
│ -

```

```
invalid.ts:59:15 lint/suspicious/noExtraNonNullAssertion FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Forbidden extra non-null assertion.

57 │ // Test case for issue #8475
58 │ const _d2 = (blob.b!)!.d!.length;
> 59 │ const _d2 = ((blob.b!))!.d!.length;
│ ^^^^^^^
60 │

i Safe fix: Remove extra non-null assertion.

59 │ const·_d2·=·((blob.b!))!.d!.length;
│ -

```
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ function issue3419(value: string | null): string {
// Test case for issue #7927: compound assignment with non-null assertions on both sides
const arr: number[] = [1, 2, 3];
arr[0]! ^= arr[1]!;

// Test case for issue #8475
const _d2 = blob.b!.d!.length;
const _d2 = blob.b?.d!.length;
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
assertion_line: 151
expression: valid.ts
---
# Input
Expand Down Expand Up @@ -31,4 +30,8 @@ function issue3419(value: string | null): string {
const arr: number[] = [1, 2, 3];
arr[0]! ^= arr[1]!;

// Test case for issue #8475
const _d2 = blob.b!.d!.length;
const _d2 = blob.b?.d!.length;

```
Loading