This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_js_analyze):
noUselessTypeConstraint
(#4484)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> Co-authored-by: Victorien ELVINGER <victorien@elvinger.fr>
- Loading branch information
1 parent
61abdc3
commit 7f72d15
Showing
16 changed files
with
678 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
127 changes: 127 additions & 0 deletions
127
crates/rome_js_analyze/src/analyzers/complexity/no_useless_type_constraint.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
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. | ||
/// | ||
/// Generic type parameters (`<T>`) in TypeScript may be **constrained** with [`extends`](https://www.typescriptlang.org/docs/handbook/generics.html#generic-constraints). | ||
/// A supplied type must then be a subtype of the supplied constraint. | ||
/// All types are subtypes of `any` and `unknown`. | ||
/// It is thus useless to extend from `any` or `unknown`. | ||
/// | ||
/// Source: https://typescript-eslint.io/rules/no-unnecessary-type-constraint/ | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// interface FooAny<T extends any> {} | ||
/// ``` | ||
/// ```ts,expect_diagnostic | ||
/// type BarAny<T extends any> = {}; | ||
/// ``` | ||
/// ```ts,expect_diagnostic | ||
/// class BazAny<T extends any> { | ||
/// } | ||
/// ``` | ||
/// ```ts,expect_diagnostic | ||
/// class BazAny { | ||
/// quxAny<U extends any>() {} | ||
/// } | ||
/// ``` | ||
/// ```ts,expect_diagnostic | ||
/// const QuuxAny = <T extends any>() => {}; | ||
/// ``` | ||
/// ```ts,expect_diagnostic | ||
/// function QuuzAny<T extends any>() {} | ||
/// ``` | ||
/// | ||
/// ```ts,expect_diagnostic | ||
/// interface FooUnknown<T extends unknown> {} | ||
/// ``` | ||
/// ```ts,expect_diagnostic | ||
/// type BarUnknown<T extends unknown> = {}; | ||
/// ``` | ||
/// ```ts,expect_diagnostic | ||
/// class BazUnknown<T extends unknown> { | ||
/// } | ||
/// ```ts,expect_diagnostic | ||
/// class BazUnknown { | ||
/// quxUnknown<U extends unknown>() {} | ||
/// } | ||
/// ``` | ||
/// ```ts,expect_diagnostic | ||
/// const QuuxUnknown = <T extends unknown>() => {}; | ||
/// ``` | ||
/// ```ts,expect_diagnostic | ||
/// function QuuzUnknown<T extends unknown>() {} | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```ts | ||
/// interface Foo<T> {} | ||
/// | ||
/// type Bar<T> = {}; | ||
///``` | ||
pub(crate) NoUselessTypeConstraint { | ||
version: "next", | ||
name: "noUselessTypeConstraint", | ||
recommended: true, | ||
} | ||
} | ||
|
||
impl Rule for NoUselessTypeConstraint { | ||
type Query = Ast<TsTypeConstraintClause>; | ||
type State = (); | ||
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(()) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.syntax().text_trimmed_range(), | ||
markup! { | ||
"Constraining a type parameter to "<Emphasis>"any"</Emphasis>" or "<Emphasis>"unknown"</Emphasis>" is useless." | ||
}, | ||
) | ||
.note(markup! { | ||
"All types are subtypes of "<Emphasis>"any"</Emphasis>" and "<Emphasis>"unknown"</Emphasis>"." | ||
}), | ||
) | ||
} | ||
|
||
fn action(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<JsRuleAction> { | ||
let node = ctx.query(); | ||
let mut mutation = ctx.root().begin(); | ||
mutation.remove_node(node.clone()); | ||
|
||
Some(JsRuleAction { | ||
category: ActionCategory::QuickFix, | ||
applicability: Applicability::MaybeIncorrect, | ||
message: markup! { "Remove the constraint." }.to_owned(), | ||
mutation, | ||
}) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
crates/rome_js_analyze/tests/specs/complexity/noUselessTypeConstraint/invalid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() {} |
149 changes: 149 additions & 0 deletions
149
crates/rome_js_analyze/tests/specs/complexity/noUselessTypeConstraint/invalid.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
assertion_line: 96 | ||
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 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Constraining a type parameter to any or unknown is useless. | ||
> 1 │ interface FooAny1<T extends any> { | ||
│ ^^^^^^^^^^^ | ||
2 │ field: T; | ||
3 │ } | ||
i All types are subtypes of any and unknown. | ||
i Suggested fix: Remove the constraint. | ||
1 │ interface·FooAny1<T·extends·any>·{ | ||
│ ----------- | ||
``` | ||
|
||
``` | ||
invalid.ts:5:21 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Constraining a type parameter to any or unknown is useless. | ||
3 │ } | ||
4 │ | ||
> 5 │ interface FooAny2<T extends unknown> { | ||
│ ^^^^^^^^^^^^^^^ | ||
6 │ field: T; | ||
7 │ } | ||
i All types are subtypes of any and unknown. | ||
i Suggested fix: Remove the constraint. | ||
5 │ interface·FooAny2<T·extends·unknown>·{ | ||
│ --------------- | ||
``` | ||
|
||
``` | ||
invalid.ts:9:16 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Constraining a type parameter to any or unknown is useless. | ||
7 │ } | ||
8 │ | ||
> 9 │ class BazAny<T extends any> { | ||
│ ^^^^^^^^^^^ | ||
10 │ quxAny<U extends any>() {} | ||
11 │ } | ||
i All types are subtypes of any and unknown. | ||
i Suggested fix: Remove the constraint. | ||
9 │ class·BazAny<T·extends·any>·{ | ||
│ ----------- | ||
``` | ||
|
||
``` | ||
invalid.ts:10:12 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Constraining a type parameter to any or unknown is useless. | ||
9 │ class BazAny<T extends any> { | ||
> 10 │ quxAny<U extends any>() {} | ||
│ ^^^^^^^^^^^ | ||
11 │ } | ||
12 │ | ||
i All types are subtypes of any and unknown. | ||
i Suggested fix: Remove the constraint. | ||
10 │ ··quxAny<U·extends·any>()·{} | ||
│ ----------- | ||
``` | ||
|
||
``` | ||
invalid.ts:13:20 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Constraining a type parameter to any or unknown is useless. | ||
11 │ } | ||
12 │ | ||
> 13 │ const QuuxAny = <T extends any>() => {}; | ||
│ ^^^^^^^^^^^ | ||
14 │ | ||
15 │ function QuuzAny<T extends any>() {} | ||
i All types are subtypes of any and unknown. | ||
i Suggested fix: Remove the constraint. | ||
13 │ const·QuuxAny·=·<T·extends·any>()·=>·{}; | ||
│ ----------- | ||
``` | ||
|
||
``` | ||
invalid.ts:15:20 lint/complexity/noUselessTypeConstraint FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Constraining a type parameter to any or unknown is useless. | ||
13 │ const QuuxAny = <T extends any>() => {}; | ||
14 │ | ||
> 15 │ function QuuzAny<T extends any>() {} | ||
│ ^^^^^^^^^^^ | ||
16 │ | ||
i All types are subtypes of any and unknown. | ||
i Suggested fix: Remove the constraint. | ||
15 │ function·QuuzAny<T·extends·any>()·{} | ||
│ ----------- | ||
``` | ||
|
||
|
11 changes: 11 additions & 0 deletions
11
crates/rome_js_analyze/tests/specs/complexity/noUselessTypeConstraint/valid.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> = {}; |
21 changes: 21 additions & 0 deletions
21
crates/rome_js_analyze/tests/specs/complexity/noUselessTypeConstraint/valid.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> = {}; | ||
|
||
``` | ||
|
||
|
Oops, something went wrong.