| // Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| |
| import '../rule_test_support.dart'; |
| |
| main() { |
| defineReflectiveSuite(() { |
| defineReflectiveTests(UseBuildContextSynchronouslyTest); |
| }); |
| } |
| |
| @reflectiveTest |
| class UseBuildContextSynchronouslyTest extends LintRuleTest { |
| @override |
| bool get addFlutterPackageDep => true; |
| |
| @override |
| String get lintRule => 'use_build_context_synchronously'; |
| |
| /// Ensure we're not run in the test dir. |
| @override |
| String get testPackageRootPath => '$workspaceRootPath/lib'; |
| |
| /// https://github.com/dart-lang/linter/issues/3818 |
| test_context_propertyAccess() async { |
| await assertDiagnostics(r''' |
| import 'package:flutter/widgets.dart'; |
| |
| class W { |
| final BuildContext context; |
| W(this.context); |
| |
| Future<void> f() async { |
| await Future.value(); |
| g(this.context); |
| } |
| |
| Future<void> g(BuildContext context) async {} |
| } |
| ''', [ |
| lint(157, 15), |
| ]); |
| } |
| |
| /// https://github.com/dart-lang/linter/issues/3676 |
| test_contextPassedAsNamedParam() async { |
| await assertDiagnostics(r''' |
| import 'package:flutter/widgets.dart'; |
| |
| Future<void> foo(BuildContext context) async { |
| await Future.value(); |
| bar(context: context); |
| } |
| |
| Future<void> bar({required BuildContext context}) async {} |
| ''', [ |
| lint(117, 21), |
| ]); |
| } |
| |
| /// https://github.com/dart-lang/linter/issues/3700 |
| test_propertyAccess_getter() async { |
| await assertDiagnostics(r''' |
| import 'package:flutter/widgets.dart'; |
| |
| extension on BuildContext { |
| BuildContext get foo => this; |
| } |
| |
| Future<void> f(BuildContext context) async { |
| await Future.value(); |
| context.foo; |
| } |
| ''', [ |
| lint(174, 11), |
| ]); |
| } |
| |
| test_propertyAccess_setter() async { |
| await assertDiagnostics(r''' |
| import 'package:flutter/widgets.dart'; |
| |
| extension on BuildContext { |
| set foo(int x){ } |
| } |
| |
| Future<void> f(BuildContext context) async { |
| await Future.value(); |
| context.foo = 1; |
| } |
| ''', [ |
| lint(162, 11), |
| ]); |
| } |
| } |