Skip to content

Commit

Permalink
chore(hooks_memoized_consideration): do not consider useFuture and …
Browse files Browse the repository at this point in the history
…`useStream`
  • Loading branch information
nikaera committed Feb 27, 2024
1 parent 5e7f26b commit a638b5c
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 76 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,9 @@ class UnuseHookConsumerWidget extends ConsumerWidget { // ⭕

### hooks_memoized_consideration

Considering performance and functionality, there may be places where it is worth considering the use of `useMemoized`.
Considering functionality, there may be places where it is worth considering the use of `useMemoized`.

* https://api.flutter.dev/flutter/widgets/GlobalKey-class.html
* https://github.com/Region40/flutter_hooks/blob/f9103ae7bfb8df94b1105e4f988764b29d1e2ad3/packages/flutter_hooks/lib/src/async.dart#L8-L24

**Bad**

Expand Down
4 changes: 4 additions & 0 deletions packages/flutter_hooks_lint/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.5

- Do not consider `useFuture` and `useStream` in `hooks_memoized_consideration`

## 1.0.4

- Fixed a bug in `hooks_memoized_consideration`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:analyzer/error/listener.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:flutter_hooks_lint/src/helpers/hooks_helper.dart';
import 'package:flutter_hooks_lint/src/visitors/hooks_method_visitor.dart';

class HooksMemoizedConsiderationRule extends DartLintRule {
const HooksMemoizedConsiderationRule() : super(code: _code);
Expand All @@ -20,23 +19,6 @@ class HooksMemoizedConsiderationRule extends DartLintRule {
ErrorReporter reporter,
CustomLintContext context,
) {
context.registry.addClassDeclaration((classNode) {
classNode.visitChildren(
HooksMethodVisitor(
onVisitMethodInvocation: (node) {
final ancestor =
node.parent?.thisOrAncestorOfType<MethodInvocation>();
if (ancestor?.methodName.name == 'useMemoized') {
return;
}
if (['useFuture', 'useStream'].contains(node.methodName.name)) {
reporter.reportErrorForNode(code, node);
}
},
),
);
});

context.registry.addVariableDeclaration((node) {
if (node.childEntities.last.toString().startsWith('useMemoized')) {
return;
Expand Down Expand Up @@ -80,31 +62,6 @@ class _LintFix extends DartFix {
AnalysisError analysisError,
List<AnalysisError> others,
) {
context.registry.addClassDeclaration((classNode) {
classNode.visitChildren(
HooksMethodVisitor(
onVisitMethodInvocation: (node) {
if (!analysisError.sourceRange.intersects(node.sourceRange)) {
return;
}
if (['useFuture', 'useStream'].contains(node.methodName.name)) {
final changeBuilder = reporter.createChangeBuilder(
message: 'Wrap with useMemoized',
priority: 30,
);

changeBuilder.addDartFileEdit((builder) {
builder.addSimpleReplacement(
SourceRange(node.offset, node.length),
'useMemoized(() => ${node.toSource()})',
);
});
}
},
),
);
});

context.registry.addVariableDeclaration((node) {
if (!analysisError.sourceRange.intersects(node.sourceRange)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_hooks_lint/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_hooks_lint
description: A lint package providing guidelines for using flutter_hooks in your Flutter widget!
version: 1.0.4
version: 1.0.5
homepage: https://github.com/nikaera/flutter_hooks_lint
repository: https://github.com/nikaera/flutter_hooks_lint
documentation: https://github.com/nikaera/flutter_hooks_lint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ class ConsiderationMemoizedWidget extends HookWidget {
}
}

class HookConsiderationMemoizedWidget extends HookWidget {
class FutureGlobalKeyMemoizedWidget extends HookWidget {
@override
Widget build(BuildContext context) {
final state = useState(0);
final snapshot =
useFuture(Future.delayed(Duration(seconds: 5), () => "Hi!"));
final _ = useMemoized(
() => useFuture(Future.delayed(Duration(seconds: 5), () => "Hi!")));
useFuture(Future.delayed(Duration(seconds: 5), () => GlobalKey()));
useEffect(() {
if (snapshot.hasData && snapshot.data != null) {
state.value = 5;
Expand All @@ -39,7 +37,7 @@ class HookConsiderationMemoizedWidget extends HookWidget {

final _globalKeyProvider = Provider((ref) => GlobalKey());

class ConsumerConsiderationMemoizedWidget extends ConsumerWidget {
class GlobalKeyMemoizedWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final globalKey = ref.watch(_globalKeyProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,3 @@ Diff for file `test/hooks_memoized_consideration/hooks_memoized_consideration.da
final __ = useMemoized(() => GlobalObjectKey("object_memo"));
```
---
Message: `Wrap with useMemoized`
Priority: 30
Diff for file `test/hooks_memoized_consideration/hooks_memoized_consideration.dart:27`:
```
final state = useState(0);
final snapshot =
- useFuture(Future.delayed(Duration(seconds: 5), () => "Hi!"));
+ useMemoized(() => useFuture(Future.delayed(Duration(seconds: 5), () => "Hi!")));
final _ = useMemoized(
() => useFuture(Future.delayed(Duration(seconds: 5), () => "Hi!")));
```
---
Message: `Wrap with useMemoized`
Priority: 30
Diff for file `test/hooks_memoized_consideration/hooks_memoized_consideration.dart:27`:
```
final state = useState(0);
final snapshot =
- useFuture(Future.delayed(Duration(seconds: 5), () => "Hi!"));
+ useMemoized(() => useFuture(Future.delayed(Duration(seconds: 5), () => "Hi!")));
final _ = useMemoized(
() => useFuture(Future.delayed(Duration(seconds: 5), () => "Hi!")));
```
---
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void main() {
(result) async {
final lint = HooksMemoizedConsiderationRule();
final errors = await lint.testRun(result);
expect(errors, hasLength(3));
expect(errors, hasLength(2));

final fixes = lint.getFixes().map((e) => e as DartFix);
final results = await Future.wait(errors
Expand Down

0 comments on commit a638b5c

Please sign in to comment.