fix(core): remove side effects from `ɵɵNgOnChangesFeature()` (#35769)

`ɵɵNgOnChangesFeature()` would set `ngInherit`, which is a side effect and also not necessary. This was pulled out to module scope so the function itself can be pure. Since it only curries another function, the call is entirely unnecessary. Updated the compiler to only generate a reference to this function, rather than a call to it, and removed the extra curry indirection.

PR Close #35769
This commit is contained in:
Doug Parker 2020-02-21 11:34:39 -08:00 committed by atscott
parent ba3612774f
commit 9cf85d2177
5 changed files with 16 additions and 16 deletions

View File

@ -2542,7 +2542,7 @@ describe('compiler compliance', () => {
type: LifecycleComp, type: LifecycleComp,
selectors: [["lifecycle-comp"]], selectors: [["lifecycle-comp"]],
inputs: {nameMin: ["name", "nameMin"]}, inputs: {nameMin: ["name", "nameMin"]},
features: [$r3$.ɵɵNgOnChangesFeature()], features: [$r3$.ɵɵNgOnChangesFeature],
decls: 0, decls: 0,
vars: 0, vars: 0,
template: function LifecycleComp_Template(rf, ctx) {}, template: function LifecycleComp_Template(rf, ctx) {},
@ -2662,7 +2662,7 @@ describe('compiler compliance', () => {
ForOfDirective.ɵdir = $r3$.ɵɵdefineDirective({ ForOfDirective.ɵdir = $r3$.ɵɵdefineDirective({
type: ForOfDirective, type: ForOfDirective,
selectors: [["", "forOf", ""]], selectors: [["", "forOf", ""]],
features: [$r3$.ɵɵNgOnChangesFeature()], features: [$r3$.ɵɵNgOnChangesFeature],
inputs: {forOf: "forOf"} inputs: {forOf: "forOf"}
}); });
`; `;
@ -2742,7 +2742,7 @@ describe('compiler compliance', () => {
ForOfDirective.ɵdir = $r3$.ɵɵdefineDirective({ ForOfDirective.ɵdir = $r3$.ɵɵdefineDirective({
type: ForOfDirective, type: ForOfDirective,
selectors: [["", "forOf", ""]], selectors: [["", "forOf", ""]],
features: [$r3$.ɵɵNgOnChangesFeature()], features: [$r3$.ɵɵNgOnChangesFeature],
inputs: {forOf: "forOf"} inputs: {forOf: "forOf"}
}); });
`; `;
@ -3767,7 +3767,7 @@ describe('compiler compliance', () => {
// ... // ...
BaseClass.ɵdir = $r3$.ɵɵdefineDirective({ BaseClass.ɵdir = $r3$.ɵɵdefineDirective({
type: BaseClass, type: BaseClass,
features: [$r3$.ɵɵNgOnChangesFeature()] features: [$r3$.ɵɵNgOnChangesFeature]
}); });
// ... // ...
`; `;

View File

@ -87,7 +87,7 @@ function baseDirectiveFields(
*/ */
function addFeatures( function addFeatures(
definitionMap: DefinitionMap, meta: R3DirectiveMetadata | R3ComponentMetadata) { definitionMap: DefinitionMap, meta: R3DirectiveMetadata | R3ComponentMetadata) {
// e.g. `features: [NgOnChangesFeature()]` // e.g. `features: [NgOnChangesFeature]`
const features: o.Expression[] = []; const features: o.Expression[] = [];
const providers = meta.providers; const providers = meta.providers;
@ -107,7 +107,7 @@ function addFeatures(
features.push(o.importExpr(R3.CopyDefinitionFeature)); features.push(o.importExpr(R3.CopyDefinitionFeature));
} }
if (meta.lifecycle.usesOnChanges) { if (meta.lifecycle.usesOnChanges) {
features.push(o.importExpr(R3.NgOnChangesFeature).callFn(EMPTY_ARRAY)); features.push(o.importExpr(R3.NgOnChangesFeature));
} }
if (features.length) { if (features.length) {
definitionMap.set('features', o.literalArr(features)); definitionMap.set('features', o.literalArr(features));

View File

@ -35,26 +35,26 @@ type OnChangesExpando = OnChanges & {
* static ɵcmp = defineComponent({ * static ɵcmp = defineComponent({
* ... * ...
* inputs: {name: 'publicName'}, * inputs: {name: 'publicName'},
* features: [NgOnChangesFeature()] * features: [NgOnChangesFeature]
* }); * });
* ``` * ```
* *
* @codeGenApi * @codeGenApi
*/ */
export function ɵɵNgOnChangesFeature<T>(): DirectiveDefFeature {
// This option ensures that the ngOnChanges lifecycle hook will be inherited
// from superclasses (in InheritDefinitionFeature).
(NgOnChangesFeatureImpl as DirectiveDefFeature).ngInherit = true;
return NgOnChangesFeatureImpl;
}
function NgOnChangesFeatureImpl<T>(definition: DirectiveDef<T>): void { export function ɵɵNgOnChangesFeature<T>(definition: DirectiveDef<T>): void {
if (definition.type.prototype.ngOnChanges) { if (definition.type.prototype.ngOnChanges) {
definition.setInput = ngOnChangesSetInput; definition.setInput = ngOnChangesSetInput;
(definition as{onChanges: Function}).onChanges = wrapOnChanges(); (definition as{onChanges: Function}).onChanges = wrapOnChanges();
} }
} }
// This option ensures that the ngOnChanges lifecycle hook will be inherited
// from superclasses (in InheritDefinitionFeature).
/** @nocollapse */
// tslint:disable-next-line:no-toplevel-property-access
(ɵɵNgOnChangesFeature as DirectiveDefFeature).ngInherit = true;
function wrapOnChanges() { function wrapOnChanges() {
return function wrapOnChangesHook_inPreviousChangesStorage(this: OnChanges) { return function wrapOnChangesHook_inPreviousChangesStorage(this: OnChanges) {
const simpleChangesStore = getSimpleChangesStore(this); const simpleChangesStore = getSimpleChangesStore(this);

View File

@ -41,7 +41,7 @@ NgIf.ɵfac = () =>
NgTemplateOutlet.ɵdir = ɵɵdefineDirective({ NgTemplateOutlet.ɵdir = ɵɵdefineDirective({
type: NgTemplateOutletDef, type: NgTemplateOutletDef,
selectors: [['', 'ngTemplateOutlet', '']], selectors: [['', 'ngTemplateOutlet', '']],
features: [ɵɵNgOnChangesFeature()], features: [ɵɵNgOnChangesFeature],
inputs: inputs:
{ngTemplateOutlet: 'ngTemplateOutlet', ngTemplateOutletContext: 'ngTemplateOutletContext'} {ngTemplateOutlet: 'ngTemplateOutlet', ngTemplateOutletContext: 'ngTemplateOutletContext'}
}); });

View File

@ -915,7 +915,7 @@ export declare function ɵɵnextContext<T = any>(level?: number): T;
export declare type ɵɵNgModuleDefWithMeta<T, Declarations, Imports, Exports> = NgModuleDef<T>; export declare type ɵɵNgModuleDefWithMeta<T, Declarations, Imports, Exports> = NgModuleDef<T>;
export declare function ɵɵNgOnChangesFeature<T>(): DirectiveDefFeature; export declare function ɵɵNgOnChangesFeature<T>(definition: DirectiveDef<T>): void;
export declare function ɵɵpipe(index: number, pipeName: string): any; export declare function ɵɵpipe(index: number, pipeName: string): any;