Skip to content

Commit 23c62a8

Browse files
author
shaark
committed
issue-167. extract exclude rule code
1 parent 956d433 commit 23c62a8

File tree

4 files changed

+65
-40
lines changed

4 files changed

+65
-40
lines changed

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: 5 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/models/exclude_params.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 ExcludeParameters exclude;
88

99
/// Constructor for [AvoidReturningWidgetsParameters] model
1010
AvoidReturningWidgetsParameters({
@@ -13,16 +13,10 @@ 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: ExcludeParameters.fromJson(
18+
excludeList: json['exclude'] as Iterable,
19+
),
2620
);
2721
}
2822
}

lib/src/models/exclude_params.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:analyzer/dart/ast/ast.dart';
2+
import 'package:collection/collection.dart';
3+
import 'package:solid_lints/src/models/exclude_rule.dart';
4+
5+
/// A data model class that represents the "avoid returning widgets" input
6+
/// parameters.
7+
class ExcludeParameters {
8+
/// A list of methods that should be excluded from the lint.
9+
final List<ExcludeRule> exclude;
10+
11+
/// Constructor for [ExcludeParameters] model
12+
ExcludeParameters({
13+
required this.exclude,
14+
});
15+
16+
/// Method for creating from json data
17+
factory ExcludeParameters.fromJson({
18+
required Iterable<dynamic> excludeList,
19+
}) {
20+
final exclude = <ExcludeRule>[];
21+
22+
for (final item in excludeList) {
23+
if (item is Map) {
24+
exclude.add(ExcludeRule.fromJson(item));
25+
}
26+
}
27+
return ExcludeParameters(
28+
exclude: exclude,
29+
);
30+
}
31+
32+
/// Method to define ignoring
33+
bool shouldIgnore(Declaration node) {
34+
final methodName = node.declaredElement?.name;
35+
36+
final excludedItem =
37+
exclude.firstWhereOrNull((e) => e.methodName == methodName);
38+
39+
if (excludedItem == null) return false;
40+
41+
final className = excludedItem.className;
42+
43+
if (className == null || node is! MethodDeclaration) {
44+
return true;
45+
} else {
46+
final classDeclaration = node.thisOrAncestorOfType<ClassDeclaration>();
47+
48+
if (classDeclaration == null) return false;
49+
50+
return classDeclaration.name.toString() == className;
51+
}
52+
}
53+
}

lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_exclude.dart renamed to lib/src/models/exclude_rule.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 ExcludeRule {
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 [ExcludeRule] model
10+
const ExcludeRule({
1111
required this.methodName,
1212
required this.className,
1313
});
1414

1515
///
16-
factory AvoidReturningWidgetsExclude.fromJson(
16+
factory ExcludeRule.fromJson(
1717
Map<dynamic, dynamic> json,
1818
) {
19-
return AvoidReturningWidgetsExclude(
19+
return ExcludeRule(
2020
methodName: json['method_name'] as String,
2121
className: json['class_name'] as String?,
2222
);

0 commit comments

Comments
 (0)