Skip to content

Commit

Permalink
refactor(lint/noUselessTypeConstraint): make code fix safe (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Sep 21, 2023
1 parent 05ff3f9 commit 2e4afd4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom
- The following rules have now safe code fixes:

- [noUselessLabel](https://biomejs.dev/linter/rules/no-useless-label)
- [noUselessTypeConstraint](https://biomejs.dev/linter/rules/no-useless-type-constraint)

#### Bug fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use biome_analyze::{declare_rule, ActionCategory, Ast, Rule, RuleDiagnostic};
use biome_console::markup;

use biome_diagnostics::Applicability;
use biome_js_syntax::TsTypeConstraintClause;
use biome_js_syntax::{AnyTsType, TsTypeConstraintClause};
use biome_rowan::{AstNode, BatchMutationExt};

use crate::JsRuleAction;
Expand Down Expand Up @@ -88,12 +88,7 @@ impl Rule for NoUselessTypeConstraint {
fn run(ctx: &RuleContext<Self>) -> Option<Self::State> {
let node = ctx.query();
let ty = node.ty().ok()?;

if ty.as_ts_any_type().is_some() || ty.as_ts_unknown_type().is_some() {
Some(())
} else {
None
}
matches!(ty, AnyTsType::TsAnyType(_) | AnyTsType::TsUnknownType(_)).then_some(())
}

fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> {
Expand All @@ -115,11 +110,15 @@ impl Rule for NoUselessTypeConstraint {
fn action(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<JsRuleAction> {
let node = ctx.query();
let mut mutation = ctx.root().begin();
let prev = node.syntax().prev_sibling()?;
mutation.replace_element_discard_trivia(
prev.clone().into(),
prev.trim_trailing_trivia()?.into(),
);
mutation.remove_node(node.clone());

Some(JsRuleAction {
category: ActionCategory::QuickFix,
applicability: Applicability::MaybeIncorrect,
applicability: Applicability::Always,
message: markup! { "Remove the constraint." }.to_owned(),
mutation,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ class BazAny<T extends any> {
const QuuxAny = <T extends any>() => {};

function QuuzAny<T extends any>() {}

function commented<T /*a*/ extends /*b*/ any /*c*/>() {}
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: 96
expression: invalid.ts
---
# Input
Expand All @@ -21,6 +20,8 @@ const QuuxAny = <T extends any>() => {};

function QuuzAny<T extends any>() {}

function commented<T /*a*/ extends /*b*/ any /*c*/>() {}

```

# Diagnostics
Expand All @@ -36,10 +37,10 @@ invalid.ts:1:21 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━
i All types are subtypes of any and unknown.
i Suggested fix: Remove the constraint.
i Safe fix: Remove the constraint.
1 │ interface·FooAny1<T·extends·any>·{
-----------
------------
```

Expand All @@ -57,10 +58,10 @@ invalid.ts:5:21 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━
i All types are subtypes of any and unknown.
i Suggested fix: Remove the constraint.
i Safe fix: Remove the constraint.
5 │ interface·FooAny2<T·extends·unknown>·{
---------------
----------------
```

Expand All @@ -78,10 +79,10 @@ invalid.ts:9:16 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━
i All types are subtypes of any and unknown.
i Suggested fix: Remove the constraint.
i Safe fix: Remove the constraint.
9 │ class·BazAny<T·extends·any>·{
-----------
------------
```

Expand All @@ -98,10 +99,10 @@ invalid.ts:10:12 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━
i All types are subtypes of any and unknown.
i Suggested fix: Remove the constraint.
i Safe fix: Remove the constraint.
10 │ ··quxAny<U·extends·any>()·{}
-----------
------------
```

Expand All @@ -119,10 +120,10 @@ invalid.ts:13:20 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━
i All types are subtypes of any and unknown.
i Suggested fix: Remove the constraint.
i Safe fix: Remove the constraint.
13 │ const·QuuxAny·=·<T·extends·any>()·=>·{};
-----------
------------
```

Expand All @@ -136,13 +137,34 @@ invalid.ts:15:20 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━
> 15 │ function QuuzAny<T extends any>() {}
│ ^^^^^^^^^^^
16 │
17 │ function commented<T /*a*/ extends /*b*/ any /*c*/>() {}
i All types are subtypes of any and unknown.
i Suggested fix: Remove the constraint.
i Safe fix: Remove the constraint.
15 │ function·QuuzAny<T·extends·any>()·{}
│ -----------
│ ------------
```

```
invalid.ts:17:28 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Constraining a type parameter to any or unknown is useless.
15 │ function QuuzAny<T extends any>() {}
16 │
> 17 │ function commented<T /*a*/ extends /*b*/ any /*c*/>() {}
│ ^^^^^^^^^^^^^^^^^
18 │
i All types are subtypes of any and unknown.
i Safe fix: Remove the constraint.
17 │ function·commented<T·/*a*/·extends·/*b*/·any·/*c*/>()·{}
│ ------------------------
```

Expand Down
1 change: 1 addition & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom
- The following rules have now safe code fixes:

- [noUselessLabel](https://biomejs.dev/linter/rules/no-useless-label)
- [noUselessTypeConstraint](https://biomejs.dev/linter/rules/no-useless-type-constraint)

#### Bug fixes

Expand Down
Loading

0 comments on commit 2e4afd4

Please sign in to comment.