Skip to content

Commit 3ee83ca

Browse files
StarovNikitashaark
andauthored
Issue 167 (#185)
* issue-167. extract exclude rule code * issue-167. fixed null check * issue-167. fixed after comments * issue-167. fixed after comments * issue-167. fixed comments * issue-167. fixed after comments * issue-167. fixed after comments --------- Co-authored-by: shaark <[email protected]>
1 parent 956d433 commit 3ee83ca

File tree

4 files changed

+70
-40
lines changed

4 files changed

+70
-40
lines changed

lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_exclude.dart renamed to lib/src/common/parameters/excluded_identifier_parameter.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
/// Model class for AvoidReturningWidgetsExclude parameters
2-
class AvoidReturningWidgetsExclude {
1+
/// Model class for ExcludeRule parameters
2+
class ExcludedIdentifierParameter {
33
/// The name of the method that should be excluded from the lint.
44
final String methodName;
55

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

9-
/// Constructor for [AvoidReturningWidgetsExclude] model
10-
const AvoidReturningWidgetsExclude({
9+
/// Constructor for [ExcludedIdentifierParameter] model
10+
const ExcludedIdentifierParameter({
1111
required this.methodName,
1212
required this.className,
1313
});
1414

1515
///
16-
factory AvoidReturningWidgetsExclude.fromJson(
16+
factory ExcludedIdentifierParameter.fromJson(
1717
Map<dynamic, dynamic> json,
1818
) {
19-
return AvoidReturningWidgetsExclude(
19+
return ExcludedIdentifierParameter(
2020
methodName: json['method_name'] as String,
2121
className: json['class_name'] as String?,
2222
);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import 'package:analyzer/dart/ast/ast.dart';
2+
import 'package:collection/collection.dart';
3+
import 'package:solid_lints/src/common/parameters/excluded_identifier_parameter.dart';
4+
5+
/// A model representing "exclude" parameters for linting, defining
6+
/// identifiers (classes, methods, functions) to be ignored during analysis.
7+
class ExcludedIdentifiersListParameter {
8+
/// A list of identifiers (classes, methods, functions) that should be
9+
/// excluded from the lint.
10+
final List<ExcludedIdentifierParameter> exclude;
11+
12+
/// A common parameter key for analysis_options.yaml
13+
static const String excludeParameterName = 'exclude';
14+
15+
/// Constructor for [ExcludedIdentifiersListParameter] model
16+
ExcludedIdentifiersListParameter({
17+
required this.exclude,
18+
});
19+
20+
/// Method for creating from json data
21+
factory ExcludedIdentifiersListParameter.fromJson({
22+
required Iterable<dynamic> excludeList,
23+
}) {
24+
final exclude = <ExcludedIdentifierParameter>[];
25+
26+
for (final item in excludeList) {
27+
if (item is Map) {
28+
exclude.add(ExcludedIdentifierParameter.fromJson(item));
29+
}
30+
}
31+
return ExcludedIdentifiersListParameter(
32+
exclude: exclude,
33+
);
34+
}
35+
36+
/// Returns whether the target node should be ignored during analysis.
37+
bool shouldIgnore(Declaration node) {
38+
final methodName = node.declaredElement?.name;
39+
40+
final excludedItem =
41+
exclude.firstWhereOrNull((e) => e.methodName == methodName);
42+
43+
if (excludedItem == null) return false;
44+
45+
final className = excludedItem.className;
46+
47+
if (className == null || node is! MethodDeclaration) {
48+
return true;
49+
} else {
50+
final classDeclaration = node.thisOrAncestorOfType<ClassDeclaration>();
51+
52+
return classDeclaration != null &&
53+
classDeclaration.name.toString() == className;
54+
}
55+
}
56+
}

lib/src/lints/avoid_returning_widgets/avoid_returning_widgets_rule.dart

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'package:analyzer/dart/ast/ast.dart';
22
import 'package:analyzer/dart/element/type.dart';
33
import 'package:analyzer/error/listener.dart';
4-
import 'package:collection/collection.dart';
54
import 'package:custom_lint_builder/custom_lint_builder.dart';
65
import 'package:solid_lints/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_parameters.dart';
76
import 'package:solid_lints/src/models/rule_config.dart';
@@ -96,7 +95,7 @@ class AvoidReturningWidgetsRule
9695

9796
final isWidgetReturned = hasWidgetType(returnType);
9897

99-
final isIgnored = _shouldIgnore(node);
98+
final isIgnored = config.parameters.exclude.shouldIgnore(node);
10099

101100
final isOverriden = node.declaredElement?.hasOverride ?? false;
102101

@@ -105,25 +104,4 @@ class AvoidReturningWidgetsRule
105104
}
106105
});
107106
}
108-
109-
bool _shouldIgnore(Declaration node) {
110-
final methodName = node.declaredElement?.name;
111-
112-
final excludedItem = config.parameters.exclude
113-
.firstWhereOrNull((e) => e.methodName == methodName);
114-
115-
if (excludedItem == null) return false;
116-
117-
final className = excludedItem.className;
118-
119-
if (className == null || node is! MethodDeclaration) {
120-
return true;
121-
} else {
122-
final classDeclaration = node.thisOrAncestorOfType<ClassDeclaration>();
123-
124-
if (classDeclaration == null) return false;
125-
126-
return classDeclaration.name.toString() == className;
127-
}
128-
}
129107
}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import 'package:solid_lints/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_exclude.dart';
1+
import 'package:solid_lints/src/common/parameters/excluded_identifiers_list_parameter.dart';
22

33
/// A data model class that represents the "avoid returning widgets" input
44
/// parameters.
55
class AvoidReturningWidgetsParameters {
66
/// A list of methods that should be excluded from the lint.
7-
final List<AvoidReturningWidgetsExclude> exclude;
7+
final ExcludedIdentifiersListParameter exclude;
88

99
/// Constructor for [AvoidReturningWidgetsParameters] model
1010
AvoidReturningWidgetsParameters({
@@ -13,16 +13,12 @@ class AvoidReturningWidgetsParameters {
1313

1414
/// Method for creating from json data
1515
factory AvoidReturningWidgetsParameters.fromJson(Map<String, dynamic> json) {
16-
final exclude = <AvoidReturningWidgetsExclude>[];
17-
18-
final excludeList = json['exclude'] as Iterable? ?? [];
19-
for (final item in excludeList) {
20-
if (item is Map) {
21-
exclude.add(AvoidReturningWidgetsExclude.fromJson(item));
22-
}
23-
}
2416
return AvoidReturningWidgetsParameters(
25-
exclude: exclude,
17+
exclude: ExcludedIdentifiersListParameter.fromJson(
18+
excludeList: json[ExcludedIdentifiersListParameter.excludeParameterName]
19+
as Iterable? ??
20+
[],
21+
),
2622
);
2723
}
2824
}

0 commit comments

Comments
 (0)