Skip to content

Commit

Permalink
refactor and fix class ignore avoid_returning_widgets rule
Browse files Browse the repository at this point in the history
  • Loading branch information
4akloon committed Apr 19, 2024
1 parent d14f982 commit eec73d7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

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

if (classDeclaration == null) return false;

return classDeclaration.name.toString() == className;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class AvoidReturningWidgetsParameters {
factory AvoidReturningWidgetsParameters.fromJson(Map<String, dynamic> json) {
final exclude = <AvoidReturningWidgetsExclude>[];

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));
}
}
Expand Down
6 changes: 3 additions & 3 deletions lint_test/avoid_returning_widget_test/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit eec73d7

Please sign in to comment.