From eec73d72fd3a659a634482d329a5e8eea1e24b7b Mon Sep 17 00:00:00 2001 From: "alexiuk.genius" Date: Fri, 19 Apr 2024 14:46:37 +0200 Subject: [PATCH] refactor and fix class ignore `avoid_returning_widgets` rule --- .../avoid_returning_widgets_rule.dart | 28 +++++++++---------- .../avoid_returning_widgets_parameters.dart | 5 ++-- .../analysis_options.yaml | 6 ++-- .../avoid_returning_widget_test.dart | 28 +++++++++++++++++-- 4 files changed, 46 insertions(+), 21 deletions(-) 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 4bddb3b..18d908d 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 @@ -3,7 +3,6 @@ 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_exclude.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'; import 'package:solid_lints/src/models/solid_lint_rule.dart'; @@ -97,7 +96,7 @@ class AvoidReturningWidgetsRule final isWidgetReturned = hasWidgetType(returnType); - final isIgnored = _hasIgnored(node, returnType); + final isIgnored = _shouldIgnore(node); final isOverriden = node.declaredElement?.hasOverride ?? false; @@ -107,23 +106,24 @@ class AvoidReturningWidgetsRule }); } - bool _hasIgnored(Declaration node, DartType returnType) { + bool _shouldIgnore(Declaration node) { final methodName = node.declaredElement?.name; final excludedItem = config.parameters.exclude .firstWhereOrNull((e) => e.methodName == methodName); - if (excludedItem - case AvoidReturningWidgetsExclude( - :final String? className, - )) { - if (className == null) { - return true; - } else { - return returnType.hasIgnoredType(ignoredTypes: {className}); - } - } + if (excludedItem == null) return false; + + final className = excludedItem.className; - return false; + 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 b9846ce..75012f5 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 @@ -15,8 +15,9 @@ class AvoidReturningWidgetsParameters { factory AvoidReturningWidgetsParameters.fromJson(Map json) { final exclude = []; - for (final item in (json['exclude'] as Iterable?) ?? []) { - if (item is Map && item['method_name'] is String) { + final excludeList = json['exclude'] as Iterable? ?? []; + for (final item in excludeList) { + if (item is Map) { exclude.add(AvoidReturningWidgetsExclude.fromJson(item)); } } diff --git a/lint_test/avoid_returning_widget_test/analysis_options.yaml b/lint_test/avoid_returning_widget_test/analysis_options.yaml index a382036..49cfe54 100644 --- a/lint_test/avoid_returning_widget_test/analysis_options.yaml +++ b/lint_test/avoid_returning_widget_test/analysis_options.yaml @@ -6,6 +6,6 @@ custom_lint: rules: - avoid_returning_widgets: exclude: - - method_name: excludeWidget - class_name: SizedBox - - method_name: excludeWidget2 + - method_name: excludeWidgetMethod + class_name: ExcludeWidget + - method_name: excludeMethod diff --git a/lint_test/avoid_returning_widget_test/avoid_returning_widget_test.dart b/lint_test/avoid_returning_widget_test/avoid_returning_widget_test.dart index 3051290..ff20d27 100644 --- a/lint_test/avoid_returning_widget_test/avoid_returning_widget_test.dart +++ b/lint_test/avoid_returning_widget_test/avoid_returning_widget_test.dart @@ -65,6 +65,30 @@ Widget build() { return Offstage(); } -SizedBox excludeWidget() => const SizedBox(); +SizedBox excludeMethod() => const SizedBox(); -Container excludeWidget2() => Container(); +class ExcludeWidget extends StatelessWidget { + const ExcludeWidget({super.key}); + + @override + Widget build(BuildContext context) { + return const Placeholder(); + } + + Widget excludeWidgetMethod() => const SizedBox(); + + // expect_lint: avoid_returning_widgets + Widget excludeWidgetMethod2() => const SizedBox(); +} + +class NotExcludeWidget extends StatelessWidget { + const NotExcludeWidget({super.key}); + + @override + Widget build(BuildContext context) { + return const Placeholder(); + } + + // expect_lint: avoid_returning_widgets + Widget excludeWidgetMethod() => const SizedBox(); +}