Skip to content
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

(riverpod_lint) Add new lint avoid_passing_build_context_to_providers #2928

Merged
merged 6 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/riverpod_lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Riverpod_lint adds various warnings with quick fixes and refactoring options, su
- [provider\_dependencies (riverpod\_generator only)](#provider_dependencies-riverpod_generator-only)
- [scoped\_providers\_should\_specify\_dependencies (generator only)](#scoped_providers_should_specify_dependencies-generator-only)
- [avoid\_manual\_providers\_as\_generated\_provider\_dependency](#avoid_manual_providers_as_generated_provider_dependency)
- [avoid\_passing\_build\_context\_to\_providers (riverpod\_generator only)](#avoid_passing_build_context_to_providers-riverpod_generator-only)
- [provider\_parameters](#provider_parameters)
- [avoid\_public\_notifier\_properties](#avoid_public_notifier_properties)
- [unsupported\_provider\_value (riverpod\_generator only)](#unsupported_provider_value-riverpod_generator-only)
Expand Down Expand Up @@ -346,6 +347,39 @@ int example(ExampleRef ref) {
}
```

### avoid_passing_build_context_to_providers (riverpod_generator only)

Providers should not interact with `BuildContext`.

**Good**:

```dart
@riverpod
int fn(FnRef ref) => 0;

@riverpod
class MyNotifier extends _$MyNotifier {
int build() => 0;

void event() {}
}
```

**Bad**:

```dart
// Providers should not receive a BuildContext as a parameter.
int fn(FnRef ref, BuildContext context) => 0;

@riverpod
class MyNotifier extends _$MyNotifier {
int build() => 0;

// Notifiers should not have methods that receive a BuildContext as a parameter.
void event(BuildContext context) {}
}
```

### provider_parameters

Providers' parameters should have a consistent ==. Meaning either the values should be cached, or the parameters should override ==.
Expand Down
2 changes: 2 additions & 0 deletions packages/riverpod_lint/lib/riverpod_lint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'src/assists/functional_to_class_based_provider.dart';
import 'src/assists/wrap_with_consumer.dart';
import 'src/assists/wrap_with_provider_scope.dart';
import 'src/lints/avoid_manual_providers_as_generated_provider_dependency.dart';
import 'src/lints/avoid_passing_build_context_to_providers.dart';
import 'src/lints/avoid_public_notifier_properties.dart';
import 'src/lints/avoid_ref_inside_state_dispose.dart';
import 'src/lints/functional_ref.dart';
Expand All @@ -24,6 +25,7 @@ PluginBase createPlugin() => _RiverpodPlugin();
class _RiverpodPlugin extends PluginBase {
@override
List<LintRule> getLintRules(CustomLintConfigs configs) => [
const AvoidPassingBuildContextToProviders(),
const AvoidPublicNotifierProperties(),
const FunctionalRef(),
const MissingProviderScope(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';

import '../riverpod_custom_lint.dart';

TypeChecker buildContextType =
const TypeChecker.fromName('BuildContext', packageName: 'flutter');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TypeChecker buildContextType =
const TypeChecker.fromName('BuildContext', packageName: 'flutter');
const buildContextType = TypeChecker.fromName(
'BuildContext',
packageName: 'flutter',
);


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

static const _code = LintCode(
name: 'avoid_passing_build_context_to_providers',
problemMessage:
'Passing BuildContext to providers indicates mixing UI with the business logic.',
errorSeverity: ErrorSeverity.WARNING,
);

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
riverpodRegistry(context).addFunctionalProviderDeclaration((declaration) {
final parameters = declaration.node.functionExpression.parameters!;

final buildContextParameters = parameters.parameters.where(
(e) =>
e.declaredElement?.type != null &&
buildContextType.isExactlyType(e.declaredElement!.type),
);

for (final contextParameter in buildContextParameters) {
reporter.reportErrorForNode(_code, contextParameter);
}
});

riverpodRegistry(context).addClassBasedProviderDeclaration((declaration) {
final methods = declaration.node.members.whereType<MethodDeclaration>();

for (final method in methods) {
final parameters = method.parameters!;

final buildContextParameters = parameters.parameters.where(
(e) =>
e.declaredElement?.type != null &&
buildContextType.isExactlyType(e.declaredElement!.type),
);

for (final contextParameter in buildContextParameters) {
reporter.reportErrorForNode(_code, contextParameter);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be refactored with the functional-based bit to avoid the duplicate?
A function which takes the list of parameter and emit warnings should be fine.

}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:flutter/widgets.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'avoid_passing_build_context_to_providers.g.dart';

@riverpod
int fn(
FnRef ref,
// expect_lint: avoid_passing_build_context_to_providers
BuildContext context1, {
// expect_lint: avoid_passing_build_context_to_providers
required BuildContext context2,
}) =>
0;

@riverpod
class MyNotifier extends _$MyNotifier {
int build(
// expect_lint: avoid_passing_build_context_to_providers
BuildContext context1, {
// expect_lint: avoid_passing_build_context_to_providers
required BuildContext context2,
}) =>
0;

void event(
// expect_lint: avoid_passing_build_context_to_providers
BuildContext context3, {
// expect_lint: avoid_passing_build_context_to_providers
required BuildContext context4,
}) {}
}
Loading