fix(transformers): support `query.read`

Closes #8172
This commit is contained in:
Tobias Bosch 2016-04-21 08:05:08 -07:00
parent 2f7045720a
commit 386cc5dbb6
6 changed files with 36 additions and 11 deletions

View File

@ -103,7 +103,8 @@ export function main() {
selectors: [new CompileTokenMetadata({value: 'selector'})], selectors: [new CompileTokenMetadata({value: 'selector'})],
descendants: true, descendants: true,
first: false, first: false,
propertyName: 'prop' propertyName: 'prop',
read: new CompileTokenMetadata({value: 'readToken'})
}) })
], ],
viewQueries: [ viewQueries: [
@ -111,7 +112,8 @@ export function main() {
selectors: [new CompileTokenMetadata({value: 'selector'})], selectors: [new CompileTokenMetadata({value: 'selector'})],
descendants: true, descendants: true,
first: false, first: false,
propertyName: 'prop' propertyName: 'prop',
read: new CompileTokenMetadata({value: 'readToken'})
}) })
] ]
}); });

View File

@ -1010,16 +1010,24 @@ List<CompileDiDependencyMetadata> _readDeps(ListLiteral deps) {
_createQueryMetadata(Annotation a, bool defaultDescendantsValue, bool first, String propertyName) { _createQueryMetadata(Annotation a, bool defaultDescendantsValue, bool first, String propertyName) {
final selector = _readToken(a.arguments.arguments.first); final selector = _readToken(a.arguments.arguments.first);
var descendants = defaultDescendantsValue; var descendants = defaultDescendantsValue;
var read = null;
a.arguments.arguments.skip(0).forEach((arg) { a.arguments.arguments.skip(0).forEach((arg) {
if (arg is NamedExpression && arg.name.toString() == "descendants:") if (arg is NamedExpression) {
var name = arg.name.toString();
if (name == "descendants:") {
descendants = naiveEval(arg.expression); descendants = naiveEval(arg.expression);
} else if (name == "read:") {
read = _readToken(arg.expression);
}
}
}); });
final selectors = selector.value is String ? final selectors = selector.value is String ?
selector.value.split(",").map( (value) => new CompileTokenMetadata(value: value) ).toList() : selector.value.split(",").map( (value) => new CompileTokenMetadata(value: value) ).toList() :
[selector]; [selector];
return new CompileQueryMetadata( return new CompileQueryMetadata(
selectors: selectors, descendants: descendants, first: first, propertyName: propertyName); selectors: selectors, descendants: descendants, first: first,
read: read, propertyName: propertyName);
} }
List<CompileDiDependencyMetadata> _getCompileDiDependencyMetadata( List<CompileDiDependencyMetadata> _getCompileDiDependencyMetadata(

View File

@ -260,6 +260,9 @@ class _NgMetaIdentifierResolver {
void _resolveQueries(Map<String, NgMeta> ngMetaMap, List queries, String neededBy) { void _resolveQueries(Map<String, NgMeta> ngMetaMap, List queries, String neededBy) {
queries.forEach((q) { queries.forEach((q) {
q.selectors.forEach((s) => s.identifier = _resolveIdentifier(ngMetaMap, neededBy, s.identifier)); q.selectors.forEach((s) => s.identifier = _resolveIdentifier(ngMetaMap, neededBy, s.identifier));
if (q.read != null) {
q.read.identifier = _resolveIdentifier(ngMetaMap, neededBy, q.read.identifier);
}
}); });
} }

View File

@ -134,13 +134,17 @@ void allTests() {
new CompileTypeMetadata(name: 'Service', moduleUrl: 'moduleUrl'); new CompileTypeMetadata(name: 'Service', moduleUrl: 'moduleUrl');
fooComponentMeta.queries = [ fooComponentMeta.queries = [
new CompileQueryMetadata(selectors: [new CompileTokenMetadata(identifier: new CompileIdentifierMetadata(name: 'Service'))]), new CompileQueryMetadata(selectors: [new CompileTokenMetadata(identifier: new CompileIdentifierMetadata(name: 'Service'))],
new CompileQueryMetadata(selectors: [new CompileTokenMetadata(value: 'one')]) read: new CompileTokenMetadata(identifier: new CompileIdentifierMetadata(name: 'Service'))),
new CompileQueryMetadata(selectors: [new CompileTokenMetadata(value: 'one')],
read: new CompileTokenMetadata(value: 'one'))
]; ];
fooComponentMeta.viewQueries = [ fooComponentMeta.viewQueries = [
new CompileQueryMetadata(selectors: [new CompileTokenMetadata(identifier: new CompileIdentifierMetadata(name: 'Service'))]), new CompileQueryMetadata(selectors: [new CompileTokenMetadata(identifier: new CompileIdentifierMetadata(name: 'Service'))],
new CompileQueryMetadata(selectors: [new CompileTokenMetadata(value: 'one')]) read: new CompileTokenMetadata(identifier: new CompileIdentifierMetadata(name: 'Service'))),
new CompileQueryMetadata(selectors: [new CompileTokenMetadata(value: 'one')],
read: new CompileTokenMetadata(value: 'one'))
]; ];
fooNgMeta.ngDeps.imports.add(new ImportModel()..uri = 'bar.dart'); fooNgMeta.ngDeps.imports.add(new ImportModel()..uri = 'bar.dart');
@ -155,11 +159,17 @@ void allTests() {
expect(cmp.queries[0].selectors[0].identifier.name, equals("Service")); expect(cmp.queries[0].selectors[0].identifier.name, equals("Service"));
expect(cmp.queries[0].selectors[0].identifier.moduleUrl, equals("moduleUrl")); expect(cmp.queries[0].selectors[0].identifier.moduleUrl, equals("moduleUrl"));
expect(cmp.queries[0].read.identifier.name, equals("Service"));
expect(cmp.queries[0].read.identifier.moduleUrl, equals("moduleUrl"));
expect(cmp.queries[1].selectors[0].value, equals("one")); expect(cmp.queries[1].selectors[0].value, equals("one"));
expect(cmp.queries[1].read.value, equals("one"));
expect(cmp.viewQueries[0].selectors[0].identifier.name, equals("Service")); expect(cmp.viewQueries[0].selectors[0].identifier.name, equals("Service"));
expect(cmp.viewQueries[0].selectors[0].identifier.moduleUrl, equals("moduleUrl")); expect(cmp.viewQueries[0].selectors[0].identifier.moduleUrl, equals("moduleUrl"));
expect(cmp.viewQueries[0].read.identifier.name, equals("Service"));
expect(cmp.viewQueries[0].read.identifier.moduleUrl, equals("moduleUrl"));
expect(cmp.viewQueries[1].selectors[0].value, equals("one")); expect(cmp.viewQueries[1].selectors[0].value, equals("one"));
expect(cmp.viewQueries[1].read.value, equals("one"));
}); });
test('should resolve providers from types.', () async { test('should resolve providers from types.', () async {

View File

@ -590,10 +590,12 @@ void allTests() {
expect(deps[5].isOptional, isTrue); expect(deps[5].isOptional, isTrue);
expect(deps[6].query.selectors[0].name, equals("ServiceDep")); expect(deps[6].query.selectors[0].name, equals("ServiceDep"));
expect(deps[6].query.descendants, isTrue); expect(deps[6].query.descendants, isTrue);
expect(deps[6].query.read.identifier.name, equals("ServiceDep"));
expect(deps[7].query.selectors[0].identifier.name, equals("ServiceDep")); expect(deps[7].query.selectors[0].identifier.name, equals("ServiceDep"));
expect(deps[7].query.descendants, isTrue); expect(deps[7].query.descendants, isTrue);
expect(deps[8].viewQuery.selectors[0].value, equals("one")); expect(deps[8].viewQuery.selectors[0].value, equals("one"));
expect(deps[8].viewQuery.selectors[1].value, equals("two")); expect(deps[8].viewQuery.selectors[1].value, equals("two"));
expect(deps[8].viewQuery.read.value, equals("three"));
expect(deps[9].viewQuery.selectors[0].value, equals("one")); expect(deps[9].viewQuery.selectors[0].value, equals("one"));
expect(deps[9].viewQuery.selectors[1].value, equals("two")); expect(deps[9].viewQuery.selectors[1].value, equals("two"));
expect(deps[10].token.identifier.name, equals("ServiceDep")); expect(deps[10].token.identifier.name, equals("ServiceDep"));

View File

@ -141,9 +141,9 @@ class ComponentWithDiDeps {
@Self() ServiceDep arg4, @Self() ServiceDep arg4,
@SkipSelf() ServiceDep arg5, @SkipSelf() ServiceDep arg5,
@Optional() ServiceDep arg6, @Optional() ServiceDep arg6,
@Query(ServiceDep, descendants: true) arg7, @Query(ServiceDep, descendants: true, read: ServiceDep) arg7,
@ContentChildren(ServiceDep) arg8, @ContentChildren(ServiceDep) arg8,
@ViewQuery("one,two") arg9, @ViewQuery("one,two", read: "three") arg9,
@ViewChildren("one,two") arg10, @ViewChildren("one,two") arg10,
this.arg11, this.arg11,
[@Optional() ServiceDep arg12, [@Optional() ServiceDep arg12,