Merge pull request #9692 from Microsoft/fix-emit-with-this-and-rest-parameters

Fix emit with this and rest parameters
This commit is contained in:
Nathan Shively-Sanders 2016-07-21 08:31:29 -07:00 committed by GitHub
commit 57eac1b1e5
9 changed files with 93 additions and 12 deletions

View File

@ -4571,14 +4571,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
function emitRestParameter(node: FunctionLikeDeclaration) {
if (languageVersion < ScriptTarget.ES6 && hasDeclaredRestParameter(node)) {
const restIndex = node.parameters.length - 1;
const restParam = node.parameters[restIndex];
const restParam = node.parameters[node.parameters.length - 1];
// A rest parameter cannot have a binding pattern, so let's just ignore it if it does.
if (isBindingPattern(restParam.name)) {
return;
}
const skipThisCount = node.parameters.length && (<Identifier>node.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword ? 1 : 0;
const restIndex = node.parameters.length - 1 - skipThisCount;
const tempName = createTempVariable(TempFlags._i).text;
writeLine();
emitLeadingComments(restParam);
@ -4726,7 +4727,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
write("(");
if (node) {
const parameters = node.parameters;
const skipCount = node.parameters.length && (<Identifier>node.parameters[0].name).text === "this" ? 1 : 0;
const skipCount = node.parameters.length && (<Identifier>node.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword ? 1 : 0;
const omitCount = languageVersion < ScriptTarget.ES6 && hasDeclaredRestParameter(node) ? 1 : 0;
emitList(parameters, skipCount, parameters.length - omitCount - skipCount, /*multiLine*/ false, /*trailingComma*/ false);
}
@ -6156,10 +6157,11 @@ const _super = (function (geti, seti) {
if (valueDeclaration) {
const parameters = valueDeclaration.parameters;
const skipThisCount = parameters.length && (<Identifier>parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword ? 1 : 0;
const parameterCount = parameters.length;
if (parameterCount > 0) {
for (let i = 0; i < parameterCount; i++) {
if (i > 0) {
if (parameterCount > skipThisCount) {
for (let i = skipThisCount; i < parameterCount; i++) {
if (i > skipThisCount) {
write(", ");
}

View File

@ -14,7 +14,7 @@ class A {
class B {
constructor(...args: number[]) {}
@MyMethodDecorator
method(...args: string[]) {}
method(this: this, ...args: string[]) {}
}

View File

@ -37,8 +37,9 @@ class B {
@MyMethodDecorator
>MyMethodDecorator : Symbol(MyMethodDecorator, Decl(emitDecoratorMetadata_restArgs.ts, 2, 13))
method(...args: string[]) {}
method(this: this, ...args: string[]) {}
>method : Symbol(B.method, Decl(emitDecoratorMetadata_restArgs.ts, 13, 37))
>args : Symbol(args, Decl(emitDecoratorMetadata_restArgs.ts, 15, 11))
>this : Symbol(this, Decl(emitDecoratorMetadata_restArgs.ts, 15, 11))
>args : Symbol(args, Decl(emitDecoratorMetadata_restArgs.ts, 15, 22))
}

View File

@ -37,8 +37,9 @@ class B {
@MyMethodDecorator
>MyMethodDecorator : <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void
method(...args: string[]) {}
>method : (...args: string[]) => void
method(this: this, ...args: string[]) {}
>method : (this: this, ...args: string[]) => void
>this : this
>args : string[]
}

View File

@ -0,0 +1,18 @@
//// [emitSkipsThisWithRestParameter.ts]
function rebase(fn: (base: any, ...args: any[]) => any): (...args: any[]) => any {
return function(this: any, ...args: any[]) {
return fn.apply(this, [ this ].concat(args));
};
}
//// [emitSkipsThisWithRestParameter.js]
function rebase(fn) {
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
return fn.apply(this, [this].concat(args));
};
}

View File

@ -0,0 +1,25 @@
=== tests/cases/compiler/emitSkipsThisWithRestParameter.ts ===
function rebase(fn: (base: any, ...args: any[]) => any): (...args: any[]) => any {
>rebase : Symbol(rebase, Decl(emitSkipsThisWithRestParameter.ts, 0, 0))
>fn : Symbol(fn, Decl(emitSkipsThisWithRestParameter.ts, 0, 16))
>base : Symbol(base, Decl(emitSkipsThisWithRestParameter.ts, 0, 21))
>args : Symbol(args, Decl(emitSkipsThisWithRestParameter.ts, 0, 31))
>args : Symbol(args, Decl(emitSkipsThisWithRestParameter.ts, 0, 58))
return function(this: any, ...args: any[]) {
>this : Symbol(this, Decl(emitSkipsThisWithRestParameter.ts, 1, 20))
>args : Symbol(args, Decl(emitSkipsThisWithRestParameter.ts, 1, 30))
return fn.apply(this, [ this ].concat(args));
>fn.apply : Symbol(Function.apply, Decl(lib.d.ts, --, --))
>fn : Symbol(fn, Decl(emitSkipsThisWithRestParameter.ts, 0, 16))
>apply : Symbol(Function.apply, Decl(lib.d.ts, --, --))
>this : Symbol(this, Decl(emitSkipsThisWithRestParameter.ts, 1, 20))
>[ this ].concat : Symbol(Array.concat, Decl(lib.d.ts, --, --))
>this : Symbol(this, Decl(emitSkipsThisWithRestParameter.ts, 1, 20))
>concat : Symbol(Array.concat, Decl(lib.d.ts, --, --))
>args : Symbol(args, Decl(emitSkipsThisWithRestParameter.ts, 1, 30))
};
}

View File

@ -0,0 +1,29 @@
=== tests/cases/compiler/emitSkipsThisWithRestParameter.ts ===
function rebase(fn: (base: any, ...args: any[]) => any): (...args: any[]) => any {
>rebase : (fn: (base: any, ...args: any[]) => any) => (...args: any[]) => any
>fn : (base: any, ...args: any[]) => any
>base : any
>args : any[]
>args : any[]
return function(this: any, ...args: any[]) {
>function(this: any, ...args: any[]) { return fn.apply(this, [ this ].concat(args)); } : (this: any, ...args: any[]) => any
>this : any
>args : any[]
return fn.apply(this, [ this ].concat(args));
>fn.apply(this, [ this ].concat(args)) : any
>fn.apply : (this: Function, thisArg: any, argArray?: any) => any
>fn : (base: any, ...args: any[]) => any
>apply : (this: Function, thisArg: any, argArray?: any) => any
>this : any
>[ this ].concat(args) : any[]
>[ this ].concat : (...items: any[]) => any[]
>[ this ] : any[]
>this : any
>concat : (...items: any[]) => any[]
>args : any[]
};
}

View File

@ -16,5 +16,5 @@ class A {
class B {
constructor(...args: number[]) {}
@MyMethodDecorator
method(...args: string[]) {}
method(this: this, ...args: string[]) {}
}

View File

@ -0,0 +1,5 @@
function rebase(fn: (base: any, ...args: any[]) => any): (...args: any[]) => any {
return function(this: any, ...args: any[]) {
return fn.apply(this, [ this ].concat(args));
};
}