Skip to content

Commit

Permalink
Issue 167 (#185)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
StarovNikita and shaark authored Dec 5, 2024
1 parent 956d433 commit 3ee83ca
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 40 deletions.
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 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<dynamic, dynamic> json,
) {
return AvoidReturningWidgetsExclude(
return ExcludedIdentifierParameter(
methodName: json['method_name'] as String,
className: json['class_name'] as String?,
);
Expand Down
56 changes: 56 additions & 0 deletions lib/src/common/parameters/excluded_identifiers_list_parameter.dart
Original file line number Diff line number Diff line change
@@ -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<ExcludedIdentifierParameter> 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<dynamic> excludeList,
}) {
final exclude = <ExcludedIdentifierParameter>[];

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<ClassDeclaration>();

return classDeclaration != null &&
classDeclaration.name.toString() == className;
}
}
}
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/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<AvoidReturningWidgetsExclude> exclude;
final ExcludedIdentifiersListParameter exclude;

/// Constructor for [AvoidReturningWidgetsParameters] model
AvoidReturningWidgetsParameters({
Expand All @@ -13,16 +13,12 @@ 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: ExcludedIdentifiersListParameter.fromJson(
excludeList: json[ExcludedIdentifiersListParameter.excludeParameterName]
as Iterable? ??
[],
),
);
}
}

0 comments on commit 3ee83ca

Please sign in to comment.