fix(compiler): support lifecycle hooks in compiler_cli

This commit is contained in:
Tobias Bosch
2016-05-04 10:00:59 -07:00
parent bdce154282
commit 7150ace7c7
15 changed files with 143 additions and 102 deletions

View File

@ -83,12 +83,12 @@ Run the compiler from source:
# Build angular2 and the compiler
./node_modules/.bin/tsc -p modules
# Run it on the test project
$ ./integrationtest.sh
$ ./modules/@angular/compiler_cli./integrationtest.sh
```
Release:
```
$ ./integrationtest.sh
$ ./modules/@angular/compiler_cli./integrationtest.sh
$ cp modules/@angular/compiler_cli/README.md modules/@angular/compiler_cli/package.json dist/all/@angular/compiler_cli
# npm login as angular
$ npm publish dist/all/@angular/compiler_cli --access=public

View File

@ -1,3 +1,4 @@
<div>{{ctxProp}}</div>
<form><input type="button" [(ngModel)]="ctxProp"/></form>
<my-comp *ngIf="ctxBool"></my-comp>
<my-comp *ngIf="ctxBool"></my-comp>
<div *ngFor="let x of ctxArr" [attr.value]="x"></div>

View File

@ -1,5 +1,5 @@
import {Component, Inject} from '@angular/core';
import {FORM_DIRECTIVES, NgIf} from '@angular/common';
import {FORM_DIRECTIVES, NgIf, NgFor} from '@angular/common';
import {MyComp} from './a/multiple_components';
@Component({
@ -7,10 +7,11 @@ import {MyComp} from './a/multiple_components';
templateUrl: './basic.html',
styles: ['.red { color: red }'],
styleUrls: ['./basic.css'],
directives: [MyComp, FORM_DIRECTIVES, NgIf]
directives: [MyComp, FORM_DIRECTIVES, NgIf, NgFor]
})
export class Basic {
ctxProp: string;
ctxBool: boolean;
ctxArr: any[] = [];
constructor() { this.ctxProp = 'initialValue'; }
}

View File

@ -28,7 +28,14 @@ describe("template codegen output", () => {
expect(fs.readFileSync(dtsOutput, {encoding: 'utf-8'})).toContain('Basic');
});
it("should be able to create the basic component and trigger an ngIf", () => {
it("should be able to create the basic component", () => {
const appInjector = ReflectiveInjector.resolveAndCreate(BROWSER_APP_STATIC_PROVIDERS,
browserPlatform().injector);
var comp = BasicNgFactory.create(appInjector);
expect(comp.instance).toBeTruthy();
});
it("should support ngIf", () => {
const appInjector = ReflectiveInjector.resolveAndCreate(BROWSER_APP_STATIC_PROVIDERS,
browserPlatform().injector);
var comp = BasicNgFactory.create(appInjector);
@ -40,4 +47,19 @@ describe("template codegen output", () => {
expect(debugElement.children.length).toBe(3);
expect(debugElement.children[2].injector.get(MyComp)).toBeTruthy();
});
it("should support ngFor", () => {
const appInjector = ReflectiveInjector.resolveAndCreate(BROWSER_APP_STATIC_PROVIDERS,
browserPlatform().injector);
var comp = BasicNgFactory.create(appInjector);
var debugElement = <DebugElement>getDebugNode(comp.location.nativeElement);
expect(debugElement.children.length).toBe(2);
// test NgFor
comp.instance.ctxArr = [1, 2];
comp.changeDetectorRef.detectChanges();
expect(debugElement.children.length).toBe(4);
expect(debugElement.children[2].attributes['value']).toBe('1');
expect(debugElement.children[3].attributes['value']).toBe('2');
});
});

View File

@ -2,7 +2,7 @@ import {reflector} from '@angular/core';
import {ReflectionCapabilities} from './core_private';
import {StaticReflector} from './static_reflector';
export class StaticAndDynamicReflectionCapabilities {
export class StaticAndDynamicReflectionCapabilities {
static install(staticDelegate: StaticReflector) {
reflector.updateCapabilities(new StaticAndDynamicReflectionCapabilities(staticDelegate));
}
@ -11,36 +11,30 @@ export class StaticAndDynamicReflectionCapabilities {
constructor(private staticDelegate: StaticReflector) {}
isReflectionEnabled(): boolean {
return true;
}
factory(type: any): Function {
return this.dynamicDelegate.factory(type);
}
interfaces(type: any): any[] {
return this.dynamicDelegate.interfaces(type);
isReflectionEnabled(): boolean { return true; }
factory(type: any): Function { return this.dynamicDelegate.factory(type); }
interfaces(type: any): any[] { return this.dynamicDelegate.interfaces(type); }
hasLifecycleHook(type: any, lcInterface: /*Type*/ any, lcProperty: string): boolean {
return isStaticType(type) ?
this.staticDelegate.hasLifecycleHook(type, lcInterface, lcProperty) :
this.dynamicDelegate.hasLifecycleHook(type, lcInterface, lcProperty);
}
parameters(type: any): any[][] {
return isStaticType(type) ? this.staticDelegate.parameters(type) : this.dynamicDelegate.parameters(type);
return isStaticType(type) ? this.staticDelegate.parameters(type) :
this.dynamicDelegate.parameters(type);
}
annotations(type: any): any[] {
return isStaticType(type) ? this.staticDelegate.annotations(type) : this.dynamicDelegate.annotations(type);
return isStaticType(type) ? this.staticDelegate.annotations(type) :
this.dynamicDelegate.annotations(type);
}
propMetadata(typeOrFunc: any): {[key: string]: any[]} {
return isStaticType(typeOrFunc) ? this.staticDelegate.propMetadata(typeOrFunc) : this.dynamicDelegate.propMetadata(typeOrFunc);
}
getter(name: string) {
return this.dynamicDelegate.getter(name);
}
setter(name: string) {
return this.dynamicDelegate.setter(name);
}
method(name: string) {
return this.dynamicDelegate.method(name);
}
importUri(type: any): string {
return this.staticDelegate.importUri(type);
return isStaticType(typeOrFunc) ? this.staticDelegate.propMetadata(typeOrFunc) :
this.dynamicDelegate.propMetadata(typeOrFunc);
}
getter(name: string) { return this.dynamicDelegate.getter(name); }
setter(name: string) { return this.dynamicDelegate.setter(name); }
method(name: string) { return this.dynamicDelegate.method(name); }
importUri(type: any): string { return this.staticDelegate.importUri(type); }
}
function isStaticType(type: any): boolean {

View File

@ -114,7 +114,7 @@ export class StaticReflector implements ReflectorReader {
public parameters(type: StaticSymbol): any[] {
if (!(type instanceof StaticSymbol)) {
throw new Error(`parameters recieved ${JSON.stringify(type)} which is not a StaticSymbol`);
throw new Error(`parameters received ${JSON.stringify(type)} which is not a StaticSymbol`);
}
try {
let parameters = this.parameterCache.get(type);
@ -124,8 +124,8 @@ export class StaticReflector implements ReflectorReader {
let ctorData = members ? members['__ctor__'] : null;
if (ctorData) {
let ctor = (<any[]>ctorData).find(a => a['__symbolic'] == 'constructor');
let parameterTypes = <any[]>this.simplify(type, ctor['parameters']);
let parameterDecorators = <any[]>this.simplify(type, ctor['parameterDecorators']);
let parameterTypes = <any[]>this.simplify(type, ctor['parameters'] || []);
let parameterDecorators = <any[]>this.simplify(type, ctor['parameterDecorators'] || []);
parameters = [];
parameterTypes.forEach((paramType, index) => {
@ -152,6 +152,17 @@ export class StaticReflector implements ReflectorReader {
}
}
hasLifecycleHook(type: any, lcInterface: /*Type*/ any, lcProperty: string): boolean {
if (!(type instanceof StaticSymbol)) {
throw new Error(
`hasLifecycleHook received ${JSON.stringify(type)} which is not a StaticSymbol`);
}
let classMetadata = this.getTypeMetadata(type);
let members = classMetadata ? classMetadata['members'] : null;
let member:any[] = members ? members[lcProperty] : null;
return member ? member.some(a => a['__symbolic'] == 'method') : false;
}
private registerDecoratorOrConstructor(type: StaticSymbol, ctor: any): void {
this.conversionMap.set(type, (context: StaticSymbol, args: any[]) => {
let argValues: any[] = [];