feat(dart/transform) Register parameter metadata information

Adds any metadata attached to a parameter to the "parameters" value
passed in to `registerType`.

For example:
`MyComponent(@Inject(Foo) foo)` generates
`"parameters": const [const [const Inject(Foo)]]`

Also reorganizes the testing code.

Closes #7
This commit is contained in:
Tim Blasi
2015-03-06 11:10:14 -08:00
parent e1a1dd07e4
commit f4e0f51f5a
41 changed files with 271 additions and 167 deletions

View File

@ -27,7 +27,8 @@ class CreateNgSettersVisitor extends ToSourceVisitor with VisitorMixin {
@override
Object visitMethodInvocation(MethodInvocation node) {
var isRegisterType = node.methodName.toString() == REGISTER_TYPE_METHOD_NAME;
var isRegisterType =
node.methodName.toString() == REGISTER_TYPE_METHOD_NAME;
// The first argument to a `registerType` call is the type.
extractVisitor.currentName = node.argumentList.arguments[0] is Identifier
? node.argumentList.arguments[0]

View File

@ -4,7 +4,7 @@ import 'package:dart_style/dart_style.dart';
import 'logging.dart';
DartFormatter _formatter;
DartFormatter _formatter = null;
void init(DartFormatter formatter) {
if (_formatter != null) {

View File

@ -7,6 +7,7 @@ import 'package:angular2/src/transform/common/visitor_mixin.dart';
/// SourceVisitor designed to accept [ConstructorDeclaration] nodes.
class _CtorTransformVisitor extends ToSourceVisitor with VisitorMixin {
bool _withParameterAnnotations = true;
bool _withParameterTypes = true;
bool _withParameterNames = true;
final PrintWriter writer;
@ -21,7 +22,13 @@ class _CtorTransformVisitor extends ToSourceVisitor with VisitorMixin {
/// If [_withParameterTypes] is true, this method outputs [node]'s type. If
/// [_withParameterNames] is true, this method outputs [node]'s identifier.
Object _visitNormalFormalParameter(TypeName type, SimpleIdentifier name) {
Object _visitNormalFormalParameter(
NodeList<Annotation> metadata, TypeName type, SimpleIdentifier name) {
if (_withParameterAnnotations && metadata != null) {
assert(_withParameterTypes);
var suffix = type != null ? ', ' : '';
visitNodeListWithSeparatorAndSuffix(metadata, ', ', suffix);
}
if (_withParameterTypes) {
visitNodeWithSuffix(type, ' ');
}
@ -48,7 +55,8 @@ class _CtorTransformVisitor extends ToSourceVisitor with VisitorMixin {
@override
Object visitSimpleFormalParameter(SimpleFormalParameter node) {
return _visitNormalFormalParameter(node.type, node.identifier);
return _visitNormalFormalParameter(
node.metadata, node.type, node.identifier);
}
@override
@ -61,14 +69,14 @@ class _CtorTransformVisitor extends ToSourceVisitor with VisitorMixin {
if (type == null) {
type = _fieldNameToType[node.identifier.toString()];
}
return _visitNormalFormalParameter(type, node.identifier);
return _visitNormalFormalParameter(node.metadata, type, node.identifier);
}
@override
Object visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) {
logger.error('Function typed formal parameters not supported '
'(${node.toSource()})');
return _visitNormalFormalParameter(null, node.identifier);
return _visitNormalFormalParameter(node.metadata, null, node.identifier);
}
@override
@ -93,18 +101,30 @@ class _CtorTransformVisitor extends ToSourceVisitor with VisitorMixin {
writer.print(')');
return null;
}
@override
Object visitAnnotation(Annotation node) {
var prefix =
node.arguments != null && node.arguments.length > 0 ? 'const ' : '';
visitNodeWithPrefix(prefix, node.name);
visitNodeWithPrefix(".", node.constructorName);
visitNode(node.arguments);
return null;
}
}
/// ToSourceVisitor designed to print 'parameters' values for Angular2's
/// [registerType] calls.
class ParameterTransformVisitor extends _CtorTransformVisitor {
ParameterTransformVisitor(PrintWriter writer) : super(writer);
ParameterTransformVisitor(PrintWriter writer) : super(writer) {
_withParameterNames = false;
_withParameterTypes = true;
_withParameterAnnotations = true;
}
@override
Object visitConstructorDeclaration(ConstructorDeclaration node) {
_buildFieldMap(node);
_withParameterNames = false;
_withParameterTypes = true;
writer.print('const [');
visitNode(node.parameters);
writer.print(']');
@ -131,7 +151,9 @@ class ParameterTransformVisitor extends _CtorTransformVisitor {
/// ToSourceVisitor designed to print 'factory' values for Angular2's
/// [registerType] calls.
class FactoryTransformVisitor extends _CtorTransformVisitor {
FactoryTransformVisitor(PrintWriter writer) : super(writer);
FactoryTransformVisitor(PrintWriter writer) : super(writer) {
_withParameterAnnotations = false;
}
@override
Object visitConstructorDeclaration(ConstructorDeclaration node) {
@ -142,6 +164,7 @@ class FactoryTransformVisitor extends _CtorTransformVisitor {
writer.print(' => new ');
visitNode(node.returnType);
visitNodeWithPrefix(".", node.name);
_withParameterTypes = false;
visitNode(node.parameters);
return null;