Skip to content

Commit 3e1711b

Browse files
StarovNikitashaark
andauthored
Issue 167 1 (#186)
* issue-167. extract exclude rule code * issue-167. fixed null check * issue-167. fixed after comments * issue-167. fixed after comments * issue-167. added ingore parametrs to some lints * issue-167. fixed comments * issue-167. fixed after comments * issue-167. fixed after comments * issue-167. fixed no_empty_block_rule format * issue-167. fixed after comments * issue-167. fixed after comments * issue-167. chaged test * issue-167. fixed after comments --------- Co-authored-by: shaark <[email protected]>
1 parent 3ee83ca commit 3e1711b

23 files changed

+274
-61
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
## 0.2.4
2-
1+
## 0.3.0
2+
- Added `exclude` parameter for the following lints:
3+
- `avoid_returning_widgets`
4+
- `avoid_unused_parameters`
5+
- `cyclomatic_complexity`
6+
- `function_lines_of_code`
7+
- `no_empty_bloc`
8+
- `number_of_parameters`
9+
- BREAKING CHANGE: Renamed `excludeNames` parameter to `exclude` for `function_lines_of_code` lint.
310
- Fixed an issue with `prefer_early_retrun` for throw expression
411

512
## 0.2.3

lib/src/common/parameters/excluded_identifier_parameter.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
/// Model class for ExcludeRule parameters
22
class ExcludedIdentifierParameter {
33
/// The name of the method that should be excluded from the lint.
4-
final String methodName;
4+
final String? methodName;
55

66
/// The name of the class that should be excluded from the lint.
77
final String? className;
88

9+
/// The name of the plain Strings that should be excluded from the lint
10+
final String? declarationName;
11+
912
/// Constructor for [ExcludedIdentifierParameter] model
1013
const ExcludedIdentifierParameter({
11-
required this.methodName,
12-
required this.className,
14+
this.methodName,
15+
this.className,
16+
this.declarationName,
1317
});
1418

1519
///

lib/src/common/parameters/excluded_identifiers_list_parameter.dart

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,51 @@ class ExcludedIdentifiersListParameter {
2626
for (final item in excludeList) {
2727
if (item is Map) {
2828
exclude.add(ExcludedIdentifierParameter.fromJson(item));
29+
} else if (item is String) {
30+
exclude.add(
31+
ExcludedIdentifierParameter(
32+
declarationName: item,
33+
),
34+
);
2935
}
3036
}
3137
return ExcludedIdentifiersListParameter(
3238
exclude: exclude,
3339
);
3440
}
3541

42+
/// Method for creating from json data with default params
43+
factory ExcludedIdentifiersListParameter.defaultFromJson(
44+
Map<String, dynamic> json,
45+
) {
46+
final excludeList =
47+
json[ExcludedIdentifiersListParameter.excludeParameterName]
48+
as Iterable? ??
49+
[];
50+
51+
return ExcludedIdentifiersListParameter.fromJson(
52+
excludeList: excludeList,
53+
);
54+
}
55+
3656
/// Returns whether the target node should be ignored during analysis.
3757
bool shouldIgnore(Declaration node) {
38-
final methodName = node.declaredElement?.name;
58+
final declarationName = node.declaredElement?.name;
3959

40-
final excludedItem =
41-
exclude.firstWhereOrNull((e) => e.methodName == methodName);
60+
final excludedItem = exclude.firstWhereOrNull(
61+
(e) {
62+
if (e.declarationName == declarationName) {
63+
return true;
64+
} else if (node is ClassDeclaration) {
65+
return e.className == declarationName;
66+
} else if (node is MethodDeclaration) {
67+
return e.methodName == declarationName;
68+
} else if (node is FunctionDeclaration) {
69+
return e.methodName == declarationName;
70+
}
71+
return false;
72+
},
73+
);
4274

4375
if (excludedItem == null) return false;
4476

lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_parameters.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ class AvoidReturningWidgetsParameters {
1414
/// Method for creating from json data
1515
factory AvoidReturningWidgetsParameters.fromJson(Map<String, dynamic> json) {
1616
return AvoidReturningWidgetsParameters(
17-
exclude: ExcludedIdentifiersListParameter.fromJson(
18-
excludeList: json[ExcludedIdentifiersListParameter.excludeParameterName]
19-
as Iterable? ??
20-
[],
21-
),
17+
exclude: ExcludedIdentifiersListParameter.defaultFromJson(json),
2218
);
2319
}
2420
}

lib/src/lints/avoid_unused_parameters/avoid_unused_parameters_rule.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:analyzer/error/listener.dart';
22
import 'package:custom_lint_builder/custom_lint_builder.dart';
3+
import 'package:solid_lints/src/lints/avoid_unused_parameters/models/avoid_unused_parameters.dart';
34
import 'package:solid_lints/src/lints/avoid_unused_parameters/visitors/avoid_unused_parameters_visitor.dart';
45
import 'package:solid_lints/src/models/rule_config.dart';
56
import 'package:solid_lints/src/models/solid_lint_rule.dart';
@@ -64,7 +65,7 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
6465
/// };
6566
///
6667
/// ```
67-
class AvoidUnusedParametersRule extends SolidLintRule {
68+
class AvoidUnusedParametersRule extends SolidLintRule<AvoidUnusedParameters> {
6869
/// This lint rule represents
6970
/// the error whether we use bad formatted double literals.
7071
static const String lintName = 'avoid_unused_parameters';
@@ -79,6 +80,7 @@ class AvoidUnusedParametersRule extends SolidLintRule {
7980
final rule = RuleConfig(
8081
configs: configs,
8182
name: lintName,
83+
paramsParser: AvoidUnusedParameters.fromJson,
8284
problemMessage: (_) => 'Parameter is unused.',
8385
);
8486

@@ -91,7 +93,11 @@ class AvoidUnusedParametersRule extends SolidLintRule {
9193
ErrorReporter reporter,
9294
CustomLintContext context,
9395
) {
94-
context.registry.addCompilationUnit((node) {
96+
context.registry.addDeclaration((node) {
97+
final isIgnored = config.parameters.exclude.shouldIgnore(node);
98+
99+
if (isIgnored) return;
100+
95101
final visitor = AvoidUnusedParametersVisitor();
96102
node.accept(visitor);
97103

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:solid_lints/src/common/parameters/excluded_identifiers_list_parameter.dart';
2+
3+
/// A data model class that represents the "avoid returning widgets" input
4+
/// parameters.
5+
class AvoidUnusedParameters {
6+
/// A list of methods that should be excluded from the lint.
7+
final ExcludedIdentifiersListParameter exclude;
8+
9+
/// Constructor for [AvoidUnusedParameters] model
10+
AvoidUnusedParameters({
11+
required this.exclude,
12+
});
13+
14+
/// Method for creating from json data
15+
factory AvoidUnusedParameters.fromJson(Map<String, dynamic> json) {
16+
return AvoidUnusedParameters(
17+
exclude: ExcludedIdentifiersListParameter.defaultFromJson(json),
18+
);
19+
}
20+
}

lib/src/lints/cyclomatic_complexity/cyclomatic_complexity_rule.dart

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,19 @@ class CyclomaticComplexityRule
5252
CustomLintContext context,
5353
) {
5454
context.registry.addBlockFunctionBody((node) {
55-
final visitor = CyclomaticComplexityFlowVisitor();
56-
node.visitChildren(visitor);
55+
context.registry.addDeclaration((declarationNode) {
56+
final isIgnored =
57+
config.parameters.exclude.shouldIgnore(declarationNode);
58+
if (isIgnored) return;
5759

58-
if (visitor.complexityEntities.length + 1 >
59-
config.parameters.maxComplexity) {
60-
reporter.atNode(node, code);
61-
}
60+
final visitor = CyclomaticComplexityFlowVisitor();
61+
node.visitChildren(visitor);
62+
63+
if (visitor.complexityEntities.length + 1 >
64+
config.parameters.maxComplexity) {
65+
reporter.atNode(node, code);
66+
}
67+
});
6268
});
6369
}
6470
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1+
import 'package:solid_lints/src/common/parameters/excluded_identifiers_list_parameter.dart';
2+
13
/// Cyclomatic complexity metric limits configuration.
24
class CyclomaticComplexityParameters {
35
/// Threshold cyclomatic complexity level, exceeding it triggers a warning.
46
final int maxComplexity;
57

8+
/// A list of methods that should be excluded from the lint.
9+
final ExcludedIdentifiersListParameter exclude;
10+
611
/// Reference: NIST 500-235 item 2.5
712
static const _defaultMaxComplexity = 10;
813

914
/// Constructor for [CyclomaticComplexityParameters] model
1015
const CyclomaticComplexityParameters({
1116
required this.maxComplexity,
17+
required this.exclude,
1218
});
1319

1420
/// Method for creating from json data
1521
factory CyclomaticComplexityParameters.fromJson(Map<String, Object?> json) =>
1622
CyclomaticComplexityParameters(
1723
maxComplexity: json['max_complexity'] as int? ?? _defaultMaxComplexity,
24+
exclude: ExcludedIdentifiersListParameter.defaultFromJson(json),
1825
);
1926
}

lib/src/lints/function_lines_of_code/function_lines_of_code_rule.dart

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ class FunctionLinesOfCodeRule
5050
) {
5151
void checkNode(AstNode node) => _checkNode(resolver, reporter, node);
5252

53-
context.registry.addMethodDeclaration(checkNode);
54-
context.registry.addFunctionDeclaration(checkNode);
55-
context.registry.addFunctionExpression(checkNode);
53+
context.registry.addDeclaration((declarationNode) {
54+
final isIgnored = config.parameters.exclude.shouldIgnore(declarationNode);
55+
56+
if (isIgnored) return;
57+
58+
context.registry.addMethodDeclaration(checkNode);
59+
context.registry.addFunctionDeclaration(checkNode);
60+
context.registry.addFunctionExpression(checkNode);
61+
});
5662
}
5763

5864
void _checkNode(
5965
CustomLintResolver resolver,
6066
ErrorReporter reporter,
6167
AstNode node,
6268
) {
63-
final functionName = _getFunctionName(node);
64-
if (functionName != null &&
65-
config.parameters.excludeNames.contains(functionName)) {
66-
return;
67-
}
68-
6969
final visitor = FunctionLinesOfCodeVisitor(resolver.lineInfo);
7070
node.visitChildren(visitor);
7171

@@ -84,16 +84,4 @@ class FunctionLinesOfCodeRule
8484
);
8585
}
8686
}
87-
88-
String? _getFunctionName(AstNode node) {
89-
if (node is FunctionDeclaration) {
90-
return node.name.lexeme;
91-
} else if (node is MethodDeclaration) {
92-
return node.name.lexeme;
93-
} else if (node is FunctionExpression) {
94-
return node.declaredElement?.name;
95-
} else {
96-
return null;
97-
}
98-
}
9987
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1+
import 'package:solid_lints/src/common/parameters/excluded_identifiers_list_parameter.dart';
2+
13
/// A data model class that represents the "function lines of code" input
24
/// parameters.
35
class FunctionLinesOfCodeParameters {
46
/// Maximum allowed number of lines of code (LoC) per function,
57
/// exceeding this limit triggers a warning.
68
final int maxLines;
79

8-
/// Function names to be excluded from the rule check
9-
final List<String> excludeNames;
10+
/// A list of methods that should be excluded from the lint.
11+
final ExcludedIdentifiersListParameter exclude;
1012

1113
static const _defaultMaxLines = 200;
1214

1315
/// Constructor for [FunctionLinesOfCodeParameters] model
1416
const FunctionLinesOfCodeParameters({
1517
required this.maxLines,
16-
required this.excludeNames,
18+
required this.exclude,
1719
});
1820

1921
/// Method for creating from json data
2022
factory FunctionLinesOfCodeParameters.fromJson(Map<String, Object?> json) =>
2123
FunctionLinesOfCodeParameters(
2224
maxLines: json['max_lines'] as int? ?? _defaultMaxLines,
23-
excludeNames:
24-
List<String>.from(json['excludeNames'] as Iterable? ?? []),
25+
exclude: ExcludedIdentifiersListParameter.defaultFromJson(json),
2526
);
2627
}

0 commit comments

Comments
 (0)