refactor(transformer): updates in properties syntax

This commit is contained in:
Victor Berchet 2015-05-29 10:40:37 +02:00
parent d7df853bde
commit 35f0ee510a
13 changed files with 42 additions and 27 deletions

View File

@ -48,9 +48,16 @@ Map<String, String> _createBindMap(NgDeps ngDeps) {
var visitor = new ExtractSettersVisitor(); var visitor = new ExtractSettersVisitor();
var bindMap = {}; var bindMap = {};
ngDeps.registeredTypes.forEach((RegisteredType t) { ngDeps.registeredTypes.forEach((RegisteredType t) {
visitor.bindMappings.clear(); visitor.bindConfig.clear();
t.annotations.accept(visitor); t.annotations.accept(visitor);
visitor.bindMappings.forEach((String prop, _) { visitor.bindConfig.forEach((String config) {
var prop;
var idx = config.indexOf(':');
if (idx > 0) {
prop = config.substring(0, idx).trim();
} else {
prop = config;
}
if (bindMap.containsKey(prop)) { if (bindMap.containsKey(prop)) {
bindMap[prop] = ''; bindMap[prop] = '';
} else { } else {

View File

@ -7,21 +7,17 @@ import 'package:angular2/src/transform/common/logging.dart';
/// `registerType` call and pulling out the properties of any "bind" /// `registerType` call and pulling out the properties of any "bind"
/// values found. /// values found.
class ExtractSettersVisitor extends Object with RecursiveAstVisitor<Object> { class ExtractSettersVisitor extends Object with RecursiveAstVisitor<Object> {
final Map<String, String> bindMappings = {};
final ConstantEvaluator _evaluator = new ConstantEvaluator(); final ConstantEvaluator _evaluator = new ConstantEvaluator();
final List<String> bindConfig = [];
@override @override
Object visitNamedExpression(NamedExpression node) { Object visitNamedExpression(NamedExpression node) {
if ('${node.name.label}' == 'properties') { if ('${node.name.label}' == 'properties') {
var evaluated = node.expression.accept(_evaluator); var evaluated = node.expression.accept(_evaluator);
if (evaluated is Map) { if (evaluated is List) {
evaluated.forEach((key, value) { bindConfig.addAll(evaluated);
if (value != null) {
bindMappings[key] = '$value';
}
});
} else { } else {
logger.error('`properties` currently only supports Map values'); logger.error('`properties` currently only supports List values');
} }
return null; return null;
} }

View File

@ -179,9 +179,23 @@ class _DirectiveMetadataVisitor extends Object
}); });
} }
/// Evaluates the [List] represented by `expression` and adds all values,
/// to `list`. If `expression` does not evaluate to a [List], throws a
/// descriptive [FormatException].
void _populateList(Expression expression, List list, String propertyName) {
var evaluated = expression.accept(_evaluator);
if (evaluated is! List) {
throw new FormatException(
'Angular 2 expects a List but could not understand the value for '
'$propertyName.', '$expression' /* source */);
}
list.addAll(evaluated);
}
void _populateProperties(Expression propertiesValue) { void _populateProperties(Expression propertiesValue) {
_checkMeta(); _checkMeta();
_populateMap(propertiesValue, meta.properties, 'Directive#properties'); _populateList(propertiesValue, meta.properties, 'Directive#properties');
} }
void _populateHostListeners(Expression hostListenersValue) { void _populateHostListeners(Expression hostListenersValue) {

View File

@ -63,10 +63,8 @@ void allTests() {
'directive_metadata_files/properties.ng_deps.dart'); 'directive_metadata_files/properties.ng_deps.dart');
expect(metadata.properties).toBeNotNull(); expect(metadata.properties).toBeNotNull();
expect(metadata.properties.length).toBe(2); expect(metadata.properties.length).toBe(2);
expect(metadata.properties).toContain('key1'); expect(metadata.properties).toContain('key1: val1');
expect(metadata.properties['key1']).toEqual('val1'); expect(metadata.properties).toContain('key2: val2');
expect(metadata.properties).toContain('key2');
expect(metadata.properties['key2']).toEqual('val2');
}); });
it('should parse host listeners.', () async { it('should parse host listeners.', () async {

View File

@ -7,7 +7,7 @@
"hostProperties": {}, "hostProperties": {},
"hostAttributes": {}, "hostAttributes": {},
"hostActions": null, "hostActions": null,
"properties": {}, "properties": [],
"readAttributes": [], "readAttributes": [],
"type": 1, "type": 1,
"version": 1 "version": 1

View File

@ -6,7 +6,7 @@
"compileChildren":true, "compileChildren":true,
"hostListeners":{}, "hostListeners":{},
"hostProperties":{}, "hostProperties":{},
"properties":{}, "properties":[],
"readAttributes":[], "readAttributes":[],
"type":1, "type":1,
"version":1 "version":1

View File

@ -6,7 +6,7 @@
"compileChildren":true, "compileChildren":true,
"hostListeners":{}, "hostListeners":{},
"hostProperties":{}, "hostProperties":{},
"properties":{}, "properties":[],
"readAttributes":[], "readAttributes":[],
"type":1, "type":1,
"version":1 "version":1

View File

@ -6,7 +6,7 @@
"compileChildren":true, "compileChildren":true,
"hostListeners":{}, "hostListeners":{},
"hostProperties":{}, "hostProperties":{},
"properties":{}, "properties":[],
"readAttributes":[], "readAttributes":[],
"type":1, "type":1,
"version":1 "version":1

View File

@ -5,8 +5,8 @@
"selector":"hello-app", "selector":"hello-app",
"compileChildren":true, "compileChildren":true,
"hostListeners":{}, "hostListeners":{},
"hostProperties":{}, "hostProperties":[],
"properties":{}, "properties":[],
"readAttributes":[], "readAttributes":[],
"type":1, "type":1,
"version":1 "version":1
@ -17,7 +17,7 @@
"compileChildren":true, "compileChildren":true,
"hostListeners":{}, "hostListeners":{},
"hostProperties":{}, "hostProperties":{},
"properties":{}, "properties":[],
"readAttributes":[], "readAttributes":[],
"type":1, "type":1,
"version":1 "version":1

View File

@ -6,7 +6,7 @@
"compileChildren":true, "compileChildren":true,
"hostListeners":{}, "hostListeners":{},
"hostProperties":{}, "hostProperties":{},
"properties":{}, "properties":[],
"readAttributes":[], "readAttributes":[],
"type":1, "type":1,
"version":1 "version":1

View File

@ -6,7 +6,7 @@
"compileChildren":true, "compileChildren":true,
"hostListeners":{}, "hostListeners":{},
"hostProperties":{}, "hostProperties":{},
"properties":{}, "properties":[],
"readAttributes":[], "readAttributes":[],
"type":1, "type":1,
"version":1 "version":1

View File

@ -4,8 +4,8 @@
"selector":"goodbye-app", "selector":"goodbye-app",
"compileChildren":true, "compileChildren":true,
"hostListeners":{}, "hostListeners":{},
"hostProperties":{}, "hostProperties":[],
"properties":{}, "properties":[],
"readAttributes":[], "readAttributes":[],
"type":1, "type":1,
"version":1 "version":1

View File

@ -6,7 +6,7 @@
"compileChildren":true, "compileChildren":true,
"hostListeners":{}, "hostListeners":{},
"hostProperties":{}, "hostProperties":{},
"properties":{}, "properties":[],
"readAttributes":[], "readAttributes":[],
"type":1, "type":1,
"version":1 "version":1