fix(ivy): emit type `type` fields (#21789)

PR Close #21789
This commit is contained in:
Chuck Jazdzewski 2018-01-25 13:20:42 -08:00 committed by Misko Hevery
parent d364117aa8
commit 87754ad5ec
2 changed files with 15 additions and 1 deletions

View File

@ -39,6 +39,10 @@ export function compileDirective(
outputCtx: OutputContext, directive: CompileDirectiveMetadata, reflector: CompileReflector) { outputCtx: OutputContext, directive: CompileDirectiveMetadata, reflector: CompileReflector) {
const definitionMapValues: {key: string, quoted: boolean, value: o.Expression}[] = []; const definitionMapValues: {key: string, quoted: boolean, value: o.Expression}[] = [];
// e.g. 'type: MyDirective`
definitionMapValues.push(
{key: 'type', value: outputCtx.importExpr(directive.type.reference), quoted: false});
// e.g. `factory: () => new MyApp(injectElementRef())` // e.g. `factory: () => new MyApp(injectElementRef())`
const templateFactory = createFactory(directive.type, outputCtx, reflector); const templateFactory = createFactory(directive.type, outputCtx, reflector);
definitionMapValues.push({key: 'factory', value: templateFactory, quoted: false}); definitionMapValues.push({key: 'factory', value: templateFactory, quoted: false});
@ -66,7 +70,11 @@ export function compileComponent(
reflector: CompileReflector) { reflector: CompileReflector) {
const definitionMapValues: {key: string, quoted: boolean, value: o.Expression}[] = []; const definitionMapValues: {key: string, quoted: boolean, value: o.Expression}[] = [];
// e.g. `tag: 'my-app' // e.g. `type: MyApp`
definitionMapValues.push(
{key: 'type', value: outputCtx.importExpr(component.type.reference), quoted: false});
// e.g. `tag: 'my-app'`
// This is optional and only included if the first selector of a component has element. // This is optional and only included if the first selector of a component has element.
const selector = component.selector && CssSelector.parse(component.selector); const selector = component.selector && CssSelector.parse(component.selector);
const firstSelector = selector && selector[0]; const firstSelector = selector && selector[0];

View File

@ -178,6 +178,7 @@ describe('r3_view_compiler', () => {
// ChildComponent definition should be: // ChildComponent definition should be:
const ChildComponentDefinition = ` const ChildComponentDefinition = `
static ngComponentDef = IDENT.ɵdefineComponent({ static ngComponentDef = IDENT.ɵdefineComponent({
type: ChildComponent,
tag: 'child', tag: 'child',
factory: function ChildComponent_Factory() { return new ChildComponent(); }, factory: function ChildComponent_Factory() { return new ChildComponent(); },
template: function ChildComponent_Template(ctx: IDENT, cm: IDENT) { template: function ChildComponent_Template(ctx: IDENT, cm: IDENT) {
@ -190,6 +191,7 @@ describe('r3_view_compiler', () => {
// SomeDirective definition should be: // SomeDirective definition should be:
const SomeDirectiveDefinition = ` const SomeDirectiveDefinition = `
static ngDirectiveDef = IDENT.ɵdefineDirective({ static ngDirectiveDef = IDENT.ɵdefineDirective({
type: SomeDirective,
factory: function SomeDirective_Factory() {return new SomeDirective(); } factory: function SomeDirective_Factory() {return new SomeDirective(); }
}); });
`; `;
@ -197,6 +199,7 @@ describe('r3_view_compiler', () => {
// MyComponent definition should be: // MyComponent definition should be:
const MyComponentDefinition = ` const MyComponentDefinition = `
static ngComponentDef = IDENT.ɵdefineComponent({ static ngComponentDef = IDENT.ɵdefineComponent({
type: MyComponent,
tag: 'my-component', tag: 'my-component',
factory: function MyComponent_Factory() { return new MyComponent(); }, factory: function MyComponent_Factory() { return new MyComponent(); },
template: function MyComponent_Template(ctx: IDENT, cm: IDENT) { template: function MyComponent_Template(ctx: IDENT, cm: IDENT) {
@ -253,10 +256,12 @@ describe('r3_view_compiler', () => {
const IfDirectiveDefinition = ` const IfDirectiveDefinition = `
static ngDirectiveDef = IDENT.ɵdefineDirective({ static ngDirectiveDef = IDENT.ɵdefineDirective({
type: IfDirective,
factory: function IfDirective_Factory() { return new IfDirective(IDENT.ɵinjectTemplateRef()); } factory: function IfDirective_Factory() { return new IfDirective(IDENT.ɵinjectTemplateRef()); }
});`; });`;
const MyComponentDefinition = ` const MyComponentDefinition = `
static ngComponentDef = IDENT.ɵdefineComponent({ static ngComponentDef = IDENT.ɵdefineComponent({
type: MyComponent,
tag: 'my-component', tag: 'my-component',
factory: function MyComponent_Factory() { return new MyComponent(); }, factory: function MyComponent_Factory() { return new MyComponent(); },
template: function MyComponent_Template(ctx: IDENT, cm: IDENT) { template: function MyComponent_Template(ctx: IDENT, cm: IDENT) {
@ -309,6 +314,7 @@ describe('r3_view_compiler', () => {
const MyComponentDefinition = ` const MyComponentDefinition = `
static ngComponentDef = IDENT.ɵdefineComponent({ static ngComponentDef = IDENT.ɵdefineComponent({
type: MyComponent,
tag: 'my-component', tag: 'my-component',
factory: function MyComponent_Factory() { return new MyComponent(); }, factory: function MyComponent_Factory() { return new MyComponent(); },
template: function MyComponent_Template(ctx: IDENT, cm: IDENT) { template: function MyComponent_Template(ctx: IDENT, cm: IDENT) {