Skip to content
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

feat(core): Add schematic to migrate control flow syntax #52035

Closed
wants to merge 1 commit into from

Conversation

jessicajaniuk
Copy link
Contributor

This adds the migration to run to migrate to the block control flow syntax. It includes ngIf, ngFor, and ngSwitch.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Feature

Does this PR introduce a breaking change?

  • No

@angular-robot angular-robot bot added the detected: feature PR contains a feature commit label Oct 4, 2023
@jessicajaniuk jessicajaniuk changed the title feat(core): Add schematic to migrate control flow syntax feat(core): Add schematic to migrate control flow syntax (WIP) Oct 4, 2023
@jessicajaniuk jessicajaniuk added the area: core Issues related to the framework runtime label Oct 4, 2023
@ngbot ngbot bot added this to the Backlog milestone Oct 4, 2023
@jessicajaniuk jessicajaniuk added target: major This PR is targeted for the next major release state: WIP labels Oct 4, 2023
@jessicajaniuk jessicajaniuk force-pushed the cf-migration branch 3 times, most recently from 8a0d7d7 to 8d6209c Compare October 5, 2023 15:52
@jessicajaniuk jessicajaniuk modified the milestones: Backlog, v17-candidates Oct 5, 2023
@jessicajaniuk jessicajaniuk force-pushed the cf-migration branch 4 times, most recently from fd31a93 to 7a6e286 Compare October 6, 2023 21:14
@jessicajaniuk jessicajaniuk changed the title feat(core): Add schematic to migrate control flow syntax (WIP) feat(core): Add schematic to migrate control flow syntax Oct 6, 2023
@jessicajaniuk jessicajaniuk marked this pull request as ready for review October 6, 2023 21:29
@pullapprove pullapprove bot requested a review from crisbeto October 6, 2023 21:29
@jessicajaniuk jessicajaniuk added action: review The PR is still awaiting reviews from at least one requested reviewer and removed state: WIP labels Oct 6, 2023
Copy link
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.

Looks great! I left a handful of nits, but they're not blockers.

}
let offset = 0;

for (const el of visitor.elements) {
Copy link
Member

Choose a reason for hiding this comment

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

It might be simpler to have the visitor do the matching since it's already checking each element. We can also distribute the elements into different "buckets" based on which directive is applied. E.g.

class ElementCollector {
  ifNodes = new Set<Node>();
  switchNodes = new Set<Node>();
  forNode = new Set<Node>();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I started trying this, but found it actually complicates things a bit more. We still need the attribute info for later and tracking the offset becomes a bit noisier since we have to traverse each type separately.

function getTmplStart(tmpl: string, elStart: number, templates: Template[]) {
let tmplStart = tmpl.slice(0, elStart);
for (const template of templates) {
if (template.count === 2 && template.el.sourceSpan.start.offset < elStart) {
Copy link
Member

Choose a reason for hiding this comment

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

I just realized that there's one case where this falls apart: a template that is referenced in a query. IMO we shouldn't handle it unless we get user reports about it, because it's highly unlikely that a template is used both as an else block and is queried for.


writeFile('/comp.html', [
`<div>`,
`<span *ngIf="show; then thenBlock; else elseBlock">Ignored</span>`,
Copy link
Member

Choose a reason for hiding this comment

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

TIL that we even support the then input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I had a similar experience when writing this migration.

@jessicajaniuk jessicajaniuk force-pushed the cf-migration branch 2 times, most recently from 0755ef1 to 896437b Compare October 9, 2023 22:04
Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

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

Just left a proposal to add test coverage for a few cases, but this is not critical (non-blocking), we can do it in a followup PR if needed.

Comment on lines +610 to +611
`<p *ngSwitchCase="1">Option 1</p>` +
`<p *ngSwitchCase="2">Option 2</p>` +
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it'd be great to cover the following cases (both are present in g3):

  • <ng-template [ngSwitchCase]="..." ...
  • <ng-template ngSwitchCase="..." ...

Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, other directives (ngIf, ngSwitch, ngSwitchDefault and ngFor) may have the same forms when added to <ng-template>.

Copy link
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.

LGTM

@jessicajaniuk jessicajaniuk added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer action: merge The PR is ready for merge by the caretaker labels Oct 10, 2023
@jessicajaniuk jessicajaniuk added the action: merge The PR is ready for merge by the caretaker label Oct 10, 2023
@jessicajaniuk jessicajaniuk force-pushed the cf-migration branch 3 times, most recently from 5c5e01a to ec67f8a Compare October 10, 2023 17:10
This adds the migration to run to migrate to the block control flow syntax. It includes ngIf, ngFor, and ngSwitch.
@jessicajaniuk jessicajaniuk added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Oct 10, 2023
@jessicajaniuk
Copy link
Contributor Author

Caretaker Note: This is safe to merge. The G3 failures are unrelated.

@angular-robot angular-robot bot closed this in 50275e5 Oct 10, 2023
@jessicajaniuk jessicajaniuk deleted the cf-migration branch November 9, 2023 20:34
@angular-automatic-lock-bot
Copy link

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 Dec 10, 2023
ChellappanRajan pushed a commit to ChellappanRajan/angular that referenced this pull request Jan 23, 2024
This adds the migration to run to migrate to the block control flow syntax. It includes ngIf, ngFor, and ngSwitch.

PR Close angular#52035
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 area: core Issues related to the framework runtime detected: feature PR contains a feature commit merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants