prefer-control-flow
What it does
Section titled “What it does”Reports a *ngIf or *ngFor attribute and a [ngSwitch] binding: the directives that the @if, @for and @switch blocks of Angular 17 replace. The *ngSwitchCase and *ngSwitchDefault inside a switch are not reported again.
Why it matters
Section titled “Why it matters”The built-in blocks are part of the template syntax, so they need no import of CommonModule or of the directive, type-check their conditions more strictly and run without the directive’s runtime code. @for also requires a track expression, which *ngFor leaves to an optional trackBy.
To migrate, run ng generate @angular/core:control-flow.
Example
Section titled “Example”...<dialog><input autofocus /></dialog>...<p *ngIf="a">x</p><p *ngFor="let x of xs">x</p><div [ngSwitch]="a"><i *ngSwitchCase="1"></i></div>...<th scope="row"></th>...Configuration
Section titled “Configuration”angular: lint: rules: prefer-control-flow: warn # off (default), warn, errorhtml: lint: rules: prefer-control-flow: warn # off (default), warn, errorsvelte: lint: rules: prefer-control-flow: warn # off (default), warn, errorvue: lint: rules: prefer-control-flow: warn # off (default), warn, errorEnabled by the presets: html/angular-eslint-recommended.
Suppress one line with sudsy-disable-next-line prefer-control-flow in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| angular-eslint | prefer-control-flow |
full |