Skip to content

In createNodeArray, create a fresh array (2) #28441

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
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
12 changes: 8 additions & 4 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ namespace ts {
* Make `elements` into a `NodeArray<T>`. If `elements` is `undefined`, returns an empty `NodeArray<T>`.
*/
export function createNodeArray<T extends Node>(elements?: ReadonlyArray<T>, hasTrailingComma?: boolean): NodeArray<T> {
if (!elements || elements === emptyArray) {
elements = [];
if (elements) {
if (isNodeArray(elements)) {
return elements;
}
// elements is a ReadonlyArray, so create a fresh array before adding properties.
elements = elements.slice();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a scenario that breaks because of this? I think from below code looks like the intention of the code is to make elements array into node array?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is generally the purpose of this function, yes. elements was not always a ReadonlyArray, but now that it is it makes sense to make a fresh copy. We should verify the performance/memory characteristics of this change, however.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, mutating the array this way does not actually change its "ReadonlyArray"-ness, just promotes it to a Node Array (which is also a ReadonlyArray).

}
else if (isNodeArray(elements)) {
return elements;
else {
elements = [];
}

const array = <NodeArray<T>>elements;
Expand Down