Angular template scanner misses reads of component inputs and outputs in several syntaxes
What happened?
unused-component-inputs and unused-component-outputs report members that the component's own template reads or emits. Each case below is credited when written in the simpler form shown in brackets.
| Template | Flagged | Credited when written as |
|---|---|---|
{{ 'abc' | slice: pipeArg() }} |
pipeArg (pipe argument) |
{{ pipeArg() | slice }} |
<div title="{{ attrInterp() }}"> |
attrInterp |
[title]="attrInterp()" |
<div class="base {{ classInterp() }}"> |
classInterp |
[class]="classInterp()" |
*ngTemplateOutlet="tpl(); context: outletCtx()" |
tpl and outletCtx |
*ngFor="let v of xs; trackBy: f" is credited |
@else if (elseIfCond(); as c) { … } |
elseIfCond |
@if (cond(); as c) |
[style.width.%]="widthPct()" |
widthPct |
[style.width.px] or [style.width] |
<p>{{ shadowed() }}</p> plus @for (shadowed of items(); …) elsewhere in the same template |
shadowed |
(any other loop variable name) |
(click)="this.viaThis.emit()" |
viaThis |
(click)="viaThis.emit()" |
An ICU case whose opening brace runs into an interpolation, e.g. {n, plural, =1 {one} other {{{n}} many} } |
Every input read and output emitted after the ICU in the template | other {{{ n }} many} or other {x {{n}} many} |
The ICU case does the most damage. One such message silently uncredits everything after it in the file, and {{{n}} is a common way to write plural strings.
Reproduction
package.json:
{ "name": "repro", "private": true, "dependencies": { "@angular/core": "20.0.0" } }
src/main.ts:
import { Outputs } from './outputs.component';
import { Inputs } from './inputs.component';
console.log(Outputs, Inputs);
src/outputs.component.ts:
import { Component, output } from '@angular/core';
@Component({ selector: 'x-outputs', templateUrl: './outputs.component.html' })
export class Outputs {
readonly beforeIcu = output();
readonly afterIcu = output();
readonly viaThis = output();
n = 1;
}
src/outputs.component.html:
<button (click)="beforeIcu.emit()">before</button>
<p>{n, plural, =1 {one} other {{{n}} many} }</p>
<button (click)="afterIcu.emit()">after</button>
<button (click)="this.viaThis.emit()">this</button>
src/inputs.component.ts:
import { Component, input } from '@angular/core';
@Component({ selector: 'x-inputs', templateUrl: './inputs.component.html' })
export class Inputs {
readonly pipeArg = input(0);
readonly attrInterp = input('');
readonly classInterp = input('');
readonly tpl = input<any>(null);
readonly outletCtx = input<object>({});
readonly elseIfCond = input(false);
readonly widthPct = input(0);
readonly shadowed = input('');
readonly items = input<string[]>([]);
n = 1;
}
src/inputs.component.html:
<span>{{ 'abc' | slice: pipeArg() }}</span>
<div title="{{ attrInterp() }}"></div>
<div class="base {{ classInterp() }}"></div>
<ng-container *ngTemplateOutlet="tpl(); context: outletCtx()"></ng-container>
@if (n > 1) { <b>a</b> } @else if (elseIfCond(); as c) { <b>{{ c }}</b> }
<div [style.width.%]="widthPct()"></div>
<p>{{ shadowed() }}</p>
@for (shadowed of items(); track shadowed) { <i>{{ shadowed }}</i> }
Run fallow dead-code --unused-component-inputs --unused-component-outputs.
- Reported inputs:
pipeArg,attrInterp,classInterp,tpl,outletCtx,elseIfCond,widthPct,shadowed. - Reported outputs:
afterIcu,viaThis.beforeIcuis credited.
Expected behavior
Every input above is read and every output is emitted by the component's own template, so none should be reported.
Fallow version
3.36.0
Operating system
macOS
Context
In a large Angular codebase, these patterns account for about 330 of ~2,000 unused-component-inputs findings and about 30 of 380 unused-component-outputs findings. Breakdown:
| Pattern | Approx. findings |
|---|---|
| Pipe arguments | ~130 |
| Attribute interpolation | ~85 |
| ICU messages | ~85, across both rules |
this. in templates |
~6 |
Microsyntax with ; |
~11 |
This blocks gating either rule in CI, because routine template code produces false positives.
v3.43.2 has this fix: npm install [email protected].
The template scanner now reads pipe arguments, interpolation in plain attributes, keyed structural directive clauses, @else if conditions, [style.width.%] bindings, explicit this. and this!. reads, ICU message heads, and host bindings in backticks. An ICU case that opens into an interpolation no longer stops the scan, and a member with the same name as a @for item is no longer hidden. The inputs and outputs in your example are no longer reported as unused.
Thank you for the detailed examples.