fix(forms): Property renaming safe code (#45271)

This fixes property renaming related issues in advanced closure compiler
optimizations.

PR Close #45271
This commit is contained in:
ananyahs96
2022-03-04 13:56:12 -08:00
committed by Andrew Kushnir
parent 8c171da29f
commit aa7b857be8
2 changed files with 46 additions and 32 deletions
@@ -12,9 +12,10 @@ import {AbstractControlDirective} from './abstract_control_directive';
import {ControlContainer} from './control_container';
import {NgControl} from './ng_control';
type AnyControlStatus =
'untouched'|'touched'|'pristine'|'dirty'|'valid'|'invalid'|'pending'|'submitted';
// DO NOT REFACTOR!
// Each status is represented by a separate function to make sure that
// advanced Closure Compiler optimizations related to property renaming
// can work correctly.
export class AbstractControlStatus {
private _cd: AbstractControlDirective|null;
@@ -22,41 +23,54 @@ export class AbstractControlStatus {
this._cd = cd;
}
is(status: AnyControlStatus): boolean {
// Currently with ViewEngine (in AOT mode) it's not possible to use private methods in host
// bindings.
// TODO: once ViewEngine is removed, this function should be refactored:
// - make the `is` method `protected`, so it's not accessible publicly
// - move the `submitted` status logic to the `NgControlStatusGroup` class
// and make it `private` or `protected` too.
if (status === 'submitted') {
// We check for the `submitted` field from `NgForm` and `FormGroupDirective` classes, but
// we avoid instanceof checks to prevent non-tree-shakable references to those types.
return !!(this._cd as unknown as {submitted: boolean} | null)?.submitted;
}
return !!this._cd?.control?.[status];
protected get isTouched() {
return !!this._cd?.control?.touched;
}
protected get isUntouched() {
return !!this._cd?.control?.untouched;
}
protected get isPristine() {
return !!this._cd?.control?.pristine;
}
protected get isDirty() {
return !!this._cd?.control?.dirty;
}
protected get isValid() {
return !!this._cd?.control?.valid;
}
protected get isInvalid() {
return !!this._cd?.control?.invalid;
}
protected get isPending() {
return !!this._cd?.control?.pending;
}
protected get isSubmitted() {
// We check for the `submitted` field from `NgForm` and `FormGroupDirective` classes, but
// we avoid instanceof checks to prevent non-tree-shakable references to those types.
return !!(this._cd as unknown as {submitted: boolean} | null)?.submitted;
}
}
export const ngControlStatusHost = {
'[class.ng-untouched]': 'is("untouched")',
'[class.ng-touched]': 'is("touched")',
'[class.ng-pristine]': 'is("pristine")',
'[class.ng-dirty]': 'is("dirty")',
'[class.ng-valid]': 'is("valid")',
'[class.ng-invalid]': 'is("invalid")',
'[class.ng-pending]': 'is("pending")',
'[class.ng-untouched]': 'isUntouched',
'[class.ng-touched]': 'isTouched',
'[class.ng-pristine]': 'isPristine',
'[class.ng-dirty]': 'isDirty',
'[class.ng-valid]': 'isValid',
'[class.ng-invalid]': 'isInvalid',
'[class.ng-pending]': 'isPending',
};
export const ngGroupStatusHost = {
'[class.ng-untouched]': 'is("untouched")',
'[class.ng-touched]': 'is("touched")',
'[class.ng-pristine]': 'is("pristine")',
'[class.ng-dirty]': 'is("dirty")',
'[class.ng-valid]': 'is("valid")',
'[class.ng-invalid]': 'is("invalid")',
'[class.ng-pending]': 'is("pending")',
'[class.ng-submitted]': 'is("submitted")',
...ngControlStatusHost,
'[class.ng-submitted]': 'isSubmitted',
};
/**