2015-05-01 13:57:44 -07:00
|
|
|
library angular2.transform.common.directive_metadata_reader;
|
2015-04-10 16:58:52 -07:00
|
|
|
|
|
|
|
import 'package:analyzer/analyzer.dart';
|
|
|
|
import 'package:angular2/src/render/api.dart';
|
2015-05-01 13:57:44 -07:00
|
|
|
import 'logging.dart';
|
|
|
|
import 'parser.dart';
|
2015-04-10 16:58:52 -07:00
|
|
|
|
|
|
|
/// Reads [DirectiveMetadata] from the `attributes` of `t`.
|
2015-04-16 08:54:44 -07:00
|
|
|
DirectiveMetadata readDirectiveMetadata(RegisteredType t) {
|
2015-04-10 16:58:52 -07:00
|
|
|
var visitor = new _DirectiveMetadataVisitor();
|
|
|
|
t.annotations.accept(visitor);
|
2015-05-05 10:31:21 -07:00
|
|
|
if (visitor.meta != null) {
|
|
|
|
visitor.meta.id = '${t.typeName}';
|
|
|
|
}
|
2015-04-16 08:54:44 -07:00
|
|
|
return visitor.meta;
|
2015-04-10 16:58:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
num _getDirectiveType(String annotationName) {
|
|
|
|
// TODO(kegluneq): Detect subtypes & implementations of `Directive`s.
|
|
|
|
switch (annotationName) {
|
2015-04-30 13:38:40 -07:00
|
|
|
case 'Directive':
|
|
|
|
return DirectiveMetadata.DIRECTIVE_TYPE;
|
2015-04-10 16:58:52 -07:00
|
|
|
case 'Component':
|
|
|
|
return DirectiveMetadata.COMPONENT_TYPE;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visitor responsible for processing the `annotations` property of a
|
|
|
|
/// [RegisterType] object and pulling out [DirectiveMetadata].
|
|
|
|
class _DirectiveMetadataVisitor extends Object
|
|
|
|
with RecursiveAstVisitor<Object> {
|
2015-04-16 08:54:44 -07:00
|
|
|
DirectiveMetadata meta;
|
2015-04-10 16:58:52 -07:00
|
|
|
|
|
|
|
@override
|
|
|
|
Object visitInstanceCreationExpression(InstanceCreationExpression node) {
|
|
|
|
var directiveType = _getDirectiveType('${node.constructorName.type.name}');
|
|
|
|
if (directiveType >= 0) {
|
2015-04-16 08:54:44 -07:00
|
|
|
if (meta != null) {
|
|
|
|
logger.error('Only one Directive is allowed per class. '
|
|
|
|
'Found "$node" but already processed "$meta".');
|
|
|
|
}
|
|
|
|
meta = new DirectiveMetadata(
|
2015-04-10 16:58:52 -07:00
|
|
|
type: directiveType,
|
2015-05-06 10:00:25 -07:00
|
|
|
compileChildren: true,
|
2015-04-10 16:58:52 -07:00
|
|
|
properties: {},
|
|
|
|
hostListeners: {},
|
2015-04-21 11:47:53 -07:00
|
|
|
hostProperties: {},
|
2015-04-10 16:58:52 -07:00
|
|
|
readAttributes: []);
|
|
|
|
super.visitInstanceCreationExpression(node);
|
|
|
|
}
|
|
|
|
// Annotation we do not recognize - no need to visit.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Object visitNamedExpression(NamedExpression node) {
|
|
|
|
// TODO(kegluneq): Remove this limitation.
|
|
|
|
if (node.name is! Label || node.name.label is! SimpleIdentifier) {
|
|
|
|
logger.error(
|
|
|
|
'Angular 2 currently only supports simple identifiers in directives.'
|
|
|
|
' Source: ${node}');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var keyString = '${node.name.label}';
|
|
|
|
// TODO(kegluneq): Populate the other values in [DirectiveMetadata] once
|
|
|
|
// they are specified as `hostAttributes` and `hostSetters`.
|
|
|
|
// See [https://github.com/angular/angular/issues/1244]
|
|
|
|
switch (keyString) {
|
|
|
|
case 'selector':
|
|
|
|
_populateSelector(node.expression);
|
|
|
|
break;
|
|
|
|
case 'compileChildren':
|
|
|
|
_populateCompileChildren(node.expression);
|
|
|
|
break;
|
|
|
|
case 'properties':
|
|
|
|
_populateProperties(node.expression);
|
|
|
|
break;
|
2015-04-21 11:47:53 -07:00
|
|
|
case 'hostProperties':
|
|
|
|
_populateHostProperties(node.expression);
|
|
|
|
break;
|
2015-04-10 16:58:52 -07:00
|
|
|
case 'hostListeners':
|
|
|
|
_populateHostListeners(node.expression);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
String _expressionToString(Expression node, String nodeDescription) {
|
|
|
|
// TODO(kegluneq): Accept more options.
|
|
|
|
if (node is! SimpleStringLiteral) {
|
|
|
|
logger.error('Angular 2 currently only supports string literals '
|
|
|
|
'in $nodeDescription. Source: ${node}');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return stringLiteralToString(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _populateSelector(Expression selectorValue) {
|
2015-04-16 08:54:44 -07:00
|
|
|
meta.selector = _expressionToString(selectorValue, 'Directive#selector');
|
2015-04-10 16:58:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void _populateCompileChildren(Expression compileChildrenValue) {
|
|
|
|
if (compileChildrenValue is! BooleanLiteral) {
|
|
|
|
logger.error(
|
|
|
|
'Angular 2 currently only supports boolean literal values for '
|
2015-04-30 13:38:40 -07:00
|
|
|
'Directive#compileChildren.'
|
2015-04-14 13:55:15 -07:00
|
|
|
' Source: ${compileChildrenValue}');
|
2015-04-10 16:58:52 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-04-16 08:54:44 -07:00
|
|
|
meta.compileChildren = (compileChildrenValue as BooleanLiteral).value;
|
2015-04-10 16:58:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void _populateProperties(Expression propertiesValue) {
|
|
|
|
if (propertiesValue is! MapLiteral) {
|
|
|
|
logger.error('Angular 2 currently only supports map literal values for '
|
|
|
|
'Directive#properties.'
|
2015-04-14 13:55:15 -07:00
|
|
|
' Source: ${propertiesValue}');
|
2015-04-10 16:58:52 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-04-14 13:55:15 -07:00
|
|
|
for (MapLiteralEntry entry in (propertiesValue as MapLiteral).entries) {
|
2015-04-10 16:58:52 -07:00
|
|
|
var sKey = _expressionToString(entry.key, 'Directive#properties keys');
|
|
|
|
var sVal = _expressionToString(entry.value, 'Direcive#properties values');
|
2015-04-16 08:54:44 -07:00
|
|
|
meta.properties[sKey] = sVal;
|
2015-04-10 16:58:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _populateHostListeners(Expression hostListenersValue) {
|
|
|
|
if (hostListenersValue is! MapLiteral) {
|
|
|
|
logger.error('Angular 2 currently only supports map literal values for '
|
|
|
|
'Directive#hostListeners.'
|
2015-04-14 13:55:15 -07:00
|
|
|
' Source: ${hostListenersValue}');
|
2015-04-10 16:58:52 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-04-14 13:55:15 -07:00
|
|
|
for (MapLiteralEntry entry in (hostListenersValue as MapLiteral).entries) {
|
2015-04-10 16:58:52 -07:00
|
|
|
var sKey = _expressionToString(entry.key, 'Directive#hostListeners keys');
|
|
|
|
var sVal =
|
|
|
|
_expressionToString(entry.value, 'Directive#hostListeners values');
|
2015-04-16 08:54:44 -07:00
|
|
|
meta.hostListeners[sKey] = sVal;
|
2015-04-10 16:58:52 -07:00
|
|
|
}
|
|
|
|
}
|
2015-04-21 11:47:53 -07:00
|
|
|
|
|
|
|
void _populateHostProperties(Expression hostPropertyValue) {
|
|
|
|
if (hostPropertyValue is! MapLiteral) {
|
|
|
|
logger.error('Angular 2 currently only supports map literal values for '
|
|
|
|
'Directive#hostProperties.'
|
|
|
|
' Source: ${hostPropertyValue}');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (MapLiteralEntry entry in (hostPropertyValue as MapLiteral).entries) {
|
2015-04-24 10:28:09 -07:00
|
|
|
var sKey =
|
|
|
|
_expressionToString(entry.key, 'Directive#hostProperties keys');
|
2015-04-21 11:47:53 -07:00
|
|
|
var sVal =
|
|
|
|
_expressionToString(entry.value, 'Directive#hostProperties values');
|
|
|
|
meta.hostProperties[sKey] = sVal;
|
|
|
|
}
|
|
|
|
}
|
2015-04-10 16:58:52 -07:00
|
|
|
}
|