Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
feat(Generics): add support for generic components and directives
Browse files Browse the repository at this point in the history
Closes #68.
Closes #1530.

PiperOrigin-RevId: 211713269
  • Loading branch information
leonsenft authored and matanlurey committed Sep 10, 2018
1 parent 7e62eff commit 2e6c806
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 20 deletions.
2 changes: 0 additions & 2 deletions _goldens/test/_files/directives/generics.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'package:angular/angular.dart';
// TODO(leonsenft): remove when `Typed` is exported publicly.
import 'package:angular/src/core/metadata/typed.dart';

/// A component with no generic type parameters.
@Component(
Expand Down
1 change: 0 additions & 1 deletion _goldens/test/_files/directives/generics.template.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import 'generics.dart';
export 'generics.dart';
import 'package:angular/angular.dart';
import 'package:angular/src/core/metadata/typed.dart';
import 'package:angular/src/di/reflector.dart' as _ngRef;
import 'package:angular/angular.template.dart' as _ref0;
import 'package:angular/src/core/linker/app_view.dart';
Expand Down
2 changes: 0 additions & 2 deletions _tests/test/core/generics/generics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import 'dart:async';

import 'package:angular/angular.dart';
// TODO(https://github.com/dart-lang/angular/issues/1530):
import 'package:angular/src/core/metadata/typed.dart';
import 'package:angular_test/angular_test.dart';
import 'package:test/test.dart';

Expand Down
40 changes: 40 additions & 0 deletions angular/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
### New features

* Added support for generic components and directives.

Type arguments can now be specified for any generic components and
directives via `Typed` instances passed to the `Component` annotation's
`directiveTypes` parameter.

```dart
@Component(
selector: 'generic',
template: '{{value}}',
)
class GenericComponent<T> {
@Input()
T value;
}
@Component(
selector: 'example',
template: '''
<generic [value]="value"></generic>
''',
directives: [
GenericComponent,
],
directiveTypes: [
Typed<GenericComponent<String>>(),
],
)
class ExampleComponent {
var value = 'Hello generics!';
}
```

The `Typed` class also has support for typing specific component and
directive instances by `#`-reference, and flowing generic type parameters
from the annotated component as type arguments to its children. See its
documentation for details.

### Bug fixes

* [#1538][]: A compile-time error is reported if the `@deferred` template
Expand Down
2 changes: 0 additions & 2 deletions angular/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ targets:
exclude:
- "lib/builder.dart"
- "lib/src/compiler/**"
# TODO(https://github.com/dart-lang/angular/issues/1530)
- "lib/src/core/metadata/typed.dart"
- "lib/src/source_gen/**"

builders:
Expand Down
3 changes: 1 addition & 2 deletions angular/lib/src/core/metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export 'metadata/lifecycle_hooks.dart'
OnDestroy,
OnInit,
DoCheck;
// TODO(leonsenft): export when support for generics is complete.
// export 'metadata/typed.dart';
export 'metadata/typed.dart';
export 'metadata/view.dart';
export 'metadata/visibility.dart';

Expand Down
11 changes: 0 additions & 11 deletions angular_compiler/test/analyzer/view/typed_reader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ import '../../src/compile.dart';
import '../../src/resolve.dart';

const testImport = 'asset:test_lib/lib/test_lib.dart';
// TODO(leonsenft): remove when `Typed` is exported publicly.
final typedImport =
angular.replaceFirst('angular.dart', 'src/core/metadata/typed.dart');

Future<TypedElement> parse(String source) async {
final amendedSource = '''
import "$typedImport";
@Component()
class GenericComponent<T> {}
Expand Down Expand Up @@ -223,7 +218,6 @@ void main() {
test('if a concrete type is used as a type argument of "Typed"', () async {
await compilesExpecting(
'''
import '$typedImport';
@Directive()
class ConcreteDirective {}
const typed = Typed<ConcreteDirective>();
Expand All @@ -243,7 +237,6 @@ void main() {
test('if a non-existent type parameter is flowed', () async {
await compilesExpecting(
'''
import '$typedImport';
@Component()
class GenericComponent<T> {}
const typed = Typed<GenericComponent>.of([#X]);
Expand All @@ -263,7 +256,6 @@ void main() {
test("if a type argument isn't a supported type", () async {
await compilesExpecting(
'''
import '$typedImport';
@Component()
class GenericComponent<T> {}
const typed = Typed<GenericComponent>.of([12]);
Expand All @@ -283,7 +275,6 @@ void main() {
test('if "Typed.on" is specified anywhere other than the root', () async {
await compilesExpecting(
'''
import '$typedImport';
@Component()
class GenericComponent<T> {}
const typed = Typed<GenericComponent>.of([
Expand All @@ -305,7 +296,6 @@ void main() {
test('if a directive with bounded type parameters is typed', () async {
await compilesExpecting(
'''
import '$typedImport';
@Component()
class GenericComponent<T extends num> {}
const typed = Typed<GenericComponent<int>>();
Expand All @@ -324,7 +314,6 @@ void main() {
test('if "Typed" isn\'t applied to a directive', () async {
await compilesExpecting(
'''
import '$typedImport';
const typed = Typed<List<int>>();
@typed
Expand Down

0 comments on commit 2e6c806

Please sign in to comment.