Skip to content

fix(migrations): correctly detect then/else keywords in control f…#69918

Open
surajy93 wants to merge 1 commit into
angular:mainfrom
surajy93:fix-control-flow-migration-then-else-prefix
Open

fix(migrations): correctly detect then/else keywords in control f…#69918
surajy93 wants to merge 1 commit into
angular:mainfrom
surajy93:fix-control-flow-migration-then-else-prefix

Conversation

@surajy93

@surajy93 surajy93 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

…low migration

The control flow migration determines whether an *ngIf uses a then and/or else clause by regex matching the raw microsyntax string for the literal keywords then/else. The regexes only checked that the keyword was preceded by a non-word character, but not that it was followed by one.

As a result, a template reference name that merely starts with then (e.g. else thenBlock) or else was misidentified as the then/else keyword itself. This caused the migration to take the wrong code path (e.g. then+else instead of else-only), which in turn made getTemplateName() compute a slice(start, end) with start > end, producing an empty template name. That empty placeholder was never resolved and was silently emitted as an invalid
<ng-template [ngTemplateOutlet]=""></ng-template>, dropping the original template content without any warning.

Add a negative lookahead (?![\w\d]) to both regexes so then/else are only matched as whole keywords, not as a prefix of a longer template reference name.

Fixes #69914

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@pullapprove
pullapprove Bot requested a review from JeanMeche July 23, 2026 18:52
@angular-robot angular-robot Bot added the area: migrations Issues related to `ng update`/`ng generate` migrations label Jul 23, 2026
@ngbot ngbot Bot added this to the Backlog milestone Jul 23, 2026
@surajy93
surajy93 force-pushed the fix-control-flow-migration-then-else-prefix branch from 7a398ff to deea4ca Compare July 23, 2026 18:57

@JeanMeche JeanMeche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

AGENT: I have left a suggestion for your consideration regarding template reference variables that include $.

Comment thread packages/core/schematics/ng-generate/control-flow-migration/ifs.ts Outdated
@surajy93
surajy93 force-pushed the fix-control-flow-migration-then-else-prefix branch from deea4ca to 7a464ec Compare July 23, 2026 21:14
…low migration

The control flow migration determines whether an `*ngIf` uses a `then`
and/or `else` clause by regex matching the raw microsyntax string for
the literal keywords `then`/`else`. The regexes only checked that the
keyword was preceded by a non-word character, but not that it was
followed by one.

As a result, a template reference name that merely starts with `then`
(e.g. `else thenBlock`) or `else` was misidentified as the `then`/`else`
keyword itself. This caused the migration to take the wrong code path
(e.g. then+else instead of else-only), which in turn made
`getTemplateName()` compute a `slice(start, end)` with `start > end`,
producing an empty template name. That empty placeholder was never
resolved and was silently emitted as an invalid
`<ng-template [ngTemplateOutlet]=""></ng-template>`, dropping the
original template content without any warning.

Add a negative lookahead `(?![\w\d])` to both regexes so `then`/`else`
are only matched as whole keywords, not as a prefix of a longer
template reference name.

Fixes angular#69914
@surajy93
surajy93 force-pushed the fix-control-flow-migration-then-else-prefix branch from 7a464ec to a255135 Compare July 23, 2026 21:30
@surajy93
surajy93 requested a review from JeanMeche July 23, 2026 21:33
@JeanMeche JeanMeche added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: migrations Issues related to `ng update`/`ng generate` migrations target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

control-flow migration misparses else template references starting with then, causing content loss

2 participants