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 #3605
Closes #4314
This commit is contained in:
Tobias Bosch
2015-09-18 10:33:23 -07:00
parent eb7839e0ec
commit cc0c30484f
37 changed files with 1480 additions and 1167 deletions

View File

@ -16,10 +16,9 @@ import {TEST_BINDINGS} from './test_bindings';
import {isPresent} from 'angular2/src/core/facade/lang';
import {TemplateParser, splitClasses} from 'angular2/src/compiler/template_parser';
import {
NormalizedDirectiveMetadata,
TypeMetadata,
ChangeDetectionMetadata,
NormalizedTemplateMetadata
CompileDirectiveMetadata,
CompileTypeMetadata,
CompileTemplateMetadata
} from 'angular2/src/compiler/directive_metadata';
import {
templateVisitAll,
@ -59,14 +58,14 @@ export function main() {
beforeEach(inject([TemplateParser], (_parser) => {
parser = _parser;
ngIf = new NormalizedDirectiveMetadata({
ngIf = CompileDirectiveMetadata.create({
selector: '[ng-if]',
type: new TypeMetadata({name: 'NgIf'}),
changeDetection: new ChangeDetectionMetadata({properties: ['ngIf']})
type: new CompileTypeMetadata({name: 'NgIf'}),
properties: ['ngIf']
});
}));
function parse(template: string, directives: NormalizedDirectiveMetadata[]): TemplateAst[] {
function parse(template: string, directives: CompileDirectiveMetadata[]): TemplateAst[] {
return parser.parse(template, directives, 'TestComp');
}
@ -319,68 +318,39 @@ export function main() {
});
describe('variables', () => {
it('should parse variables via #... and not report them as attributes', () => {
expect(humanizeTemplateAsts(parse('<div #a="b">', [])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', 'b', 'TestComp > div:nth-child(0)[#a=b]']
]);
});
it('should parse variables via var-... and not report them as attributes', () => {
expect(humanizeTemplateAsts(parse('<div var-a="b">', [])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', 'b', 'TestComp > div:nth-child(0)[var-a=b]']
]);
});
it('should camel case variables', () => {
expect(humanizeTemplateAsts(parse('<div var-some-a="b">', [])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[VariableAst, 'someA', 'b', 'TestComp > div:nth-child(0)[var-some-a=b]']
]);
});
it('should use $implicit as variable name if none was specified', () => {
expect(humanizeTemplateAsts(parse('<div var-a>', [])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', '$implicit', 'TestComp > div:nth-child(0)[var-a=]']
]);
});
});
describe('directives', () => {
it('should locate directives ordered by name and components first', () => {
var dirA = new NormalizedDirectiveMetadata(
{selector: '[a=b]', type: new TypeMetadata({name: 'DirA'})});
var dirB = new NormalizedDirectiveMetadata(
{selector: '[a]', type: new TypeMetadata({name: 'DirB'})});
var comp = new NormalizedDirectiveMetadata({
selector: 'div',
isComponent: true,
type: new TypeMetadata({name: 'ZComp'}),
template: new NormalizedTemplateMetadata({ngContentSelectors: []})
});
expect(humanizeTemplateAsts(parse('<div a="b">', [dirB, dirA, comp])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[AttrAst, 'a', 'b', 'TestComp > div:nth-child(0)[a=b]'],
[DirectiveAst, comp, 'TestComp > div:nth-child(0)'],
[DirectiveAst, dirA, 'TestComp > div:nth-child(0)'],
[DirectiveAst, dirB, 'TestComp > div:nth-child(0)']
]);
});
it('should locate directives components first and ordered by the directives array in the View',
() => {
var dirA = CompileDirectiveMetadata.create(
{selector: '[a]', type: new CompileTypeMetadata({name: 'DirA', id: 3})});
var dirB = CompileDirectiveMetadata.create(
{selector: '[b]', type: new CompileTypeMetadata({name: 'DirB', id: 2})});
var dirC = CompileDirectiveMetadata.create(
{selector: '[c]', type: new CompileTypeMetadata({name: 'DirC', id: 1})});
var comp = CompileDirectiveMetadata.create({
selector: 'div',
isComponent: true,
type: new CompileTypeMetadata({name: 'ZComp'}),
template: new CompileTemplateMetadata({ngContentSelectors: []})
});
expect(humanizeTemplateAsts(parse('<div a c b>', [dirA, dirB, dirC, comp])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[AttrAst, 'a', '', 'TestComp > div:nth-child(0)[a=]'],
[AttrAst, 'b', '', 'TestComp > div:nth-child(0)[b=]'],
[AttrAst, 'c', '', 'TestComp > div:nth-child(0)[c=]'],
[DirectiveAst, comp, 'TestComp > div:nth-child(0)'],
[DirectiveAst, dirA, 'TestComp > div:nth-child(0)'],
[DirectiveAst, dirB, 'TestComp > div:nth-child(0)'],
[DirectiveAst, dirC, 'TestComp > div:nth-child(0)']
]);
});
it('should locate directives in property bindings', () => {
var dirA = new NormalizedDirectiveMetadata(
{selector: '[a=b]', type: new TypeMetadata({name: 'DirA'})});
var dirB = new NormalizedDirectiveMetadata(
{selector: '[b]', type: new TypeMetadata({name: 'DirB'})});
var dirA = CompileDirectiveMetadata.create(
{selector: '[a=b]', type: new CompileTypeMetadata({name: 'DirA'})});
var dirB = CompileDirectiveMetadata.create(
{selector: '[b]', type: new CompileTypeMetadata({name: 'DirB'})});
expect(humanizeTemplateAsts(parse('<div [a]="b">', [dirA, dirB])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
@ -397,23 +367,23 @@ export function main() {
});
it('should locate directives in variable bindings', () => {
var dirA = new NormalizedDirectiveMetadata(
{selector: '[a=b]', type: new TypeMetadata({name: 'DirA'})});
var dirB = new NormalizedDirectiveMetadata(
{selector: '[b]', type: new TypeMetadata({name: 'DirB'})});
var dirA = CompileDirectiveMetadata.create(
{selector: '[a=b]', exportAs: 'b', type: new CompileTypeMetadata({name: 'DirA'})});
var dirB = CompileDirectiveMetadata.create(
{selector: '[b]', type: new CompileTypeMetadata({name: 'DirB'})});
expect(humanizeTemplateAsts(parse('<div #a="b">', [dirA, dirB])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', 'b', 'TestComp > div:nth-child(0)[#a=b]'],
[DirectiveAst, dirA, 'TestComp > div:nth-child(0)']
[DirectiveAst, dirA, 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', 'b', 'TestComp > div:nth-child(0)[#a=b]']
]);
});
it('should parse directive host properties', () => {
var dirA = new NormalizedDirectiveMetadata({
var dirA = CompileDirectiveMetadata.create({
selector: 'div',
type: new TypeMetadata({name: 'DirA'}),
changeDetection: new ChangeDetectionMetadata({hostProperties: {'a': 'expr'}})
type: new CompileTypeMetadata({name: 'DirA'}),
host: {'[a]': 'expr'}
});
expect(humanizeTemplateAsts(parse('<div></div>', [dirA])))
.toEqual([
@ -431,10 +401,10 @@ export function main() {
});
it('should parse directive host listeners', () => {
var dirA = new NormalizedDirectiveMetadata({
var dirA = CompileDirectiveMetadata.create({
selector: 'div',
type: new TypeMetadata({name: 'DirA'}),
changeDetection: new ChangeDetectionMetadata({hostListeners: {'a': 'expr'}})
type: new CompileTypeMetadata({name: 'DirA'}),
host: {'(a)': 'expr'}
});
expect(humanizeTemplateAsts(parse('<div></div>', [dirA])))
.toEqual([
@ -445,10 +415,10 @@ export function main() {
});
it('should parse directive properties', () => {
var dirA = new NormalizedDirectiveMetadata({
var dirA = CompileDirectiveMetadata.create({
selector: 'div',
type: new TypeMetadata({name: 'DirA'}),
changeDetection: new ChangeDetectionMetadata({properties: ['aProp']})
type: new CompileTypeMetadata({name: 'DirA'}),
properties: ['aProp']
});
expect(humanizeTemplateAsts(parse('<div [a-prop]="expr"></div>', [dirA])))
.toEqual([
@ -464,10 +434,10 @@ export function main() {
});
it('should parse renamed directive properties', () => {
var dirA = new NormalizedDirectiveMetadata({
var dirA = CompileDirectiveMetadata.create({
selector: 'div',
type: new TypeMetadata({name: 'DirA'}),
changeDetection: new ChangeDetectionMetadata({properties: ['b:a']})
type: new CompileTypeMetadata({name: 'DirA'}),
properties: ['b:a']
});
expect(humanizeTemplateAsts(parse('<div [a]="expr"></div>', [dirA])))
.toEqual([
@ -478,11 +448,8 @@ export function main() {
});
it('should parse literal directive properties', () => {
var dirA = new NormalizedDirectiveMetadata({
selector: 'div',
type: new TypeMetadata({name: 'DirA'}),
changeDetection: new ChangeDetectionMetadata({properties: ['a']})
});
var dirA = CompileDirectiveMetadata.create(
{selector: 'div', type: new CompileTypeMetadata({name: 'DirA'}), properties: ['a']});
expect(humanizeTemplateAsts(parse('<div a="literal"></div>', [dirA])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
@ -498,11 +465,8 @@ export function main() {
});
it('should support optional directive properties', () => {
var dirA = new NormalizedDirectiveMetadata({
selector: 'div',
type: new TypeMetadata({name: 'DirA'}),
changeDetection: new ChangeDetectionMetadata({properties: ['a']})
});
var dirA = CompileDirectiveMetadata.create(
{selector: 'div', type: new CompileTypeMetadata({name: 'DirA'}), properties: ['a']});
expect(humanizeTemplateAsts(parse('<div></div>', [dirA])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
@ -512,6 +476,83 @@ export function main() {
});
describe('variables', () => {
it('should parse variables via #... and not report them as attributes', () => {
expect(humanizeTemplateAsts(parse('<div #a>', [])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', '', 'TestComp > div:nth-child(0)[#a=]']
]);
});
it('should parse variables via var-... and not report them as attributes', () => {
expect(humanizeTemplateAsts(parse('<div var-a>', [])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', '', 'TestComp > div:nth-child(0)[var-a=]']
]);
});
it('should camel case variables', () => {
expect(humanizeTemplateAsts(parse('<div var-some-a>', [])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[VariableAst, 'someA', '', 'TestComp > div:nth-child(0)[var-some-a=]']
]);
});
it('should assign variables with empty value to element', () => {
expect(humanizeTemplateAsts(parse('<div #a></div>', [])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', '', 'TestComp > div:nth-child(0)[#a=]']
]);
});
it('should assign variables to directives via exportAs', () => {
var dirA = CompileDirectiveMetadata.create(
{selector: '[a]', type: new CompileTypeMetadata({name: 'DirA'}), exportAs: 'dirA'});
expect(humanizeTemplateAsts(parse('<div #a="dirA"></div>', [dirA])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[DirectiveAst, dirA, 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', 'dirA', 'TestComp > div:nth-child(0)[#a=dirA]']
]);
});
it('should report variables with values that dont match a directive as errors', () => {
expect(() => parse('<div #a="dirA"></div>', [])).toThrowError(`Template parse errors:
There is no directive with "exportAs" set to "dirA" at TestComp > div:nth-child(0)[#a=dirA]`);
});
it('should allow variables with values that dont match a directive on embedded template elements',
() => {
expect(humanizeTemplateAsts(parse('<template #a="b"></template>', [])))
.toEqual([
[EmbeddedTemplateAst, 'TestComp > template:nth-child(0)'],
[VariableAst, 'a', 'b', 'TestComp > template:nth-child(0)[#a=b]']
]);
});
it('should assign variables with empty value to components', () => {
var dirA = CompileDirectiveMetadata.create({
selector: '[a]',
isComponent: true,
type: new CompileTypeMetadata({name: 'DirA'}),
exportAs: 'dirA', template: new CompileTemplateMetadata({ngContentSelectors: []})
});
expect(humanizeTemplateAsts(parse('<div #a></div>', [dirA])))
.toEqual([
[ElementAst, 'div', 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', '', 'TestComp > div:nth-child(0)[#a=]'],
[DirectiveAst, dirA, 'TestComp > div:nth-child(0)'],
[VariableAst, 'a', '', 'TestComp > div:nth-child(0)[#a=]']
]);
});
});
describe('explicit templates', () => {
it('should create embedded templates for <template> elements', () => {
expect(humanizeTemplateAsts(parse('<template></template>', [])))
@ -563,13 +604,13 @@ export function main() {
describe('directives', () => {
it('should locate directives in property bindings', () => {
var dirA = new NormalizedDirectiveMetadata({
var dirA = CompileDirectiveMetadata.create({
selector: '[a=b]',
type: new TypeMetadata({name: 'DirA'}),
changeDetection: new ChangeDetectionMetadata({properties: ['a']})
type: new CompileTypeMetadata({name: 'DirA'}),
properties: ['a']
});
var dirB = new NormalizedDirectiveMetadata(
{selector: '[b]', type: new TypeMetadata({name: 'DirB'})});
var dirB = CompileDirectiveMetadata.create(
{selector: '[b]', type: new CompileTypeMetadata({name: 'DirB'})});
expect(humanizeTemplateAsts(parse('<div template="a b" b>', [dirA, dirB])))
.toEqual([
[EmbeddedTemplateAst, 'TestComp > div:nth-child(0)'],
@ -587,10 +628,10 @@ export function main() {
});
it('should locate directives in variable bindings', () => {
var dirA = new NormalizedDirectiveMetadata(
{selector: '[a=b]', type: new TypeMetadata({name: 'DirA'})});
var dirB = new NormalizedDirectiveMetadata(
{selector: '[b]', type: new TypeMetadata({name: 'DirB'})});
var dirA = CompileDirectiveMetadata.create(
{selector: '[a=b]', type: new CompileTypeMetadata({name: 'DirA'})});
var dirB = CompileDirectiveMetadata.create(
{selector: '[b]', type: new CompileTypeMetadata({name: 'DirB'})});
expect(humanizeTemplateAsts(parse('<div template="#a=b" b>', [dirA, dirB])))
.toEqual([
[EmbeddedTemplateAst, 'TestComp > div:nth-child(0)'],
@ -624,12 +665,12 @@ export function main() {
describe('content projection', () => {
function createComp(selector: string, ngContentSelectors: string[]):
NormalizedDirectiveMetadata {
return new NormalizedDirectiveMetadata({
CompileDirectiveMetadata {
return CompileDirectiveMetadata.create({
selector: selector,
isComponent: true,
type: new TypeMetadata({name: 'SomeComp'}),
template: new NormalizedTemplateMetadata({ngContentSelectors: ngContentSelectors})
type: new CompileTypeMetadata({name: 'SomeComp'}),
template: new CompileTemplateMetadata({ngContentSelectors: ngContentSelectors})
})
}
@ -725,38 +766,38 @@ Parser Error: Unexpected token 'b' at column 3 in [a b] in TestComp > div:nth-ch
it('should not throw on invalid property names if the property is used by a directive',
() => {
var dirA = new NormalizedDirectiveMetadata({
var dirA = CompileDirectiveMetadata.create({
selector: 'div',
type: new TypeMetadata({name: 'DirA'}),
changeDetection: new ChangeDetectionMetadata({properties: ['invalidProp']})
type: new CompileTypeMetadata({name: 'DirA'}),
properties: ['invalidProp']
});
expect(() => parse('<div [invalid-prop]></div>', [dirA])).not.toThrow();
});
it('should not allow more than 1 component per element', () => {
var dirA = new NormalizedDirectiveMetadata({
var dirA = CompileDirectiveMetadata.create({
selector: 'div',
isComponent: true,
type: new TypeMetadata({name: 'DirA'}),
template: new NormalizedTemplateMetadata({ngContentSelectors: []})
type: new CompileTypeMetadata({name: 'DirA'}),
template: new CompileTemplateMetadata({ngContentSelectors: []})
});
var dirB = new NormalizedDirectiveMetadata({
var dirB = CompileDirectiveMetadata.create({
selector: 'div',
isComponent: true,
type: new TypeMetadata({name: 'DirB'}),
template: new NormalizedTemplateMetadata({ngContentSelectors: []})
type: new CompileTypeMetadata({name: 'DirB'}),
template: new CompileTemplateMetadata({ngContentSelectors: []})
});
expect(() => parse('<div>', [dirB, dirA])).toThrowError(`Template parse errors:
More than one component: DirA,DirB in TestComp > div:nth-child(0)`);
More than one component: DirB,DirA in TestComp > div:nth-child(0)`);
});
it('should not allow components or element nor event bindings on explicit embedded templates',
() => {
var dirA = new NormalizedDirectiveMetadata({
var dirA = CompileDirectiveMetadata.create({
selector: '[a]',
isComponent: true,
type: new TypeMetadata({name: 'DirA'}),
template: new NormalizedTemplateMetadata({ngContentSelectors: []})
type: new CompileTypeMetadata({name: 'DirA'}),
template: new CompileTemplateMetadata({ngContentSelectors: []})
});
expect(() => parse('<template [a]="b" (e)="f"></template>', [dirA]))
.toThrowError(`Template parse errors:
@ -766,17 +807,45 @@ Event binding e on an embedded template in TestComp > template:nth-child(0)[(e)=
});
it('should not allow components or element bindings on inline embedded templates', () => {
var dirA = new NormalizedDirectiveMetadata({
var dirA = CompileDirectiveMetadata.create({
selector: '[a]',
isComponent: true,
type: new TypeMetadata({name: 'DirA'}),
template: new NormalizedTemplateMetadata({ngContentSelectors: []})
type: new CompileTypeMetadata({name: 'DirA'}),
template: new CompileTemplateMetadata({ngContentSelectors: []})
});
expect(() => parse('<div *a="b">', [dirA])).toThrowError(`Template parse errors:
Components on an embedded template: DirA in TestComp > div:nth-child(0)
Property binding a not used by any directive on an embedded template in TestComp > div:nth-child(0)[*a=b]`);
});
});
describe('ignore elements', () => {
it('should ignore <script> elements but include them for source info', () => {
expect(humanizeTemplateAsts(parse('<script></script>a', [])))
.toEqual([[TextAst, 'a', 'TestComp > #text(a):nth-child(1)']]);
});
it('should ignore <style> elements but include them for source info', () => {
expect(humanizeTemplateAsts(parse('<style></style>a', [])))
.toEqual([[TextAst, 'a', 'TestComp > #text(a):nth-child(1)']]);
});
it('should ignore <link rel="stylesheet"> elements but include them for source info', () => {
expect(humanizeTemplateAsts(parse('<link rel="stylesheet"></link>a', [])))
.toEqual([[TextAst, 'a', 'TestComp > #text(a):nth-child(1)']]);
});
it('should ignore elements with ng-non-bindable, including their children, but include them for source info',
() => {
expect(humanizeTemplateAsts(parse('<div ng-non-bindable>b</div>a', [])))
.toEqual([[TextAst, 'a', 'TestComp > #text(a):nth-child(1)']]);
});
});
});
}
@ -805,7 +874,7 @@ class TemplateHumanizer implements TemplateAstVisitor {
templateVisitAll(this, ast.attrs);
templateVisitAll(this, ast.properties);
templateVisitAll(this, ast.events);
templateVisitAll(this, ast.vars);
templateVisitAll(this, ast.exportAsVars);
templateVisitAll(this, ast.directives);
templateVisitAll(this, ast.children);
return null;
@ -852,6 +921,7 @@ class TemplateHumanizer implements TemplateAstVisitor {
templateVisitAll(this, ast.properties);
templateVisitAll(this, ast.hostProperties);
templateVisitAll(this, ast.hostEvents);
templateVisitAll(this, ast.exportAsVars);
return null;
}
visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: any): any {