Skip to content

Commit 6f24144

Browse files
committed
Do not transform the emit of function with rest parameter unless declared in AST
Fixes #7749
1 parent ef4b661 commit 6f24144

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13811,7 +13811,7 @@ namespace ts {
1381113811

1381213812
function checkCollisionWithArgumentsInGeneratedCode(node: SignatureDeclaration) {
1381313813
// no rest parameters \ declaration context \ overload - no codegen impact
13814-
if (!hasRestParameter(node) || isInAmbientContext(node) || nodeIsMissing((<FunctionLikeDeclaration>node).body)) {
13814+
if (!hasDeclaredRestParameter(node) || isInAmbientContext(node) || nodeIsMissing((<FunctionLikeDeclaration>node).body)) {
1381513815
return;
1381613816
}
1381713817

src/compiler/emitter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4487,7 +4487,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
44874487
}
44884488

44894489
function emitRestParameter(node: FunctionLikeDeclaration) {
4490-
if (languageVersion < ScriptTarget.ES6 && hasRestParameter(node)) {
4490+
if (languageVersion < ScriptTarget.ES6 && hasDeclaredRestParameter(node)) {
44914491
const restIndex = node.parameters.length - 1;
44924492
const restParam = node.parameters[restIndex];
44934493

@@ -4644,7 +4644,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
46444644
if (node) {
46454645
const parameters = node.parameters;
46464646
const skipCount = node.parameters.length && (<Identifier>node.parameters[0].name).text === "this" ? 1 : 0;
4647-
const omitCount = languageVersion < ScriptTarget.ES6 && hasRestParameter(node) ? 1 : 0;
4647+
const omitCount = languageVersion < ScriptTarget.ES6 && hasDeclaredRestParameter(node) ? 1 : 0;
46484648
emitList(parameters, skipCount, parameters.length - omitCount - skipCount, /*multiLine*/ false, /*trailingComma*/ false);
46494649
}
46504650
write(")");

src/compiler/utilities.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,23 +1377,26 @@ namespace ts {
13771377
return isRestParameter(lastOrUndefined(s.parameters));
13781378
}
13791379

1380-
export function isRestParameter(node: ParameterDeclaration) {
1381-
if (node) {
1382-
if (node.flags & NodeFlags.JavaScriptFile) {
1383-
if (node.type && node.type.kind === SyntaxKind.JSDocVariadicType) {
1384-
return true;
1385-
}
1380+
export function hasDeclaredRestParameter(s: SignatureDeclaration): boolean {
1381+
return isDeclaredRestParam(lastOrUndefined(s.parameters));
1382+
}
13861383

1387-
const paramTag = getCorrespondingJSDocParameterTag(node);
1388-
if (paramTag && paramTag.typeExpression) {
1389-
return paramTag.typeExpression.type.kind === SyntaxKind.JSDocVariadicType;
1390-
}
1384+
export function isRestParameter(node: ParameterDeclaration) {
1385+
if (node && (node.flags & NodeFlags.JavaScriptFile)) {
1386+
if (node.type && node.type.kind === SyntaxKind.JSDocVariadicType) {
1387+
return true;
13911388
}
13921389

1393-
return node.dotDotDotToken !== undefined;
1390+
const paramTag = getCorrespondingJSDocParameterTag(node);
1391+
if (paramTag && paramTag.typeExpression) {
1392+
return paramTag.typeExpression.type.kind === SyntaxKind.JSDocVariadicType;
1393+
}
13941394
}
1395+
return isDeclaredRestParam(node);
1396+
}
13951397

1396-
return false;
1398+
export function isDeclaredRestParam(node: ParameterDeclaration) {
1399+
return node && node.dotDotDotToken !== undefined;
13971400
}
13981401

13991402
export function isLiteralKind(kind: SyntaxKind): boolean {

tests/baselines/reference/jsFileCompilationRestParamJsDocFunction.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ define("_apply", ["require", "exports"], function (require, exports) {
3636
* @param {...*} args The arguments to invoke `func` with.
3737
* @returns {*} Returns the result of `func`.
3838
*/
39-
function apply(func, thisArg) {
40-
var args = [];
41-
for (var _i = 2; _i < arguments.length; _i++) {
42-
args[_i - 2] = arguments[_i];
43-
}
39+
function apply(func, thisArg, args) {
4440
var length = args.length;
4541
switch (length) {
4642
case 0: return func.call(thisArg);

0 commit comments

Comments
 (0)