Skip to content

Commit a06824a

Browse files
alxhubAndrewKushnir
authored andcommitted
fix(ivy): correctly evaluate enum references in template expressions (angular#29062)
The ngtsc partial evaluator previously would not handle an enum reference inside a template string expression correctly. Enums are resolved to an `EnumValue` type, which has a `resolved` property with the actual value. When effectively toString-ing a `ResolvedValue` as part of visiting a template expression, the partial evaluator needs to translate `EnumValue`s to their fully resolved value, which this commit does. PR Close angular#29062
1 parent ba602db commit a06824a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

packages/compiler-cli/src/ngtsc/partial_evaluator/src/interpreter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ export class StaticInterpreter {
193193
const pieces: string[] = [node.head.text];
194194
for (let i = 0; i < node.templateSpans.length; i++) {
195195
const span = node.templateSpans[i];
196-
const value = this.visit(span.expression, context);
196+
let value = this.visit(span.expression, context);
197+
if (value instanceof EnumValue) {
198+
value = value.resolved;
199+
}
197200
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ||
198201
value == null) {
199202
pieces.push(`${value}`);

packages/compiler-cli/src/ngtsc/partial_evaluator/test/evaluator_spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ describe('ngtsc metadata', () => {
307307
const prop = expr.properties[0] as ts.PropertyAssignment;
308308
expect(value.node).toBe(prop.initializer);
309309
});
310+
311+
it('should resolve enums in template expressions', () => {
312+
const value =
313+
evaluate(`enum Test { VALUE = 'test', } const value = \`a.\${Test.VALUE}.b\`;`, 'value');
314+
expect(value).toBe('a.test.b');
315+
});
310316
});
311317

312318
function owningModuleOf(ref: Reference): string|null {

0 commit comments

Comments
 (0)