Skip to content

Commit f752ab9

Browse files
petebacondarwinatscott
authored andcommittedOct 14, 2020
fix(compiler-cli): support namespaced query types in directives (#38959) (#39272)
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 PR Close #39272
1 parent 9c875b3 commit f752ab9

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed
 

‎packages/compiler-cli/src/ngtsc/annotations/src/directive.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,20 @@ export function extractQueriesFromDecorator(
463463
}
464464
reflectObjectLiteral(queryData).forEach((queryExpr, propertyName) => {
465465
queryExpr = unwrapExpression(queryExpr);
466-
if (!ts.isNewExpression(queryExpr) || !ts.isIdentifier(queryExpr.expression)) {
466+
if (!ts.isNewExpression(queryExpr)) {
467467
throw new FatalDiagnosticError(
468468
ErrorCode.VALUE_HAS_WRONG_TYPE, queryData,
469469
'Decorator query metadata must be an instance of a query type');
470470
}
471-
const type = reflector.getImportOfIdentifier(queryExpr.expression);
471+
const queryType = ts.isPropertyAccessExpression(queryExpr.expression) ?
472+
queryExpr.expression.name :
473+
queryExpr.expression;
474+
if (!ts.isIdentifier(queryType)) {
475+
throw new FatalDiagnosticError(
476+
ErrorCode.VALUE_HAS_WRONG_TYPE, queryData,
477+
'Decorator query metadata must be an instance of a query type');
478+
}
479+
const type = reflector.getImportOfIdentifier(queryType);
472480
if (type === null || (!isCore && type.from !== '@angular/core') ||
473481
!QUERY_TYPES.has(type.name)) {
474482
throw new FatalDiagnosticError(

‎packages/compiler-cli/test/ngtsc/ngtsc_spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3064,12 +3064,13 @@ runInEachFileSystem(os => {
30643064
it('should generate queries for directives', () => {
30653065
env.write(`test.ts`, `
30663066
import {Directive, ContentChild, ContentChildren, TemplateRef, ViewChild} from '@angular/core';
3067+
import * as core from '@angular/core';
30673068
30683069
@Directive({
30693070
selector: '[test]',
30703071
queries: {
30713072
'mview': new ViewChild('test1'),
3072-
'mcontent': new ContentChild('test2'),
3073+
'mcontent': new core.ContentChild('test2'),
30733074
}
30743075
})
30753076
class FooCmp {

0 commit comments

Comments
 (0)
Please sign in to comment.