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

Commit bbb2f0a

Browse files
matanlureyalorenzen
authored andcommitted
misc(Goldens): Add some examples of template expressions.
Most of these correctly are emitted as expressions that are non-dynamic. Notably: * Using collection literals (`[]`, `{}`) causes a dynamic call. * Using nested `*ngFor` causes a dynamic call. There are probably other ways to (accidentally) cause a dynamic call that otherwise appears to be static. We should continue to add examples, as these would block the use of advanced Dart language features such as dart-lang/language#41 or dart-lang/sdk#35084 in the template. PiperOrigin-RevId: 221693970
1 parent 564e492 commit bbb2f0a

File tree

3 files changed

+849
-0
lines changed

3 files changed

+849
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import 'package:angular/angular.dart';
2+
3+
// OK: Compiles to "_ctx.a + _ctx.b"
4+
@Component(
5+
selector: 'comp-1',
6+
template: '{{a + b}}',
7+
)
8+
class Comp1 {
9+
var a = 1;
10+
var b = 2;
11+
}
12+
13+
// OK: Compiles to "_ctx.a.b(_ctx.c)"
14+
@Component(
15+
selector: 'comp-2',
16+
template: '{{a.b(c)}}',
17+
)
18+
class Comp2 {
19+
var a = Comp2Model();
20+
}
21+
22+
class Comp2Model {
23+
String b(String c) => c;
24+
}
25+
26+
// OK: Compiles to "import1.Comp3.max(_ctx.a, _ctx.b).isEven"
27+
@Component(
28+
selector: 'comp-3',
29+
template: '{{max(a, b).isEven}}',
30+
)
31+
class Comp3 {
32+
static T max<T extends Comparable<T>>(T a, T b) {
33+
return a.compareTo(b) < 0 ? b : a;
34+
}
35+
36+
var a = 0;
37+
var b = 1;
38+
}
39+
40+
// Dynamic :(
41+
//
42+
// Compiles to:
43+
// "List<dynamic> Function(dynamic, dynamic) _arr_0;"
44+
// "_arr_0 = import6.pureProxy2((p0, p1) => [p0, p1])"
45+
// ...
46+
// "_arr_0(_ctx.a, _ctx.b).first.inMilliseconds"
47+
@Component(
48+
selector: 'comp-4',
49+
template: '{{[a, b].first.inMilliseconds}}',
50+
)
51+
class Comp4 {
52+
var a = Duration(seconds: 1);
53+
var b = Duration(seconds: 2);
54+
}
55+
56+
// Dynamic :(
57+
//
58+
// Compiles to:
59+
// "Map<String, dynamic> Function(dynamic) _map_0;"
60+
// "_map_0 = import6.pureProxy1((p0) => {'1': p0}))"
61+
// ...
62+
// "_map_0(_ctx.b).values.first.inMilliseconds"
63+
@Component(
64+
selector: 'comp-5',
65+
template: '{{{"1": b}.values.first.inMilliseconds}}}',
66+
)
67+
class Comp5 {
68+
var v = Duration(seconds: 1);
69+
}
70+
71+
// OK!
72+
//
73+
// Compiles to:
74+
// "final local_duration = import7.unsafeCast<Duration>(locals['\$implicit']);"
75+
// "final currVal_0 = import6.interpolate0(local_duration.inMilliseconds);"
76+
@Component(
77+
selector: 'comp-6',
78+
directives: [NgFor],
79+
template: r'''
80+
<ng-container *ngFor="let duration of durations">
81+
{{duration.inMilliseconds}}
82+
</ng-container>
83+
''',
84+
)
85+
class Comp6 {
86+
var durations = [Duration(seconds: 1)];
87+
}
88+
89+
// Dynamic :(
90+
//
91+
// Compiles to:
92+
// "final local_durations = import7.unsafeCast<List<Duration>>(locals['\$implicit']);"
93+
// "final currVal_0 = local_durations;"
94+
// "if (import6.checkBinding(_expr_0, currVal_0)) {"
95+
// "_NgFor_0_9.ngForOf = currVal_0;"
96+
// "_expr_0 = currVal_0;"
97+
// "}"
98+
// ...
99+
// "final local_duration = locals['\$implicit'];"
100+
// "final currVal_0 = import6.interpolate0(local_duration.inMilliseconds);"
101+
@Component(
102+
selector: 'comp-7',
103+
directives: [NgFor],
104+
template: r'''
105+
<ng-container *ngFor="let durations of items">
106+
<ng-container *ngFor="let duration of durations">
107+
{{duration.inMilliseconds}}
108+
</ng-container>
109+
</ng-container>
110+
''',
111+
)
112+
class Comp7 {
113+
var items = [
114+
[Duration(seconds: 1)]
115+
];
116+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// ignore_for_file: library_prefixes,unused_import,no_default_super_constructor_explicit
2+
// The .template.dart files also export the user code.
3+
export 'dynamic_expressions.dart';
4+
5+
// Required for referencing runtime code.
6+
import 'dart:html' as _html;
7+
import 'package:angular/angular.dart' as _ng;
8+
import 'package:angular/src/core/change_detection/directive_change_detector.dart' as _ng;
9+
import 'package:angular/src/core/linker/app_view.dart' as _ng;
10+
11+
// Required for specifically referencing user code.
12+
import 'dynamic_expressions.dart';
13+
14+
// Required for "type inference" (scoping).
15+
import 'package:angular/angular.dart';
16+
17+
// For @Component class Comp1.
18+
external List<dynamic> get styles$Comp1;
19+
external _ng.ComponentFactory<Comp1> get Comp1NgFactory;
20+
external _ng.AppView<Comp1> viewFactory_Comp10(_ng.AppView<dynamic> parentView, int parentIndex);
21+
class ViewComp10 extends _ng.AppView<Comp1> {
22+
external ViewComp10(_ng.AppView<dynamic> parentView, int parentIndex);
23+
}
24+
// For @Component class Comp2.
25+
external List<dynamic> get styles$Comp2;
26+
external _ng.ComponentFactory<Comp2> get Comp2NgFactory;
27+
external _ng.AppView<Comp2> viewFactory_Comp20(_ng.AppView<dynamic> parentView, int parentIndex);
28+
class ViewComp20 extends _ng.AppView<Comp2> {
29+
external ViewComp20(_ng.AppView<dynamic> parentView, int parentIndex);
30+
}
31+
// For @Component class Comp3.
32+
external List<dynamic> get styles$Comp3;
33+
external _ng.ComponentFactory<Comp3> get Comp3NgFactory;
34+
external _ng.AppView<Comp3> viewFactory_Comp30(_ng.AppView<dynamic> parentView, int parentIndex);
35+
class ViewComp30 extends _ng.AppView<Comp3> {
36+
external ViewComp30(_ng.AppView<dynamic> parentView, int parentIndex);
37+
}
38+
// For @Component class Comp4.
39+
external List<dynamic> get styles$Comp4;
40+
external _ng.ComponentFactory<Comp4> get Comp4NgFactory;
41+
external _ng.AppView<Comp4> viewFactory_Comp40(_ng.AppView<dynamic> parentView, int parentIndex);
42+
class ViewComp40 extends _ng.AppView<Comp4> {
43+
external ViewComp40(_ng.AppView<dynamic> parentView, int parentIndex);
44+
}
45+
// For @Component class Comp5.
46+
external List<dynamic> get styles$Comp5;
47+
external _ng.ComponentFactory<Comp5> get Comp5NgFactory;
48+
external _ng.AppView<Comp5> viewFactory_Comp50(_ng.AppView<dynamic> parentView, int parentIndex);
49+
class ViewComp50 extends _ng.AppView<Comp5> {
50+
external ViewComp50(_ng.AppView<dynamic> parentView, int parentIndex);
51+
}
52+
// For @Component class Comp6.
53+
external List<dynamic> get styles$Comp6;
54+
external _ng.ComponentFactory<Comp6> get Comp6NgFactory;
55+
external _ng.AppView<Comp6> viewFactory_Comp60(_ng.AppView<dynamic> parentView, int parentIndex);
56+
class ViewComp60 extends _ng.AppView<Comp6> {
57+
external ViewComp60(_ng.AppView<dynamic> parentView, int parentIndex);
58+
}
59+
// For @Component class Comp7.
60+
external List<dynamic> get styles$Comp7;
61+
external _ng.ComponentFactory<Comp7> get Comp7NgFactory;
62+
external _ng.AppView<Comp7> viewFactory_Comp70(_ng.AppView<dynamic> parentView, int parentIndex);
63+
class ViewComp70 extends _ng.AppView<Comp7> {
64+
external ViewComp70(_ng.AppView<dynamic> parentView, int parentIndex);
65+
}
66+
67+
external void initReflector();

0 commit comments

Comments
 (0)