This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 650
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
666cd8c
chore: Add no_useless_type_constraint type
nikeee 1d6c619
chore: Run codegen
nikeee 4b36b63
feat: Implement matching of useless type constraints
nikeee fe0fa04
chore: Add test baselines
nikeee 4097e40
feat: Also match on `unknown`
nikeee 74d937a
feat: Add support for fixing `noUselessTypeConstraint`
nikeee c5d6e26
chore: Add more test cases
nikeee b3f2b58
chore: Run `cargo insta accept`
nikeee 1595b8a
docs(changelog): Add new role to changelog
nikeee 12cc4c3
chore: Bump version
nikeee 7be4439
chore: Fix wording
nikeee 0f783fb
docs: Add explanation for `noUselessTypeConstraint`
nikeee 2f6b2d0
docs: Fix samples for `noUselessTypeConstraint`
nikeee 52e08c6
chore: Apply suggestions from code review
nikeee d7927d8
chore: Use suggested wording
nikeee 25e837c
chore: Update test baseline
nikeee 5ff6438
chore: Run just codegen
nikeee 6dfe254
chore: Revert back to unit state
nikeee e1d82e3
chore: Merge from main
nikeee b548144
chore: Format
nikeee f9fc2fb
Merge branch 'main' into feature/noUselessTypeConstraint
nikeee d6709fe
chore: just codegen-linter
nikeee 71bf584
chore: Fix examples
nikeee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.