Skip to content

Commit 8794ebd

Browse files
authored
Merge pull request microsoft#30179 from Microsoft/fixGetParameterNameAtPosition2
Fix out-of-bounds issue in getParameterNameAtPosition
2 parents f776bea + 22a2eb8 commit 8794ebd

File tree

6 files changed

+115
-1
lines changed

6 files changed

+115
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21641,7 +21641,7 @@ namespace ts {
2164121641
if (isTupleType(restType)) {
2164221642
const associatedNames = (<TupleType>(<TypeReference>restType).target).associatedNames;
2164321643
const index = pos - paramCount;
21644-
return associatedNames ? associatedNames[index] : restParameter.escapedName + "_" + index as __String;
21644+
return associatedNames && associatedNames[index] || restParameter.escapedName + "_" + index as __String;
2164521645
}
2164621646
return restParameter.escapedName;
2164721647
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
tests/cases/compiler/getParameterNameAtPosition.ts(9,7): error TS2345: Argument of type 'Mock<[any]>' is not assignable to parameter of type 'Tester'.
2+
Types of parameters 'args_1' and 'done' are incompatible.
3+
Type '(...args: any[]) => any' is not assignable to type 'undefined'.
4+
5+
6+
==== tests/cases/compiler/getParameterNameAtPosition.ts (1 errors) ====
7+
// Repro from #30171
8+
9+
interface Mock<Y extends any[]> extends Function {
10+
(...args: Y): any;
11+
}
12+
type Tester = (opts: any, done: (...args: any[]) => any) => any;
13+
declare function cases(tester: Tester): void;
14+
declare function fn<Y extends any[]>(implementation?: (...args: Y) => any): Mock<Y>;
15+
cases(fn(opts => { }));
16+
~~~~~~~~~~~~~~~
17+
!!! error TS2345: Argument of type 'Mock<[any]>' is not assignable to parameter of type 'Tester'.
18+
!!! error TS2345: Types of parameters 'args_1' and 'done' are incompatible.
19+
!!! error TS2345: Type '(...args: any[]) => any' is not assignable to type 'undefined'.
20+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [getParameterNameAtPosition.ts]
2+
// Repro from #30171
3+
4+
interface Mock<Y extends any[]> extends Function {
5+
(...args: Y): any;
6+
}
7+
type Tester = (opts: any, done: (...args: any[]) => any) => any;
8+
declare function cases(tester: Tester): void;
9+
declare function fn<Y extends any[]>(implementation?: (...args: Y) => any): Mock<Y>;
10+
cases(fn(opts => { }));
11+
12+
13+
//// [getParameterNameAtPosition.js]
14+
"use strict";
15+
// Repro from #30171
16+
cases(fn(function (opts) { }));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=== tests/cases/compiler/getParameterNameAtPosition.ts ===
2+
// Repro from #30171
3+
4+
interface Mock<Y extends any[]> extends Function {
5+
>Mock : Symbol(Mock, Decl(getParameterNameAtPosition.ts, 0, 0))
6+
>Y : Symbol(Y, Decl(getParameterNameAtPosition.ts, 2, 15))
7+
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
8+
9+
(...args: Y): any;
10+
>args : Symbol(args, Decl(getParameterNameAtPosition.ts, 3, 5))
11+
>Y : Symbol(Y, Decl(getParameterNameAtPosition.ts, 2, 15))
12+
}
13+
type Tester = (opts: any, done: (...args: any[]) => any) => any;
14+
>Tester : Symbol(Tester, Decl(getParameterNameAtPosition.ts, 4, 1))
15+
>opts : Symbol(opts, Decl(getParameterNameAtPosition.ts, 5, 15))
16+
>done : Symbol(done, Decl(getParameterNameAtPosition.ts, 5, 25))
17+
>args : Symbol(args, Decl(getParameterNameAtPosition.ts, 5, 33))
18+
19+
declare function cases(tester: Tester): void;
20+
>cases : Symbol(cases, Decl(getParameterNameAtPosition.ts, 5, 64))
21+
>tester : Symbol(tester, Decl(getParameterNameAtPosition.ts, 6, 23))
22+
>Tester : Symbol(Tester, Decl(getParameterNameAtPosition.ts, 4, 1))
23+
24+
declare function fn<Y extends any[]>(implementation?: (...args: Y) => any): Mock<Y>;
25+
>fn : Symbol(fn, Decl(getParameterNameAtPosition.ts, 6, 45))
26+
>Y : Symbol(Y, Decl(getParameterNameAtPosition.ts, 7, 20))
27+
>implementation : Symbol(implementation, Decl(getParameterNameAtPosition.ts, 7, 37))
28+
>args : Symbol(args, Decl(getParameterNameAtPosition.ts, 7, 55))
29+
>Y : Symbol(Y, Decl(getParameterNameAtPosition.ts, 7, 20))
30+
>Mock : Symbol(Mock, Decl(getParameterNameAtPosition.ts, 0, 0))
31+
>Y : Symbol(Y, Decl(getParameterNameAtPosition.ts, 7, 20))
32+
33+
cases(fn(opts => { }));
34+
>cases : Symbol(cases, Decl(getParameterNameAtPosition.ts, 5, 64))
35+
>fn : Symbol(fn, Decl(getParameterNameAtPosition.ts, 6, 45))
36+
>opts : Symbol(opts, Decl(getParameterNameAtPosition.ts, 8, 9))
37+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/getParameterNameAtPosition.ts ===
2+
// Repro from #30171
3+
4+
interface Mock<Y extends any[]> extends Function {
5+
(...args: Y): any;
6+
>args : Y
7+
}
8+
type Tester = (opts: any, done: (...args: any[]) => any) => any;
9+
>Tester : Tester
10+
>opts : any
11+
>done : (...args: any[]) => any
12+
>args : any[]
13+
14+
declare function cases(tester: Tester): void;
15+
>cases : (tester: Tester) => void
16+
>tester : Tester
17+
18+
declare function fn<Y extends any[]>(implementation?: (...args: Y) => any): Mock<Y>;
19+
>fn : <Y extends any[]>(implementation?: ((...args: Y) => any) | undefined) => Mock<Y>
20+
>implementation : ((...args: Y) => any) | undefined
21+
>args : Y
22+
23+
cases(fn(opts => { }));
24+
>cases(fn(opts => { })) : void
25+
>cases : (tester: Tester) => void
26+
>fn(opts => { }) : Mock<[any]>
27+
>fn : <Y extends any[]>(implementation?: ((...args: Y) => any) | undefined) => Mock<Y>
28+
>opts => { } : (opts: any) => void
29+
>opts : any
30+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @strict: true
2+
3+
// Repro from #30171
4+
5+
interface Mock<Y extends any[]> extends Function {
6+
(...args: Y): any;
7+
}
8+
type Tester = (opts: any, done: (...args: any[]) => any) => any;
9+
declare function cases(tester: Tester): void;
10+
declare function fn<Y extends any[]>(implementation?: (...args: Y) => any): Mock<Y>;
11+
cases(fn(opts => { }));

0 commit comments

Comments
 (0)