From 7cddd57344ab2490b7ebf67d1cd382b8f4331fbf Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 2 May 2024 18:14:36 +0300 Subject: [PATCH] Added `exclude` configuration to the `number_of_parameters` rule --- .../lints/no_empty_block/no_empty_block_rule.dart | 2 -- .../models/number_of_parameters_parameters.dart | 7 +++++++ .../number_of_parameters_rule.dart | 5 +++++ lint_test/number_of_parameters/analysis_options.yaml | 12 ++++++++++++ .../number_of_parameters_test.dart | 9 +++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 lint_test/number_of_parameters/analysis_options.yaml rename lint_test/{ => number_of_parameters}/number_of_parameters_test.dart (51%) diff --git a/lib/src/lints/no_empty_block/no_empty_block_rule.dart b/lib/src/lints/no_empty_block/no_empty_block_rule.dart index 3eb1971f..da678d29 100644 --- a/lib/src/lints/no_empty_block/no_empty_block_rule.dart +++ b/lib/src/lints/no_empty_block/no_empty_block_rule.dart @@ -78,8 +78,6 @@ class NoEmptyBlockRule extends SolidLintRule { CustomLintContext context, ) { context.registry.addCompilationUnit((node) { - - final visitor = NoEmptyBlockVisitor(config.parameters); node.accept(visitor); diff --git a/lib/src/lints/number_of_parameters/models/number_of_parameters_parameters.dart b/lib/src/lints/number_of_parameters/models/number_of_parameters_parameters.dart index e17e63d8..9a99276c 100644 --- a/lib/src/lints/number_of_parameters/models/number_of_parameters_parameters.dart +++ b/lib/src/lints/number_of_parameters/models/number_of_parameters_parameters.dart @@ -1,19 +1,26 @@ +import 'package:solid_lints/src/models/ignored_entities_model/ignored_entities_model.dart'; + /// A data model class that represents the "number of parameters" input /// parameters. class NumberOfParametersParameters { /// Maximum number of parameters allowed before a warning is triggered. final int maxParameters; + /// The methods/classes that this lint should ignore. + final IgnoredEntitiesModel ignoredEntitiesModel; + static const _defaultMaxParameters = 2; /// Constructor for [NumberOfParametersParameters] model const NumberOfParametersParameters({ required this.maxParameters, + required this.ignoredEntitiesModel, }); /// Method for creating from json data factory NumberOfParametersParameters.fromJson(Map json) => NumberOfParametersParameters( maxParameters: json['max_parameters'] as int? ?? _defaultMaxParameters, + ignoredEntitiesModel: IgnoredEntitiesModel.fromJson(json), ); } diff --git a/lib/src/lints/number_of_parameters/number_of_parameters_rule.dart b/lib/src/lints/number_of_parameters/number_of_parameters_rule.dart index bacc4074..3d5652a0 100644 --- a/lib/src/lints/number_of_parameters/number_of_parameters_rule.dart +++ b/lib/src/lints/number_of_parameters/number_of_parameters_rule.dart @@ -65,6 +65,11 @@ class NumberOfParametersRule CustomLintContext context, ) { context.registry.addDeclaration((node) { + if (config.parameters.ignoredEntitiesModel.matchClass(node) || + config.parameters.ignoredEntitiesModel.matchMethod(node)) { + return; + } + final parameters = switch (node) { (final MethodDeclaration node) => node.parameters?.parameters.length ?? 0, diff --git a/lint_test/number_of_parameters/analysis_options.yaml b/lint_test/number_of_parameters/analysis_options.yaml new file mode 100644 index 00000000..0097e840 --- /dev/null +++ b/lint_test/number_of_parameters/analysis_options.yaml @@ -0,0 +1,12 @@ +analyzer: + plugins: + - ../custom_lint + +custom_lint: + rules: + - number_of_parameters: + exclude: + - method_name: excludeMethod + class_name: ExcludeClass + - method_name: excludeFunction + - class_name: ExcludeEntireClass diff --git a/lint_test/number_of_parameters_test.dart b/lint_test/number_of_parameters/number_of_parameters_test.dart similarity index 51% rename from lint_test/number_of_parameters_test.dart rename to lint_test/number_of_parameters/number_of_parameters_test.dart index 12fa6e21..8d87e4c6 100644 --- a/lint_test/number_of_parameters_test.dart +++ b/lint_test/number_of_parameters/number_of_parameters_test.dart @@ -5,3 +5,12 @@ String numberOfParameters(String a, String b, String c) { return a + b + c; } + +void excludeFunction(int a, int, b, int c) {} + +class ExcludeClass { + // expect_lint: number_of_parameters + void foo(int a, int b, int c) {} + + void excludeMethod(int a, int b, int c) {} +}