-
-
Notifications
You must be signed in to change notification settings - Fork 956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(riverpod_lint): added avoid use ref inside dispose #2701
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a4ee82e
feat(lints): added avoid use ref inside dispose
LeonardoRosaa 70e40a4
formatted files
LeonardoRosaa 8ff0c84
feat(lint): added tests for avoid_use_ref_inside_dispose lint
LeonardoRosaa a86607b
formatted files
LeonardoRosaa e9db803
feat(lint): looking for the dispose method correctly
LeonardoRosaa 2e2f75e
feat(lints): added early pattern
LeonardoRosaa d40c49a
feat(lints): added documentation for avoid use ref in the dispose method
LeonardoRosaa dd4f082
Apply suggestions from code review
rrousselGit 99249f1
Apply suggestions from code review
rrousselGit 10f3beb
Apply suggestions from code review
rrousselGit 45ad9bf
Apply suggestions from code review
rrousselGit e2f14f9
Apply suggestions from code review
rrousselGit bc2ba92
Apply suggestions from code review
rrousselGit cd1d648
Apply suggestions from code review
rrousselGit 7399958
Apply suggestions from code review
rrousselGit 0cdc60d
Apply suggestions from code review
rrousselGit 95c680f
Apply suggestions from code review
rrousselGit 5e5e42c
renamed classes
LeonardoRosaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/riverpod_lint/lib/src/lints/avoid_use_ref_inside_dispose.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/error/listener.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 disposeMethod = 'dispose'; | ||
|
||
class AvoidUseRefInsideDispose extends RiverpodLintRule { | ||
const AvoidUseRefInsideDispose() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'avoid_ref_inside_state_dispose', | ||
problemMessage: "Avoid using 'Ref' inside State.dispose.", | ||
); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ErrorReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
context.registry.addMethodInvocation((node) { | ||
final targetType = node.realTarget?.staticType; | ||
if (targetType == null) return; | ||
if (!widgetRefType.isAssignableFromType(targetType)) return; | ||
|
||
final ancestor = node.thisOrAncestorMatching((method) { | ||
final isDisposeMethod = | ||
method is MethodDeclaration && method.name.lexeme == disposeMethod; | ||
if (!isDisposeMethod) return false; | ||
|
||
return _findConsumerStateClass(node) != null; | ||
}); | ||
|
||
if (ancestor != null) { | ||
reporter.reportErrorForNode(_code, node); | ||
} | ||
}); | ||
} | ||
|
||
/// Looking for the ConsumerState class ancestor | ||
/// into the [node] parent. | ||
AstNode? _findConsumerStateClass(AstNode node) { | ||
return node.parent?.thisOrAncestorMatching((node) { | ||
if (node is! ClassDeclaration) return false; | ||
|
||
/// Looking for the class which is a [ConsumerState] | ||
final extendsClause = node.extendsClause; | ||
if (extendsClause == null) return false; | ||
final extendsType = extendsClause.superclass.type; | ||
if (extendsType == null) return false; | ||
|
||
return consumerStateType.isExactlyType(extendsType); | ||
}); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/riverpod_lint_flutter_test/test/goldens/lints/avoid_use_ref_inside_dispose.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
|
||
final provider = Provider((ref) => 0); | ||
|
||
class MyWidget extends ConsumerStatefulWidget { | ||
rrousselGit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const MyWidget({super.key}); | ||
|
||
@override | ||
ConsumerState<ConsumerStatefulWidget> createState() => _MyWidgetState(); | ||
} | ||
|
||
class _MyWidgetState extends ConsumerState<MyWidget> { | ||
@override | ||
void dispose() { | ||
// expect_lint: avoid_ref_inside_state_dispose | ||
ref.read(provider); | ||
// expect_lint: avoid_ref_inside_state_dispose | ||
ref.watch(provider); | ||
|
||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container(); | ||
} | ||
} | ||
|
||
class PlainClass with ChangeNotifier { | ||
PlainClass(this.ref); | ||
|
||
final WidgetRef ref; | ||
|
||
@override | ||
void dispose() { | ||
ref.read(provider); | ||
ref.watch(provider); | ||
|
||
super.dispose(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rename the class and file name to match the lint code? avoid_ref_inside_state_dispose.dart and AvoidRefInsideStateDispose