From 3477610f6ded089e835412c6bc4bd4157a693683 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Fri, 1 Feb 2019 13:07:18 -0800 Subject: [PATCH] fix(ivy): resolve enum values in host bindings (#28523) Some applications use enum values in their host bindings: @Component({ host: { '[prop]': EnumType.Key, }, ... }) This commit changes the resolution of host properties to follow the enum declaration and extract the correct value for the binding. PR Close #28523 --- .../src/ngtsc/annotations/src/directive.ts | 7 +++++- .../compiler-cli/test/ngtsc/ngtsc_spec.ts | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts b/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts index c56a375c99..8c3c5c66d2 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts @@ -11,7 +11,7 @@ import * as ts from 'typescript'; import {ErrorCode, FatalDiagnosticError} from '../../diagnostics'; import {Reference, ResolvedReference} from '../../imports'; -import {PartialEvaluator} from '../../partial_evaluator'; +import {EnumValue, PartialEvaluator} from '../../partial_evaluator'; import {ClassMember, ClassMemberKind, Decorator, ReflectionHost, filterToMembersWithDecorator, reflectObjectLiteral} from '../../reflection'; import {AnalysisOutput, CompileResult, DecoratorHandler} from '../../transform'; @@ -434,6 +434,11 @@ function extractHostBindings( ErrorCode.DECORATOR_ARG_NOT_LITERAL, expr, `Decorator host metadata must be an object`); } hostMetaMap.forEach((value, key) => { + // Resolve Enum references to their declared value. + if (value instanceof EnumValue) { + value = value.resolved; + } + if (typeof value !== 'string' || typeof key !== 'string') { throw new Error(`Decorator host metadata must be a string -> string object, got ${value}`); } diff --git a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts index 2aa6e01a7f..4d4a55098d 100644 --- a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts +++ b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts @@ -1004,6 +1004,31 @@ describe('ngtsc behavioral tests', () => { expect(trim(jsContents)).toContain(trim(hostBindingsFn)); }); + it('should accept enum values as host bindings', () => { + env.tsconfig(); + env.write(`test.ts`, ` + import {Component, HostBinding, HostListener, TemplateRef} from '@angular/core'; + + enum HostBindings { + Hello = 'foo' + } + + @Component({ + selector: 'test', + template: 'Test', + host: { + '[attr.hello]': HostBindings.Hello, + }, + }) + class FooCmp { + foo = 'test'; + } + `); + + env.driveMain(); + expect(env.getContents('test.js')).toContain('"hello", i0.ɵbind(ctx.foo)'); + }); + it('should generate host listeners for directives within hostBindings section', () => { env.tsconfig(); env.write(`test.ts`, `