diff --git a/packages/core/src/core_render3_private_export.ts b/packages/core/src/core_render3_private_export.ts index 4d54157595..216e69d120 100644 --- a/packages/core/src/core_render3_private_export.ts +++ b/packages/core/src/core_render3_private_export.ts @@ -21,6 +21,7 @@ export { PublicFeature as ɵPublicFeature, NgOnChangesFeature as ɵNgOnChangesFeature, CssSelector as ɵCssSelector, + NC as ɵNC, C as ɵC, E as ɵE, L as ɵL, @@ -43,15 +44,15 @@ export { pb3 as ɵpb3, pb4 as ɵpb4, pbV as ɵpbV, - o1 as ɵo1, - o2 as ɵo2, - o3 as ɵo3, - o4 as ɵo4, - o5 as ɵo5, - o6 as ɵo6, - o7 as ɵo7, - o8 as ɵo8, - oV as ɵoV, + f1 as ɵf1, + f2 as ɵf2, + f3 as ɵf3, + f4 as ɵf4, + f5 as ɵf5, + f6 as ɵf6, + f7 as ɵf7, + f8 as ɵf8, + fV as ɵfV, cR as ɵcR, cr as ɵcr, qR as ɵqR, diff --git a/packages/core/src/render3/index.ts b/packages/core/src/render3/index.ts index 980795d5b1..6769e0fd85 100644 --- a/packages/core/src/render3/index.ts +++ b/packages/core/src/render3/index.ts @@ -79,16 +79,16 @@ export { queryRefresh as qR, } from './query'; export { - objectLiteral1 as o1, - objectLiteral2 as o2, - objectLiteral3 as o3, - objectLiteral4 as o4, - objectLiteral5 as o5, - objectLiteral6 as o6, - objectLiteral7 as o7, - objectLiteral8 as o8, - objectLiteralV as oV, -} from './object_literal'; + pureFunction1 as f1, + pureFunction2 as f2, + pureFunction3 as f3, + pureFunction4 as f4, + pureFunction5 as f5, + pureFunction6 as f6, + pureFunction7 as f7, + pureFunction8 as f8, + pureFunctionV as fV, +} from './pure_function'; // clang-format on diff --git a/packages/core/src/render3/object_literal.ts b/packages/core/src/render3/pure_function.ts similarity index 60% rename from packages/core/src/render3/object_literal.ts rename to packages/core/src/render3/pure_function.ts index 79784cc1a6..22bc3acc9b 100644 --- a/packages/core/src/render3/object_literal.ts +++ b/packages/core/src/render3/pure_function.ts @@ -9,31 +9,31 @@ import {NO_CHANGE, bind, peekBinding} from './instructions'; /** - * If the object or array has changed, returns a copy with the updated expression. - * Or if the expression hasn't changed, returns NO_CHANGE. + * If the value of the provided exp has changed, calls the pure function to + * return an updated value. Or if the value has not changed, returns NO_CHANGE. * - * @param factoryFn Function that returns an updated instance of the object/array + * @param pureFn Function that returns an updated value * @param exp Updated expression value - * @returns A copy of the object/array or NO_CHANGE + * @returns Updated value or NO_CHANGE */ -export function objectLiteral1(factoryFn: (v: any) => any, exp: any): any { +export function pureFunction1(pureFn: (v: any) => any, exp: any): any { let different = false; const latestValue = exp === NO_CHANGE ? peekBinding() : exp; if (bind(exp) !== NO_CHANGE) different = true; - return different ? factoryFn(latestValue) : NO_CHANGE; + return different ? pureFn(latestValue) : NO_CHANGE; } /** - * If the object or array has changed, returns a copy with all updated expressions. - * Or if no expressions have changed, returns NO_CHANGE. + * If the value of any provided exp has changed, calls the pure function to + * return an updated value. Or if no values have changed, returns NO_CHANGE. * - * @param factoryFn + * @param pureFn * @param exp1 * @param exp2 - * @returns A copy of the object/array or NO_CHANGE + * @returns Updated value or NO_CHANGE */ -export function objectLiteral2(factoryFn: (v1: any, v2: any) => any, exp1: any, exp2: any): any { +export function pureFunction2(pureFn: (v1: any, v2: any) => any, exp1: any, exp2: any): any { let different = false; const latestVal1 = exp1 === NO_CHANGE ? peekBinding() : exp1; @@ -42,21 +42,21 @@ export function objectLiteral2(factoryFn: (v1: any, v2: any) => any, exp1: any, const latestVal2 = exp2 === NO_CHANGE ? peekBinding() : exp2; if (bind(exp2) !== NO_CHANGE) different = true; - return different ? factoryFn(latestVal1, latestVal2) : NO_CHANGE; + return different ? pureFn(latestVal1, latestVal2) : NO_CHANGE; } /** - * If the object or array has changed, returns a copy with all updated expressions. - * Or if no expressions have changed, returns NO_CHANGE. + * If the value of any provided exp has changed, calls the pure function to + * return an updated value. Or if no values have changed, returns NO_CHANGE. * - * @param factoryFn + * @param pureFn * @param exp1 * @param exp2 * @param exp3 - * @returns A copy of the object/array or NO_CHANGE + * @returns Updated value or NO_CHANGE */ -export function objectLiteral3( - factoryFn: (v1: any, v2: any, v3: any) => any, exp1: any, exp2: any, exp3: any): any { +export function pureFunction3( + pureFn: (v1: any, v2: any, v3: any) => any, exp1: any, exp2: any, exp3: any): any { let different = false; const latestVal1 = exp1 === NO_CHANGE ? peekBinding() : exp1; @@ -68,22 +68,22 @@ export function objectLiteral3( const latestVal3 = exp3 === NO_CHANGE ? peekBinding() : exp3; if (bind(exp3) !== NO_CHANGE) different = true; - return different ? factoryFn(latestVal1, latestVal2, latestVal3) : NO_CHANGE; + return different ? pureFn(latestVal1, latestVal2, latestVal3) : NO_CHANGE; } /** - * If the object or array has changed, returns a copy with all updated expressions. - * Or if no expressions have changed, returns NO_CHANGE. + * If the value of any provided exp has changed, calls the pure function to + * return an updated value. Or if no values have changed, returns NO_CHANGE. * - * @param factoryFn + * @param pureFn * @param exp1 * @param exp2 * @param exp3 * @param exp4 - * @returns A copy of the object/array or NO_CHANGE + * @returns Updated value or NO_CHANGE */ -export function objectLiteral4( - factoryFn: (v1: any, v2: any, v3: any, v4: any) => any, exp1: any, exp2: any, exp3: any, +export function pureFunction4( + pureFn: (v1: any, v2: any, v3: any, v4: any) => any, exp1: any, exp2: any, exp3: any, exp4: any): any { let different = false; @@ -99,24 +99,24 @@ export function objectLiteral4( const latestVal4 = exp4 === NO_CHANGE ? peekBinding() : exp4; if (bind(exp4) !== NO_CHANGE) different = true; - return different ? factoryFn(latestVal1, latestVal2, latestVal3, latestVal4) : NO_CHANGE; + return different ? pureFn(latestVal1, latestVal2, latestVal3, latestVal4) : NO_CHANGE; } /** - * If the object or array has changed, returns a copy with all updated expressions. - * Or if no expressions have changed, returns NO_CHANGE. + * If the value of any provided exp has changed, calls the pure function to + * return an updated value. Or if no values have changed, returns NO_CHANGE. * - * @param factoryFn + * @param pureFn * @param exp1 * @param exp2 * @param exp3 * @param exp4 * @param exp5 - * @returns A copy of the object/array or NO_CHANGE + * @returns Updated value or NO_CHANGE */ -export function objectLiteral5( - factoryFn: (v1: any, v2: any, v3: any, v4: any, v5: any) => any, exp1: any, exp2: any, - exp3: any, exp4: any, exp5: any): any { +export function pureFunction5( + pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any) => any, exp1: any, exp2: any, exp3: any, + exp4: any, exp5: any): any { let different = false; const latestVal1 = exp1 === NO_CHANGE ? peekBinding() : exp1; @@ -134,25 +134,24 @@ export function objectLiteral5( const latestVal5 = exp5 === NO_CHANGE ? peekBinding() : exp5; if (bind(exp5) !== NO_CHANGE) different = true; - return different ? factoryFn(latestVal1, latestVal2, latestVal3, latestVal4, latestVal5) : - NO_CHANGE; + return different ? pureFn(latestVal1, latestVal2, latestVal3, latestVal4, latestVal5) : NO_CHANGE; } /** - * If the object or array has changed, returns a copy with all updated expressions. - * Or if no expressions have changed, returns NO_CHANGE. + * If the value of any provided exp has changed, calls the pure function to + * return an updated value. Or if no values have changed, returns NO_CHANGE. * - * @param factoryFn + * @param pureFn * @param exp1 * @param exp2 * @param exp3 * @param exp4 * @param exp5 * @param exp6 - * @returns A copy of the object/array or NO_CHANGE + * @returns Updated value or NO_CHANGE */ -export function objectLiteral6( - factoryFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any) => any, exp1: any, exp2: any, +export function pureFunction6( + pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any) => any, exp1: any, exp2: any, exp3: any, exp4: any, exp5: any, exp6: any): any { let different = false; @@ -175,15 +174,15 @@ export function objectLiteral6( if (bind(exp6) !== NO_CHANGE) different = true; return different ? - factoryFn(latestVal1, latestVal2, latestVal3, latestVal4, latestVal5, latestVal6) : + pureFn(latestVal1, latestVal2, latestVal3, latestVal4, latestVal5, latestVal6) : NO_CHANGE; } /** - * If the object or array has changed, returns a copy with all updated expressions. - * Or if no expressions have changed, returns NO_CHANGE. + * If the value of any provided exp has changed, calls the pure function to + * return an updated value. Or if no values have changed, returns NO_CHANGE. * - * @param factoryFn + * @param pureFn * @param exp1 * @param exp2 * @param exp3 @@ -191,10 +190,10 @@ export function objectLiteral6( * @param exp5 * @param exp6 * @param exp7 - * @returns A copy of the object/array or NO_CHANGE + * @returns Updated value or NO_CHANGE */ -export function objectLiteral7( - factoryFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, v7: any) => any, exp1: any, +export function pureFunction7( + pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, v7: any) => any, exp1: any, exp2: any, exp3: any, exp4: any, exp5: any, exp6: any, exp7: any): any { let different = false; @@ -220,16 +219,15 @@ export function objectLiteral7( if (bind(exp7) !== NO_CHANGE) different = true; return different ? - factoryFn( - latestVal1, latestVal2, latestVal3, latestVal4, latestVal5, latestVal6, latestVal7) : + pureFn(latestVal1, latestVal2, latestVal3, latestVal4, latestVal5, latestVal6, latestVal7) : NO_CHANGE; } /** - * If the object or array has changed, returns a copy with all updated expressions. - * Or if no expressions have changed, returns NO_CHANGE. + * If the value of any provided exp has changed, calls the pure function to + * return an updated value. Or if no values have changed, returns NO_CHANGE. * - * @param factoryFn + * @param pureFn * @param exp1 * @param exp2 * @param exp3 @@ -238,10 +236,10 @@ export function objectLiteral7( * @param exp6 * @param exp7 * @param exp8 - * @returns A copy of the object/array or NO_CHANGE + * @returns Updated value or NO_CHANGE */ -export function objectLiteral8( - factoryFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, v7: any, v8: any) => any, +export function pureFunction8( + pureFn: (v1: any, v2: any, v3: any, v4: any, v5: any, v6: any, v7: any, v8: any) => any, exp1: any, exp2: any, exp3: any, exp4: any, exp5: any, exp6: any, exp7: any, exp8: any): any { let different = false; @@ -269,24 +267,24 @@ export function objectLiteral8( const latestVal8 = exp8 === NO_CHANGE ? peekBinding() : exp8; if (bind(exp8) !== NO_CHANGE) different = true; - return different ? factoryFn( + return different ? pureFn( latestVal1, latestVal2, latestVal3, latestVal4, latestVal5, latestVal6, latestVal7, latestVal8) : NO_CHANGE; } /** - * objectLiteral instruction that can support any number of bindings. + * pureFunction instruction that can support any number of bindings. * - * If the object or array has changed, returns a copy with all updated expressions. - * Or if no expressions have changed, returns NO_CHANGE. + * If the value of any provided exp has changed, calls the pure function to + * return an updated value. Or if no values have changed, returns NO_CHANGE. * - * @param factoryFn A factory function that takes binding values and builds an object or array + * @param pureFn A pure function that takes binding values and builds an object or array * containing those values. * @param exp An array of binding values - * @returns A copy of the object/array or NO_CHANGE + * @returns Updated value or NO_CHANGE */ -export function objectLiteralV(factoryFn: (v: any[]) => any, exps: any[]): any { +export function pureFunctionV(pureFn: (v: any[]) => any, exps: any[]): any { let different = false; for (let i = 0; i < exps.length; i++) { @@ -295,5 +293,5 @@ export function objectLiteralV(factoryFn: (v: any[]) => any, exps: any[]): any { if (bind(exp) !== NO_CHANGE) different = true; } - return different ? factoryFn(exps) : NO_CHANGE; + return different ? pureFn(exps) : NO_CHANGE; } diff --git a/packages/core/test/render3/compiler_canonical_spec.ts b/packages/core/test/render3/compiler_canonical_spec.ts index f9f31bafb2..5c56658a77 100644 --- a/packages/core/test/render3/compiler_canonical_spec.ts +++ b/packages/core/test/render3/compiler_canonical_spec.ts @@ -190,41 +190,69 @@ describe('compiler specification', () => { }); describe('value composition', () => { + type $MyArrayComp$ = MyArrayComp; - it('should support array literals', () => { - type $MyComp$ = MyComp; + @Component({ + selector: 'my-array-comp', + template: ` + {{ names[0] }} {{ names[1] }} + ` + }) + class MyArrayComp { + @Input() names: string[]; + + static ngComponentDef = $r3$.ɵdefineComponent({ + type: MyArrayComp, + tag: 'my-array-comp', + factory: function MyArrayComp_Factory() { return new MyArrayComp(); }, + template: function MyArrayComp_Template(ctx: $MyArrayComp$, cm: $boolean$) { + if (cm) { + $r3$.ɵT(0); + } + $r3$.ɵt(0, $r3$.ɵb2('', ctx.names[0], ' ', ctx.names[1], '')); + }, + inputs: {names: 'names'} + }); + } + + it('should support array literals of constants', () => { type $MyApp$ = MyApp; - @Component({ - selector: 'my-comp', - template: ` -
{{ names[0] }}
-{{ names[1] }}
- ` - }) - class MyComp { - @Input() names: string[]; + // NORMATIVE + const $e0_arr$ = ['Nancy', 'Bess']; + // /NORMATIVE + @Component({ + selector: 'my-app', + template: ` +Nancy
Bess