* Transforms an array of Expression nodes that contains a SpreadExpression. * * @param elements The array of Expression nodes. * @param isArgumentList A value indicating whether to ensure that the result is a fresh array. * This should be `false` when spreading in
(elements, isArgumentList, multiLine, hasTrailingComma)
| 101291 | * @param multiLine A value indicating whether the result should be emitted on multiple lines. |
| 101292 | */ |
| 101293 | function transformAndSpreadElements(elements, isArgumentList, multiLine, hasTrailingComma) { |
| 101294 | // When there is no leading SpreadElement: |
| 101295 | // |
| 101296 | // [source] |
| 101297 | // [a, ...b, c] |
| 101298 | // |
| 101299 | // [output (downlevelIteration)] |
| 101300 | // __spreadArray(__spreadArray([a], __read(b)), [c]) |
| 101301 | // |
| 101302 | // [output] |
| 101303 | // __spreadArray(__spreadArray([a], b), [c]) |
| 101304 | // |
| 101305 | // When there *is* a leading SpreadElement: |
| 101306 | // |
| 101307 | // [source] |
| 101308 | // [...a, b] |
| 101309 | // |
| 101310 | // [output (downlevelIteration)] |
| 101311 | // __spreadArray(__spreadArray([], __read(a)), [b]) |
| 101312 | // |
| 101313 | // [output] |
| 101314 | // __spreadArray(__spreadArray([], a), [b]) |
| 101315 | // |
| 101316 | // NOTE: We use `isPackedArrayLiteral` below rather than just `isArrayLiteral` |
| 101317 | // because ES2015 spread will replace _missing_ array elements with `undefined`, |
| 101318 | // so we cannot just use an array as is. For example: |
| 101319 | // |
| 101320 | // `[1, ...[2, , 3]]` becomes `[1, 2, undefined, 3]` |
| 101321 | // |
| 101322 | // However, for packed array literals (i.e., an array literal with no OmittedExpression |
| 101323 | // elements), we can use the array as-is. |
| 101324 | // Map spans of spread expressions into their expressions and spans of other |
| 101325 | // expressions into an array literal. |
| 101326 | var numElements = elements.length; |
| 101327 | var segments = ts.flatten( |
| 101328 | // As we visit each element, we return one of two functions to use as the "key": |
| 101329 | // - `visitSpanOfSpreads` for one or more contiguous `...` spread expressions, i.e. `...a, ...b` in `[1, 2, ...a, ...b]` |
| 101330 | // - `visitSpanOfNonSpreads` for one or more contiguous non-spread elements, i.e. `1, 2`, in `[1, 2, ...a, ...b]` |
| 101331 | ts.spanMap(elements, partitionSpread, function (partition, visitPartition, _start, end) { |
| 101332 | return visitPartition(partition, multiLine, hasTrailingComma && end === numElements); |
| 101333 | })); |
| 101334 | if (segments.length === 1) { |
| 101335 | var firstSegment = segments[0]; |
| 101336 | // If we don't need a unique copy, then we are spreading into an argument list for |
| 101337 | // a CallExpression or NewExpression. When using `--downlevelIteration`, we need |
| 101338 | // to coerce this into an array for use with `apply`, so we will use the code path |
| 101339 | // that follows instead. |
| 101340 | if (isArgumentList && !compilerOptions.downlevelIteration |
| 101341 | || ts.isPackedArrayLiteral(firstSegment.expression) // see NOTE (above) |
| 101342 | || ts.isCallToHelper(firstSegment.expression, "___spreadArray")) { |
| 101343 | return firstSegment.expression; |
| 101344 | } |
| 101345 | } |
| 101346 | var helpers = emitHelpers(); |
| 101347 | var startsWithSpread = segments[0].kind !== 0 /* SpreadSegmentKind.None */; |
| 101348 | var expression = startsWithSpread ? factory.createArrayLiteralExpression() : |
| 101349 | segments[0].expression; |
| 101350 | for (var i = startsWithSpread ? 0 : 1; i < segments.length; i++) { |
no test coverage detected
searching dependent graphs…