|
|
|
@ -1,30 +1,47 @@
|
|
|
|
|
library angular2.transform.common.directive_metadata_reader;
|
|
|
|
|
|
|
|
|
|
import 'package:analyzer/analyzer.dart';
|
|
|
|
|
import 'package:analyzer/src/generated/element.dart';
|
|
|
|
|
import 'package:angular2/src/render/api.dart';
|
|
|
|
|
import 'logging.dart';
|
|
|
|
|
import 'parser.dart';
|
|
|
|
|
|
|
|
|
|
/// Reads [DirectiveMetadata] from the `attributes` of `t`.
|
|
|
|
|
DirectiveMetadata readDirectiveMetadata(RegisteredType t) {
|
|
|
|
|
/// Reads [DirectiveMetadata] from the `node`. `node` is expected to be an
|
|
|
|
|
/// instance of [Annotation], [NodeList<Annotation>], ListLiteral, or
|
|
|
|
|
/// [InstanceCreationExpression].
|
|
|
|
|
DirectiveMetadata readDirectiveMetadata(dynamic node) {
|
|
|
|
|
assert(node is Annotation ||
|
|
|
|
|
node is NodeList ||
|
|
|
|
|
node is InstanceCreationExpression ||
|
|
|
|
|
node is ListLiteral);
|
|
|
|
|
var visitor = new _DirectiveMetadataVisitor();
|
|
|
|
|
t.annotations.accept(visitor);
|
|
|
|
|
if (visitor.meta != null) {
|
|
|
|
|
visitor.meta.id = '${t.typeName}';
|
|
|
|
|
}
|
|
|
|
|
node.accept(visitor);
|
|
|
|
|
return visitor.meta;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
num _getDirectiveType(String annotationName) {
|
|
|
|
|
num _getDirectiveType(String annotationName, Element element) {
|
|
|
|
|
var byNameMatch = -1;
|
|
|
|
|
// TODO(kegluneq): Detect subtypes & implementations of `Directive`s.
|
|
|
|
|
switch (annotationName) {
|
|
|
|
|
case 'Directive':
|
|
|
|
|
return DirectiveMetadata.DIRECTIVE_TYPE;
|
|
|
|
|
byNameMatch = DirectiveMetadata.DIRECTIVE_TYPE;
|
|
|
|
|
break;
|
|
|
|
|
case 'Component':
|
|
|
|
|
return DirectiveMetadata.COMPONENT_TYPE;
|
|
|
|
|
byNameMatch = DirectiveMetadata.COMPONENT_TYPE;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (element != null) {
|
|
|
|
|
var byResolvedAst = -1;
|
|
|
|
|
var libName = element.library.name;
|
|
|
|
|
// If we have resolved, ensure the library is correct.
|
|
|
|
|
if (libName == 'angular2.src.core.annotations.annotations' ||
|
|
|
|
|
libName == 'angular2.src.core.annotations_impl.annotations') {
|
|
|
|
|
byResolvedAst = byNameMatch;
|
|
|
|
|
}
|
|
|
|
|
// TODO(kegluneq): @keertip, can we expose this as a warning?
|
|
|
|
|
assert(byNameMatch == byResolvedAst);
|
|
|
|
|
}
|
|
|
|
|
return byNameMatch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Visitor responsible for processing the `annotations` property of a
|
|
|
|
@ -33,22 +50,45 @@ class _DirectiveMetadataVisitor extends Object
|
|
|
|
|
with RecursiveAstVisitor<Object> {
|
|
|
|
|
DirectiveMetadata meta;
|
|
|
|
|
|
|
|
|
|
void _createEmptyMetadata(num type) {
|
|
|
|
|
assert(type >= 0);
|
|
|
|
|
meta = new DirectiveMetadata(
|
|
|
|
|
type: type,
|
|
|
|
|
compileChildren: true,
|
|
|
|
|
properties: {},
|
|
|
|
|
hostListeners: {},
|
|
|
|
|
hostProperties: {},
|
|
|
|
|
hostAttributes: {},
|
|
|
|
|
readAttributes: []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Object visitInstanceCreationExpression(InstanceCreationExpression node) {
|
|
|
|
|
var directiveType = _getDirectiveType('${node.constructorName.type.name}');
|
|
|
|
|
Object visitAnnotation(Annotation node) {
|
|
|
|
|
var directiveType = _getDirectiveType('${node.name}', node.element);
|
|
|
|
|
if (directiveType >= 0) {
|
|
|
|
|
if (meta != null) {
|
|
|
|
|
logger.error('Only one Directive is allowed per class. '
|
|
|
|
|
'Found "$node" but already processed "$meta".');
|
|
|
|
|
throw new FormatException('Only one Directive is allowed per class. '
|
|
|
|
|
'Found "$node" but already processed "$meta".',
|
|
|
|
|
'$node' /* source */);
|
|
|
|
|
}
|
|
|
|
|
meta = new DirectiveMetadata(
|
|
|
|
|
type: directiveType,
|
|
|
|
|
compileChildren: true,
|
|
|
|
|
properties: {},
|
|
|
|
|
hostListeners: {},
|
|
|
|
|
hostProperties: {},
|
|
|
|
|
hostAttributes: {},
|
|
|
|
|
readAttributes: []);
|
|
|
|
|
_createEmptyMetadata(directiveType);
|
|
|
|
|
super.visitAnnotation(node);
|
|
|
|
|
}
|
|
|
|
|
// Annotation we do not recognize - no need to visit.
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Object visitInstanceCreationExpression(InstanceCreationExpression node) {
|
|
|
|
|
var directiveType = _getDirectiveType(
|
|
|
|
|
'${node.constructorName.type.name}', node.staticElement);
|
|
|
|
|
if (directiveType >= 0) {
|
|
|
|
|
if (meta != null) {
|
|
|
|
|
throw new FormatException('Only one Directive is allowed per class. '
|
|
|
|
|
'Found "$node" but already processed "$meta".',
|
|
|
|
|
'$node' /* source */);
|
|
|
|
|
}
|
|
|
|
|
_createEmptyMetadata(directiveType);
|
|
|
|
|
super.visitInstanceCreationExpression(node);
|
|
|
|
|
}
|
|
|
|
|
// Annotation we do not recognize - no need to visit.
|
|
|
|
@ -59,10 +99,9 @@ class _DirectiveMetadataVisitor extends Object
|
|
|
|
|
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;
|
|
|
|
|
throw new FormatException(
|
|
|
|
|
'Angular 2 currently only supports simple identifiers in directives.',
|
|
|
|
|
'$node' /* source */);
|
|
|
|
|
}
|
|
|
|
|
var keyString = '${node.name.label}';
|
|
|
|
|
// TODO(kegluneq): Populate the other values in [DirectiveMetadata] once
|
|
|
|
@ -93,9 +132,9 @@ class _DirectiveMetadataVisitor extends Object
|
|
|
|
|
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;
|
|
|
|
|
throw new FormatException(
|
|
|
|
|
'Angular 2 currently only supports string literals '
|
|
|
|
|
'in $nodeDescription.', '$node' /* source */);
|
|
|
|
|
}
|
|
|
|
|
return stringLiteralToString(node);
|
|
|
|
|
}
|
|
|
|
@ -104,23 +143,30 @@ class _DirectiveMetadataVisitor extends Object
|
|
|
|
|
meta.selector = _expressionToString(selectorValue, 'Directive#selector');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _checkMeta() {
|
|
|
|
|
if (meta == null) {
|
|
|
|
|
throw new ArgumentError(
|
|
|
|
|
'Incorrect value passed to readDirectiveMetadata. '
|
|
|
|
|
'Expected types are Annotation and InstanceCreationExpression');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _populateCompileChildren(Expression compileChildrenValue) {
|
|
|
|
|
_checkMeta();
|
|
|
|
|
if (compileChildrenValue is! BooleanLiteral) {
|
|
|
|
|
logger.error(
|
|
|
|
|
throw new FormatException(
|
|
|
|
|
'Angular 2 currently only supports boolean literal values for '
|
|
|
|
|
'Directive#compileChildren.'
|
|
|
|
|
' Source: ${compileChildrenValue}');
|
|
|
|
|
return;
|
|
|
|
|
'Directive#compileChildren.', '$compileChildrenValue' /* source */);
|
|
|
|
|
}
|
|
|
|
|
meta.compileChildren = (compileChildrenValue as BooleanLiteral).value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _populateProperties(Expression propertiesValue) {
|
|
|
|
|
_checkMeta();
|
|
|
|
|
if (propertiesValue is! MapLiteral) {
|
|
|
|
|
logger.error('Angular 2 currently only supports map literal values for '
|
|
|
|
|
'Directive#properties.'
|
|
|
|
|
' Source: ${propertiesValue}');
|
|
|
|
|
return;
|
|
|
|
|
throw new FormatException(
|
|
|
|
|
'Angular 2 currently only supports map literal values for '
|
|
|
|
|
'Directive#properties.', '$propertiesValue' /* source */);
|
|
|
|
|
}
|
|
|
|
|
for (MapLiteralEntry entry in (propertiesValue as MapLiteral).entries) {
|
|
|
|
|
var sKey = _expressionToString(entry.key, 'Directive#properties keys');
|
|
|
|
@ -130,11 +176,11 @@ class _DirectiveMetadataVisitor extends Object
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _populateHostListeners(Expression hostListenersValue) {
|
|
|
|
|
_checkMeta();
|
|
|
|
|
if (hostListenersValue is! MapLiteral) {
|
|
|
|
|
logger.error('Angular 2 currently only supports map literal values for '
|
|
|
|
|
'Directive#hostListeners.'
|
|
|
|
|
' Source: ${hostListenersValue}');
|
|
|
|
|
return;
|
|
|
|
|
throw new FormatException(
|
|
|
|
|
'Angular 2 currently only supports map literal values for '
|
|
|
|
|
'Directive#hostListeners.', '$hostListenersValue' /* source */);
|
|
|
|
|
}
|
|
|
|
|
for (MapLiteralEntry entry in (hostListenersValue as MapLiteral).entries) {
|
|
|
|
|
var sKey = _expressionToString(entry.key, 'Directive#hostListeners keys');
|
|
|
|
@ -145,11 +191,11 @@ class _DirectiveMetadataVisitor extends Object
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _populateHostProperties(Expression hostPropertyValue) {
|
|
|
|
|
_checkMeta();
|
|
|
|
|
if (hostPropertyValue is! MapLiteral) {
|
|
|
|
|
logger.error('Angular 2 currently only supports map literal values for '
|
|
|
|
|
'Directive#hostProperties.'
|
|
|
|
|
' Source: ${hostPropertyValue}');
|
|
|
|
|
return;
|
|
|
|
|
throw new FormatException(
|
|
|
|
|
'Angular 2 currently only supports map literal values for '
|
|
|
|
|
'Directive#hostProperties.', '$hostPropertyValue' /* source */);
|
|
|
|
|
}
|
|
|
|
|
for (MapLiteralEntry entry in (hostPropertyValue as MapLiteral).entries) {
|
|
|
|
|
var sKey =
|
|
|
|
@ -161,11 +207,11 @@ class _DirectiveMetadataVisitor extends Object
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _populateHostAttributes(Expression hostAttributeValue) {
|
|
|
|
|
_checkMeta();
|
|
|
|
|
if (hostAttributeValue is! MapLiteral) {
|
|
|
|
|
logger.error('Angular 2 currently only supports map literal values for '
|
|
|
|
|
'Directive#hostAttributes.'
|
|
|
|
|
' Source: ${hostAttributeValue}');
|
|
|
|
|
return;
|
|
|
|
|
throw new FormatException(
|
|
|
|
|
'Angular 2 currently only supports map literal values for '
|
|
|
|
|
'Directive#hostAttributes.', '$hostAttributeValue' /* source */);
|
|
|
|
|
}
|
|
|
|
|
for (MapLiteralEntry entry in (hostAttributeValue as MapLiteral).entries) {
|
|
|
|
|
var sKey =
|
|
|
|
|