refactor(forms): tree-shake _checkParentType in production (#60041)

In this commit, we move `_checkParentType` to a separate function to avoid a redundant prototype method in production.

PR Close #60041
This commit is contained in:
arturovt
2025-02-20 22:25:08 +02:00
committed by kirjs
parent b9306719df
commit 27dfe272b3
4 changed files with 28 additions and 46 deletions
@@ -222,7 +222,6 @@
"_bind", "_bind",
"_currentInjector", "_currentInjector",
"_global", "_global",
"_hasInvalidParent",
"_injectImplementation", "_injectImplementation",
"_isRefreshingViews", "_isRefreshingViews",
"_keyMap", "_keyMap",
@@ -411,6 +410,7 @@
"handleUnhandledError", "handleUnhandledError",
"hasApplyArgsData", "hasApplyArgsData",
"hasInSkipHydrationBlockFlag", "hasInSkipHydrationBlockFlag",
"hasInvalidParent",
"hasParentInjector", "hasParentInjector",
"hasTagAndTypeMatch", "hasTagAndTypeMatch",
"hasValidator", "hasValidator",
+9 -14
View File
@@ -334,24 +334,11 @@ export class NgModel extends NgControl implements OnChanges, OnDestroy {
private _checkForErrors(): void { private _checkForErrors(): void {
if ((typeof ngDevMode === 'undefined' || ngDevMode) && !this._isStandalone()) { if ((typeof ngDevMode === 'undefined' || ngDevMode) && !this._isStandalone()) {
this._checkParentType(); checkParentType(this._parent);
} }
this._checkName(); this._checkName();
} }
private _checkParentType(): void {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
if (
!(this._parent instanceof NgModelGroup) &&
this._parent instanceof AbstractFormGroupDirective
) {
throw formGroupNameException();
} else if (!(this._parent instanceof NgModelGroup) && !(this._parent instanceof NgForm)) {
throw modelParentException();
}
}
}
private _checkName(): void { private _checkName(): void {
if (this.options && this.options.name) this.name = this.options.name; if (this.options && this.options.name) this.name = this.options.name;
@@ -387,3 +374,11 @@ export class NgModel extends NgControl implements OnChanges, OnDestroy {
return this._parent ? controlPath(controlName, this._parent) : [controlName]; return this._parent ? controlPath(controlName, this._parent) : [controlName];
} }
} }
function checkParentType(parent: ControlContainer | null) {
if (!(parent instanceof NgModelGroup) && parent instanceof AbstractFormGroupDirective) {
throw formGroupNameException();
} else if (!(parent instanceof NgModelGroup) && !(parent instanceof NgForm)) {
throw modelParentException();
}
}
@@ -213,28 +213,23 @@ export class FormControlName extends NgControl implements OnChanges, OnDestroy {
return this._parent ? this._parent.formDirective : null; return this._parent ? this._parent.formDirective : null;
} }
private _checkParentType(): void {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
if (
!(this._parent instanceof FormGroupName) &&
this._parent instanceof AbstractFormGroupDirective
) {
throw ngModelGroupException();
} else if (
!(this._parent instanceof FormGroupName) &&
!(this._parent instanceof FormGroupDirective) &&
!(this._parent instanceof FormArrayName)
) {
throw controlParentException(this.name);
}
}
}
private _setUpControl() { private _setUpControl() {
if (typeof ngDevMode === 'undefined' || ngDevMode) { if (typeof ngDevMode === 'undefined' || ngDevMode) {
this._checkParentType(); checkParentType(this._parent, this.name);
} }
(this as Writable<this>).control = this.formDirective.addControl(this); (this as Writable<this>).control = this.formDirective.addControl(this);
this._added = true; this._added = true;
} }
} }
function checkParentType(parent: ControlContainer | null, name: string | number | null) {
if (!(parent instanceof FormGroupName) && parent instanceof AbstractFormGroupDirective) {
throw ngModelGroupException();
} else if (
!(parent instanceof FormGroupName) &&
!(parent instanceof FormGroupDirective) &&
!(parent instanceof FormArrayName)
) {
throw controlParentException(name);
}
}
@@ -115,7 +115,7 @@ export class FormGroupName extends AbstractFormGroupDirective implements OnInit,
/** @internal */ /** @internal */
override _checkParentType(): void { override _checkParentType(): void {
if (_hasInvalidParent(this._parent) && (typeof ngDevMode === 'undefined' || ngDevMode)) { if (hasInvalidParent(this._parent) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw groupParentException(); throw groupParentException();
} }
} }
@@ -190,8 +190,8 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
* @nodoc * @nodoc
*/ */
ngOnInit(): void { ngOnInit(): void {
if (typeof ngDevMode === 'undefined' || ngDevMode) { if (hasInvalidParent(this._parent) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
this._checkParentType(); throw arrayParentException();
} }
this.formDirective!.addFormArray(this); this.formDirective!.addFormArray(this);
} }
@@ -201,9 +201,7 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
* @nodoc * @nodoc
*/ */
ngOnDestroy(): void { ngOnDestroy(): void {
if (this.formDirective) { this.formDirective?.removeFormArray(this);
this.formDirective.removeFormArray(this);
}
} }
/** /**
@@ -230,15 +228,9 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
override get path(): string[] { override get path(): string[] {
return controlPath(this.name == null ? this.name : this.name.toString(), this._parent); return controlPath(this.name == null ? this.name : this.name.toString(), this._parent);
} }
private _checkParentType(): void {
if (_hasInvalidParent(this._parent) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw arrayParentException();
}
}
} }
function _hasInvalidParent(parent: ControlContainer): boolean { function hasInvalidParent(parent: ControlContainer): boolean {
return ( return (
!(parent instanceof FormGroupName) && !(parent instanceof FormGroupName) &&
!(parent instanceof FormGroupDirective) && !(parent instanceof FormGroupDirective) &&