Skip to content

Commit

Permalink
Added exclude configuration to the number_of_parameters rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sufftea committed May 2, 2024
1 parent f647904 commit a914dc0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
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;
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<String, Object?> json) =>
NumberOfParametersParameters(
maxParameters: json['max_parameters'] as int? ?? _defaultMaxParameters,
ignoredEntitiesModel: IgnoredEntitiesModel.fromJson(json),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions lint_test/number_of_parameters/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}

0 comments on commit a914dc0

Please sign in to comment.