|
| 1 | +library angular2.transform.template_compiler.directive_metadata_reader; |
| 2 | + |
| 3 | +import 'package:analyzer/analyzer.dart'; |
| 4 | +import 'package:angular2/src/render/api.dart'; |
| 5 | +import 'package:angular2/src/transform/common/asset_reader.dart'; |
| 6 | +import 'package:angular2/src/transform/common/logging.dart'; |
| 7 | +import 'package:angular2/src/transform/common/names.dart'; |
| 8 | +import 'package:angular2/src/transform/common/parser.dart'; |
| 9 | +import 'package:angular2/src/transform/common/property_utils.dart' as prop; |
| 10 | + |
| 11 | +/// Reads [DirectiveMetadata] from the `attributes` of `t`. |
| 12 | +List<DirectiveMetadata> readDirectiveMetadata(RegisteredType t) { |
| 13 | + var visitor = new _DirectiveMetadataVisitor(); |
| 14 | + t.annotations.accept(visitor); |
| 15 | + return visitor.directiveMetadata; |
| 16 | +} |
| 17 | + |
| 18 | +num _getDirectiveType(String annotationName) { |
| 19 | + // TODO(kegluneq): Detect subtypes & implementations of `Directive`s. |
| 20 | + switch (annotationName) { |
| 21 | + case 'Decorator': |
| 22 | + return DirectiveMetadata.DECORATOR_TYPE; |
| 23 | + case 'Component': |
| 24 | + return DirectiveMetadata.COMPONENT_TYPE; |
| 25 | + case 'Viewport': |
| 26 | + return DirectiveMetadata.VIEWPORT_TYPE; |
| 27 | + default: |
| 28 | + return -1; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Visitor responsible for processing the `annotations` property of a |
| 33 | +/// [RegisterType] object and pulling out [DirectiveMetadata]. |
| 34 | +class _DirectiveMetadataVisitor extends Object |
| 35 | + with RecursiveAstVisitor<Object> { |
| 36 | + DirectiveMetadata current; |
| 37 | + final List<DirectiveMetadata> directiveMetadata = []; |
| 38 | + |
| 39 | + @override |
| 40 | + Object visitInstanceCreationExpression(InstanceCreationExpression node) { |
| 41 | + var directiveType = _getDirectiveType('${node.constructorName.type.name}'); |
| 42 | + if (directiveType >= 0) { |
| 43 | + current = new DirectiveMetadata( |
| 44 | + type: directiveType, |
| 45 | + compileChildren: false, |
| 46 | + properties: {}, |
| 47 | + hostListeners: {}, |
| 48 | + setters: [], |
| 49 | + readAttributes: []); |
| 50 | + directiveMetadata.add(current); |
| 51 | + super.visitInstanceCreationExpression(node); |
| 52 | + } |
| 53 | + // Annotation we do not recognize - no need to visit. |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + @override |
| 58 | + Object visitNamedExpression(NamedExpression node) { |
| 59 | + // TODO(kegluneq): Remove this limitation. |
| 60 | + if (node.name is! Label || node.name.label is! SimpleIdentifier) { |
| 61 | + logger.error( |
| 62 | + 'Angular 2 currently only supports simple identifiers in directives.' |
| 63 | + ' Source: ${node}'); |
| 64 | + return null; |
| 65 | + } |
| 66 | + var keyString = '${node.name.label}'; |
| 67 | + // TODO(kegluneq): Populate the other values in [DirectiveMetadata] once |
| 68 | + // they are specified as `hostAttributes` and `hostSetters`. |
| 69 | + // See [https://github.com/angular/angular/issues/1244] |
| 70 | + switch (keyString) { |
| 71 | + case 'selector': |
| 72 | + _populateSelector(node.expression); |
| 73 | + break; |
| 74 | + case 'compileChildren': |
| 75 | + _populateCompileChildren(node.expression); |
| 76 | + break; |
| 77 | + case 'properties': |
| 78 | + _populateProperties(node.expression); |
| 79 | + break; |
| 80 | + case 'hostListeners': |
| 81 | + _populateHostListeners(node.expression); |
| 82 | + } |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + String _expressionToString(Expression node, String nodeDescription) { |
| 87 | + // TODO(kegluneq): Accept more options. |
| 88 | + if (node is! SimpleStringLiteral) { |
| 89 | + logger.error('Angular 2 currently only supports string literals ' |
| 90 | + 'in $nodeDescription. Source: ${node}'); |
| 91 | + return null; |
| 92 | + } |
| 93 | + return stringLiteralToString(node); |
| 94 | + } |
| 95 | + |
| 96 | + void _populateSelector(Expression selectorValue) { |
| 97 | + current.selector = _expressionToString(selectorValue, 'Directive#selector'); |
| 98 | + } |
| 99 | + |
| 100 | + void _populateCompileChildren(Expression compileChildrenValue) { |
| 101 | + if (compileChildrenValue is! BooleanLiteral) { |
| 102 | + logger.error( |
| 103 | + 'Angular 2 currently only supports boolean literal values for ' |
| 104 | + 'Decorator#compileChildren.' |
| 105 | + ' Source: ${node}'); |
| 106 | + return; |
| 107 | + } |
| 108 | + current.compileChildren = compileChildrenValue.value; |
| 109 | + } |
| 110 | + |
| 111 | + void _populateProperties(Expression propertiesValue) { |
| 112 | + if (propertiesValue is! MapLiteral) { |
| 113 | + logger.error('Angular 2 currently only supports map literal values for ' |
| 114 | + 'Directive#properties.' |
| 115 | + ' Source: ${node}'); |
| 116 | + return; |
| 117 | + } |
| 118 | + for (MapLiteralEntry entry in propertiesValue.entries) { |
| 119 | + var sKey = _expressionToString(entry.key, 'Directive#properties keys'); |
| 120 | + var sVal = _expressionToString(entry.value, 'Direcive#properties values'); |
| 121 | + current.properties[sKey] = sVal; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + void _populateHostListeners(Expression hostListenersValue) { |
| 126 | + if (hostListenersValue is! MapLiteral) { |
| 127 | + logger.error('Angular 2 currently only supports map literal values for ' |
| 128 | + 'Directive#hostListeners.' |
| 129 | + ' Source: ${node}'); |
| 130 | + return; |
| 131 | + } |
| 132 | + for (MapLiteralEntry entry in hostListenersValue.entries) { |
| 133 | + var sKey = _expressionToString(entry.key, 'Directive#hostListeners keys'); |
| 134 | + var sVal = |
| 135 | + _expressionToString(entry.value, 'Directive#hostListeners values'); |
| 136 | + current.hostListeners[sKey] = sVal; |
| 137 | + ; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments