diff --git a/test/rules/all.dart b/test/rules/all.dart index b71ae6b03..0b6e0aa43 100644 --- a/test/rules/all.dart +++ b/test/rules/all.dart @@ -34,6 +34,8 @@ import 'avoid_single_cascade_in_expression_statements_test.dart' as avoid_single_cascade_in_expression_statements; import 'avoid_types_as_parameter_names_test.dart' as avoid_types_as_parameter_names; +import 'avoid_types_on_closure_parameters_test.dart' + as avoid_types_on_closure_parameters; import 'avoid_unused_constructor_parameters_test.dart' as avoid_unused_constructor_parameters; import 'avoid_void_async_test.dart' as avoid_void_async; @@ -243,6 +245,7 @@ void main() { avoid_shadowing_type_parameters.main(); avoid_single_cascade_in_expression_statements.main(); avoid_types_as_parameter_names.main(); + avoid_types_on_closure_parameters.main(); avoid_unused_constructor_parameters.main(); avoid_void_async.main(); await_only_futures.main(); diff --git a/test/rules/avoid_types_on_closure_parameters_test.dart b/test/rules/avoid_types_on_closure_parameters_test.dart new file mode 100644 index 000000000..5f0f586f1 --- /dev/null +++ b/test/rules/avoid_types_on_closure_parameters_test.dart @@ -0,0 +1,125 @@ +// Copyright (c) 2023, 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(AvoidTypesOnClosureParametersTest); + }); +} + +@reflectiveTest +class AvoidTypesOnClosureParametersTest extends LintRuleTest { + @override + String get lintRule => 'avoid_types_on_closure_parameters'; + + test_argument() async { + await assertNoDiagnostics(r''' +void f(List list) { + list.map((e) => e.isEven); +} +'''); + } + + test_argument_typedParameter() async { + await assertDiagnostics(r''' +void f(List list) { + list.map((int e) => e.isEven); +} +''', [ + lint(37, 3), + ]); + } + + test_initializerInDeclaration_namedOptional() async { + // TODO(srawlins): Any test which just assigns a function literal to a + // variable should be converted to something like an argument, or let + // downwards inference work in some other way. + // See https://github.com/dart-lang/linter/issues/1099. + await assertNoDiagnostics(r''' +var f = ({e}) => e.isEven; +'''); + } + + test_initializerInDeclaration_optionalNullable() async { + // TODO(srawlins): Any test which just assigns a function literal to a + // variable should be converted to something like an argument, or let + // downwards inference work in some other way. + // See https://github.com/dart-lang/linter/issues/1099. + await assertNoDiagnostics(r''' +var f = ([e]) => e.name; +'''); + } + + test_initializerInDeclaration_optionalWithDefault() async { + // TODO(srawlins): Any test which just assigns a function literal to a + // variable should be converted to something like an argument, or let + // downwards inference work in some other way. + // See https://github.com/dart-lang/linter/issues/1099. + await assertNoDiagnostics(r''' +var f = ({e = ''}) => e; +'''); + } + + test_initializerInDeclaration_parameterIsTyped_dynamic() async { + // TODO(srawlins): Any test which just assigns a function literal to a + // variable should be converted to something like an argument, or let + // downwards inference work in some other way. + // See https://github.com/dart-lang/linter/issues/1099. + await assertNoDiagnostics(r''' +var goodName5 = (dynamic person) => person.name; +'''); + } + + test_initializerInDeclaration_parameterIsTyped_functionType() async { + // TODO(srawlins): Any test which just assigns a function literal to a + // variable should be converted to something like an argument, or let + // downwards inference work in some other way. + // See https://github.com/dart-lang/linter/issues/1099. + await assertDiagnostics(r''' +var functionWithFunction = (int f(int x)) => f(0); +''', [ + lint(28, 12), + ]); + } + + test_initializerInDeclaration_parameterIsTyped_namedRequired() async { + // TODO(srawlins): Any test which just assigns a function literal to a + // variable should be converted to something like an argument, or let + // downwards inference work in some other way. + // See https://github.com/dart-lang/linter/issues/1099. + await assertDiagnostics(r''' +var f = ({required int e}) => e.isEven; +''', [ + lint(19, 3), + ]); + } + + test_initializerInDeclaration_parameterIsTyped_optionalNullable() async { + // TODO(srawlins): Any test which just assigns a function literal to a + // variable should be converted to something like an argument, or let + // downwards inference work in some other way. + // See https://github.com/dart-lang/linter/issues/1099. + await assertDiagnostics(r''' +var f = ([int? e]) => e?.isEven; +''', [ + lint(10, 4), + ]); + } + + test_initializerInDeclaration_parameterIsTyped_optionalWithDefault() async { + // TODO(srawlins): Any test which just assigns a function literal to a + // variable should be converted to something like an argument, or let + // downwards inference work in some other way. + // See https://github.com/dart-lang/linter/issues/1099. + await assertDiagnostics(r''' +var f = ([String e = '']) => e; +''', [ + lint(10, 6), + ]); + } +} diff --git a/test_data/rules/avoid_types_on_closure_parameters.dart b/test_data/rules/avoid_types_on_closure_parameters.dart deleted file mode 100644 index e43cb51f9..000000000 --- a/test_data/rules/avoid_types_on_closure_parameters.dart +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2017, 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. - -// test w/ `dart test -N avoid_types_on_closure_parameters` - -class Person { - String name = ''; -} - -List people = []; - -var goodName1 = people.map((person) => person.name); // OK -var badName1 = people.map((Person person) => person.name); // LINT - -var goodName2 = ({person}) => person.name; // OK -var badName2 = ({required Person person}) => person.name; // LINT - -var goodName3 = ({person : ''}) => person; // OK -var badName3 = ([String person = '']) => person; // LINT - -var goodName4 = ([person]) => person.name; // OK -var badName4 = ([Person? person]) => person?.name; // LINT - -var goodName5 = (dynamic person) => person.name; // OK - -var functionWithFunction = (int f(int x)) => f(0); // LINT