Skip to content

Control flow migrations produces broken code when ngSwitchCase is used as text attribute #56030

@P4

Description

@P4

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

When ngSwitchCase is used without * or [], the @angular/core:control-flow schematic produces code that doesn't compile:

<ng-container [ngSwitch]="value">
  <ng-template ngSwitchCase="">empty string</ng-template>
  <ng-template ngSwitchCase="foo">"foo"</ng-template>
</ng-container>

gets converted to

@switch (value) {
  @case () {
    empty string
  }
  @case (foo) {
    "foo"
  }
}

when it should be

@switch (value) {
  @case ('') {
    empty string
  }
  @case ('foo') {
    "foo"
  }
}

Please provide a link to a minimal reproduction of the bug

https://github.com/P4/ng-control-flow-migration/blob/master/src/app/app.component.ts

Please provide the exception or error you saw

Application bundle generation failed. [2.297 seconds]

✘ [ERROR] NG5002: @case block must have exactly one parameter [plugin angular-compiler]

    src/app/app.component.ts:10:2:
      10 │   @case () {
         ╵   ~~~~~~~~~~


✘ [ERROR] NG9: Property 'foo' does not exist on type 'AppComponent'. [plugin angular-compiler]

    src/app/app.component.ts:13:9:
      13 │   @case (foo) {
         ╵          ~~~

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 18.0.0
Node: 20.9.0
Package Manager: npm 10.1.0
OS: linux x64

Angular: 18.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.0
@angular-devkit/build-angular   18.0.0
@angular-devkit/core            18.0.0
@angular-devkit/schematics      18.0.0
@schematics/angular             18.0.0
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.6

Anything else?

We encountered this when migrating a real project at work, there's a more practical example in the linked repo based on the real code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: migrationsIssues related to `ng update`/`ng generate` migrationscore: control flowIssues related to the built-in control flow (@if, @for, @switch)

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions