Skip to content

Commit

Permalink
Treat HookConsumer and HookConsumerWidget as hook widgets (#248)
Browse files Browse the repository at this point in the history
* Treat HookConsumer and HookConsumerWidget as hook widgets

* Add changelog entry
  • Loading branch information
shilangyu authored Mar 5, 2024
1 parent fa93164 commit c1d8d4b
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/leancode_lint/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 11.0.0

- Treat `HookConsumer` and `HookConsumerWidget` from `package:hooks_riverpod` as hook widgets

# 10.0.0

- Bump `custom_lint_builder` dependency to 0.6.2
Expand Down
28 changes: 20 additions & 8 deletions packages/leancode_lint/lib/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,16 @@ extension LintRuleNodeRegistryExtensions on LintRuleNodeRegistry {
return;
}

final isHookBuilder = const TypeChecker.fromName(
'HookBuilder',
packageName: 'flutter_hooks',
).isExactly(classElement);
final isHookBuilder = const TypeChecker.any([
TypeChecker.fromName(
'HookBuilder',
packageName: 'flutter_hooks',
),
TypeChecker.fromName(
'HookConsumer',
packageName: 'hooks_riverpod',
),
]).isExactly(classElement);
if (!isHookBuilder) {
return;
}
Expand All @@ -161,10 +167,16 @@ extension LintRuleNodeRegistryExtensions on LintRuleNodeRegistry {
return;
}

const checker = TypeChecker.fromName(
'HookWidget',
packageName: 'flutter_hooks',
);
const checker = TypeChecker.any([
TypeChecker.fromName(
'HookWidget',
packageName: 'flutter_hooks',
),
TypeChecker.fromName(
'HookConsumerWidget',
packageName: 'hooks_riverpod',
),
]);

final AstNode diagnosticNode;
if (isExactly) {
Expand Down
2 changes: 1 addition & 1 deletion packages/leancode_lint/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: leancode_lint
version: 10.0.0
version: 11.0.0
homepage: https://github.com/leancodepl/flutter_corelibrary/tree/master/packages/leancode_lint
repository: https://github.com/leancodepl/flutter_corelibrary
description: Robust, high-quality lint rules used at LeanCode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

void someOtherFunction() {
final b = Random().nextBool() ? useState(true) : useState(false);
Expand Down Expand Up @@ -83,6 +84,18 @@ class SampleConditionalExpressionHookWidget2 extends HookWidget {
: Container();
}

class HookConsumerWidgetIsSeenAsAHookWidget extends HookConsumerWidget {
const HookConsumerWidgetIsSeenAsAHookWidget({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) => Random().nextBool()
? TextField(
controller: // expect_lint: avoid_conditional_hooks
useTextEditingController(),
)
: Container();
}

class SampleNotConditionalExpressionHookWidget extends HookWidget {
const SampleNotConditionalExpressionHookWidget({super.key});

Expand Down Expand Up @@ -212,6 +225,17 @@ final a = HookBuilder(
},
);

final b = HookConsumer(
builder: (context, ref, child) {
if (Random().nextBool()) {
// expect_lint: avoid_conditional_hooks
useState(1);
}
useState(2);
return const SizedBox();
},
);

class HookBuilderInIfIsOk extends StatelessWidget {
const HookBuilderInIfIsOk({super.key});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

class SampleHookWidgetNotUsingHooks extends
// expect_lint: hook_widget_does_not_use_hooks
Expand All @@ -12,6 +13,17 @@ class SampleHookWidgetNotUsingHooks extends
}
}

class HookConsumerWidgetIsSeenAsAHookWidget extends
// expect_lint: hook_widget_does_not_use_hooks
HookConsumerWidget {
const HookConsumerWidgetIsSeenAsAHookWidget({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
return Container();
}
}

class SampleHookWidgetUsingHooks extends HookWidget {
const SampleHookWidgetUsingHooks({super.key});

Expand Down Expand Up @@ -96,3 +108,11 @@ final b = HookBuilder(
return const SizedBox();
},
);

final c =
// expect_lint: hook_widget_does_not_use_hooks
HookConsumer(
builder: (context, ref, child) {
return const SizedBox();
},
);
32 changes: 32 additions & 0 deletions packages/leancode_lint/test/lints_test_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.20.5"
flutter_riverpod:
dependency: transitive
description:
name: flutter_riverpod
sha256: "4bce556b7ecbfea26109638d5237684538d4abc509d253e6c5c4c5733b360098"
url: "https://pub.dev"
source: hosted
version: "2.4.10"
freezed_annotation:
dependency: transitive
description:
Expand All @@ -198,6 +206,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.2"
hooks_riverpod:
dependency: "direct main"
description:
name: hooks_riverpod
sha256: "758b07eba336e3cbacbd81dba481f2228a14102083fdde07045e8514e8054c49"
url: "https://pub.dev"
source: hosted
version: "2.4.10"
hotreloader:
dependency: transitive
description:
Expand Down Expand Up @@ -301,6 +317,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.3"
riverpod:
dependency: transitive
description:
name: riverpod
sha256: "548e2192eb7aeb826eb89387f814edb76594f3363e2c0bb99dd733d795ba3589"
url: "https://pub.dev"
source: hosted
version: "2.5.0"
rxdart:
dependency: transitive
description:
Expand Down Expand Up @@ -346,6 +370,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.11.1"
state_notifier:
dependency: transitive
description:
name: state_notifier
sha256: b8677376aa54f2d7c58280d5a007f9e8774f1968d1fb1c096adcb4792fba29bb
url: "https://pub.dev"
source: hosted
version: "1.0.0"
stream_channel:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions packages/leancode_lint/test/lints_test_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
sdk: flutter
flutter_bloc: ^8.0.1
flutter_hooks: ^0.20.2
hooks_riverpod: ^2.4.10
sliver_tools: ^0.2.12

dev_dependencies:
Expand Down

0 comments on commit c1d8d4b

Please sign in to comment.