Skip to content

Commit

Permalink
feat(lint): looking for the dispose method correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoRosaa committed Jul 12, 2023
1 parent a86607b commit e9db803
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/error/listener.dart';
import 'package:collection/collection.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:riverpod_analyzer_utils/riverpod_analyzer_utils.dart';

import '../riverpod_custom_lint.dart';

const overrideAnnotation = 'override';
const disposeMethod = 'dispose';

class AvoidUseRefInsideDispose extends RiverpodLintRule {
const AvoidUseRefInsideDispose() : super(code: _code);

static const _code = LintCode(
name: 'avoid_use_ref_inside_dispose',
name: 'avoid_ref_inside_state_dispose',
problemMessage: "Avoid using 'Ref' in the dispose method.",
);

Expand All @@ -30,14 +28,29 @@ class AvoidUseRefInsideDispose extends RiverpodLintRule {

if (widgetRefType.isAssignableFromType(targetType)) {
final ancestor = node.thisOrAncestorMatching((method) {
if (method is MethodDeclaration) {
final hasOverride = method.metadata.firstWhereOrNull((element) {
return element.name.name == overrideAnnotation;
if (method is MethodDeclaration &&
method.name.lexeme == disposeMethod) {
/// This [thisOrAncestorMatching] is to look for an ancestor of the
/// [dispose] method found
final classe = method.thisOrAncestorMatching((element) {
/// Looking for the class which is a [ConsumeState]
final classe = element.thisOrAncestorMatching((classe) {
if (classe is ClassDeclaration) {
final extendsClause = classe.extendsClause;
if (extendsClause == null) return false;
final extendsType = extendsClause.superclass.type;
if (extendsType == null) return false;

return consumerStateType.isExactlyType(extendsType);
}

return false;
});

return classe != null;
});

if (method.name.lexeme == disposeMethod && hasOverride != null) {
return true;
}
return classe != null;
}
return false;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class MyWidget extends ConsumerStatefulWidget {
class _MyWidgetState extends ConsumerState<MyWidget> {
@override
void dispose() {
// ignore: avoid_use_ref_inside_dispose
// expect_lint: avoid_ref_inside_state_dispose
ref.read(provider);
// ignore: avoid_use_ref_inside_dispose
// expect_lint: avoid_ref_inside_state_dispose
ref.watch(provider);

super.dispose();
Expand All @@ -26,3 +26,17 @@ class _MyWidgetState extends ConsumerState<MyWidget> {
return Container();
}
}

class PlainClass with ChangeNotifier {
PlainClass(this.ref);

final WidgetRef ref;

@override
void dispose() {
ref.read(provider);
ref.watch(provider);

super.dispose();
}
}

0 comments on commit e9db803

Please sign in to comment.