Skip to content

Commit

Permalink
issue-167. extract exclude rule code
Browse files Browse the repository at this point in the history
  • Loading branch information
shaark committed Dec 4, 2024
1 parent 956d433 commit 23c62a8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/listener.dart';
import 'package:collection/collection.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:solid_lints/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_parameters.dart';
import 'package:solid_lints/src/models/rule_config.dart';
Expand Down Expand Up @@ -96,7 +95,7 @@ class AvoidReturningWidgetsRule

final isWidgetReturned = hasWidgetType(returnType);

final isIgnored = _shouldIgnore(node);
final isIgnored = config.parameters.exclude.shouldIgnore(node);

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

Expand All @@ -105,25 +104,4 @@ class AvoidReturningWidgetsRule
}
});
}

bool _shouldIgnore(Declaration node) {
final methodName = node.declaredElement?.name;

final excludedItem = config.parameters.exclude
.firstWhereOrNull((e) => e.methodName == methodName);

if (excludedItem == null) return false;

final className = excludedItem.className;

if (className == null || node is! MethodDeclaration) {
return true;
} else {
final classDeclaration = node.thisOrAncestorOfType<ClassDeclaration>();

if (classDeclaration == null) return false;

return classDeclaration.name.toString() == className;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:solid_lints/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_exclude.dart';
import 'package:solid_lints/src/models/exclude_params.dart';

/// A data model class that represents the "avoid returning widgets" input
/// parameters.
class AvoidReturningWidgetsParameters {
/// A list of methods that should be excluded from the lint.
final List<AvoidReturningWidgetsExclude> exclude;
final ExcludeParameters exclude;

/// Constructor for [AvoidReturningWidgetsParameters] model
AvoidReturningWidgetsParameters({
Expand All @@ -13,16 +13,10 @@ class AvoidReturningWidgetsParameters {

/// Method for creating from json data
factory AvoidReturningWidgetsParameters.fromJson(Map<String, dynamic> json) {
final exclude = <AvoidReturningWidgetsExclude>[];

final excludeList = json['exclude'] as Iterable? ?? [];
for (final item in excludeList) {
if (item is Map) {
exclude.add(AvoidReturningWidgetsExclude.fromJson(item));
}
}
return AvoidReturningWidgetsParameters(
exclude: exclude,
exclude: ExcludeParameters.fromJson(
excludeList: json['exclude'] as Iterable,
),
);
}
}
53 changes: 53 additions & 0 deletions lib/src/models/exclude_params.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:collection/collection.dart';
import 'package:solid_lints/src/models/exclude_rule.dart';

/// A data model class that represents the "avoid returning widgets" input
/// parameters.
class ExcludeParameters {
/// A list of methods that should be excluded from the lint.
final List<ExcludeRule> exclude;

/// Constructor for [ExcludeParameters] model
ExcludeParameters({
required this.exclude,
});

/// Method for creating from json data
factory ExcludeParameters.fromJson({
required Iterable<dynamic> excludeList,
}) {
final exclude = <ExcludeRule>[];

for (final item in excludeList) {
if (item is Map) {
exclude.add(ExcludeRule.fromJson(item));
}
}
return ExcludeParameters(
exclude: exclude,
);
}

/// Method to define ignoring
bool shouldIgnore(Declaration node) {
final methodName = node.declaredElement?.name;

final excludedItem =
exclude.firstWhereOrNull((e) => e.methodName == methodName);

if (excludedItem == null) return false;

final className = excludedItem.className;

if (className == null || node is! MethodDeclaration) {
return true;
} else {
final classDeclaration = node.thisOrAncestorOfType<ClassDeclaration>();

if (classDeclaration == null) return false;

return classDeclaration.name.toString() == className;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/// Model class for AvoidReturningWidgetsExclude parameters
class AvoidReturningWidgetsExclude {
/// Model class for ExcludeRule parameters
class ExcludeRule {
/// The name of the method that should be excluded from the lint.
final String methodName;

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

/// Constructor for [AvoidReturningWidgetsExclude] model
const AvoidReturningWidgetsExclude({
/// Constructor for [ExcludeRule] model
const ExcludeRule({
required this.methodName,
required this.className,
});

///
factory AvoidReturningWidgetsExclude.fromJson(
factory ExcludeRule.fromJson(
Map<dynamic, dynamic> json,
) {
return AvoidReturningWidgetsExclude(
return ExcludeRule(
methodName: json['method_name'] as String,
className: json['class_name'] as String?,
);
Expand Down

0 comments on commit 23c62a8

Please sign in to comment.