Skip to content

Commit 9d8b4c4

Browse files
authored
fix(scope-manager): support rest function type parameters (typescript-eslint#2491)
Fixes typescript-eslint#2449
1 parent 36305df commit 9d8b4c4

File tree

8 files changed

+310
-1
lines changed

8 files changed

+310
-1
lines changed

packages/eslint-plugin/tests/rules/no-unused-vars.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,14 @@ export abstract class Foo {
767767
protected abstract readonly type: FooType;
768768
}
769769
`,
770+
// https://github.com/typescript-eslint/typescript-eslint/issues/2449
771+
`
772+
export type F<A extends unknown[]> = (...a: A) => unknown;
773+
`,
774+
`
775+
import { Foo } from './bar';
776+
export type F<A extends unknown[]> = (...a: Foo<A>) => unknown;
777+
`,
770778
],
771779

772780
invalid: [

packages/scope-manager/src/referencer/TypeVisitor.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TypeVisitor extends Visitor {
3333
this.visit(node.typeParameters);
3434

3535
for (const param of node.params) {
36+
let didVisitAnnotation = false;
3637
this.visitPattern(param, (pattern, info) => {
3738
// a parameter name creates a value type variable which can be referenced later via typeof arg
3839
this.#referencer
@@ -41,8 +42,17 @@ class TypeVisitor extends Visitor {
4142
pattern,
4243
new ParameterDefinition(pattern, node, info.rest),
4344
);
44-
this.visit(pattern.typeAnnotation);
45+
46+
if (pattern.typeAnnotation) {
47+
this.visit(pattern.typeAnnotation);
48+
didVisitAnnotation = true;
49+
}
4550
});
51+
52+
// there are a few special cases where the type annotation is owned by the parameter, not the pattern
53+
if (!didVisitAnnotation && 'typeAnnotation' in param) {
54+
this.visit(param.typeAnnotation);
55+
}
4656
}
4757
this.visit(node.returnType);
4858

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type Fn<A extends unknown[]> = ([a]: A) => unknown;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`type-declaration function params array-pattern 1`] = `
4+
ScopeManager {
5+
variables: Array [
6+
Variable$1 {
7+
defs: Array [
8+
TypeDefinition$1 {
9+
name: Identifier<"Fn">,
10+
node: TSTypeAliasDeclaration$1,
11+
},
12+
],
13+
name: "Fn",
14+
references: Array [],
15+
isValueVariable: false,
16+
isTypeVariable: true,
17+
},
18+
Variable$2 {
19+
defs: Array [
20+
TypeDefinition$2 {
21+
name: Identifier<"A">,
22+
node: TSTypeParameter$2,
23+
},
24+
],
25+
name: "A",
26+
references: Array [
27+
Reference$1 {
28+
identifier: Identifier<"A">,
29+
isRead: true,
30+
isTypeReference: true,
31+
isValueReference: false,
32+
isWrite: false,
33+
resolved: Variable$2,
34+
},
35+
],
36+
isValueVariable: false,
37+
isTypeVariable: true,
38+
},
39+
Variable$3 {
40+
defs: Array [
41+
ParameterDefinition$3 {
42+
name: Identifier<"a">,
43+
node: TSFunctionType$3,
44+
},
45+
],
46+
name: "a",
47+
references: Array [],
48+
isValueVariable: true,
49+
isTypeVariable: false,
50+
},
51+
],
52+
scopes: Array [
53+
GlobalScope$1 {
54+
block: Program$4,
55+
isStrict: false,
56+
references: Array [],
57+
set: Map {
58+
"Fn" => Variable$1,
59+
},
60+
type: "global",
61+
upper: null,
62+
variables: Array [
63+
Variable$1,
64+
],
65+
},
66+
TypeScope$2 {
67+
block: TSTypeAliasDeclaration$1,
68+
isStrict: true,
69+
references: Array [],
70+
set: Map {
71+
"A" => Variable$2,
72+
},
73+
type: "type",
74+
upper: GlobalScope$1,
75+
variables: Array [
76+
Variable$2,
77+
],
78+
},
79+
FunctionTypeScope$3 {
80+
block: TSFunctionType$3,
81+
isStrict: true,
82+
references: Array [
83+
Reference$1,
84+
],
85+
set: Map {
86+
"a" => Variable$3,
87+
},
88+
type: "functionType",
89+
upper: TypeScope$2,
90+
variables: Array [
91+
Variable$3,
92+
],
93+
},
94+
],
95+
}
96+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type Fn<A extends { a: string }> = ({ a }: A) => unknown;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`type-declaration function params object-pattern 1`] = `
4+
ScopeManager {
5+
variables: Array [
6+
Variable$1 {
7+
defs: Array [
8+
TypeDefinition$1 {
9+
name: Identifier<"Fn">,
10+
node: TSTypeAliasDeclaration$1,
11+
},
12+
],
13+
name: "Fn",
14+
references: Array [],
15+
isValueVariable: false,
16+
isTypeVariable: true,
17+
},
18+
Variable$2 {
19+
defs: Array [
20+
TypeDefinition$2 {
21+
name: Identifier<"A">,
22+
node: TSTypeParameter$2,
23+
},
24+
],
25+
name: "A",
26+
references: Array [
27+
Reference$1 {
28+
identifier: Identifier<"A">,
29+
isRead: true,
30+
isTypeReference: true,
31+
isValueReference: false,
32+
isWrite: false,
33+
resolved: Variable$2,
34+
},
35+
],
36+
isValueVariable: false,
37+
isTypeVariable: true,
38+
},
39+
Variable$3 {
40+
defs: Array [
41+
ParameterDefinition$3 {
42+
name: Identifier<"a">,
43+
node: TSFunctionType$3,
44+
},
45+
],
46+
name: "a",
47+
references: Array [],
48+
isValueVariable: true,
49+
isTypeVariable: false,
50+
},
51+
],
52+
scopes: Array [
53+
GlobalScope$1 {
54+
block: Program$4,
55+
isStrict: false,
56+
references: Array [],
57+
set: Map {
58+
"Fn" => Variable$1,
59+
},
60+
type: "global",
61+
upper: null,
62+
variables: Array [
63+
Variable$1,
64+
],
65+
},
66+
TypeScope$2 {
67+
block: TSTypeAliasDeclaration$1,
68+
isStrict: true,
69+
references: Array [],
70+
set: Map {
71+
"A" => Variable$2,
72+
},
73+
type: "type",
74+
upper: GlobalScope$1,
75+
variables: Array [
76+
Variable$2,
77+
],
78+
},
79+
FunctionTypeScope$3 {
80+
block: TSFunctionType$3,
81+
isStrict: true,
82+
references: Array [
83+
Reference$1,
84+
],
85+
set: Map {
86+
"a" => Variable$3,
87+
},
88+
type: "functionType",
89+
upper: TypeScope$2,
90+
variables: Array [
91+
Variable$3,
92+
],
93+
},
94+
],
95+
}
96+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type Fn<A extends unknown[]> = (...a: A) => unknown;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`type-declaration function params rest-element 1`] = `
4+
ScopeManager {
5+
variables: Array [
6+
Variable$1 {
7+
defs: Array [
8+
TypeDefinition$1 {
9+
name: Identifier<"Fn">,
10+
node: TSTypeAliasDeclaration$1,
11+
},
12+
],
13+
name: "Fn",
14+
references: Array [],
15+
isValueVariable: false,
16+
isTypeVariable: true,
17+
},
18+
Variable$2 {
19+
defs: Array [
20+
TypeDefinition$2 {
21+
name: Identifier<"A">,
22+
node: TSTypeParameter$2,
23+
},
24+
],
25+
name: "A",
26+
references: Array [
27+
Reference$1 {
28+
identifier: Identifier<"A">,
29+
isRead: true,
30+
isTypeReference: true,
31+
isValueReference: false,
32+
isWrite: false,
33+
resolved: Variable$2,
34+
},
35+
],
36+
isValueVariable: false,
37+
isTypeVariable: true,
38+
},
39+
Variable$3 {
40+
defs: Array [
41+
ParameterDefinition$3 {
42+
name: Identifier<"a">,
43+
node: TSFunctionType$3,
44+
},
45+
],
46+
name: "a",
47+
references: Array [],
48+
isValueVariable: true,
49+
isTypeVariable: false,
50+
},
51+
],
52+
scopes: Array [
53+
GlobalScope$1 {
54+
block: Program$4,
55+
isStrict: false,
56+
references: Array [],
57+
set: Map {
58+
"Fn" => Variable$1,
59+
},
60+
type: "global",
61+
upper: null,
62+
variables: Array [
63+
Variable$1,
64+
],
65+
},
66+
TypeScope$2 {
67+
block: TSTypeAliasDeclaration$1,
68+
isStrict: true,
69+
references: Array [],
70+
set: Map {
71+
"A" => Variable$2,
72+
},
73+
type: "type",
74+
upper: GlobalScope$1,
75+
variables: Array [
76+
Variable$2,
77+
],
78+
},
79+
FunctionTypeScope$3 {
80+
block: TSFunctionType$3,
81+
isStrict: true,
82+
references: Array [
83+
Reference$1,
84+
],
85+
set: Map {
86+
"a" => Variable$3,
87+
},
88+
type: "functionType",
89+
upper: TypeScope$2,
90+
variables: Array [
91+
Variable$3,
92+
],
93+
},
94+
],
95+
}
96+
`;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy