Skip to content

feat(ngcc): support __spreadArray helper as used by TypeScript 4.2#41201

Closed
JoostK wants to merge 3 commits intoangular:masterfrom
JoostK:ngcc/ts42-spread-array
Closed

feat(ngcc): support __spreadArray helper as used by TypeScript 4.2#41201
JoostK wants to merge 3 commits intoangular:masterfrom
JoostK:ngcc/ts42-spread-array

Conversation

@JoostK
Copy link
Copy Markdown
Member

@JoostK JoostK commented Mar 13, 2021

In TypeScript 4.2 the __spread and __spreadArrays helpers were both
replaced by the new helper function __spreadArray in
microsoft/TypeScript#41523. These helpers may be used in downleveled
JavaScript bundles that ngcc has to process, so ngcc has the ability to
statically detect these helpers and provide evaluation logic for them.
Because Angular is adopting support for TypeScript 4.2 it becomes
possible for libraries to be compiled by TypeScript 4.2 and thus ngcc
has to add support for the __spreadArray helper. The deprecated
__spread and __spreadArrays helpers are not affected by this change.

Closes #40394

In TypeScript 4.2 the `__spread` and `__spreadArrays` helpers were both
replaced by the new helper function `__spreadArray` in
microsoft/TypeScript#41523. These helpers may be used in downleveled
JavaScript bundles that ngcc has to process, so ngcc has the ability to
statically detect these helpers and provide evaluation logic for them.
Because Angular is adopting support for TypeScript 4.2 it becomes
possible for libraries to be compiled by TypeScript 4.2 and thus ngcc
has to add support for the `__spreadArray` helper. The deprecated
`__spread` and `__spreadArrays` helpers are not affected by this change.

Closes angular#40394
@JoostK JoostK added feature Label used to distinguish feature request from other issues comp: ngcc labels Mar 13, 2021
@google-cla google-cla bot added the cla: yes label Mar 13, 2021
@ngbot ngbot bot added this to the Backlog milestone Mar 13, 2021
Copy link
Copy Markdown
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

Ahead of the game @JoostK

return DynamicValue.fromInvalidExpressionType(node, from);
}

return from.concat(to);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oops, I spoke too soon. This should be

Suggested change
return from.concat(to);
return to.concat(from);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That is entirely correct! I didn't notice as the evaluator spec are part of ngtsc/partial_evaluator and I only ran the ngcc test targets.

Copy link
Copy Markdown
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

I don't have a lot of context here, but do we also have to account for the __read helper? It seems to be used together with __spreadArray. See https://github.com/angular/angular/pull/41158/files#diff-e5db61a4fea6171227d1b25300c43bc2f33613fefb1d98bb37ad746264c3661bR3006

@JoostK
Copy link
Copy Markdown
Member Author

JoostK commented Mar 14, 2021

I don't have a lot of context here, but do we also have to account for the __read helper? It seems to be used together with __spreadArray. See https://github.com/angular/angular/pull/41158/files#diff-e5db61a4fea6171227d1b25300c43bc2f33613fefb1d98bb37ad746264c3661bR3006

Very good point, yes we do! The __read helper is emitted if downlevelIteration is set, which is quite likely for libraries (ng-packagr has that on by default). I'll add another commit to add support for __read

JoostK added 2 commits March 14, 2021 14:05
This commit complements the support for the `__spreadArray` helper that
was added in microsoft/TypeScript#41523. The prior helpers `__spread`
and `__spreadArrays` used the `__read` helper internally, but the helper
is now emitted as an argument to `__spreadArray` so ngcc now needs to
support evaluating it statically. The real implementation of `__read`
reads an iterable into an array, but for ngcc's static evaluation
support it is sufficient to only deal with arrays as is. Additionally,
the optional `n` parameter is not supported as that is only emitted for
array destructuring syntax, which ngcc does not have to support.
@JoostK JoostK requested a review from petebacondarwin March 14, 2021 14:20
@JoostK JoostK marked this pull request as ready for review March 15, 2021 17:16
@JoostK JoostK added action: review The PR is still awaiting reviews from at least one requested reviewer target: major This PR is targeted for the next major release labels Mar 15, 2021
@petebacondarwin
Copy link
Copy Markdown
Contributor

but for ngcc's static evaluation support it is sufficient to only deal with arrays as is

Is it not possible for a developer to use an iterator in a spread expression?

Copy link
Copy Markdown
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

I would like to know why the implementation of __read doesn't require iterator support. But that could be added on as a subsequent PR if it is found to be necessary.

@JoostK
Copy link
Copy Markdown
Member Author

JoostK commented Mar 16, 2021

but for ngcc's static evaluation support it is sufficient to only deal with arrays as is

Is it not possible for a developer to use an iterator in a spread expression?

AFAIK this would not be statically evaluable, i.e. instantiating a Set needs the new operator which is not supported, and generator functions nor the iterator protocol is implemented by ngtsc's evaluator.

@JoostK JoostK added action: merge The PR is ready for merge by the caretaker action: presubmit The PR is in need of a google3 presubmit and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Mar 16, 2021
@AndrewKushnir
Copy link
Copy Markdown
Contributor

Presubmit.

@AndrewKushnir AndrewKushnir removed the action: presubmit The PR is in need of a google3 presubmit label Mar 16, 2021
jessicajaniuk pushed a commit that referenced this pull request Mar 16, 2021
This commit complements the support for the `__spreadArray` helper that
was added in microsoft/TypeScript#41523. The prior helpers `__spread`
and `__spreadArrays` used the `__read` helper internally, but the helper
is now emitted as an argument to `__spreadArray` so ngcc now needs to
support evaluating it statically. The real implementation of `__read`
reads an iterable into an array, but for ngcc's static evaluation
support it is sufficient to only deal with arrays as is. Additionally,
the optional `n` parameter is not supported as that is only emitted for
array destructuring syntax, which ngcc does not have to support.

PR Close #41201
@angular-automatic-lock-bot
Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker cla: yes feature Label used to distinguish feature request from other issues target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ngcc - support the new spread helper in TS 4.2

4 participants