From 0f3fa5a2af57a59d001cc4df3cd263dd27042293 Mon Sep 17 00:00:00 2001 From: arturovt Date: Fri, 24 Jan 2025 17:25:32 +0200 Subject: [PATCH] refactor(common): drop `ngIf` assert template error in production (#59705) This commit removes the `assertTemplate` error message in production. PR Close #59705 --- goldens/public-api/common/errors.api.md | 2 ++ packages/common/src/directives/ng_if.ts | 21 +++++++++++++++------ packages/common/src/errors.ts | 3 +++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/goldens/public-api/common/errors.api.md b/goldens/public-api/common/errors.api.md index b60df1fa20c..35d7f23f208 100644 --- a/goldens/public-api/common/errors.api.md +++ b/goldens/public-api/common/errors.api.md @@ -25,6 +25,8 @@ export const enum RuntimeErrorCode { // (undocumented) NG_FOR_MISSING_DIFFER = -2200, // (undocumented) + NG_IF_NOT_A_TEMPLATE_REF = 2020, + // (undocumented) OVERSIZED_IMAGE = 2960, // (undocumented) OVERSIZED_PLACEHOLDER = 2965, diff --git a/packages/common/src/directives/ng_if.ts b/packages/common/src/directives/ng_if.ts index 793d3d20920..d90e575923b 100644 --- a/packages/common/src/directives/ng_if.ts +++ b/packages/common/src/directives/ng_if.ts @@ -13,8 +13,11 @@ import { TemplateRef, ViewContainerRef, ɵstringify as stringify, + ɵRuntimeError as RuntimeError, } from '@angular/core'; +import {RuntimeErrorCode} from '../errors'; + /** * A structural directive that conditionally includes a template based on the value of * an expression coerced to Boolean. @@ -185,7 +188,7 @@ export class NgIf { */ @Input() set ngIfThen(templateRef: TemplateRef> | null) { - assertTemplate('ngIfThen', templateRef); + assertTemplate(templateRef, (typeof ngDevMode === 'undefined' || ngDevMode) && 'ngIfThen'); this._thenTemplateRef = templateRef; this._thenViewRef = null; // clear previous view if any. this._updateView(); @@ -196,7 +199,7 @@ export class NgIf { */ @Input() set ngIfElse(templateRef: TemplateRef> | null) { - assertTemplate('ngIfElse', templateRef); + assertTemplate(templateRef, (typeof ngDevMode === 'undefined' || ngDevMode) && 'ngIfElse'); this._elseTemplateRef = templateRef; this._elseViewRef = null; // clear previous view if any. this._updateView(); @@ -263,9 +266,15 @@ export class NgIfContext { public ngIf: T = null!; } -function assertTemplate(property: string, templateRef: TemplateRef | null): void { - const isTemplateRefOrNull = !!(!templateRef || templateRef.createEmbeddedView); - if (!isTemplateRefOrNull) { - throw new Error(`${property} must be a TemplateRef, but received '${stringify(templateRef)}'.`); +function assertTemplate( + templateRef: TemplateRef | null, + property: string | false | null, +): void { + if (templateRef && !templateRef.createEmbeddedView) { + throw new RuntimeError( + RuntimeErrorCode.NG_IF_NOT_A_TEMPLATE_REF, + (typeof ngDevMode === 'undefined' || ngDevMode) && + `${property} must be a TemplateRef, but received '${stringify(templateRef)}'.`, + ); } } diff --git a/packages/common/src/errors.ts b/packages/common/src/errors.ts index 3f5bf976059..bd9c03f5021 100644 --- a/packages/common/src/errors.ts +++ b/packages/common/src/errors.ts @@ -15,6 +15,9 @@ export const enum RuntimeErrorCode { PARENT_NG_SWITCH_NOT_FOUND = 2000, EQUALITY_NG_SWITCH_DIFFERENCE = 2001, + // NgIf errors + NG_IF_NOT_A_TEMPLATE_REF = 2020, + // Pipe errors INVALID_PIPE_ARGUMENT = 2100,