Skip to content

Commit

Permalink
perf(forms): avoid direct references to the Validators class (#41189)
Browse files Browse the repository at this point in the history
Currently the `Validators` class contains a number of static methods that represent different validators as well as some helper methods. Since class methods are not tree-shakable, any reference to the `Validator` class retains all of its methods (even if you've used just one).

This commit refactors the code to extract the logic into standalone functions and use these functions in the code instead of referencing them via `Validators` class. That should make the code more tree-shakable. The `Validators` class still retains its structure and calls these standalone methods internally to keep this change backwards-compatible.

PR Close #41189
  • Loading branch information
AndrewKushnir authored and thePunderWoman committed Mar 15, 2021
1 parent 1644d64 commit 3bd1992
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,12 @@
{
"name": "collectStylingFromTAttrs"
},
{
"name": "compose"
},
{
"name": "composeAsync"
},
{
"name": "composeAsyncValidators"
},
Expand Down Expand Up @@ -1343,6 +1349,9 @@
{
"name": "notFoundValueOrThrow"
},
{
"name": "nullValidator"
},
{
"name": "observable"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@
{
"name": "DomSharedStylesHost"
},
{
"name": "EMAIL_REGEXP"
},
{
"name": "EMPTY_ARRAY"
},
Expand Down Expand Up @@ -563,9 +560,6 @@
{
"name": "VE_ViewContainerRef"
},
{
"name": "Validators"
},
{
"name": "Version"
},
Expand Down Expand Up @@ -1046,9 +1040,6 @@
{
"name": "hasTagAndTypeMatch"
},
{
"name": "hasValidLength"
},
{
"name": "hostReportError"
},
Expand Down Expand Up @@ -1130,9 +1121,6 @@
{
"name": "isDirectiveHost"
},
{
"name": "isEmptyInputValue"
},
{
"name": "isForwardRef"
},
Expand Down
26 changes: 13 additions & 13 deletions packages/forms/src/directives/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {Directive, forwardRef, Input, OnChanges, SimpleChanges, StaticProvider}
import {Observable} from 'rxjs';

import {AbstractControl} from '../model';
import {NG_VALIDATORS, Validators} from '../validators';
import {emailValidator, maxLengthValidator, maxValidator, minLengthValidator, minValidator, NG_VALIDATORS, nullValidator, patternValidator, requiredTrueValidator, requiredValidator} from '../validators';


/**
Expand Down Expand Up @@ -77,7 +77,7 @@ export interface Validator {
*/
@Directive()
abstract class AbstractValidatorDirective implements Validator {
private _validator: ValidatorFn = Validators.nullValidator;
private _validator: ValidatorFn = nullValidator;
private _onChange!: () => void;

/**
Expand Down Expand Up @@ -180,7 +180,7 @@ export class MaxValidator extends AbstractValidatorDirective implements OnChange
/** @internal */
normalizeInput = (input: string): number => parseInt(input, 10);
/** @internal */
createValidator = (max: number): ValidatorFn => Validators.max(max);
createValidator = (max: number): ValidatorFn => maxValidator(max);
/**
* Declare `ngOnChanges` lifecycle hook at the main directive level (vs keeping it in base class)
* to avoid differences in handling inheritance of lifecycle hooks between Ivy and ViewEngine in
Expand Down Expand Up @@ -240,7 +240,7 @@ export class MinValidator extends AbstractValidatorDirective implements OnChange
/** @internal */
normalizeInput = (input: string): number => parseInt(input, 10);
/** @internal */
createValidator = (min: number): ValidatorFn => Validators.min(min);
createValidator = (min: number): ValidatorFn => minValidator(min);
/**
* Declare `ngOnChanges` lifecycle hook at the main directive level (vs keeping it in base class)
* to avoid differences in handling inheritance of lifecycle hooks between Ivy and ViewEngine in
Expand Down Expand Up @@ -364,7 +364,7 @@ export class RequiredValidator implements Validator {
* @nodoc
*/
validate(control: AbstractControl): ValidationErrors|null {
return this.required ? Validators.required(control) : null;
return this.required ? requiredValidator(control) : null;
}

/**
Expand Down Expand Up @@ -411,7 +411,7 @@ export class CheckboxRequiredValidator extends RequiredValidator {
* @nodoc
*/
validate(control: AbstractControl): ValidationErrors|null {
return this.required ? Validators.requiredTrue(control) : null;
return this.required ? requiredTrueValidator(control) : null;
}
}

Expand Down Expand Up @@ -472,7 +472,7 @@ export class EmailValidator implements Validator {
* @nodoc
*/
validate(control: AbstractControl): ValidationErrors|null {
return this._enabled ? Validators.email(control) : null;
return this._enabled ? emailValidator(control) : null;
}

/**
Expand Down Expand Up @@ -543,7 +543,7 @@ export const MIN_LENGTH_VALIDATOR: any = {
host: {'[attr.minlength]': 'minlength ? minlength : null'}
})
export class MinLengthValidator implements Validator, OnChanges {
private _validator: ValidatorFn = Validators.nullValidator;
private _validator: ValidatorFn = nullValidator;
private _onChange?: () => void;

/**
Expand Down Expand Up @@ -579,7 +579,7 @@ export class MinLengthValidator implements Validator, OnChanges {
}

private _createValidator(): void {
this._validator = Validators.minLength(
this._validator = minLengthValidator(
typeof this.minlength === 'number' ? this.minlength : parseInt(this.minlength, 10));
}
}
Expand Down Expand Up @@ -621,7 +621,7 @@ export const MAX_LENGTH_VALIDATOR: any = {
host: {'[attr.maxlength]': 'maxlength ? maxlength : null'}
})
export class MaxLengthValidator implements Validator, OnChanges {
private _validator: ValidatorFn = Validators.nullValidator;
private _validator: ValidatorFn = nullValidator;
private _onChange?: () => void;

/**
Expand Down Expand Up @@ -656,7 +656,7 @@ export class MaxLengthValidator implements Validator, OnChanges {
}

private _createValidator(): void {
this._validator = Validators.maxLength(
this._validator = maxLengthValidator(
typeof this.maxlength === 'number' ? this.maxlength : parseInt(this.maxlength, 10));
}
}
Expand Down Expand Up @@ -701,7 +701,7 @@ export const PATTERN_VALIDATOR: any = {
host: {'[attr.pattern]': 'pattern ? pattern : null'}
})
export class PatternValidator implements Validator, OnChanges {
private _validator: ValidatorFn = Validators.nullValidator;
private _validator: ValidatorFn = nullValidator;
private _onChange?: () => void;

/**
Expand Down Expand Up @@ -736,6 +736,6 @@ export class PatternValidator implements Validator, OnChanges {
}

private _createValidator(): void {
this._validator = Validators.pattern(this.pattern);
this._validator = patternValidator(this.pattern);
}
}

0 comments on commit 3bd1992

Please sign in to comment.