Skip to content

Commit e412fda

Browse files
crisbetothePunderWoman
authored andcommitted
refactor(core): replace classMapInterpolateX with classMap (angular#61639)
Replaces the `classMapInterpolateX` instructions with `classMap` plus a call to `interpolate` in order to simplify the runtime. The only difference between `classMapInterpolateX` and `classMap` was that the former passes `keyValueArraySet` into `checkStylingMap` while the latter passes `classKeyValueArraySet`. This doesn't appear to matter, because the interpolation instructions always have a string value which means that the function is never called. PR Close angular#61639
1 parent 73c8139 commit e412fda

File tree

12 files changed

+55
-636
lines changed

12 files changed

+55
-636
lines changed

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_styling/host_bindings/class_interpolation.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ function MyComponent_Template(rf, ctx) {
33
// ...
44
}
55
if (rf & 2) {
6-
$r3$.ɵɵclassMapInterpolate1("A", ctx.p1, "B");
6+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate1("A", ctx.p1, "B"));
77
$r3$.ɵɵadvance();
8-
$r3$.ɵɵclassMapInterpolate2("A", ctx.p1, "B", ctx.p2, "C");
8+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate2("A", ctx.p1, "B", ctx.p2, "C"));
99
$r3$.ɵɵadvance();
10-
$r3$.ɵɵclassMapInterpolate3("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D");
10+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate3("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D"));
1111
$r3$.ɵɵadvance();
12-
$r3$.ɵɵclassMapInterpolate4("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E");
12+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate4("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E"));
1313
$r3$.ɵɵadvance();
14-
$r3$.ɵɵclassMapInterpolate5("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E", ctx.p5, "F");
14+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate5("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E", ctx.p5, "F"));
1515
$r3$.ɵɵadvance();
16-
$r3$.ɵɵclassMapInterpolate6("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E", ctx.p5, "F", ctx.p6, "G");
16+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate6("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E", ctx.p5, "F", ctx.p6, "G"));
1717
$r3$.ɵɵadvance();
18-
$r3$.ɵɵclassMapInterpolate7("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E", ctx.p5, "F", ctx.p6, "G", ctx.p7, "H");
18+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate7("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E", ctx.p5, "F", ctx.p6, "G", ctx.p7, "H"));
1919
$r3$.ɵɵadvance();
20-
$r3$.ɵɵclassMapInterpolate8("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E", ctx.p5, "F", ctx.p6, "G", ctx.p7, "H", ctx.p8, "I");
20+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate8("A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E", ctx.p5, "F", ctx.p6, "G", ctx.p7, "H", ctx.p8, "I"));
2121
$r3$.ɵɵadvance();
22-
$r3$.ɵɵclassMapInterpolateV(["A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E", ctx.p5, "F", ctx.p6, "G", ctx.p7, "H", ctx.p8, "I", ctx.p9, "J"]);
22+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolateV(["A", ctx.p1, "B", ctx.p2, "C", ctx.p3, "D", ctx.p4, "E", ctx.p5, "F", ctx.p6, "G", ctx.p7, "H", ctx.p8, "I", ctx.p9, "J"]));
2323
}
2424
},

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_styling/interpolations/class_interpolations.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
// ...
22
if (rf & 2) {
3-
$r3$.ɵɵclassMapInterpolateV(["a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h", ctx.eight, "i", ctx.nine, "j"]);
3+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolateV(["a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h", ctx.eight, "i", ctx.nine, "j"]));
44
$r3$.ɵɵadvance();
5-
$r3$.ɵɵclassMapInterpolate8("a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h", ctx.eight, "i");
5+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate8("a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h", ctx.eight, "i"));
66
$r3$.ɵɵadvance();
7-
$r3$.ɵɵclassMapInterpolate7("a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h");
7+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate7("a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g", ctx.seven, "h"));
88
$r3$.ɵɵadvance();
9-
$r3$.ɵɵclassMapInterpolate6("a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g");
9+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate6("a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f", ctx.six, "g"));
1010
$r3$.ɵɵadvance();
11-
$r3$.ɵɵclassMapInterpolate5("a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f");
11+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate5("a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e", ctx.five, "f"));
1212
$r3$.ɵɵadvance();
13-
$r3$.ɵɵclassMapInterpolate4("a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e");
13+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate4("a", ctx.one, "b", ctx.two, "c", ctx.three, "d", ctx.four, "e"));
1414
$r3$.ɵɵadvance();
15-
$r3$.ɵɵclassMapInterpolate3("a", ctx.one, "b", ctx.two, "c", ctx.three, "d");
15+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate3("a", ctx.one, "b", ctx.two, "c", ctx.three, "d"));
1616
$r3$.ɵɵadvance();
17-
$r3$.ɵɵclassMapInterpolate2("a", ctx.one, "b", ctx.two, "c");
17+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate2("a", ctx.one, "b", ctx.two, "c"));
1818
$r3$.ɵɵadvance();
19-
$r3$.ɵɵclassMapInterpolate1("a", ctx.one, "b");
19+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate1("a", ctx.one, "b"));
2020
$r3$.ɵɵadvance();
2121
$r3$.ɵɵclassMap(ctx.one);
2222
}

packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_styling/style_bindings/binding_slots.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
$r3$.ɵɵelement(0, "div");
77
}
88
if (rf & 2) {
9-
$r3$.ɵɵclassMapInterpolate1("foo foo-", $ctx$.fooId);
9+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate1("foo foo-", $ctx$.fooId));
1010
}
1111
}
1212
// ...
@@ -17,7 +17,7 @@
1717
$r3$.ɵɵelement(0, "div");
1818
}
1919
if (rf & 2) {
20-
$r3$.ɵɵclassMapInterpolate2("foo foo-", $ctx$.fooId, "-", $ctx$.fooUsername);
20+
$r3$.ɵɵclassMap($r3$.ɵɵinterpolate2("foo foo-", $ctx$.fooId, "-", $ctx$.fooUsername));
2121
}
2222
}
2323
// ...

packages/compiler/src/render3/r3_identifiers.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -63,43 +63,6 @@ export class Identifiers {
6363

6464
static classMap: o.ExternalReference = {name: 'ɵɵclassMap', moduleName: CORE};
6565

66-
static classMapInterpolate1: o.ExternalReference = {
67-
name: 'ɵɵclassMapInterpolate1',
68-
moduleName: CORE,
69-
};
70-
static classMapInterpolate2: o.ExternalReference = {
71-
name: 'ɵɵclassMapInterpolate2',
72-
moduleName: CORE,
73-
};
74-
static classMapInterpolate3: o.ExternalReference = {
75-
name: 'ɵɵclassMapInterpolate3',
76-
moduleName: CORE,
77-
};
78-
static classMapInterpolate4: o.ExternalReference = {
79-
name: 'ɵɵclassMapInterpolate4',
80-
moduleName: CORE,
81-
};
82-
static classMapInterpolate5: o.ExternalReference = {
83-
name: 'ɵɵclassMapInterpolate5',
84-
moduleName: CORE,
85-
};
86-
static classMapInterpolate6: o.ExternalReference = {
87-
name: 'ɵɵclassMapInterpolate6',
88-
moduleName: CORE,
89-
};
90-
static classMapInterpolate7: o.ExternalReference = {
91-
name: 'ɵɵclassMapInterpolate7',
92-
moduleName: CORE,
93-
};
94-
static classMapInterpolate8: o.ExternalReference = {
95-
name: 'ɵɵclassMapInterpolate8',
96-
moduleName: CORE,
97-
};
98-
static classMapInterpolateV: o.ExternalReference = {
99-
name: 'ɵɵclassMapInterpolateV',
100-
moduleName: CORE,
101-
};
102-
10366
static styleProp: o.ExternalReference = {name: 'ɵɵstyleProp', moduleName: CORE};
10467

10568
static interpolate: o.ExternalReference = {

packages/compiler/src/template/pipeline/src/instruction.ts

Lines changed: 32 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,7 @@ export function attribute(
573573
const args: o.Expression[] = [o.literal(name)];
574574

575575
if (expression instanceof ir.Interpolation) {
576-
const interpolationArgs = collateInterpolationArgs(expression.strings, expression.expressions);
577-
args.push(
578-
callVariadicInstructionExpr(VALUE_INTERPOLATE_CONFIG, [], interpolationArgs, [], sourceSpan),
579-
);
576+
args.push(interpolationToExpression(expression, sourceSpan));
580577
} else {
581578
args.push(expression);
582579
}
@@ -598,10 +595,7 @@ export function styleProp(
598595
const args: o.Expression[] = [o.literal(name)];
599596

600597
if (expression instanceof ir.Interpolation) {
601-
const interpolationArgs = collateInterpolationArgs(expression.strings, expression.expressions);
602-
args.push(
603-
callVariadicInstructionExpr(VALUE_INTERPOLATE_CONFIG, [], interpolationArgs, [], sourceSpan),
604-
);
598+
args.push(interpolationToExpression(expression, sourceSpan));
605599
} else {
606600
args.push(expression);
607601
}
@@ -624,26 +618,22 @@ export function styleMap(
624618
expression: o.Expression | ir.Interpolation,
625619
sourceSpan: ParseSourceSpan,
626620
): ir.UpdateOp {
627-
let value: o.Expression;
628-
629-
if (expression instanceof ir.Interpolation) {
630-
const interpolationArgs = collateInterpolationArgs(expression.strings, expression.expressions);
631-
value = callVariadicInstructionExpr(
632-
VALUE_INTERPOLATE_CONFIG,
633-
[],
634-
interpolationArgs,
635-
[],
636-
sourceSpan,
637-
);
638-
} else {
639-
value = expression;
640-
}
641-
621+
const value =
622+
expression instanceof ir.Interpolation
623+
? interpolationToExpression(expression, sourceSpan)
624+
: expression;
642625
return call(Identifiers.styleMap, [value], sourceSpan);
643626
}
644627

645-
export function classMap(expression: o.Expression, sourceSpan: ParseSourceSpan): ir.UpdateOp {
646-
return call(Identifiers.classMap, [expression], sourceSpan);
628+
export function classMap(
629+
expression: o.Expression | ir.Interpolation,
630+
sourceSpan: ParseSourceSpan,
631+
): ir.UpdateOp {
632+
const value =
633+
expression instanceof ir.Interpolation
634+
? interpolationToExpression(expression, sourceSpan)
635+
: expression;
636+
return call(Identifiers.classMap, [value], sourceSpan);
647637
}
648638

649639
const PIPE_BINDINGS: o.ExternalReference[] = [
@@ -707,22 +697,6 @@ export function propertyInterpolate(
707697
);
708698
}
709699

710-
export function classMapInterpolate(
711-
strings: string[],
712-
expressions: o.Expression[],
713-
sourceSpan: ParseSourceSpan,
714-
): ir.UpdateOp {
715-
const interpolationArgs = collateInterpolationArgs(strings, expressions);
716-
717-
return callVariadicInstruction(
718-
CLASS_MAP_INTERPOLATE_CONFIG,
719-
[],
720-
interpolationArgs,
721-
[],
722-
sourceSpan,
723-
);
724-
}
725-
726700
export function domProperty(
727701
name: string,
728702
expression: o.Expression,
@@ -791,6 +765,23 @@ function collateInterpolationArgs(strings: string[], expressions: o.Expression[]
791765
return interpolationArgs;
792766
}
793767

768+
function interpolationToExpression(
769+
interpolation: ir.Interpolation,
770+
sourceSpan: ParseSourceSpan,
771+
): o.Expression {
772+
const interpolationArgs = collateInterpolationArgs(
773+
interpolation.strings,
774+
interpolation.expressions,
775+
);
776+
return callVariadicInstructionExpr(
777+
VALUE_INTERPOLATE_CONFIG,
778+
[],
779+
interpolationArgs,
780+
[],
781+
sourceSpan,
782+
);
783+
}
784+
794785
function call<OpT extends ir.CreateOp | ir.UpdateOp>(
795786
instruction: o.ExternalReference,
796787
args: o.Expression[],
@@ -891,30 +882,6 @@ const VALUE_INTERPOLATE_CONFIG: VariadicInstructionConfig = {
891882
},
892883
};
893884

894-
/**
895-
* `InterpolationConfig` for the `classMapInterpolate` instruction.
896-
*/
897-
const CLASS_MAP_INTERPOLATE_CONFIG: VariadicInstructionConfig = {
898-
constant: [
899-
Identifiers.classMap,
900-
Identifiers.classMapInterpolate1,
901-
Identifiers.classMapInterpolate2,
902-
Identifiers.classMapInterpolate3,
903-
Identifiers.classMapInterpolate4,
904-
Identifiers.classMapInterpolate5,
905-
Identifiers.classMapInterpolate6,
906-
Identifiers.classMapInterpolate7,
907-
Identifiers.classMapInterpolate8,
908-
],
909-
variable: Identifiers.classMapInterpolateV,
910-
mapping: (n) => {
911-
if (n % 2 === 0) {
912-
throw new Error(`Expected odd number of arguments`);
913-
}
914-
return (n - 1) / 2;
915-
},
916-
};
917-
918885
const PURE_FUNCTION_CONFIG: VariadicInstructionConfig = {
919886
constant: [
920887
Identifiers.pureFunction0,

packages/compiler/src/template/pipeline/src/phases/collapse_singleton_interpolations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export function collapseSingletonInterpolations(job: CompilationJob): void {
2525
const eligibleOpKind =
2626
op.kind === ir.OpKind.Attribute ||
2727
op.kind === ir.OpKind.StyleProp ||
28-
op.kind == ir.OpKind.StyleMap;
28+
op.kind == ir.OpKind.StyleMap ||
29+
op.kind === ir.OpKind.ClassMap;
2930
if (
3031
eligibleOpKind &&
3132
op.expression instanceof ir.Interpolation &&

packages/compiler/src/template/pipeline/src/phases/reify.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,7 @@ function reifyUpdateOperations(_unit: CompilationUnit, ops: ir.OpList<ir.UpdateO
518518
ir.OpList.replace(op, ng.styleMap(op.expression, op.sourceSpan));
519519
break;
520520
case ir.OpKind.ClassMap:
521-
if (op.expression instanceof ir.Interpolation) {
522-
ir.OpList.replace(
523-
op,
524-
ng.classMapInterpolate(op.expression.strings, op.expression.expressions, op.sourceSpan),
525-
);
526-
} else {
527-
ir.OpList.replace(op, ng.classMap(op.expression, op.sourceSpan));
528-
}
521+
ir.OpList.replace(op, ng.classMap(op.expression, op.sourceSpan));
529522
break;
530523
case ir.OpKind.I18nExpression:
531524
ir.OpList.replace(op, ng.i18nExp(op.expression, op.sourceSpan));

packages/core/src/core_render3_private_export.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,6 @@ export {
8484
ɵɵinterpolate8,
8585
ɵɵinterpolateV,
8686
ɵɵclassMap,
87-
ɵɵclassMapInterpolate1,
88-
ɵɵclassMapInterpolate2,
89-
ɵɵclassMapInterpolate3,
90-
ɵɵclassMapInterpolate4,
91-
ɵɵclassMapInterpolate5,
92-
ɵɵclassMapInterpolate6,
93-
ɵɵclassMapInterpolate7,
94-
ɵɵclassMapInterpolate8,
95-
ɵɵclassMapInterpolateV,
9687
ɵɵclassProp,
9788
ɵɵComponentDeclaration,
9889
ɵɵconditional,

packages/core/src/render3/index.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@ export {
6565
ɵɵinterpolate8,
6666
ɵɵinterpolateV,
6767
ɵɵclassMap,
68-
ɵɵclassMapInterpolate1,
69-
ɵɵclassMapInterpolate2,
70-
ɵɵclassMapInterpolate3,
71-
ɵɵclassMapInterpolate4,
72-
ɵɵclassMapInterpolate5,
73-
ɵɵclassMapInterpolate6,
74-
ɵɵclassMapInterpolate7,
75-
ɵɵclassMapInterpolate8,
76-
ɵɵclassMapInterpolateV,
7768
ɵɵclassProp,
7869
ɵɵcomponentInstance,
7970
ɵɵdirectiveInject,

packages/core/src/render3/instructions/all.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export * from '../../defer/instructions';
2929
export * from './advance';
3030
export * from './attribute';
3131
export * from './change_detection';
32-
export * from './class_map_interpolation';
3332
export * from './component_instance';
3433
export * from './control_flow';
3534
export * from './di';

0 commit comments

Comments
 (0)