Skip to content

Commit

Permalink
Merge pull request #30179 from Microsoft/fixGetParameterNameAtPosition2
Browse files Browse the repository at this point in the history
Fix out-of-bounds issue in getParameterNameAtPosition
  • Loading branch information
ahejlsberg authored Mar 2, 2019
2 parents f776bea + 22a2eb8 commit 8794ebd
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
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;
}
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 => { }));

0 comments on commit 8794ebd

Please sign in to comment.