From 2736a43ecb6ed4282655b3ac5d5baf1afd1539b6 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Wed, 30 Sep 2020 20:55:30 +0100 Subject: [PATCH] fix(compiler-cli): support namespaced query types in directives (#38959) Previously directive "queries" that relied upon a namespaced type ```ts queries: { 'mcontent': new core.ContentChild('test2'), } ``` caused an error to be thrown. This is now supported. PR Close #38959 --- .../src/ngtsc/annotations/src/directive.ts | 12 ++++++++++-- packages/compiler-cli/test/ngtsc/ngtsc_spec.ts | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts b/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts index 89b3b4e26a..2505b3ef44 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/directive.ts @@ -463,12 +463,20 @@ export function extractQueriesFromDecorator( } reflectObjectLiteral(queryData).forEach((queryExpr, propertyName) => { queryExpr = unwrapExpression(queryExpr); - if (!ts.isNewExpression(queryExpr) || !ts.isIdentifier(queryExpr.expression)) { + if (!ts.isNewExpression(queryExpr)) { throw new FatalDiagnosticError( ErrorCode.VALUE_HAS_WRONG_TYPE, queryData, 'Decorator query metadata must be an instance of a query type'); } - const type = reflector.getImportOfIdentifier(queryExpr.expression); + const queryType = ts.isPropertyAccessExpression(queryExpr.expression) ? + queryExpr.expression.name : + queryExpr.expression; + if (!ts.isIdentifier(queryType)) { + throw new FatalDiagnosticError( + ErrorCode.VALUE_HAS_WRONG_TYPE, queryData, + 'Decorator query metadata must be an instance of a query type'); + } + const type = reflector.getImportOfIdentifier(queryType); if (type === null || (!isCore && type.from !== '@angular/core') || !QUERY_TYPES.has(type.name)) { throw new FatalDiagnosticError( diff --git a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts index 193373668b..2642e233be 100644 --- a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts +++ b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts @@ -3064,12 +3064,13 @@ runInEachFileSystem(os => { it('should generate queries for directives', () => { env.write(`test.ts`, ` import {Directive, ContentChild, ContentChildren, TemplateRef, ViewChild} from '@angular/core'; + import * as core from '@angular/core'; @Directive({ selector: '[test]', queries: { 'mview': new ViewChild('test1'), - 'mcontent': new ContentChild('test2'), + 'mcontent': new core.ContentChild('test2'), } }) class FooCmp {