Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out-of-bounds issue in getParameterNameAtPosition #30179

Merged
merged 3 commits into from
Mar 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21641,7 +21641,7 @@ namespace ts {
if (isTupleType(restType)) {
const associatedNames = (<TupleType>(<TypeReference>restType).target).associatedNames;
const index = pos - paramCount;
return associatedNames ? associatedNames[index] : restParameter.escapedName + "_" + index as __String;
return associatedNames && associatedNames[index] || restParameter.escapedName + "_" + index as __String;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cute fix 😄

}
return restParameter.escapedName;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/getParameterNameAtPosition.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
tests/cases/compiler/getParameterNameAtPosition.ts(9,7): error TS2345: Argument of type 'Mock<[any]>' is not assignable to parameter of type 'Tester'.
Types of parameters 'args_1' and 'done' are incompatible.
Type '(...args: any[]) => any' is not assignable to type 'undefined'.


==== tests/cases/compiler/getParameterNameAtPosition.ts (1 errors) ====
// Repro from #30171

interface Mock<Y extends any[]> extends Function {
(...args: Y): any;
}
type Tester = (opts: any, done: (...args: any[]) => any) => any;
declare function cases(tester: Tester): void;
declare function fn<Y extends any[]>(implementation?: (...args: Y) => any): Mock<Y>;
cases(fn(opts => { }));
~~~~~~~~~~~~~~~
!!! error TS2345: Argument of type 'Mock<[any]>' is not assignable to parameter of type 'Tester'.
!!! error TS2345: Types of parameters 'args_1' and 'done' are incompatible.
!!! error TS2345: Type '(...args: any[]) => any' is not assignable to type 'undefined'.

16 changes: 16 additions & 0 deletions tests/baselines/reference/getParameterNameAtPosition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [getParameterNameAtPosition.ts]
// Repro from #30171

interface Mock<Y extends any[]> extends Function {
(...args: Y): any;
}
type Tester = (opts: any, done: (...args: any[]) => any) => any;
declare function cases(tester: Tester): void;
declare function fn<Y extends any[]>(implementation?: (...args: Y) => any): Mock<Y>;
cases(fn(opts => { }));


//// [getParameterNameAtPosition.js]
"use strict";
// Repro from #30171
cases(fn(function (opts) { }));
37 changes: 37 additions & 0 deletions tests/baselines/reference/getParameterNameAtPosition.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
=== tests/cases/compiler/getParameterNameAtPosition.ts ===
// Repro from #30171

interface Mock<Y extends any[]> extends Function {
>Mock : Symbol(Mock, Decl(getParameterNameAtPosition.ts, 0, 0))
>Y : Symbol(Y, Decl(getParameterNameAtPosition.ts, 2, 15))
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

(...args: Y): any;
>args : Symbol(args, Decl(getParameterNameAtPosition.ts, 3, 5))
>Y : Symbol(Y, Decl(getParameterNameAtPosition.ts, 2, 15))
}
type Tester = (opts: any, done: (...args: any[]) => any) => any;
>Tester : Symbol(Tester, Decl(getParameterNameAtPosition.ts, 4, 1))
>opts : Symbol(opts, Decl(getParameterNameAtPosition.ts, 5, 15))
>done : Symbol(done, Decl(getParameterNameAtPosition.ts, 5, 25))
>args : Symbol(args, Decl(getParameterNameAtPosition.ts, 5, 33))

declare function cases(tester: Tester): void;
>cases : Symbol(cases, Decl(getParameterNameAtPosition.ts, 5, 64))
>tester : Symbol(tester, Decl(getParameterNameAtPosition.ts, 6, 23))
>Tester : Symbol(Tester, Decl(getParameterNameAtPosition.ts, 4, 1))

declare function fn<Y extends any[]>(implementation?: (...args: Y) => any): Mock<Y>;
>fn : Symbol(fn, Decl(getParameterNameAtPosition.ts, 6, 45))
>Y : Symbol(Y, Decl(getParameterNameAtPosition.ts, 7, 20))
>implementation : Symbol(implementation, Decl(getParameterNameAtPosition.ts, 7, 37))
>args : Symbol(args, Decl(getParameterNameAtPosition.ts, 7, 55))
>Y : Symbol(Y, Decl(getParameterNameAtPosition.ts, 7, 20))
>Mock : Symbol(Mock, Decl(getParameterNameAtPosition.ts, 0, 0))
>Y : Symbol(Y, Decl(getParameterNameAtPosition.ts, 7, 20))

cases(fn(opts => { }));
>cases : Symbol(cases, Decl(getParameterNameAtPosition.ts, 5, 64))
>fn : Symbol(fn, Decl(getParameterNameAtPosition.ts, 6, 45))
>opts : Symbol(opts, Decl(getParameterNameAtPosition.ts, 8, 9))

30 changes: 30 additions & 0 deletions tests/baselines/reference/getParameterNameAtPosition.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
=== tests/cases/compiler/getParameterNameAtPosition.ts ===
// Repro from #30171

interface Mock<Y extends any[]> extends Function {
(...args: Y): any;
>args : Y
}
type Tester = (opts: any, done: (...args: any[]) => any) => any;
>Tester : Tester
>opts : any
>done : (...args: any[]) => any
>args : any[]

declare function cases(tester: Tester): void;
>cases : (tester: Tester) => void
>tester : Tester

declare function fn<Y extends any[]>(implementation?: (...args: Y) => any): Mock<Y>;
>fn : <Y extends any[]>(implementation?: ((...args: Y) => any) | undefined) => Mock<Y>
>implementation : ((...args: Y) => any) | undefined
>args : Y

cases(fn(opts => { }));
>cases(fn(opts => { })) : void
>cases : (tester: Tester) => void
>fn(opts => { }) : Mock<[any]>
>fn : <Y extends any[]>(implementation?: ((...args: Y) => any) | undefined) => Mock<Y>
>opts => { } : (opts: any) => void
>opts : any

11 changes: 11 additions & 0 deletions tests/cases/compiler/getParameterNameAtPosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @strict: true

// Repro from #30171

interface Mock<Y extends any[]> extends Function {
(...args: Y): any;
}
type Tester = (opts: any, done: (...args: any[]) => any) => any;
declare function cases(tester: Tester): void;
declare function fn<Y extends any[]>(implementation?: (...args: Y) => any): Mock<Y>;
cases(fn(opts => { }));