Skip to content

Commit

Permalink
fix(StaticMetadataExtractor): Map members annotations to all annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Apr 15, 2014
1 parent 6d6e9ee commit 996b5cb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 46 deletions.
85 changes: 42 additions & 43 deletions lib/tools/transformer/metadata_extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -325,53 +325,52 @@ class AnnotationExtractor {
return;
}

// Default to using the first acceptable annotation- not sure if
// more than one should ever occur.
var sourceAnnotation = acceptableAnnotations.first;

// Clone the annotation so we don't modify the one in the persistent AST.
var index = type.annotations.indexOf(sourceAnnotation);
var annotation = new AstCloner().visitAnnotation(sourceAnnotation);
ResolutionCopier.copyResolutionData(sourceAnnotation, annotation);
type.annotations[index] = annotation;

var mapArg = annotation.arguments.arguments.firstWhere(
(arg) => (arg is NamedExpression) && (arg.name.label.name == 'map'),
orElse: () => null);

// If we don't have a 'map' parameter yet, add one.
if (mapArg == null) {
var map = new MapLiteral(null, null, null, [], null);
var label = new Label(new SimpleIdentifier(
new _GeneratedToken(TokenType.STRING, 'map')),
new _GeneratedToken(TokenType.COLON, ':'));
mapArg = new NamedExpression(label, map);
annotation.arguments.arguments.add(mapArg);
}
// Merge attribute annotations in all of the class annotations
acceptableAnnotations.forEach((srcAnnotation) {
// Clone the annotation so we don't modify the one in the persistent AST.
var index = type.annotations.indexOf(srcAnnotation);
var annotation = new AstCloner().visitAnnotation(srcAnnotation);
ResolutionCopier.copyResolutionData(srcAnnotation, annotation);
type.annotations[index] = annotation;

var mapArg = annotation.arguments.arguments.firstWhere(
(arg) => (arg is NamedExpression) && (arg.name.label.name == 'map'),
orElse: () => null);

// If we don't have a 'map' parameter yet, add one.
if (mapArg == null) {
var map = new MapLiteral(null, null, null, [], null);
var label = new Label(new SimpleIdentifier(
new _GeneratedToken(TokenType.STRING, 'map')),
new _GeneratedToken(TokenType.COLON, ':'));
mapArg = new NamedExpression(label, map);
annotation.arguments.arguments.add(mapArg);
}

var map = mapArg.expression;
if (map is! MapLiteral) {
warn('Expected \'map\' argument of $annotation to be a map literal',
type.type);
return;
}
memberAnnotations.forEach((memberName, annotation) {
var key = annotation.arguments.arguments.first;
// If the key already exists then it means we have two annotations for
// same member.
if (map.entries.any((entry) => entry.key.toString() == key.toString())) {
warn('Directive $annotation already contains an entry for $key',
type.type);
var map = mapArg.expression;
if (map is! MapLiteral) {
warn('Expected \'map\' argument of $annotation to be a map literal',
type.type);
return;
}
memberAnnotations.forEach((memberName, annotation) {
var key = annotation.arguments.arguments.first;
// If the key already exists then it means we have two annotations for
// same member.
if (map.entries.any((entry) => entry.key.toString() == key.toString())) {
warn('Directive $annotation already contains an entry for $key',
type.type);
return;
}

var typeName = annotation.element.enclosingElement.name;
var value = '${_annotationToMapping[typeName]}$memberName';
var entry = new MapLiteralEntry(
key,
new _GeneratedToken(TokenType.COLON, ':'),
new SimpleStringLiteral(stringToken(value), value));
map.entries.add(entry);
var typeName = annotation.element.enclosingElement.name;
var value = '${_annotationToMapping[typeName]}$memberName';
var entry = new MapLiteralEntry(
key,
new _GeneratedToken(TokenType.COLON, ':'),
new SimpleStringLiteral(stringToken(value), value));
map.entries.add(entry);
});
});
}

Expand Down
16 changes: 13 additions & 3 deletions test/tools/transformer/metadata_generator_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ main() {
'a|web/main.dart': '''
import 'package:angular/angular.dart';
@NgDirective(map: {'another-expression': '=>anotherExpression'})
@NgDirective(
selector: 'first',
map: {'first-expression': '=>anotherExpression'})
@NgDirective(
selector: 'second',
map: {'second-expression': '=>anotherExpression'})
class Engine {
set anotherExpression(Function) {}
Expand All @@ -132,8 +137,13 @@ main() {
],
classes: {
'import_0.Engine': [
'const import_1.NgDirective(map: const {'
'\'another-expression\': \'=>anotherExpression\', '
'const import_1.NgDirective(selector: \'first\', '
'map: const {'
'\'first-expression\': \'=>anotherExpression\', '
'\'two-way-stuff\': \'<=>twoWayStuff\'})',
'const import_1.NgDirective(selector: \'second\', '
'map: const {'
'\'second-expression\': \'=>anotherExpression\', '
'\'two-way-stuff\': \'<=>twoWayStuff\'})',
]
});
Expand Down

0 comments on commit 996b5cb

Please sign in to comment.