@@ -24,6 +24,213 @@ class UseBuildContextSynchronouslyTest extends LintRuleTest {
24
24
@override
25
25
String get testPackageRootPath => '$workspaceRootPath /lib' ;
26
26
27
+ test_awaitBeforeIfStatement_beforeReferenceToContext () async {
28
+ await assertDiagnostics (r'''
29
+ import 'package:flutter/widgets.dart';
30
+
31
+ void foo(BuildContext context, Future<bool> condition) async {
32
+ var b = await condition;
33
+ if (b) {
34
+ Navigator.of(context);
35
+ }
36
+ }
37
+ ''' , [
38
+ lint (145 , 21 ),
39
+ ]);
40
+ }
41
+
42
+ test_awaitBeforeReferenceToContext_inClosure () async {
43
+ // todo (pq): what about closures?
44
+ await assertNoDiagnostics (r'''
45
+ import 'package:flutter/widgets.dart';
46
+
47
+ void foo(BuildContext context, Future<bool> condition) async {
48
+ await condition;
49
+ f1(() {
50
+ f2(context);
51
+ });
52
+ }
53
+
54
+ void f1(Function f) {}
55
+ void f2(BuildContext c) {}
56
+ ''' );
57
+ }
58
+
59
+ // https://github.com/dart-lang/linter/issues/3457
60
+ test_awaitInIfCondition_aboveReferenceToContext () async {
61
+ await assertDiagnostics (r'''
62
+ import 'package:flutter/widgets.dart';
63
+
64
+ void foo(BuildContext context, Future<bool> condition) async {
65
+ if (await condition) {
66
+ Navigator.of(context);
67
+ }
68
+ }
69
+
70
+ ''' , [
71
+ lint (132 , 21 ),
72
+ ]);
73
+ }
74
+
75
+ test_awaitInIfCondition_beforeReferenceToContext () async {
76
+ await assertDiagnostics (r'''
77
+ import 'package:flutter/widgets.dart';
78
+
79
+ void foo(BuildContext context, Future<bool> condition) async {
80
+ if (await condition) return;
81
+ Navigator.of(context);
82
+ }
83
+ ''' , [
84
+ lint (136 , 21 ),
85
+ ]);
86
+ }
87
+
88
+ test_awaitInIfReferencesContext_beforeReferenceToContext () async {
89
+ await assertDiagnostics (r'''
90
+ import 'package:flutter/widgets.dart';
91
+
92
+ void foo(
93
+ BuildContext context, Future<bool> Function(BuildContext) condition) async {
94
+ if (await condition(context)) {
95
+ Navigator.of(context);
96
+ }
97
+ }
98
+ ''' , [
99
+ lint (169 , 21 ),
100
+ ]);
101
+ }
102
+
103
+ test_awaitInIfThen_afterReferenceToContext () async {
104
+ await assertNoDiagnostics (r'''
105
+ import 'package:flutter/widgets.dart';
106
+
107
+ void foo(BuildContext context, Future<bool> condition) async {
108
+ Navigator.of(context);
109
+ if (1 == 2) {
110
+ await condition;
111
+ return;
112
+ }
113
+ }
114
+ ''' );
115
+ }
116
+
117
+ test_awaitInIfThen_beforeReferenceToContext () async {
118
+ // TODO(srawlins): I think this should report a lint, since an `await` is
119
+ // encountered in the if-body.
120
+ await assertNoDiagnostics (r'''
121
+ import 'package:flutter/widgets.dart';
122
+
123
+ void foo(BuildContext context, Future<bool> condition) async {
124
+ if (1 == 2) {
125
+ await condition;
126
+ return;
127
+ }
128
+ Navigator.of(context);
129
+ }
130
+ ''' );
131
+ }
132
+
133
+ test_awaitInIfThenAndExitInElse_afterReferenceToContext () async {
134
+ await assertNoDiagnostics (r'''
135
+ import 'package:flutter/widgets.dart';
136
+
137
+ void foo(BuildContext context, Future<bool> condition) async {
138
+ Navigator.of(context);
139
+ if (1 == 2) {
140
+ await condition;
141
+ } else {
142
+ await condition;
143
+ return;
144
+ }
145
+ }
146
+ ''' );
147
+ }
148
+
149
+ test_awaitInIfThenAndExitInElse_beforeReferenceToContext () async {
150
+ await assertDiagnostics (r'''
151
+ import 'package:flutter/widgets.dart';
152
+
153
+ void foo(BuildContext context, Future<bool> condition) async {
154
+ if (1 == 2) {
155
+ await condition;
156
+ } else {
157
+ await condition;
158
+ return;
159
+ }
160
+ Navigator.of(context);
161
+ }
162
+ ''' , [
163
+ lint (190 , 21 ),
164
+ ]);
165
+ }
166
+
167
+ test_awaitInWhileBody_afterReferenceToContext () async {
168
+ await assertNoDiagnostics (r'''
169
+ import 'package:flutter/widgets.dart';
170
+
171
+ void foo(BuildContext context, Future<void> condition) async {
172
+ Navigator.of(context);
173
+ while (true) {
174
+ await condition;
175
+ break;
176
+ }
177
+ }
178
+ ''' );
179
+ }
180
+
181
+ test_awaitInWhileBody_beforeReferenceToContext () async {
182
+ await assertDiagnostics (r'''
183
+ import 'package:flutter/widgets.dart';
184
+
185
+ void foo(BuildContext context, Future<void> condition) async {
186
+ while (true) {
187
+ await condition;
188
+ break;
189
+ }
190
+ Navigator.of(context);
191
+ }
192
+ ''' , [
193
+ lint (158 , 21 ),
194
+ ]);
195
+ }
196
+
197
+ test_awaitThenExitInIfThenAndElse_afterReferenceToContext () async {
198
+ await assertNoDiagnostics (r'''
199
+ import 'package:flutter/widgets.dart';
200
+
201
+ void foo(BuildContext context, Future<bool> condition) async {
202
+ Navigator.of(context);
203
+ if (1 == 2) {
204
+ await condition;
205
+ return;
206
+ } else {
207
+ await condition;
208
+ return;
209
+ }
210
+ }
211
+ ''' );
212
+ }
213
+
214
+ test_awaitThenExitInIfThenAndElse_beforeReferenceToContext () async {
215
+ await assertDiagnostics (r'''
216
+ import 'package:flutter/widgets.dart';
217
+
218
+ void foo(BuildContext context, Future<bool> condition) async {
219
+ if (1 == 2) {
220
+ await condition;
221
+ return;
222
+ } else {
223
+ await condition;
224
+ return;
225
+ }
226
+ Navigator.of(context);
227
+ }
228
+ ''' , [
229
+ // No lint.
230
+ error (WarningCode .DEAD_CODE , 202 , 22 ),
231
+ ]);
232
+ }
233
+
27
234
/// https://github.com/dart-lang/linter/issues/3818
28
235
test_context_propertyAccess () async {
29
236
await assertDiagnostics (r'''
0 commit comments