Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat: Add noUselessTypeConstraint #4484

Merged
merged 23 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
### Formatter
### Linter
- Fixed an issue where the `noAssignInExpressions` rule replaced the operator with an invalid token, which caused other lint rules to crash. [#4464](https://github.com/rome/tools/issues/4464)

#### New rules
- [`noUselessTypeConstraint`](https://docs.rome.tools/lint/rules/noUselessTypeConstraint/)

### Parser
### VSCode
### JavaScript APIs
Expand Down
1 change: 1 addition & 0 deletions crates/rome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ define_categories! {
"lint/complexity/noUselessLabel":"https://docs.rome.tools/lint/rules/noUselessLabel",
"lint/complexity/noUselessRename": "https://docs.rome.tools/lint/rules/noUselessRename",
"lint/complexity/noUselessSwitchCase": "https://docs.rome.tools/lint/rules/noUselessSwitchCase",
"lint/complexity/noUselessTypeConstraint": "https://docs.rome.tools/lint/rules/noUselessTypeConstraint",
"lint/complexity/noWith": "https://docs.rome.tools/lint/rules/noWith",
"lint/complexity/useFlatMap": "https://docs.rome.tools/lint/rules/useFlatMap",
"lint/complexity/useOptionalChain": "https://docs.rome.tools/lint/rules/useOptionalChain",
Expand Down
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/analyzers/complexity.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, ActionCategory, Ast, Rule, RuleDiagnostic};
use rome_console::markup;

use rome_diagnostics::Applicability;
use rome_js_syntax::TsTypeConstraintClause;
use rome_rowan::{AstNode, BatchMutationExt};

use crate::JsRuleAction;

declare_rule! {
/// Disallow using `any` or `unknown` as type constraint.
///
nikeee marked this conversation as resolved.
Show resolved Hide resolved
/// ## Examples
///
/// ### Invalid
///
/// ```ts,expect_diagnostic
///
/// interface FooAny<T extends any> {}
///
/// type BarAny<T extends any> = {};
///
/// class BazAny<T extends any> {
/// quxAny<U extends any>() {}
/// }
///
/// const QuuxAny = <T extends any>() => {};
///
/// function QuuzAny<T extends any>() {}
/// ```
nikeee marked this conversation as resolved.
Show resolved Hide resolved
///
/// ### Valid
///
/// ```ts
/// interface Foo<T> {}
///
/// type Bar<T> = {};
///```
pub(crate) NoUselessTypeConstraint {
version: "12.1.0",
nikeee marked this conversation as resolved.
Show resolved Hide resolved
name: "noUselessTypeConstraint",
recommended: true,
}
}

impl Rule for NoUselessTypeConstraint {
type Query = Ast<TsTypeConstraintClause>;
type State = TsTypeConstraintClause;
type Signals = Option<Self::State>;
type Options = ();

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(node.clone())
} else {
None
}
}

fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> {
state.ty().ok()?;
nikeee marked this conversation as resolved.
Show resolved Hide resolved

Some(
RuleDiagnostic::new(
rule_category!(),
state.syntax().text_trimmed_range(),
markup! {
"Useless type constraint."
nikeee marked this conversation as resolved.
Show resolved Hide resolved
},
)
.note("Constraining the type to `any` or `unknown` doesn't have any effect."),
)
}

fn action(ctx: &RuleContext<Self>, node_replace: &Self::State) -> Option<JsRuleAction> {
let mut mutation = ctx.root().begin();
mutation.remove_node(node_replace.clone());

Some(JsRuleAction {
category: ActionCategory::QuickFix,
applicability: Applicability::MaybeIncorrect,
message: markup! { "Remove useless type constraint." }.to_owned(),
nikeee marked this conversation as resolved.
Show resolved Hide resolved
mutation,
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interface FooAny1<T extends any> {
field: T;
}

interface FooAny2<T extends unknown> {
field: T;
}

class BazAny<T extends any> {
quxAny<U extends any>() {}
}

const QuuxAny = <T extends any>() => {};

function QuuzAny<T extends any>() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: invalid.ts
---
# Input
```js
interface FooAny1<T extends any> {
field: T;
}

interface FooAny2<T extends unknown> {
field: T;
}

class BazAny<T extends any> {
quxAny<U extends any>() {}
}

const QuuxAny = <T extends any>() => {};

function QuuzAny<T extends any>() {}

```

# Diagnostics
```
invalid.ts:1:21 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Useless type constraint.

> 1 │ interface FooAny1<T extends any> {
│ ^^^^^^^^^^^
2 │ field: T;
3 │ }

i Constraining the type to `any` or `unknown` doesn't have any effect.

i Suggested fix: Remove useless type constraint.

1 │ interface·FooAny1<T·extends·any>·{
│ -----------

```

```
invalid.ts:5:21 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Useless type constraint.

3 │ }
4 │
> 5 │ interface FooAny2<T extends unknown> {
│ ^^^^^^^^^^^^^^^
6 │ field: T;
7 │ }

i Constraining the type to `any` or `unknown` doesn't have any effect.

i Suggested fix: Remove useless type constraint.

5 │ interface·FooAny2<T·extends·unknown>·{
│ ---------------

```

```
invalid.ts:9:16 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Useless type constraint.

7 │ }
8 │
> 9 │ class BazAny<T extends any> {
│ ^^^^^^^^^^^
10 │ quxAny<U extends any>() {}
11 │ }

i Constraining the type to `any` or `unknown` doesn't have any effect.

i Suggested fix: Remove useless type constraint.

9 │ class·BazAny<T·extends·any>·{
│ -----------

```

```
invalid.ts:10:12 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Useless type constraint.

9 │ class BazAny<T extends any> {
> 10 │ quxAny<U extends any>() {}
│ ^^^^^^^^^^^
11 │ }
12 │

i Constraining the type to `any` or `unknown` doesn't have any effect.

i Suggested fix: Remove useless type constraint.

10 │ ··quxAny<U·extends·any>()·{}
│ -----------

```

```
invalid.ts:13:20 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Useless type constraint.

11 │ }
12 │
> 13 │ const QuuxAny = <T extends any>() => {};
│ ^^^^^^^^^^^
14 │
15 │ function QuuzAny<T extends any>() {}

i Constraining the type to `any` or `unknown` doesn't have any effect.

i Suggested fix: Remove useless type constraint.

13 │ const·QuuxAny·=·<T·extends·any>()·=>·{};
│ -----------

```

```
invalid.ts:15:20 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Useless type constraint.

13 │ const QuuxAny = <T extends any>() => {};
14 │
> 15 │ function QuuzAny<T extends any>() {}
│ ^^^^^^^^^^^
16 │

i Constraining the type to `any` or `unknown` doesn't have any effect.

i Suggested fix: Remove useless type constraint.

15 │ function·QuuzAny<T·extends·any>()·{}
│ -----------

```


Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface FooAny0<T> {
field: T;
}

interface FooNotAny0<T extends string> {
field: T;
}

type Bar<T> = {};

type Bar2<T extends string> = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: valid.ts
---
# Input
```js
interface FooAny0<T> {
field: T;
}

interface FooNotAny0<T extends string> {
field: T;
}

type Bar<T> = {};

type Bar2<T extends string> = {};

```


8 changes: 2 additions & 6 deletions crates/rome_js_unicode_table/src/tables.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.