* Adds statements to the body of a function-like node if it contains a rest parameter. * * @param statements The statements for the new function body. * @param node A function-like node. * @param inConstructorWithSynthesizedSuper A value indicating whether the par
(statements, node, inConstructorWithSynthesizedSuper)
| 99453 | * synthesized call to `super` |
| 99454 | */ |
| 99455 | function addRestParameterIfNeeded(statements, node, inConstructorWithSynthesizedSuper) { |
| 99456 | var prologueStatements = []; |
| 99457 | var parameter = ts.lastOrUndefined(node.parameters); |
| 99458 | if (!shouldAddRestParameter(parameter, inConstructorWithSynthesizedSuper)) { |
| 99459 | return false; |
| 99460 | } |
| 99461 | // `declarationName` is the name of the local declaration for the parameter. |
| 99462 | // TODO(rbuckton): Does this need to be parented? |
| 99463 | var declarationName = parameter.name.kind === 79 /* SyntaxKind.Identifier */ ? ts.setParent(ts.setTextRange(factory.cloneNode(parameter.name), parameter.name), parameter.name.parent) : factory.createTempVariable(/*recordTempVariable*/ undefined); |
| 99464 | ts.setEmitFlags(declarationName, 48 /* EmitFlags.NoSourceMap */); |
| 99465 | // `expressionName` is the name of the parameter used in expressions. |
| 99466 | var expressionName = parameter.name.kind === 79 /* SyntaxKind.Identifier */ ? factory.cloneNode(parameter.name) : declarationName; |
| 99467 | var restIndex = node.parameters.length - 1; |
| 99468 | var temp = factory.createLoopVariable(); |
| 99469 | // var param = []; |
| 99470 | prologueStatements.push(ts.setEmitFlags(ts.setTextRange(factory.createVariableStatement( |
| 99471 | /*modifiers*/ undefined, factory.createVariableDeclarationList([ |
| 99472 | factory.createVariableDeclaration(declarationName, |
| 99473 | /*exclamationToken*/ undefined, |
| 99474 | /*type*/ undefined, factory.createArrayLiteralExpression([])) |
| 99475 | ])), |
| 99476 | /*location*/ parameter), 1048576 /* EmitFlags.CustomPrologue */)); |
| 99477 | // for (var _i = restIndex; _i < arguments.length; _i++) { |
| 99478 | // param[_i - restIndex] = arguments[_i]; |
| 99479 | // } |
| 99480 | var forStatement = factory.createForStatement(ts.setTextRange(factory.createVariableDeclarationList([ |
| 99481 | factory.createVariableDeclaration(temp, /*exclamationToken*/ undefined, /*type*/ undefined, factory.createNumericLiteral(restIndex)) |
| 99482 | ]), parameter), ts.setTextRange(factory.createLessThan(temp, factory.createPropertyAccessExpression(factory.createIdentifier("arguments"), "length")), parameter), ts.setTextRange(factory.createPostfixIncrement(temp), parameter), factory.createBlock([ |
| 99483 | ts.startOnNewLine(ts.setTextRange(factory.createExpressionStatement(factory.createAssignment(factory.createElementAccessExpression(expressionName, restIndex === 0 |
| 99484 | ? temp |
| 99485 | : factory.createSubtract(temp, factory.createNumericLiteral(restIndex))), factory.createElementAccessExpression(factory.createIdentifier("arguments"), temp))), |
| 99486 | /*location*/ parameter)) |
| 99487 | ])); |
| 99488 | ts.setEmitFlags(forStatement, 1048576 /* EmitFlags.CustomPrologue */); |
| 99489 | ts.startOnNewLine(forStatement); |
| 99490 | prologueStatements.push(forStatement); |
| 99491 | if (parameter.name.kind !== 79 /* SyntaxKind.Identifier */) { |
| 99492 | // do the actual destructuring of the rest parameter if necessary |
| 99493 | prologueStatements.push(ts.setEmitFlags(ts.setTextRange(factory.createVariableStatement( |
| 99494 | /*modifiers*/ undefined, factory.createVariableDeclarationList(ts.flattenDestructuringBinding(parameter, visitor, context, 0 /* FlattenLevel.All */, expressionName))), parameter), 1048576 /* EmitFlags.CustomPrologue */)); |
| 99495 | } |
| 99496 | ts.insertStatementsAfterCustomPrologue(statements, prologueStatements); |
| 99497 | return true; |
| 99498 | } |
| 99499 | /** |
| 99500 | * Adds a statement to capture the `this` of a function declaration if it is needed. |
| 99501 | * NOTE: This must be executed *after* the subtree has been visited. |
no test coverage detected