feat(transformers): added support for lifecycle events

This commit is contained in:
vsavkin 2015-05-28 17:28:53 -07:00
parent 6f0631c978
commit f19970a481
6 changed files with 95 additions and 5 deletions

View File

@ -18,7 +18,12 @@ export function directiveMetadataToMap(meta: DirectiveMetadata): Map<string, any
['properties', _cloneIfPresent(meta.properties)],
['readAttributes', _cloneIfPresent(meta.readAttributes)],
['type', meta.type],
['version', 1]
['callOnDestroy', meta.callOnDestroy],
['callOnCheck', meta.callOnCheck],
['callOnInit', meta.callOnInit],
['callOnChange', meta.callOnChange],
['callOnAllChangesDone', meta.callOnAllChangesDone],
['version', 1],
]);
}
@ -38,7 +43,12 @@ export function directiveMetadataFromMap(map: Map<string, any>): DirectiveMetada
hostAttributes:<Map<string, string>>_cloneIfPresent(MapWrapper.get(map, 'hostAttributes')),
properties:<List<string>>_cloneIfPresent(MapWrapper.get(map, 'properties')),
readAttributes:<List<string>>_cloneIfPresent(MapWrapper.get(map, 'readAttributes')),
type:<number>MapWrapper.get(map, 'type')
type:<number>MapWrapper.get(map, 'type'),
callOnDestroy:<boolean>MapWrapper.get(map, 'callOnDestroy'),
callOnCheck:<boolean>MapWrapper.get(map, 'callOnCheck'),
callOnChange:<boolean>MapWrapper.get(map, 'callOnChange'),
callOnInit:<boolean>MapWrapper.get(map, 'callOnInit'),
callOnAllChangesDone:<boolean>MapWrapper.get(map, 'callOnAllChangesDone')
});
}

View File

@ -60,7 +60,13 @@ class _DirectiveMetadataVisitor extends Object
hostListeners: {},
hostProperties: {},
hostAttributes: {},
readAttributes: []);
readAttributes: [],
callOnDestroy: false,
callOnChange: false,
callOnCheck: false,
callOnInit: false,
callOnAllChangesDone: false
);
}
@override
@ -126,6 +132,10 @@ class _DirectiveMetadataVisitor extends Object
break;
case 'hostListeners':
_populateHostListeners(node.expression);
break;
case 'lifecycle':
_populateLifecycle(node.expression);
break;
}
return null;
}
@ -214,4 +224,20 @@ class _DirectiveMetadataVisitor extends Object
_populateMap(
hostAttributeValue, meta.hostAttributes, 'Directive#hostAttributes');
}
void _populateLifecycle(Expression lifecycleValue) {
_checkMeta();
if (lifecycleValue is! ListLiteral) {
throw new FormatException(
'Angular 2 expects a List but could not understand the value for lifecycle. '
'$lifecycleValue');
}
ListLiteral l = lifecycleValue;
var lifecycleEvents = l.elements.map((s) => s.toSource());
meta.callOnDestroy = lifecycleEvents.contains("onDestroy");
meta.callOnChange = lifecycleEvents.contains("onChange");
meta.callOnCheck = lifecycleEvents.contains("onCheck");
meta.callOnInit = lifecycleEvents.contains("onInit");
meta.callOnAllChangesDone = lifecycleEvents.contains("onAllChangesDone");
}
}

View File

@ -15,7 +15,12 @@ export function main() {
properties: ['propKey: propVal'],
readAttributes: ['read1', 'read2'],
selector: 'some-comp',
type: DirectiveMetadata.COMPONENT_TYPE
type: DirectiveMetadata.COMPONENT_TYPE,
callOnDestroy: true,
callOnChange: true,
callOnCheck: true,
callOnInit: true,
callOnAllChangesDone: true
});
var map = directiveMetadataToMap(someComponent);
expect(MapWrapper.get(map, 'compileChildren')).toEqual(false);
@ -30,6 +35,11 @@ export function main() {
expect(MapWrapper.get(map, 'readAttributes')).toEqual(['read1', 'read2']);
expect(MapWrapper.get(map, 'selector')).toEqual('some-comp');
expect(MapWrapper.get(map, 'type')).toEqual(DirectiveMetadata.COMPONENT_TYPE);
expect(MapWrapper.get(map, 'callOnDestroy')).toEqual(true);
expect(MapWrapper.get(map, 'callOnCheck')).toEqual(true);
expect(MapWrapper.get(map, 'callOnChange')).toEqual(true);
expect(MapWrapper.get(map, 'callOnInit')).toEqual(true);
expect(MapWrapper.get(map, 'callOnAllChangesDone')).toEqual(true);
});
it('mapToDirectiveMetadata', () => {
@ -42,7 +52,12 @@ export function main() {
['properties', ['propKey: propVal']],
['readAttributes', ['readTest1', 'readTest2']],
['selector', 'testSelector'],
['type', DirectiveMetadata.DIRECTIVE_TYPE]
['type', DirectiveMetadata.DIRECTIVE_TYPE],
['callOnDestroy', true],
['callOnCheck', true],
['callOnInit', true],
['callOnChange', true],
['callOnAllChangesDone', true]
]);
var meta = directiveMetadataFromMap(map);
expect(meta.compileChildren).toEqual(false);
@ -56,6 +71,11 @@ export function main() {
expect(meta.readAttributes).toEqual(['readTest1', 'readTest2']);
expect(meta.selector).toEqual('testSelector');
expect(meta.type).toEqual(DirectiveMetadata.DIRECTIVE_TYPE);
expect(meta.callOnDestroy).toEqual(true);
expect(meta.callOnCheck).toEqual(true);
expect(meta.callOnInit).toEqual(true);
expect(meta.callOnChange).toEqual(true);
expect(meta.callOnAllChangesDone).toEqual(true);
});
});
}

View File

@ -78,6 +78,16 @@ void allTests() {
expect(metadata.hostListeners['keyDown']).toEqual('onKeyDown(\$event)');
});
it('should parse lifecycle events.', () async {
var metadata = await readMetadata('directive_metadata_extractor/'
'directive_metadata_files/lifecycle.ng_deps.dart');
expect(metadata.callOnDestroy).toBe(true);
expect(metadata.callOnChange).toBe(true);
expect(metadata.callOnCheck).toBe(true);
expect(metadata.callOnInit).toBe(true);
expect(metadata.callOnAllChangesDone).toBe(true);
});
it('should fail when a class is annotated with multiple Directives.',
() async {
var ngDeps = await parser.parse(new AssetId('a',

View File

@ -0,0 +1,19 @@
library examples.hello_world.index_common_dart.ng_deps.dart;
import 'hello.dart';
import 'package:angular2/angular2.dart'
show bootstrap, Component, Directive, View, NgElement, onChange, onDestroy, onInit, onCheck, onAllChangesDone;
var _visited = false;
void initReflector(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(HelloCmp, {
'factory': () => new HelloCmp(),
'parameters': const [const []],
'annotations': const [
const Component(lifecycle: [onChange, onDestroy, onInit, onCheck, onAllChangesDone])
]
});
}

View File

@ -10,6 +10,11 @@
"properties": [],
"readAttributes": [],
"type": 1,
"callOnDestroy": false,
"callOnCheck": false,
"callOnInit": false,
"callOnChange": false,
"callOnAllChangesDone": false,
"version": 1
}
}