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
Conversation
8a0d7d7
to
8d6209c
Compare
fd31a93
to
7a6e286
Compare
There was a problem hiding this 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.
packages/core/schematics/ng-generate/control-flow-migration/README.md
Outdated
Show resolved
Hide resolved
packages/core/schematics/ng-generate/control-flow-migration/README.md
Outdated
Show resolved
Hide resolved
} | ||
let offset = 0; | ||
|
||
for (const el of visitor.elements) { |
There was a problem hiding this comment.
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>();
}
There was a problem hiding this comment.
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.
packages/core/schematics/ng-generate/control-flow-migration/util.ts
Outdated
Show resolved
Hide resolved
packages/core/schematics/ng-generate/control-flow-migration/util.ts
Outdated
Show resolved
Hide resolved
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) { |
There was a problem hiding this comment.
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.
packages/core/schematics/ng-generate/control-flow-migration/util.ts
Outdated
Show resolved
Hide resolved
|
||
writeFile('/comp.html', [ | ||
`<div>`, | ||
`<span *ngIf="show; then thenBlock; else elseBlock">Ignored</span>`, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
0755ef1
to
896437b
Compare
There was a problem hiding this 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.
`<p *ngSwitchCase="1">Option 1</p>` + | ||
`<p *ngSwitchCase="2">Option 2</p>` + |
There was a problem hiding this comment.
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="..." ...
There was a problem hiding this comment.
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>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
896437b
to
c4b11aa
Compare
5c5e01a
to
ec67f8a
Compare
This adds the migration to run to migrate to the block control flow syntax. It includes ngIf, ngFor, and ngSwitch.
ec67f8a
to
3851e8f
Compare
Caretaker Note: This is safe to merge. The G3 failures are unrelated. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This adds the migration to run to migrate to the block control flow syntax. It includes ngIf, ngFor, and ngSwitch. PR Close angular#52035
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?
Does this PR introduce a breaking change?