Skip to content

Commit

Permalink
refactor(compiler): cleanup and preparation for integration
Browse files Browse the repository at this point in the history
- 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 #3605
Closes #4314
  • Loading branch information
tbosch committed Sep 22, 2015
1 parent eb7839e commit cc0c304
Show file tree
Hide file tree
Showing 37 changed files with 1,477 additions and 1,164 deletions.
42 changes: 25 additions & 17 deletions modules/angular2/src/compiler/change_definition_factory.ts
Expand Up @@ -14,7 +14,7 @@ import {
ASTWithSource
} from 'angular2/src/core/change_detection/change_detection';

import {NormalizedDirectiveMetadata, TypeMetadata} from './directive_metadata';
import {CompileDirectiveMetadata, CompileTypeMetadata} from './directive_metadata';
import {
TemplateAst,
ElementAst,
Expand All @@ -32,9 +32,10 @@ import {
AttrAst,
TextAst
} from './template_ast';
import {LifecycleHooks} from 'angular2/src/core/compiler/interfaces';

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

visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any {
this.boundElementCount++;
templateVisitAll(this, ast.directives);
for (var i = 0; i < ast.directives.length; i++) {
ast.directives[i].visit(this, i);
}

var childVisitor =
new ProtoViewVisitor(this, this.allVisitors, ChangeDetectionStrategy.Default);
Expand All @@ -76,7 +79,7 @@ class ProtoViewVisitor implements TemplateAstVisitor {
}
templateVisitAll(this, ast.properties, null);
templateVisitAll(this, ast.events);
templateVisitAll(this, ast.vars);
templateVisitAll(this, ast.exportAsVars);
for (var i = 0; i < ast.directives.length; i++) {
ast.directives[i].visit(this, i);
}
Expand All @@ -94,8 +97,8 @@ class ProtoViewVisitor implements TemplateAstVisitor {
visitEvent(ast: BoundEventAst, directiveRecord: DirectiveRecord): any {
var bindingRecord =
isPresent(directiveRecord) ?
BindingRecord.createForHostEvent(ast.handler, ast.name, directiveRecord) :
BindingRecord.createForEvent(ast.handler, ast.name, this.boundElementCount - 1);
BindingRecord.createForHostEvent(ast.handler, ast.fullName, directiveRecord) :
BindingRecord.createForEvent(ast.handler, ast.fullName, this.boundElementCount - 1);
this.eventRecords.push(bindingRecord);
return null;
}
Expand Down Expand Up @@ -138,17 +141,20 @@ class ProtoViewVisitor implements TemplateAstVisitor {
visitDirective(ast: DirectiveAst, directiveIndexAsNumber: number): any {
var directiveIndex = new DirectiveIndex(this.boundElementCount - 1, directiveIndexAsNumber);
var directiveMetadata = ast.directive;
var changeDetectionMeta = directiveMetadata.changeDetection;
var directiveRecord = new DirectiveRecord({
directiveIndex: directiveIndex,
callAfterContentInit: changeDetectionMeta.callAfterContentInit,
callAfterContentChecked: changeDetectionMeta.callAfterContentChecked,
callAfterViewInit: changeDetectionMeta.callAfterViewInit,
callAfterViewChecked: changeDetectionMeta.callAfterViewChecked,
callOnChanges: changeDetectionMeta.callOnChanges,
callDoCheck: changeDetectionMeta.callDoCheck,
callOnInit: changeDetectionMeta.callOnInit,
changeDetection: changeDetectionMeta.changeDetection
callAfterContentInit:
directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.AfterContentInit) !== -1,
callAfterContentChecked:
directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.AfterContentChecked) !== -1,
callAfterViewInit:
directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.AfterViewInit) !== -1,
callAfterViewChecked:
directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.AfterViewChecked) !== -1,
callOnChanges: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnChanges) !== -1,
callDoCheck: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.DoCheck) !== -1,
callOnInit: directiveMetadata.lifecycleHooks.indexOf(LifecycleHooks.OnInit) !== -1,
changeDetection: directiveMetadata.changeDetection
});
this.directiveRecords.push(directiveRecord);

Expand All @@ -165,6 +171,7 @@ class ProtoViewVisitor implements TemplateAstVisitor {
}
templateVisitAll(this, ast.hostProperties, directiveRecord);
templateVisitAll(this, ast.hostEvents, directiveRecord);
templateVisitAll(this, ast.exportAsVars);
return null;
}
visitDirectiveProperty(ast: BoundDirectivePropertyAst, directiveRecord: DirectiveRecord): any {
Expand All @@ -178,7 +185,7 @@ class ProtoViewVisitor implements TemplateAstVisitor {
}


function createChangeDefinitions(pvVisitors: ProtoViewVisitor[], componentType: TypeMetadata,
function createChangeDefinitions(pvVisitors: ProtoViewVisitor[], componentType: CompileTypeMetadata,
genConfig: ChangeDetectorGenConfig): ChangeDetectorDefinition[] {
var pvVariableNames = _collectNestedProtoViewsVariableNames(pvVisitors);
return pvVisitors.map(pvVisitor => {
Expand All @@ -202,6 +209,7 @@ function _collectNestedProtoViewsVariableNames(pvVisitors: ProtoViewVisitor[]):
}


function _protoViewId(hostComponentType: TypeMetadata, pvIndex: number, viewType: string): string {
function _protoViewId(hostComponentType: CompileTypeMetadata, pvIndex: number, viewType: string):
string {
return `${hostComponentType.name}_${viewType}_${pvIndex}`;
}
13 changes: 6 additions & 7 deletions modules/angular2/src/compiler/change_detector_compiler.ts
@@ -1,5 +1,5 @@
import {TypeMetadata} from './directive_metadata';
import {SourceExpression, moduleRef} from './source_module';
import {CompileTypeMetadata} from './directive_metadata';
import {SourceExpressions, moduleRef} from './source_module';
import {
ChangeDetectorJITGenerator
} from 'angular2/src/core/change_detection/change_detection_jit_generator';
Expand Down Expand Up @@ -32,7 +32,7 @@ var PREGEN_PROTO_CHANGE_DETECTOR_MODULE =
export class ChangeDetectionCompiler {
constructor(private _genConfig: ChangeDetectorGenConfig) {}

compileComponentRuntime(componentType: TypeMetadata, strategy: ChangeDetectionStrategy,
compileComponentRuntime(componentType: CompileTypeMetadata, strategy: ChangeDetectionStrategy,
parsedTemplate: TemplateAst[]): Function[] {
var changeDetectorDefinitions =
createChangeDetectorDefinitions(componentType, strategy, this._genConfig, parsedTemplate);
Expand All @@ -51,8 +51,8 @@ export class ChangeDetectionCompiler {
}
}

compileComponentCodeGen(componentType: TypeMetadata, strategy: ChangeDetectionStrategy,
parsedTemplate: TemplateAst[]): SourceExpression {
compileComponentCodeGen(componentType: CompileTypeMetadata, strategy: ChangeDetectionStrategy,
parsedTemplate: TemplateAst[]): SourceExpressions {
var changeDetectorDefinitions =
createChangeDetectorDefinitions(componentType, strategy, this._genConfig, parsedTemplate);
var factories = [];
Expand All @@ -75,7 +75,6 @@ export class ChangeDetectionCompiler {
return codegen.generateSource();
}
});
var expression = `[ ${factories.join(',')} ]`;
return new SourceExpression(sourceParts, expression);
return new SourceExpressions(sourceParts, factories);
}
}

0 comments on commit cc0c304

Please sign in to comment.