Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Move several if-concerned use_build_context_synchronously tests #4255

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion test/rule_test_support.dart
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,14 @@ abstract class BuildContext {
Widget get widget;
}

class Widget {
class Navigator {
static NavigatorState of(
BuildContext context, {bool rootNavigator = false}) => NavigatorState();
}

class NavigatorState {}

class Widget {}
''');
}
}
Expand Down
207 changes: 207 additions & 0 deletions test/rules/use_build_context_synchronously_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,213 @@ class UseBuildContextSynchronouslyTest extends LintRuleTest {
@override
String get testPackageRootPath => '$workspaceRootPath/lib';

test_awaitBeforeIfStatement_beforeReferenceToContext() async {
await assertDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<bool> condition) async {
var b = await condition;
if (b) {
Navigator.of(context);
}
}
''', [
lint(145, 21),
]);
}

test_awaitBeforeReferenceToContext_inClosure() async {
// todo (pq): what about closures?
await assertNoDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<bool> condition) async {
await condition;
f1(() {
f2(context);
});
}

void f1(Function f) {}
void f2(BuildContext c) {}
''');
}

// https://github.com/dart-lang/linter/issues/3457
test_awaitInIfCondition_aboveReferenceToContext() async {
await assertDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<bool> condition) async {
if (await condition) {
Navigator.of(context);
}
}

''', [
lint(132, 21),
]);
}

test_awaitInIfCondition_beforeReferenceToContext() async {
await assertDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<bool> condition) async {
if (await condition) return;
Navigator.of(context);
}
''', [
lint(136, 21),
]);
}

test_awaitInIfReferencesContext_beforeReferenceToContext() async {
await assertDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(
BuildContext context, Future<bool> Function(BuildContext) condition) async {
if (await condition(context)) {
Navigator.of(context);
}
}
''', [
lint(169, 21),
]);
}

test_awaitInIfThen_afterReferenceToContext() async {
await assertNoDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<bool> condition) async {
Navigator.of(context);
if (1 == 2) {
await condition;
return;
}
}
''');
}

test_awaitInIfThen_beforeReferenceToContext() async {
// TODO(srawlins): I think this should report a lint, since an `await` is
// encountered in the if-body.
await assertNoDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<bool> condition) async {
if (1 == 2) {
await condition;
return;
}
Navigator.of(context);
}
''');
}

test_awaitInIfThenAndExitInElse_afterReferenceToContext() async {
await assertNoDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<bool> condition) async {
Navigator.of(context);
if (1 == 2) {
await condition;
} else {
await condition;
return;
}
}
''');
}

test_awaitInIfThenAndExitInElse_beforeReferenceToContext() async {
await assertDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<bool> condition) async {
if (1 == 2) {
await condition;
} else {
await condition;
return;
}
Navigator.of(context);
}
''', [
lint(190, 21),
]);
}

test_awaitInWhileBody_afterReferenceToContext() async {
await assertNoDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<void> condition) async {
Navigator.of(context);
while (true) {
await condition;
break;
}
}
''');
}

test_awaitInWhileBody_beforeReferenceToContext() async {
await assertDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<void> condition) async {
while (true) {
await condition;
break;
}
Navigator.of(context);
}
''', [
lint(158, 21),
]);
}

test_awaitThenExitInIfThenAndElse_afterReferenceToContext() async {
await assertNoDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<bool> condition) async {
Navigator.of(context);
if (1 == 2) {
await condition;
return;
} else {
await condition;
return;
}
}
''');
}

test_awaitThenExitInIfThenAndElse_beforeReferenceToContext() async {
await assertDiagnostics(r'''
import 'package:flutter/widgets.dart';

void foo(BuildContext context, Future<bool> condition) async {
if (1 == 2) {
await condition;
return;
} else {
await condition;
return;
}
Navigator.of(context);
}
''', [
// No lint.
error(WarningCode.DEAD_CODE, 202, 22),
]);
}

/// https://github.com/dart-lang/linter/issues/3818
test_context_propertyAccess() async {
await assertDiagnostics(r'''
Expand Down
76 changes: 0 additions & 76 deletions test_data/rules/use_build_context_synchronously.dart
Original file line number Diff line number Diff line change
Expand Up @@ -353,79 +353,3 @@ void topLevel5(BuildContext context) async {
default: //nothing.
}
}

void topLevel6(BuildContext context) async {
Navigator.of(context).pushNamed('route1'); // OK
if (true) {
await Future<void>.delayed(Duration());
return;
}
Navigator.of(context).pushNamed('route2'); // OK
}

void topLevel7(BuildContext context) async {
Navigator.of(context).pushNamed('route1'); // OK
if (true) {
await Future<void>.delayed(Duration());
return;
} else {
await Future<void>.delayed(Duration());
return;
}
Navigator.of(context).pushNamed('route2'); // OK
}

void topLevel8(BuildContext context) async {
Navigator.of(context).pushNamed('route1'); // OK
if (true) {
await Future<void>.delayed(Duration());
} else {
await Future<void>.delayed(Duration());
return;
}
Navigator.of(context).pushNamed('route2'); // LINT
}

void topLevel9(BuildContext context) async {
Navigator.of(context).pushNamed('route1'); // OK
while (true) {
await Future<void>.delayed(Duration());
break;
}
Navigator.of(context).pushNamed('route2'); // LINT
}

void closure(BuildContext context) async {
await Future<void>.delayed(Duration());

// todo (pq): what about closures?
func(() {
f(context); // TODO: LINT
});
}

// https://github.com/dart-lang/linter/issues/3457
void awaitInIf1(BuildContext context, Future<bool> condition) async {
if (await condition) {
Navigator.of(context).pushNamed('routeName'); // LINT
}
}

void awaitInIf2(BuildContext context, Future<bool> condition) async {
var b = await condition;
if (b) {
Navigator.of(context).pushNamed('routeName'); // LINT
}
}

void awaitInIf3(BuildContext context, Future<bool> condition) async {
if (await condition) return;
Navigator.of(context).pushNamed('routeName'); // LINT
}

void awaitInIf4(
BuildContext context, Future<bool> Function(BuildContext) condition) async {
if (await condition(context)) { // OK
Navigator.of(context).pushNamed('routeName'); // LINT
}
}