refactor(compiler): simplify metadata

This commit is contained in:
Tobias Bosch 2015-08-27 09:03:18 -07:00
parent 3468f7cfd5
commit 0f4eb1b524
3 changed files with 17 additions and 16 deletions

View File

@ -1,3 +1,4 @@
import {isPresent} from 'angular2/src/core/facade/lang';
import {HtmlAst} from './html_ast'; import {HtmlAst} from './html_ast';
export class TypeMeta { export class TypeMeta {
@ -54,18 +55,18 @@ export enum ViewEncapsulation {
export class DirectiveMetadata { export class DirectiveMetadata {
type: TypeMeta; type: TypeMeta;
isComponent: boolean;
selector: string; selector: string;
constructor({type, selector}: {type?: TypeMeta, selector?: string} = {}) {
this.type = type;
this.selector = selector;
}
}
export class ComponentMetadata extends DirectiveMetadata {
template: TemplateMeta; template: TemplateMeta;
constructor({type, selector, template}: constructor({type, isComponent, selector, template}: {
{type?: TypeMeta, selector?: string, template?: TemplateMeta}) { type?: TypeMeta,
super({type: type, selector: selector}); isComponent?: boolean,
selector?: string,
template?: TemplateMeta
} = {}) {
this.type = type;
this.isComponent = isPresent(isComponent) ? isComponent : false;
this.selector = selector;
this.template = template; this.template = template;
} }
} }

View File

@ -12,7 +12,7 @@ import {
import {Parser, AST, ASTWithSource} from 'angular2/src/core/change_detection/change_detection'; import {Parser, AST, ASTWithSource} from 'angular2/src/core/change_detection/change_detection';
import {DirectiveMetadata, ComponentMetadata} from './api'; import {DirectiveMetadata} from './api';
import { import {
ElementAst, ElementAst,
BoundPropertyAst, BoundPropertyAst,
@ -279,8 +279,8 @@ class TemplateParseVisitor implements HtmlAstVisitor {
// as selectorMatcher uses Maps inside. // as selectorMatcher uses Maps inside.
// Also need to make components the first directive in the array // Also need to make components the first directive in the array
ListWrapper.sort(directives, (dir1: DirectiveMetadata, dir2: DirectiveMetadata) => { ListWrapper.sort(directives, (dir1: DirectiveMetadata, dir2: DirectiveMetadata) => {
var dir1Comp = dir1 instanceof ComponentMetadata; var dir1Comp = dir1.isComponent;
var dir2Comp = dir2 instanceof ComponentMetadata; var dir2Comp = dir2.isComponent;
if (dir1Comp && !dir2Comp) { if (dir1Comp && !dir2Comp) {
return -1; return -1;
} else if (!dir1Comp && dir2Comp) { } else if (!dir1Comp && dir2Comp) {

View File

@ -3,7 +3,7 @@ import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach} from '
import {Parser, Lexer} from 'angular2/src/core/change_detection/change_detection'; import {Parser, Lexer} from 'angular2/src/core/change_detection/change_detection';
import {TemplateParser, splitClasses} from 'angular2/src/compiler/template_parser'; import {TemplateParser, splitClasses} from 'angular2/src/compiler/template_parser';
import {HtmlParser} from 'angular2/src/compiler/html_parser'; import {HtmlParser} from 'angular2/src/compiler/html_parser';
import {DirectiveMetadata, ComponentMetadata, TypeMeta} from 'angular2/src/compiler/api'; import {DirectiveMetadata, TypeMeta} from 'angular2/src/compiler/api';
import { import {
templateVisitAll, templateVisitAll,
TemplateAstVisitor, TemplateAstVisitor,
@ -192,8 +192,8 @@ export function main() {
new DirectiveMetadata({selector: '[a=b]', type: new TypeMeta({typeName: 'DirA'})}); new DirectiveMetadata({selector: '[a=b]', type: new TypeMeta({typeName: 'DirA'})});
var dirB = var dirB =
new DirectiveMetadata({selector: '[a]', type: new TypeMeta({typeName: 'DirB'})}); new DirectiveMetadata({selector: '[a]', type: new TypeMeta({typeName: 'DirB'})});
var comp = var comp = new DirectiveMetadata(
new ComponentMetadata({selector: 'div', type: new TypeMeta({typeName: 'ZComp'})}); {selector: 'div', isComponent: true, type: new TypeMeta({typeName: 'ZComp'})});
expect(humanizeTemplateAsts(parse('<div a="b">', [dirB, dirA, comp]))) expect(humanizeTemplateAsts(parse('<div a="b">', [dirB, dirA, comp])))
.toEqual([ .toEqual([
[ElementAst, [comp, dirA, dirB], 'TestComp > div:nth-child(0)'], [ElementAst, [comp, dirA, dirB], 'TestComp > div:nth-child(0)'],