From 1bccef7f8fe6de1d9f6ae566e993f4e9d44e170d Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 19 Apr 2016 16:02:29 -0700 Subject: [PATCH] Do not transform the emit of function with rest parameter unless declared in AST Fixes #7749 --- src/compiler/checker.ts | 2 +- src/compiler/emitter.ts | 4 +-- src/compiler/utilities.ts | 27 ++++++++++--------- ...jsFileCompilationRestParamJsDocFunction.js | 6 +---- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a18c3c45680..3113b56557d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13771,7 +13771,7 @@ namespace ts { function checkCollisionWithArgumentsInGeneratedCode(node: SignatureDeclaration) { // no rest parameters \ declaration context \ overload - no codegen impact - if (!hasRestParameter(node) || isInAmbientContext(node) || nodeIsMissing((node).body)) { + if (!hasDeclaredRestParameter(node) || isInAmbientContext(node) || nodeIsMissing((node).body)) { return; } diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 5c0b249aa4c..8bcdd4c8201 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4467,7 +4467,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge } function emitRestParameter(node: FunctionLikeDeclaration) { - if (languageVersion < ScriptTarget.ES6 && hasRestParameter(node)) { + if (languageVersion < ScriptTarget.ES6 && hasDeclaredRestParameter(node)) { const restIndex = node.parameters.length - 1; const restParam = node.parameters[restIndex]; @@ -4624,7 +4624,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge if (node) { const parameters = node.parameters; const skipCount = node.parameters.length && (node.parameters[0].name).text === "this" ? 1 : 0; - const omitCount = languageVersion < ScriptTarget.ES6 && hasRestParameter(node) ? 1 : 0; + const omitCount = languageVersion < ScriptTarget.ES6 && hasDeclaredRestParameter(node) ? 1 : 0; emitList(parameters, skipCount, parameters.length - omitCount - skipCount, /*multiLine*/ false, /*trailingComma*/ false); } write(")"); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 7e393c5a35a..b538b06ee49 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1500,23 +1500,26 @@ namespace ts { return isRestParameter(lastOrUndefined(s.parameters)); } - export function isRestParameter(node: ParameterDeclaration) { - if (node) { - if (node.flags & NodeFlags.JavaScriptFile) { - if (node.type && node.type.kind === SyntaxKind.JSDocVariadicType) { - return true; - } + export function hasDeclaredRestParameter(s: SignatureDeclaration): boolean { + return isDeclaredRestParam(lastOrUndefined(s.parameters)); + } - const paramTag = getCorrespondingJSDocParameterTag(node); - if (paramTag && paramTag.typeExpression) { - return paramTag.typeExpression.type.kind === SyntaxKind.JSDocVariadicType; - } + export function isRestParameter(node: ParameterDeclaration) { + if (node && (node.flags & NodeFlags.JavaScriptFile)) { + if (node.type && node.type.kind === SyntaxKind.JSDocVariadicType) { + return true; } - return node.dotDotDotToken !== undefined; + const paramTag = getCorrespondingJSDocParameterTag(node); + if (paramTag && paramTag.typeExpression) { + return paramTag.typeExpression.type.kind === SyntaxKind.JSDocVariadicType; + } } + return isDeclaredRestParam(node); + } - return false; + export function isDeclaredRestParam(node: ParameterDeclaration) { + return node && node.dotDotDotToken !== undefined; } diff --git a/tests/baselines/reference/jsFileCompilationRestParamJsDocFunction.js b/tests/baselines/reference/jsFileCompilationRestParamJsDocFunction.js index 9e5c53fbe05..e1675ff7131 100644 --- a/tests/baselines/reference/jsFileCompilationRestParamJsDocFunction.js +++ b/tests/baselines/reference/jsFileCompilationRestParamJsDocFunction.js @@ -36,11 +36,7 @@ define("_apply", ["require", "exports"], function (require, exports) { * @param {...*} args The arguments to invoke `func` with. * @returns {*} Returns the result of `func`. */ - function apply(func, thisArg) { - var args = []; - for (var _i = 2; _i < arguments.length; _i++) { - args[_i - 2] = arguments[_i]; - } + function apply(func, thisArg, args) { var length = args.length; switch (length) { case 0: return func.call(thisArg);