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",
"_currentInjector",
"_global",
"_hasInvalidParent",
"_injectImplementation",
"_isRefreshingViews",
"_keyMap",
@@ -411,6 +410,7 @@
"handleUnhandledError",
"hasApplyArgsData",
"hasInSkipHydrationBlockFlag",
"hasInvalidParent",
"hasParentInjector",
"hasTagAndTypeMatch",
"hasValidator",
+9 -14
View File
@@ -334,24 +334,11 @@ export class NgModel extends NgControl implements OnChanges, OnDestroy {
private _checkForErrors(): void {
if ((typeof ngDevMode === 'undefined' || ngDevMode) && !this._isStandalone()) {
this._checkParentType();
checkParentType(this._parent);
}
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 {
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];
}
}
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;
}
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() {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
this._checkParentType();
checkParentType(this._parent, this.name);
}
(this as Writable<this>).control = this.formDirective.addControl(this);
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 */
override _checkParentType(): void {
if (_hasInvalidParent(this._parent) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
if (hasInvalidParent(this._parent) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw groupParentException();
}
}
@@ -190,8 +190,8 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
* @nodoc
*/
ngOnInit(): void {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
this._checkParentType();
if (hasInvalidParent(this._parent) && (typeof ngDevMode === 'undefined' || ngDevMode)) {
throw arrayParentException();
}
this.formDirective!.addFormArray(this);
}
@@ -201,9 +201,7 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
* @nodoc
*/
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[] {
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 (
!(parent instanceof FormGroupName) &&
!(parent instanceof FormGroupDirective) &&