Merge pull request #30179 from Microsoft/fixGetParameterNameAtPosition2

Fix out-of-bounds issue in getParameterNameAtPosition
This commit is contained in:
Anders Hejlsberg 2019-03-02 05:17:36 -10:00 committed by GitHub
commit 8794ebdff5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 115 additions and 1 deletions

View File

@ -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;
}

View File

@ -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'.

View File

@ -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) { }));

View File

@ -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))

View File

@ -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

View File

@ -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 => { }));