Skip to content

Commit dc6e7eb

Browse files
author
Tim Blasi
committed
feat(dart/transform): Record Type interfaces
To support interface-based lifecycle methods (angular#2220), we need to be able to query for the `interface`s a class supports. Record implemented interfaces to allow mirror-less querying at runtime. Closes angular#2204
1 parent e5419fe commit dc6e7eb

File tree

10 files changed

+110
-4
lines changed

10 files changed

+110
-4
lines changed

modules/angular2/src/transform/common/directive_metadata_reader.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ class _DirectiveMetadataVisitor extends Object
6666
callOnChange: false,
6767
callOnCheck: false,
6868
callOnInit: false,
69-
callOnAllChangesDone: false
70-
);
69+
callOnAllChangesDone: false);
7170
}
7271

7372
@override

modules/angular2/src/transform/directive_processor/rewriter.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ class CreateNgDepsVisitor extends Object with SimpleAstVisitor<Object> {
192192
}
193193
writer.print(''', 'annotations': ''');
194194
node.accept(_metaVisitor);
195+
if (node.implementsClause != null &&
196+
node.implementsClause.interfaces != null &&
197+
node.implementsClause.interfaces.isNotEmpty) {
198+
writer.print(''', 'interfaces': const [''');
199+
node.implementsClause.interfaces.forEach((interface) {
200+
writer.print('${interface.name}');
201+
});
202+
writer.print(']');
203+
}
195204
writer.print('})');
196205
return null;
197206
}

modules/angular2/test/transform/directive_metadata_extractor/directive_metadata_files/lifecycle.ng_deps.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@ library examples.hello_world.index_common_dart.ng_deps.dart;
22

33
import 'hello.dart';
44
import 'package:angular2/angular2.dart'
5-
show bootstrap, Component, Directive, View, NgElement, onChange, onDestroy, onInit, onCheck, onAllChangesDone;
5+
show
6+
bootstrap,
7+
Component,
8+
Directive,
9+
View,
10+
NgElement,
11+
onChange,
12+
onDestroy,
13+
onInit,
14+
onCheck,
15+
onAllChangesDone;
616

717
var _visited = false;
818
void initReflector(reflector) {
@@ -13,7 +23,8 @@ void initReflector(reflector) {
1323
'factory': () => new HelloCmp(),
1424
'parameters': const [const []],
1525
'annotations': const [
16-
const Component(lifecycle: [onChange, onDestroy, onInit, onCheck, onAllChangesDone])
26+
const Component(
27+
lifecycle: [onChange, onDestroy, onInit, onCheck, onAllChangesDone])
1728
]
1829
});
1930
}

modules/angular2/test/transform/directive_processor/all_tests.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ void allTests() {
4242

4343
_testNgDeps('should inline `templateUrl`s expressed as adjacent strings.',
4444
'split_url_expression_files/hello.dart');
45+
46+
_testNgDeps('should report implemented types as `interfaces`.',
47+
'interfaces_files/soup.dart');
48+
49+
_testNgDeps('should not include transitively implemented types.',
50+
'interface_chain_files/soup.dart');
51+
52+
_testNgDeps('should not include superclasses in `interfaces`.',
53+
'superclass_files/soup.dart');
4554
}
4655

4756
void _testNgDeps(String name, String inputPath,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
library dinner.soup.ng_deps.dart;
2+
3+
import 'soup.dart';
4+
import 'package:angular2/src/core/annotations_impl/annotations.dart';
5+
6+
var _visited = false;
7+
void initReflector(reflector) {
8+
if (_visited) return;
9+
_visited = true;
10+
reflector
11+
..registerType(ChangingSoupComponent, {
12+
'factory': () => new ChangingSoupComponent(),
13+
'parameters': const [],
14+
'annotations': const [const Component(selector: '[soup]')],
15+
'interfaces': const [PrimaryInterface]
16+
});
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
library dinner.soup;
2+
3+
import 'package:angular2/src/core/annotations_impl/annotations.dart';
4+
5+
@Component(selector: '[soup]')
6+
class ChangingSoupComponent implements PrimaryInterface {}
7+
8+
class TernaryInterface {}
9+
10+
class SecondaryInterface implements TernaryInterface {}
11+
12+
class PrimaryInterface implements SecondaryInterface {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
library dinner.soup.ng_deps.dart;
2+
3+
import 'soup.dart';
4+
import 'package:angular2/src/core/annotations_impl/annotations.dart';
5+
6+
var _visited = false;
7+
void initReflector(reflector) {
8+
if (_visited) return;
9+
_visited = true;
10+
reflector
11+
..registerType(ChangingSoupComponent, {
12+
'factory': () => new ChangingSoupComponent(),
13+
'parameters': const [],
14+
'annotations': const [const Component(selector: '[soup]')],
15+
'interfaces': const [OnChange]
16+
});
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
library dinner.soup;
2+
3+
import 'package:angular2/src/core/annotations_impl/annotations.dart';
4+
5+
@Component(selector: '[soup]')
6+
class ChangingSoupComponent implements OnChange {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
library dinner.soup.ng_deps.dart;
2+
3+
import 'soup.dart';
4+
import 'package:angular2/src/core/annotations_impl/annotations.dart';
5+
6+
var _visited = false;
7+
void initReflector(reflector) {
8+
if (_visited) return;
9+
_visited = true;
10+
reflector
11+
..registerType(ChangingSoupComponent, {
12+
'factory': () => new ChangingSoupComponent(),
13+
'parameters': const [],
14+
'annotations': const [const Component(selector: '[soup]')]
15+
});
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
library dinner.soup;
2+
3+
import 'package:angular2/src/core/annotations_impl/annotations.dart';
4+
5+
@Component(selector: '[soup]')
6+
class ChangingSoupComponent extends Super {}
7+
8+
class Iface {}
9+
10+
class Super implements Iface {}

0 commit comments

Comments
 (0)