Skip to content

In addRange, don't check for undefined values; do support slice #16161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(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<T>(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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/transformers/esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down