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 7cddd57
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
2 changes: 0 additions & 2 deletions lib/src/lints/no_empty_block/no_empty_block_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ class NoEmptyBlockRule extends SolidLintRule<IgnoredEntitiesModel> {
CustomLintContext context,
) {
context.registry.addCompilationUnit((node) {


final visitor = NoEmptyBlockVisitor(config.parameters);
node.accept(visitor);

Expand Down
Original file line number Diff line number Diff line change
@@ -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<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 7cddd57

Please sign in to comment.