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;

View File

@ -0,0 +1,14 @@
library angular2.test.transform.common.read_file;
import 'dart:io';
/// Smooths over differences in CWD between IDEs and running tests in Travis.
String readFile(String path) {
for (var myPath in [path, 'test/transform/${path}']) {
var file = new File(myPath);
if (file.existsSync()) {
return file.readAsStringSync();
}
}
return null;
}

View File

@ -0,0 +1,30 @@
library angular2.test.transform.directive_processor.all_tests;
import 'dart:io';
import 'package:barback/barback.dart';
import 'package:angular2/src/transform/directive_processor/rewriter.dart';
import 'package:angular2/src/transform/common/formatter.dart';
import 'package:code_transformers/tests.dart';
import 'package:dart_style/dart_style.dart';
import 'package:path/path.dart' as path;
import 'package:unittest/unittest.dart';
import 'package:unittest/vm_config.dart';
import '../common/read_file.dart';
var formatter = new DartFormatter();
void allTests() {
test('should preserve parameter annotations as const instances', () {
var inputPath = 'parameter_metadata/soup.dart';
var expected = _readFile('parameter_metadata/expected/soup.ngDeps.dart');
var output =
formatter.format(createNgDeps(_readFile(inputPath), inputPath));
expect(output, equals(expected));
});
}
var pathBase = 'directive_processor';
/// Smooths over differences in CWD between IDEs and running tests in Travis.
String _readFile(String path) => readFile('$pathBase/$path');

View File

@ -0,0 +1,17 @@
library dinner.soup;
import 'soup.dart';
import 'package:angular2/src/core/annotations/annotations.dart';
bool _visited = false;
void setupReflection(reflector) {
if (_visited) return;
_visited = true;
reflector
..registerType(SoupComponent, {
"factory":
(String description, salt) => new SoupComponent(description, salt),
"parameters": const [const [Tasty, String], const [const Inject(Salt)]],
"annotations": const [const Component(selector: '[soup]')]
});
} // {"version":1,"importOffset":104,"registerOffset":451,"imports":["package:angular2/src/core/annotations/annotations.dart"]}

View File

@ -0,0 +1,8 @@
library dinner.soup;
import 'package:angular2/src/core/annotations/annotations.dart';
@Component(selector: '[soup]')
class SoupComponent {
SoupComponent(@Tasty String description, @Inject(Salt) salt);
}

View File

@ -0,0 +1,141 @@
library angular2.test.transform.integration;
import 'dart:io';
import 'package:angular2/transformer.dart';
import 'package:code_transformers/tests.dart';
import 'package:dart_style/dart_style.dart';
import 'package:unittest/unittest.dart';
import '../common/read_file.dart';
var formatter = new DartFormatter();
var transform = new AngularTransformerGroup(new TransformerOptions(
'web/index.dart', reflectionEntryPoint: 'web/index.dart'));
class IntegrationTestConfig {
final String name;
final Map<String, String> assetPathToInputPath;
final Map<String, String> assetPathToExpectedOutputPath;
IntegrationTestConfig(this.name,
{Map<String, String> inputs, Map<String, String> outputs})
: this.assetPathToInputPath = inputs,
this.assetPathToExpectedOutputPath = outputs;
}
void allTests() {
/*
* Each test has its own directory for inputs & an `expected` directory for
* expected outputs.
*
* In addition to these declared inputs, we inject a set of common inputs for
* every test.
*/
var commonInputs = {
'angular2|lib/src/core/annotations/annotations.dart':
'../../../lib/src/core/annotations/annotations.dart',
'angular2|lib/src/core/application.dart': '../common/application.dart',
'angular2|lib/src/reflection/reflection_capabilities.dart':
'../common/reflection_capabilities.dart'
};
var tests = [
new IntegrationTestConfig(
'should generate proper code for a Component defining only a selector.',
inputs: {
'a|web/index.dart': 'simple_annotation_files/index.dart',
'a|web/bar.dart': 'simple_annotation_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart':
'simple_annotation_files/expected/bar.ngDeps.dart',
'a|web/index.ngDeps.dart':
'simple_annotation_files/expected/index.ngDeps.dart'
}),
new IntegrationTestConfig(
'should generate proper code for a Component using a selector defined '
'in another file.',
inputs: {
'a|web/index.dart': 'two_deps_files/index.dart',
'a|web/foo.dart': 'two_deps_files/foo.dart',
'a|web/bar.dart': 'two_deps_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'two_deps_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig(
'should generate proper code for a Component declaring a '
'componentService defined in another file.',
inputs: {
'a|web/index.dart': 'list_of_types_files/index.dart',
'a|web/foo.dart': 'list_of_types_files/foo.dart',
'a|web/bar.dart': 'list_of_types_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'list_of_types_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig(
'should generate a factory for a class with no declared ctor.',
inputs: {
'a|web/index.dart': 'synthetic_ctor_files/index.dart',
'a|web/bar.dart': 'synthetic_ctor_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'synthetic_ctor_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig('should preserve multiple annotations.',
inputs: {
'a|web/index.dart': 'two_annotations_files/index.dart',
'a|web/bar.dart': 'two_annotations_files/bar.dart',
'angular2|lib/src/core/annotations/template.dart':
'../../../lib/src/core/annotations/template.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'two_annotations_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig('should generate setters for `bind` values.',
inputs: {
'a|web/index.dart': 'basic_bind_files/index.dart',
'a|web/bar.dart': 'basic_bind_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'basic_bind_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig(
'should ensure that dependencies are property chained.',
inputs: {
'a|web/index.dart': 'chained_deps_files/index.dart',
'a|web/foo.dart': 'chained_deps_files/foo.dart',
'a|web/bar.dart': 'chained_deps_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'chained_deps_files/expected/bar.ngDeps.dart',
'a|web/foo.ngDeps.dart': 'chained_deps_files/expected/foo.ngDeps.dart'
})
];
var cache = {};
for (var config in tests) {
// Read in input & output files.
config.assetPathToInputPath
..addAll(commonInputs)
..forEach((key, value) {
config.assetPathToInputPath[key] =
cache.putIfAbsent(value, () => _readFile(value));
});
config.assetPathToExpectedOutputPath.forEach((key, value) {
config.assetPathToExpectedOutputPath[key] = cache.putIfAbsent(value, () {
var code = _readFile(value);
return value.endsWith('dart') ? formatter.format(code) : code;
});
});
testPhases(config.name, [
[transform]
], config.assetPathToInputPath, config.assetPathToExpectedOutputPath, []);
}
}
/// Smooths over differences in CWD between IDEs and running tests in Travis.
String _readFile(String path) => readFile('integration/$path');

View File

@ -0,0 +1,21 @@
library angular2.test.transform.reflection_remover;
import 'package:analyzer/analyzer.dart';
import 'package:angular2/src/transform/reflection_remover/codegen.dart';
import 'package:angular2/src/transform/reflection_remover/rewriter.dart';
import 'package:unittest/unittest.dart';
import 'reflection_remover_files/expected/index.dart' as expected;
import '../common/read_file.dart';
void allTests() {
var codegen = new Codegen('web/index.dart', 'web/index.ngDeps.dart');
test('should remove uses of mirrors & insert calls to generated code', () {
var code =
readFile('reflection_remover/reflection_remover_files/index.dart');
var output =
new Rewriter(code, codegen).rewrite(parseCompilationUnit(code));
expect(output, equals(expected.code));
});
}

View File

@ -1,13 +0,0 @@
library web_foo;
import 'package:angular2/src/core/application.dart';
import 'package:angular2/src/reflection/reflection.dart';
import 'index.ngDeps.dart' as ngStaticInit;
import 'package:angular2/src/reflection/reflection_capabilities.dart';
import 'bar.dart';
void main() {
ngStaticInit.setupReflection(reflector);
reflector.reflectionCapabilities = new ReflectionCapabilities();
bootstrap(MyComponent);
}

View File

@ -1,153 +1,15 @@
library angular2.test.transform;
import 'dart:io';
import 'package:barback/barback.dart';
import 'package:angular2/transformer.dart';
import 'package:code_transformers/tests.dart';
import 'package:dart_style/dart_style.dart';
import 'package:unittest/unittest.dart';
import 'package:unittest/vm_config.dart';
import 'reflection_remover_files/expected/index.dart'
as reflection_remover_output;
import 'directive_processor/all_tests.dart' as directiveProcessor;
import 'integration/all_tests.dart' as integration;
import 'reflection_remover/all_tests.dart' as reflectionRemover;
main() {
useVMConfiguration();
group('Integration tests:', _integrationTests);
}
var formatter = new DartFormatter();
var transform = new AngularTransformerGroup(new TransformerOptions(
'web/index.dart', reflectionEntryPoint: 'web/index.dart'));
class IntegrationTestConfig {
final String name;
final Map<String, String> assetPathToInputPath;
final Map<String, String> assetPathToExpectedOutputPath;
IntegrationTestConfig(this.name,
{Map<String, String> inputs, Map<String, String> outputs})
: this.assetPathToInputPath = inputs,
this.assetPathToExpectedOutputPath = outputs;
}
void _integrationTests() {
/*
* Each test has its own directory for inputs & an `expected` directory for
* expected outputs.
*
* In addition to these declared inputs, we inject a set of common inputs for
* every test.
*/
var commonInputs = {
'angular2|lib/src/core/annotations/annotations.dart':
'../../lib/src/core/annotations/annotations.dart',
'angular2|lib/src/core/application.dart': 'common/application.dart',
'angular2|lib/src/reflection/reflection_capabilities.dart':
'common/reflection_capabilities.dart'
};
var tests = [
new IntegrationTestConfig('Simple',
inputs: {
'a|web/index.dart': 'simple_annotation_files/index.dart',
'a|web/bar.dart': 'simple_annotation_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart':
'simple_annotation_files/expected/bar.ngDeps.dart',
'a|web/index.ngDeps.dart':
'simple_annotation_files/expected/index.ngDeps.dart'
}),
new IntegrationTestConfig('Reflection Remover',
inputs: {'a|web/index.dart': 'reflection_remover_files/index.dart'},
outputs: {'a|web/index.dart': reflection_remover_output.code}),
new IntegrationTestConfig('Two injected dependencies',
inputs: {
'a|web/index.dart': 'two_deps_files/index.dart',
'a|web/foo.dart': 'two_deps_files/foo.dart',
'a|web/bar.dart': 'two_deps_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'two_deps_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig('List of types',
inputs: {
'a|web/index.dart': 'list_of_types_files/index.dart',
'a|web/foo.dart': 'list_of_types_files/foo.dart',
'a|web/bar.dart': 'list_of_types_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'list_of_types_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig('Component with synthetic Constructor',
inputs: {
'a|web/index.dart': 'synthetic_ctor_files/index.dart',
'a|web/bar.dart': 'synthetic_ctor_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'synthetic_ctor_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig('Component with two annotations',
inputs: {
'a|web/index.dart': 'two_annotations_files/index.dart',
'a|web/bar.dart': 'two_annotations_files/bar.dart',
'angular2|lib/src/core/annotations/template.dart':
'../../lib/src/core/annotations/template.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'two_annotations_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig('Basic `bind`',
inputs: {
'a|web/index.dart': 'basic_bind_files/index.dart',
'a|web/bar.dart': 'basic_bind_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'basic_bind_files/expected/bar.ngDeps.dart'
}),
new IntegrationTestConfig('Chained dependencies',
inputs: {
'a|web/index.dart': 'chained_deps_files/index.dart',
'a|web/foo.dart': 'chained_deps_files/foo.dart',
'a|web/bar.dart': 'chained_deps_files/bar.dart'
},
outputs: {
'a|web/bar.ngDeps.dart': 'chained_deps_files/expected/bar.ngDeps.dart',
'a|web/foo.ngDeps.dart': 'chained_deps_files/expected/foo.ngDeps.dart'
})
];
var cache = {};
for (var config in tests) {
// Read in input & output files.
config.assetPathToInputPath
..addAll(commonInputs)
..forEach((key, value) {
config.assetPathToInputPath[key] =
cache.putIfAbsent(value, () => _readFile(value));
});
config.assetPathToExpectedOutputPath.forEach((key, value) {
config.assetPathToExpectedOutputPath[key] = cache.putIfAbsent(value, () {
var code = _readFile(value);
return value.endsWith('dart') ? formatter.format(code) : code;
});
});
testPhases(config.name, [
[transform]
], config.assetPathToInputPath, config.assetPathToExpectedOutputPath, []);
}
}
/// Smooths over differences in CWD between IDEs and running tests in Travis.
String _readFile(String path) {
for (var myPath in [path, 'test/transform/${path}']) {
var file = new File(myPath);
if (file.existsSync()) {
return file.readAsStringSync();
}
}
return path;
group('Directive Processor', directiveProcessor.allTests);
group('Reflection Remover', reflectionRemover.allTests);
group('Transformer Pipeline', integration.allTests);
}