Skip to content

Fix emit with this and rest parameters #9692

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

Merged
merged 3 commits into from
Jul 21, 2016
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
14 changes: 8 additions & 6 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -6155,10 +6156,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(", ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class A {
class B {
constructor(...args: number[]) {}
@MyMethodDecorator
method(...args: string[]) {}
method(this: this, ...args: string[]) {}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Original file line number Diff line number Diff line change
Expand Up @@ -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[]
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/emitSkipsThisWithRestParameter.js
Original file line number Diff line number Diff line change
@@ -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));
};
}
25 changes: 25 additions & 0 deletions tests/baselines/reference/emitSkipsThisWithRestParameter.symbols
Original file line number Diff line number Diff line change
@@ -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))

};
}

29 changes: 29 additions & 0 deletions tests/baselines/reference/emitSkipsThisWithRestParameter.types
Original file line number Diff line number Diff line change
@@ -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[]

};
}

2 changes: 1 addition & 1 deletion tests/cases/compiler/emitDecoratorMetadata_restArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class A {
class B {
constructor(...args: number[]) {}
@MyMethodDecorator
method(...args: string[]) {}
method(this: this, ...args: string[]) {}
}
5 changes: 5 additions & 0 deletions tests/cases/compiler/emitSkipsThisWithRestParameter.ts
Original file line number Diff line number Diff line change
@@ -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));
};
}