Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit a981feb

Browse files
vicbjbdeboer
authored andcommitted
fix(source_metadata_extractor): Extract config from controllers
fixes #1308 Previous to this commit, the configuration would only be extracted for Decorators and Components
1 parent 1eb6d2a commit a981feb

File tree

4 files changed

+46
-38
lines changed

4 files changed

+46
-38
lines changed

lib/tools/common.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,16 @@ class DirectiveInfo {
2828
}
2929
}
3030

31-
const String DIRECTIVE = 'DIRECTIVE';
32-
const String COMPONENT = 'COMPONENT';
33-
3431
class DirectiveMetadata {
3532
String className;
36-
String type; // DIRECTIVE/COMPONENT
3733
String selector;
3834
String template;
3935
Map<String, String> attributeMappings;
4036
List<String> exportExpressionAttrs;
4137
List<String> exportExpressions;
4238

43-
DirectiveMetadata([this.className, this.type, this.selector,
44-
this.attributeMappings, this.exportExpressionAttrs,
45-
this.exportExpressions]) {
39+
DirectiveMetadata([this.className, this.selector, this.attributeMappings,
40+
this.exportExpressionAttrs, this.exportExpressions]) {
4641
if (attributeMappings == null) {
4742
attributeMappings = <String, String>{};
4843
}

lib/tools/source_metadata_extractor.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class SourceMetadataExtractor {
6868
}
6969
});
7070

71-
7271
// No explicit selector specified on the directive, compute one.
7372
var className = meta.className;
7473
if (dirInfo.selector == null) {
@@ -144,14 +143,11 @@ class DirectiveMetadataCollectingAstVisitor extends RecursiveAstVisitor {
144143
if (ann.arguments == null) return; // Ignore non-class annotations.
145144
// TODO(pavelj): this is not a safe check for the type of the
146145
// annotations, but good enough for now.
147-
if (ann.name.name != 'Component'
148-
&& ann.name.name != 'Decorator') return;
149-
150-
bool isComponent = ann.name.name == 'Component';
146+
if (ann.name.name != 'Component' &&
147+
ann.name.name != 'Decorator' &&
148+
ann.name.name != 'Controller') return;
151149

152-
var meta = new DirectiveMetadata()
153-
..className = clazz.name.name
154-
..type = isComponent ? COMPONENT : DIRECTIVE;
150+
var meta = new DirectiveMetadata()..className = clazz.name.name;
155151
metadata.add(meta);
156152

157153
ann.arguments.arguments.forEach((Expression arg) {

test/tools/source_metadata_extractor_spec.dart

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void main() {
1313

1414
it('should extract expressions and attribute names with expressions', () {
1515
var info = extractDirectiveInfo([
16-
new DirectiveMetadata('FooComponent', COMPONENT, 'foo-component', {
16+
new DirectiveMetadata('FooComponent', 'foo-component', {
1717
'barVal': '@bar',
1818
'baz-expr1': '<=>baz1',
1919
'baz-expr2': '=>baz2',
@@ -37,9 +37,7 @@ void main() {
3737

3838
it('should build a component selector if one is not explicitly specified', () {
3939
var info = extractDirectiveInfo([
40-
new DirectiveMetadata('MyFooComponent', COMPONENT, 'my-foo', {
41-
'foo-expr': '=>fooExpr'
42-
})
40+
new DirectiveMetadata('MyFooComponent', 'my-foo', {'foo-expr': '=>fooExpr'})
4341
]);
4442

4543
expect(info, hasLength(1));
@@ -48,9 +46,7 @@ void main() {
4846

4947
it('should build an element directive selector if one is not explicitly specified', () {
5048
var info = extractDirectiveInfo([
51-
new DirectiveMetadata('MyFooDirective', DIRECTIVE, 'my-foo', {
52-
'foo-expr': '=>fooExpr'
53-
})
49+
new DirectiveMetadata('MyFooDirective', 'my-foo', {'foo-expr': '=>fooExpr'})
5450
]);
5551

5652
expect(info, hasLength(1));
@@ -59,9 +55,7 @@ void main() {
5955

6056
it('should build an attr directive selector if one is not explicitly specified', () {
6157
var info = extractDirectiveInfo([
62-
new DirectiveMetadata('MyFooAttrDirective', '[my-foo]', '[my-foo]', {
63-
'foo-expr': '=>fooExpr'
64-
})
58+
new DirectiveMetadata('MyFooAttrDirective', '[my-foo]', {'foo-expr': '=>fooExpr'})
6559
]);
6660

6761
expect(info, hasLength(1));
@@ -70,9 +64,7 @@ void main() {
7064

7165
it('should figure out attribute name if dot(.) is used', () {
7266
var info = extractDirectiveInfo([
73-
new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[my-foo]', {
74-
'.': '=>fooExpr'
75-
})
67+
new DirectiveMetadata('MyFooAttrDirective', '[my-foo]', {'.': '=>fooExpr'})
7668
]);
7769

7870
expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs),
@@ -81,9 +73,7 @@ void main() {
8173

8274
it('should figure out attribute name from selector if dot(.) is used', () {
8375
var info = extractDirectiveInfo([
84-
new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]', {
85-
'.': '=>fooExpr'
86-
})
76+
new DirectiveMetadata('MyFooAttrDirective', '[blah][foo]', {'.': '=>fooExpr'})
8777
]);
8878

8979
expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs),
@@ -92,9 +82,7 @@ void main() {
9282

9383
it('should include exported expression attributes', () {
9484
var info = extractDirectiveInfo([
95-
new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]', {
96-
'.': '=>fooExpr'
97-
}, ['baz'])
85+
new DirectiveMetadata('MyFooAttrDirective', '[blah][foo]', {'.': '=>fooExpr'}, ['baz'])
9886
]);
9987

10088
expect(flattenList(info, (DirectiveInfo i) => i.expressionAttrs),
@@ -103,13 +91,12 @@ void main() {
10391

10492
it('should include exported expressions', () {
10593
var info = extractDirectiveInfo([
106-
new DirectiveMetadata('MyFooAttrDirective', DIRECTIVE, '[blah][foo]', {
107-
'.': '=>fooExpr'
108-
}, null, ['ctrl.baz'])
94+
new DirectiveMetadata('MyFooAttrDirective', '[blah][foo]', {'.': '=>fooExpr'}, null,
95+
['ctrl.baz'])
10996
]);
11097

11198
expect(flattenList(info, (DirectiveInfo i) => i.expressions),
112-
equals(['fooExpr', 'ctrl.baz']));
99+
equals(['fooExpr', 'ctrl.baz']));
113100
});
114101

115102
});

test/tools/transformer/metadata_generator_spec.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,31 @@ main() {
285285
});
286286
});
287287

288+
it('should extract list arguments from Controllers', () {
289+
return generates(phases,
290+
inputs: {
291+
'angular|lib/angular.dart': libAngular,
292+
'a|web/main.dart': '''
293+
import 'package:angular/angular.dart';
294+
295+
@Controller(exportExpressions: ['one', 'two'])
296+
class Engine {}
297+
298+
main() {}
299+
'''
300+
},
301+
imports: [
302+
'import \'main.dart\' as import_0;',
303+
'import \'package:angular/angular.dart\' as import_1;',
304+
],
305+
classes: {
306+
'import_0.Engine': [
307+
"const import_1.Controller(exportExpressions: "
308+
"const ['one','two',])",
309+
]
310+
});
311+
});
312+
288313
it('should extract primitive literals', () {
289314
return generates(phases,
290315
inputs: {
@@ -726,6 +751,11 @@ class Decorator extends Directive {
726751
super(map: map);
727752
}
728753
754+
class Controller extends Directive {
755+
const Decorator({selector, module, map, visibility, exportExpressions}) :
756+
super(map: map);
757+
}
758+
729759
class DummyAnnotation extends Directive {
730760
const DummyAnnotation(object);
731761
}

0 commit comments

Comments
 (0)