feat(ivy): resolve forwardRef() for queries (#25080)

@ContentChild[ren] and @ViewChild[ren] can contain a forwardRef() to a
type. This commit allows ngtsc to unwrap the forward reference and
deal with the node inside.

It includes two modes of support for forward reference resolution -
a foreign function resolver which understands deeply nested forward
references in expressions that are being statically evaluated, and
an unwrapForwardRef() function which deals only with top-level nodes.

Both will be useful in the future, but for now only unwrapForwardRef()
is used.

PR Close #25080
This commit is contained in:
Alex Rickabaugh
2018-07-24 16:05:23 -07:00
committed by Igor Minar
parent 48d7205873
commit f902b5ec59
6 changed files with 122 additions and 23 deletions

View File

@ -449,6 +449,30 @@ describe('ngtsc behavioral tests', () => {
expect(jsContents).toContain(`i0.ɵQ(1, ["test1"], true)`);
});
it('should handle queries that use forwardRef', () => {
writeConfig();
write(`test.ts`, `
import {Component, ContentChild, TemplateRef, ViewContainerRef, forwardRef} from '@angular/core';
@Component({
selector: 'test',
template: '<div #foo></div>',
})
class FooCmp {
@ContentChild(forwardRef(() => TemplateRef)) child: any;
@ContentChild(forwardRef(function() { return ViewContainerRef; })) child2: any;
}
`);
const exitCode = main(['-p', basePath], errorSpy);
expect(errorSpy).not.toHaveBeenCalled();
expect(exitCode).toBe(0);
const jsContents = getContents('test.js');
expect(jsContents).toContain(`i0.ɵQ(null, TemplateRef, true)`);
expect(jsContents).toContain(`i0.ɵQ(null, ViewContainerRef, true)`);
});
it('should generate host bindings for directives', () => {
writeConfig();
write(`test.ts`, `