Skip to content

Commit

Permalink
fix(useShorthandFunctionType): generic interface
Browse files Browse the repository at this point in the history
Preserve type parameters of generic interface when applying fix
  • Loading branch information
Sec-ant committed Mar 9, 2024
1 parent 0763c47 commit 87ab7c6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ impl Rule for UseShorthandFunctionType {
node,
)?),
)
.build();
.build()
.with_type_parameters(interface_decl.type_parameters());

mutation.replace_node(
AnyJsDeclarationClause::from(interface_decl),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ type GenericCallSignature<T> = { (arg: T): T };

// Object type with optional call signature
let optionalCall: { (): number | undefined };

// Generic interface with a call signature
interface GenericInterface<T> {
(value: T): boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type GenericCallSignature<T> = { (arg: T): T };
// Object type with optional call signature
let optionalCall: { (): number | undefined };

// Generic interface with a call signature
interface GenericInterface<T> {
(value: T): boolean;
}

```

# Diagnostics
Expand Down Expand Up @@ -229,6 +234,7 @@ invalid.ts:33:21 lint/style/useShorthandFunctionType FIXABLE ━━━━━
> 33 │ let optionalCall: { (): number | undefined };
│ ^^^^^^^^^^^^^^^^^^^^^^
34 │
35 │ // Generic interface with a call signature
i Types containing only a call signature can be shortened to a function type.
Expand All @@ -239,8 +245,34 @@ invalid.ts:33:21 lint/style/useShorthandFunctionType FIXABLE ━━━━━
33 │ - let·optionalCall:·{·():·number·|·undefined·};
33 │ + let·optionalCall:·()·=>·number·|·undefined;
34 34 │
35 35 │ // Generic interface with a call signature
```

```
invalid.ts:37:2 lint/style/useShorthandFunctionType FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Use a function type instead of a call signature.
35 │ // Generic interface with a call signature
36 │ interface GenericInterface<T> {
> 37 │ (value: T): boolean;
│ ^^^^^^^^^^^^^^^^^^^^
38 │ }
39 │
i Types containing only a call signature can be shortened to a function type.
i Safe fix: Alias a function type instead of using an interface with a call signature.
34 34 │
35 35 │ // Generic interface with a call signature
36 │ - interface·GenericInterface<T>·{
37 │ - → (value:·T):·boolean;
38 │ - }
36 │ + type·GenericInterface<T>·=·(value:·T)·=>·boolean
39 37 │
```

0 comments on commit 87ab7c6

Please sign in to comment.