diff --git a/lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_exclude.dart b/lib/src/common/parameters/excluded_identifier_parameter.dart similarity index 59% rename from lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_exclude.dart rename to lib/src/common/parameters/excluded_identifier_parameter.dart index f2cd81be..8fb05354 100644 --- a/lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_exclude.dart +++ b/lib/src/common/parameters/excluded_identifier_parameter.dart @@ -1,22 +1,22 @@ -/// Model class for AvoidReturningWidgetsExclude parameters -class AvoidReturningWidgetsExclude { +/// Model class for ExcludeRule parameters +class ExcludedIdentifierParameter { /// 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 [ExcludedIdentifierParameter] model + const ExcludedIdentifierParameter({ required this.methodName, required this.className, }); /// - factory AvoidReturningWidgetsExclude.fromJson( + factory ExcludedIdentifierParameter.fromJson( Map json, ) { - return AvoidReturningWidgetsExclude( + return ExcludedIdentifierParameter( methodName: json['method_name'] as String, className: json['class_name'] as String?, ); diff --git a/lib/src/common/parameters/excluded_identifiers_list_parameter.dart b/lib/src/common/parameters/excluded_identifiers_list_parameter.dart new file mode 100644 index 00000000..2dc84989 --- /dev/null +++ b/lib/src/common/parameters/excluded_identifiers_list_parameter.dart @@ -0,0 +1,56 @@ +import 'package:analyzer/dart/ast/ast.dart'; +import 'package:collection/collection.dart'; +import 'package:solid_lints/src/common/parameters/excluded_identifier_parameter.dart'; + +/// A model representing "exclude" parameters for linting, defining +/// identifiers (classes, methods, functions) to be ignored during analysis. +class ExcludedIdentifiersListParameter { + /// A list of identifiers (classes, methods, functions) that should be + /// excluded from the lint. + final List exclude; + + /// A common parameter key for analysis_options.yaml + static const String excludeParameterName = 'exclude'; + + /// Constructor for [ExcludedIdentifiersListParameter] model + ExcludedIdentifiersListParameter({ + required this.exclude, + }); + + /// Method for creating from json data + factory ExcludedIdentifiersListParameter.fromJson({ + required Iterable excludeList, + }) { + final exclude = []; + + for (final item in excludeList) { + if (item is Map) { + exclude.add(ExcludedIdentifierParameter.fromJson(item)); + } + } + return ExcludedIdentifiersListParameter( + exclude: exclude, + ); + } + + /// Returns whether the target node should be ignored during analysis. + 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(); + + return classDeclaration != null && + classDeclaration.name.toString() == className; + } + } +} diff --git a/lib/src/lints/avoid_returning_widgets/avoid_returning_widgets_rule.dart b/lib/src/lints/avoid_returning_widgets/avoid_returning_widgets_rule.dart index d1a7195a..3c113552 100644 --- a/lib/src/lints/avoid_returning_widgets/avoid_returning_widgets_rule.dart +++ b/lib/src/lints/avoid_returning_widgets/avoid_returning_widgets_rule.dart @@ -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'; @@ -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; @@ -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(); - - if (classDeclaration == null) return false; - - return classDeclaration.name.toString() == className; - } - } } diff --git a/lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_parameters.dart b/lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_parameters.dart index 75012f5d..7fbbf7aa 100644 --- a/lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_parameters.dart +++ b/lib/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_parameters.dart @@ -1,10 +1,10 @@ -import 'package:solid_lints/src/lints/avoid_returning_widgets/models/avoid_returning_widgets_exclude.dart'; +import 'package:solid_lints/src/common/parameters/excluded_identifiers_list_parameter.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 exclude; + final ExcludedIdentifiersListParameter exclude; /// Constructor for [AvoidReturningWidgetsParameters] model AvoidReturningWidgetsParameters({ @@ -13,16 +13,12 @@ class AvoidReturningWidgetsParameters { /// Method for creating from json data factory AvoidReturningWidgetsParameters.fromJson(Map json) { - final exclude = []; - - 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: ExcludedIdentifiersListParameter.fromJson( + excludeList: json[ExcludedIdentifiersListParameter.excludeParameterName] + as Iterable? ?? + [], + ), ); } }