Visitar URL original
refactor(compiler): enforce exhaustive defer trigger handling · angular/angular@8201ceb · GitHub
Skip to content

Commit 8201ceb

Browse files
splincodepkozlowski-opensource
authored andcommitted
refactor(compiler): enforce exhaustive defer trigger handling
Store the trigger kind before each switch and assign the value to `never` in the fallback branch. This removes the `any` casts and makes the switches exhaustive. Adding a new `DeferTriggerKind` without handling it in either phase now produces a TypeScript compilation error. Runtime behavior and error messages remain unchanged.
1 parent 9bf8b8c commit 8201ceb

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

‎packages/compiler/src/template/pipeline/src/phases/defer_resolve_targets.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ export function resolveDeferTargetNames(job: ComponentCompilationJob): void {
5252
op: ir.DeferOnOp,
5353
placeholderView: ir.XrefId | null,
5454
): void {
55-
switch (op.trigger.kind) {
55+
const triggerKind = op.trigger.kind;
56+
57+
switch (triggerKind) {
5658
case ir.DeferTriggerKind.Idle:
5759
case ir.DeferTriggerKind.Never:
5860
case ir.DeferTriggerKind.Immediate:
@@ -106,8 +108,11 @@ export function resolveDeferTargetNames(job: ComponentCompilationJob): void {
106108
step++;
107109
}
108110
break;
109-
default:
110-
throw new Error(`Trigger kind ${(op.trigger as any).kind} not handled`);
111+
default: {
112+
const unhandledTriggerKind: never = triggerKind;
113+
114+
throw new Error(`Trigger kind ${unhandledTriggerKind} not handled`);
115+
}
111116
}
112117
}
113118

‎packages/compiler/src/template/pipeline/src/phases/reify.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,9 @@ function reifyCreateOperations(unit: CompilationUnit, ops: ir.OpList<ir.CreateOp
403403
break;
404404
case ir.OpKind.DeferOn:
405405
let args: o.Expression[] = [];
406-
switch (op.trigger.kind) {
406+
const triggerKind = op.trigger.kind;
407+
408+
switch (triggerKind) {
407409
case ir.DeferTriggerKind.Never:
408410
case ir.DeferTriggerKind.Immediate:
409411
break;
@@ -447,12 +449,13 @@ function reifyCreateOperations(unit: CompilationUnit, ops: ir.OpList<ir.CreateOp
447449
}
448450
}
449451
break;
450-
default:
452+
default: {
453+
const unhandledTriggerKind: never = triggerKind;
454+
451455
throw new Error(
452-
`AssertionError: Unsupported reification of defer trigger kind ${
453-
(op.trigger as any).kind
454-
}`,
456+
`AssertionError: Unsupported reification of defer trigger kind ${unhandledTriggerKind}`,
455457
);
458+
}
456459
}
457460
ir.OpList.replace(op, ng.deferOn(op.trigger.kind, args, op.modifier, op.sourceSpan));
458461
break;

0 commit comments

Comments
 (0)