diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 2d5acb3b48667..bdf50a2c4da0b 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -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 && (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 && (node.parameters[0].name).text === "this" ? 1 : 0; + const skipCount = node.parameters.length && (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); } @@ -6155,10 +6156,11 @@ const _super = (function (geti, seti) { if (valueDeclaration) { const parameters = valueDeclaration.parameters; + const skipThisCount = parameters.length && (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(", "); } diff --git a/tests/baselines/reference/emitDecoratorMetadata_restArgs.js b/tests/baselines/reference/emitDecoratorMetadata_restArgs.js index 35350c54e0a21..6ca7e40d413ad 100644 --- a/tests/baselines/reference/emitDecoratorMetadata_restArgs.js +++ b/tests/baselines/reference/emitDecoratorMetadata_restArgs.js @@ -14,7 +14,7 @@ class A { class B { constructor(...args: number[]) {} @MyMethodDecorator - method(...args: string[]) {} + method(this: this, ...args: string[]) {} } diff --git a/tests/baselines/reference/emitDecoratorMetadata_restArgs.symbols b/tests/baselines/reference/emitDecoratorMetadata_restArgs.symbols index b665b778f8615..29ccb8b2711d2 100644 --- a/tests/baselines/reference/emitDecoratorMetadata_restArgs.symbols +++ b/tests/baselines/reference/emitDecoratorMetadata_restArgs.symbols @@ -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)) } diff --git a/tests/baselines/reference/emitDecoratorMetadata_restArgs.types b/tests/baselines/reference/emitDecoratorMetadata_restArgs.types index 4820d3a7d8dc2..e08261e45036a 100644 --- a/tests/baselines/reference/emitDecoratorMetadata_restArgs.types +++ b/tests/baselines/reference/emitDecoratorMetadata_restArgs.types @@ -37,8 +37,9 @@ class B { @MyMethodDecorator >MyMethodDecorator : (target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor) => TypedPropertyDescriptor | void - method(...args: string[]) {} ->method : (...args: string[]) => void + method(this: this, ...args: string[]) {} +>method : (this: this, ...args: string[]) => void +>this : this >args : string[] } diff --git a/tests/baselines/reference/emitSkipsThisWithRestParameter.js b/tests/baselines/reference/emitSkipsThisWithRestParameter.js new file mode 100644 index 0000000000000..d1e99f9409dc5 --- /dev/null +++ b/tests/baselines/reference/emitSkipsThisWithRestParameter.js @@ -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)); + }; +} diff --git a/tests/baselines/reference/emitSkipsThisWithRestParameter.symbols b/tests/baselines/reference/emitSkipsThisWithRestParameter.symbols new file mode 100644 index 0000000000000..4f1e2bc1aec07 --- /dev/null +++ b/tests/baselines/reference/emitSkipsThisWithRestParameter.symbols @@ -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)) + + }; +} + diff --git a/tests/baselines/reference/emitSkipsThisWithRestParameter.types b/tests/baselines/reference/emitSkipsThisWithRestParameter.types new file mode 100644 index 0000000000000..97276fa117de0 --- /dev/null +++ b/tests/baselines/reference/emitSkipsThisWithRestParameter.types @@ -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[] + + }; +} + diff --git a/tests/cases/compiler/emitDecoratorMetadata_restArgs.ts b/tests/cases/compiler/emitDecoratorMetadata_restArgs.ts index 6b0741e22058a..f58e9b704d48e 100644 --- a/tests/cases/compiler/emitDecoratorMetadata_restArgs.ts +++ b/tests/cases/compiler/emitDecoratorMetadata_restArgs.ts @@ -16,5 +16,5 @@ class A { class B { constructor(...args: number[]) {} @MyMethodDecorator - method(...args: string[]) {} + method(this: this, ...args: string[]) {} } diff --git a/tests/cases/compiler/emitSkipsThisWithRestParameter.ts b/tests/cases/compiler/emitSkipsThisWithRestParameter.ts new file mode 100644 index 0000000000000..09411a28cd25e --- /dev/null +++ b/tests/cases/compiler/emitSkipsThisWithRestParameter.ts @@ -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)); + }; +}