diff --git a/src/compiler/core.ts b/src/compiler/core.ts index d1d21e9469498..fabdfaa901ef6 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -755,15 +755,16 @@ namespace ts { /** * Appends a range of value to an array, returning the array. * - * @param to The array to which `value` is to be appended. If `to` is `undefined`, a new array - * is created if `value` was appended. - * @param from The values to append to the array. If `from` is `undefined`, nothing is - * appended. If an element of `from` is `undefined`, that element is not appended. + * @param to The array to which values are to be appended. If `to` is `undefined` and `from` is non-empty, a new array will be created. + * @param from The values to append to the array. If `from` is `undefined`, nothing is appended. + * @param sliceStart Index of the first element in `from` to append. (inclusive) + * @param sliceEnd Index after the last element in `from` to append. (exclusive) */ - export function addRange(to: T[] | undefined, from: T[] | undefined): T[] | undefined { - if (from === undefined) return to; - for (const v of from) { - to = append(to, v); + export function addRange(to: T[] | undefined, from: T[] | undefined, sliceStart = 0, sliceEnd = from && from.length): T[] | undefined { + if (from === undefined || from.length === 0) return to; + to = to || []; + for (let i = sliceStart; i < sliceEnd; i++) { + to.push(from[i]); } return to; } diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 943d011c350df..c35a1425a7bec 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -2485,8 +2485,8 @@ namespace ts { helpers } = sourceEmitNode; if (!destEmitNode) destEmitNode = {}; - if (leadingComments) destEmitNode.leadingComments = addRange(leadingComments.slice(), destEmitNode.leadingComments); - if (trailingComments) destEmitNode.trailingComments = addRange(trailingComments.slice(), destEmitNode.trailingComments); + if (leadingComments) destEmitNode.leadingComments = concatenate(leadingComments, destEmitNode.leadingComments); + if (trailingComments) destEmitNode.trailingComments = concatenate(trailingComments, destEmitNode.trailingComments); if (flags) destEmitNode.flags = flags; if (commentRange) destEmitNode.commentRange = commentRange; if (sourceMapRange) destEmitNode.sourceMapRange = sourceMapRange; diff --git a/src/compiler/transformers/esnext.ts b/src/compiler/transformers/esnext.ts index 6e2f70b5c35d3..078f4b715b787 100644 --- a/src/compiler/transformers/esnext.ts +++ b/src/compiler/transformers/esnext.ts @@ -667,7 +667,7 @@ namespace ts { const trailingStatements = endLexicalEnvironment(); if (statementOffset > 0 || some(statements) || some(trailingStatements)) { const block = convertToFunctionBody(body, /*multiLine*/ true); - addRange(statements, block.statements.slice(statementOffset)); + addRange(statements, block.statements, statementOffset); addRange(statements, trailingStatements); return updateBlock(block, setTextRange(createNodeArray(statements), block.statements)); }