Skip to content

Commit cc0c304

Browse files
committed
refactor(compiler): cleanup and preparation for integration
- Rename `DirectiveMetadata` into `CompileDirectiveMetadata`, merge with `NormalizedDirectiveMetadata` and remove `ChangeDetectionMetadata` - Store change detector factories not as array but directly at the `CompiledTemplate` or the embedded template to make instantiation easier later on - Already analyze variable values and map them to `Directive.exportAs` - Keep the directive sort order as specified in the `@View()` annotation - Allow to clear the runtime cache in `StyleCompiler` and `TemplateCompiler` - Ignore `script` elements to match the semantics of the current compiler - Make all components dynamically loadable and remove the previously introduced property `@Component#dynamicLoadable` for now until we find a better option to configure this - Don’t allow to specify bindings in `@View#directives` and `@View#pipes` as this was never supported by the transformer (see below for the breaking change) BREAKING CHANGE: - don't support DI bindings in `@View#directives` and `@View@pipes` any more in preparation of integrating the new compiler. Use `@Directive#bindings` to reexport directives under a different token instead. Part of angular#3605 Closes angular#4314
1 parent eb7839e commit cc0c304

37 files changed

+1477
-1164
lines changed

modules/angular2/src/compiler/change_definition_factory.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
ASTWithSource
1515
} from 'angular2/src/core/change_detection/change_detection';
1616

17-
import {NormalizedDirectiveMetadata, TypeMetadata} from './directive_metadata';
17+
import {CompileDirectiveMetadata, CompileTypeMetadata} from './directive_metadata';
1818
import {
1919
TemplateAst,
2020
ElementAst,
@@ -32,9 +32,10 @@ import {
3232
AttrAst,
3333
TextAst
3434
} from './template_ast';
35+
import {LifecycleHooks} from 'angular2/src/core/compiler/interfaces';
3536

3637
export function createChangeDetectorDefinitions(
37-
componentType: TypeMetadata, componentStrategy: ChangeDetectionStrategy,
38+
componentType: CompileTypeMetadata, componentStrategy: ChangeDetectionStrategy,
3839
genConfig: ChangeDetectorGenConfig, parsedTemplate: TemplateAst[]): ChangeDetectorDefinition[] {
3940
var pvVisitors = [];
4041
var visitor = new ProtoViewVisitor(null, pvVisitors, componentStrategy);
@@ -59,7 +60,9 @@ class ProtoViewVisitor implements TemplateAstVisitor {
5960

6061
visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any {
6162
this.boundElementCount++;
62-
templateVisitAll(this, ast.directives);
63+
for (var i = 0; i < ast.directives.length; i++) {
64+
ast.directives[i].visit(this, i);
65+
}
6366

6467
var childVisitor =
6568
new ProtoViewVisitor(this, this.allVisitors, ChangeDetectionStrategy.Default);
@@ -76,7 +79,7 @@ class ProtoViewVisitor implements TemplateAstVisitor {
7679
}
7780
templateVisitAll(this, ast.properties, null);
7881
templateVisitAll(this, ast.events);
79-
templateVisitAll(this, ast.vars);
82+
templateVisitAll(this, ast.exportAsVars);
8083
for (var i = 0; i < ast.directives.length; i++) {
8184
ast.directives[i].visit(this, i);
8285
}
@@ -94,8 +97,8 @@ class ProtoViewVisitor implements TemplateAstVisitor {
9497
visitEvent(ast: BoundEventAst, directiveRecord: DirectiveRecord): any {
9598
var bindingRecord =
9699
isPresent(directiveRecord) ?
97-
BindingRecord.createForHostEvent(ast.handler, ast.name, directiveRecord) :
98-
BindingRecord.createForEvent(ast.handler, ast.name, this.boundElementCount - 1);
100+
BindingRecord.createForHostEvent(ast.handler, ast.fullName, directiveRecord) :
101+
BindingRecord.createForEvent(ast.handler, ast.fullName, this.boundElementCount - 1);
99102
this.eventRecords.push(bindingRecord);
100103
return null;
101104
}
@@ -138,17 +141,20 @@ class ProtoViewVisitor implements TemplateAstVisitor {
138141
visitDirective(ast: DirectiveAst, directiveIndexAsNumber: number): any {
139142
var directiveIndex = new DirectiveIndex(this.boundElementCount - 1, directiveIndexAsNumber);
140143
var directiveMetadata = ast.directive;
141-
var changeDetectionMeta = directiveMetadata.changeDetection;
142144
var directiveRecord = new DirectiveRecord({
143145
directiveIndex: directiveIndex,
144-
callAfterContentInit: changeDetectionMeta.callAfterContentInit,
145-
callAfterContentChecked: changeDetectionMeta.callAfterContentChecked,
146-
callAfterViewInit: changeDetectionMeta.callAfterViewInit,
147-
callAfterViewChecked: changeDetectionMeta.callAfterViewChecked,
148-
callOnChanges: changeDetectionMeta.callOnChanges,
149-
callDoCheck: changeDetectionMeta.callDoCheck,
150-
callOnInit: changeDetectionMeta.callOnInit,
151-
changeDetection: changeDetectionMeta.changeDetection
146+
callAfterContentInit:
147+
directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.AfterContentInit) !== -1,
148+
callAfterContentChecked:
149+
directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.AfterContentChecked) !== -1,
150+
callAfterViewInit:
151+
directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.AfterViewInit) !== -1,
152+
callAfterViewChecked:
153+
directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.AfterViewChecked) !== -1,
154+
callOnChanges: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnChanges) !== -1,
155+
callDoCheck: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.DoCheck) !== -1,
156+
callOnInit: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnInit) !== -1,
157+
changeDetection: directiveMetadata.changeDetection
152158
});
153159
this.directiveRecords.push(directiveRecord);
154160

@@ -165,6 +171,7 @@ class ProtoViewVisitor implements TemplateAstVisitor {
165171
}
166172
templateVisitAll(this, ast.hostProperties, directiveRecord);
167173
templateVisitAll(this, ast.hostEvents, directiveRecord);
174+
templateVisitAll(this, ast.exportAsVars);
168175
return null;
169176
}
170177
visitDirectiveProperty(ast: BoundDirectivePropertyAst, directiveRecord: DirectiveRecord): any {
@@ -178,7 +185,7 @@ class ProtoViewVisitor implements TemplateAstVisitor {
178185
}
179186

180187

181-
function createChangeDefinitions(pvVisitors: ProtoViewVisitor[], componentType: TypeMetadata,
188+
function createChangeDefinitions(pvVisitors: ProtoViewVisitor[], componentType: CompileTypeMetadata,
182189
genConfig: ChangeDetectorGenConfig): ChangeDetectorDefinition[] {
183190
var pvVariableNames = _collectNestedProtoViewsVariableNames(pvVisitors);
184191
return pvVisitors.map(pvVisitor => {
@@ -202,6 +209,7 @@ function _collectNestedProtoViewsVariableNames(pvVisitors: ProtoViewVisitor[]):
202209
}
203210

204211

205-
function _protoViewId(hostComponentType: TypeMetadata, pvIndex: number, viewType: string): string {
212+
function _protoViewId(hostComponentType: CompileTypeMetadata, pvIndex: number, viewType: string):
213+
string {
206214
return `${hostComponentType.name}_${viewType}_${pvIndex}`;
207215
}

modules/angular2/src/compiler/change_detector_compiler.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {TypeMetadata} from './directive_metadata';
2-
import {SourceExpression, moduleRef} from './source_module';
1+
import {CompileTypeMetadata} from './directive_metadata';
2+
import {SourceExpressions, moduleRef} from './source_module';
33
import {
44
ChangeDetectorJITGenerator
55
} from 'angular2/src/core/change_detection/change_detection_jit_generator';
@@ -32,7 +32,7 @@ var PREGEN_PROTO_CHANGE_DETECTOR_MODULE =
3232
export class ChangeDetectionCompiler {
3333
constructor(private _genConfig: ChangeDetectorGenConfig) {}
3434

35-
compileComponentRuntime(componentType: TypeMetadata, strategy: ChangeDetectionStrategy,
35+
compileComponentRuntime(componentType: CompileTypeMetadata, strategy: ChangeDetectionStrategy,
3636
parsedTemplate: TemplateAst[]): Function[] {
3737
var changeDetectorDefinitions =
3838
createChangeDetectorDefinitions(componentType, strategy, this._genConfig, parsedTemplate);
@@ -51,8 +51,8 @@ export class ChangeDetectionCompiler {
5151
}
5252
}
5353

54-
compileComponentCodeGen(componentType: TypeMetadata, strategy: ChangeDetectionStrategy,
55-
parsedTemplate: TemplateAst[]): SourceExpression {
54+
compileComponentCodeGen(componentType: CompileTypeMetadata, strategy: ChangeDetectionStrategy,
55+
parsedTemplate: TemplateAst[]): SourceExpressions {
5656
var changeDetectorDefinitions =
5757
createChangeDetectorDefinitions(componentType, strategy, this._genConfig, parsedTemplate);
5858
var factories = [];
@@ -75,7 +75,6 @@ export class ChangeDetectionCompiler {
7575
return codegen.generateSource();
7676
}
7777
});
78-
var expression = `[ ${factories.join(',')} ]`;
79-
return new SourceExpression(sourceParts, expression);
78+
return new SourceExpressions(sourceParts, factories);
8079
}
8180
}

0 commit comments

Comments
 (0)