-
-
Notifications
You must be signed in to change notification settings - Fork 956
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(riverpod_lint) Add new lint avoid_passing_build_context_to_providers (…
- Loading branch information
1 parent
dfe84e0
commit 7642120
Showing
5 changed files
with
462 additions
and
0 deletions.
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_build_context_in_providers.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/error.dart'; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
import '../riverpod_custom_lint.dart'; | ||
|
||
const TypeChecker buildContextType = TypeChecker.fromName( | ||
'BuildContext', | ||
packageName: 'flutter', | ||
); | ||
|
||
class AvoidBuildContextInProviders extends RiverpodLintRule { | ||
const AvoidBuildContextInProviders() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'avoid_build_context_in_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!; | ||
_emitWarningsForBuildContext(reporter, parameters); | ||
}); | ||
|
||
riverpodRegistry(context).addClassBasedProviderDeclaration((declaration) { | ||
final methods = declaration.node.members.whereType<MethodDeclaration>(); | ||
|
||
for (final method in methods) { | ||
final parameters = method.parameters!; | ||
_emitWarningsForBuildContext(reporter, parameters); | ||
} | ||
}); | ||
} | ||
|
||
void _emitWarningsForBuildContext( | ||
ErrorReporter reporter, | ||
FormalParameterList 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); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/riverpod_lint_flutter_test/test/lints/avoid_build_context_in_providers.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,32 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
part 'avoid_build_context_in_providers.g.dart'; | ||
|
||
@riverpod | ||
int fn( | ||
FnRef ref, | ||
// expect_lint: avoid_build_context_in_providers | ||
BuildContext context1, { | ||
// expect_lint: avoid_build_context_in_providers | ||
required BuildContext context2, | ||
}) => | ||
0; | ||
|
||
@riverpod | ||
class MyNotifier extends _$MyNotifier { | ||
int build( | ||
// expect_lint: avoid_build_context_in_providers | ||
BuildContext context1, { | ||
// expect_lint: avoid_build_context_in_providers | ||
required BuildContext context2, | ||
}) => | ||
0; | ||
|
||
void event( | ||
// expect_lint: avoid_build_context_in_providers | ||
BuildContext context3, { | ||
// expect_lint: avoid_build_context_in_providers | ||
required BuildContext context4, | ||
}) {} | ||
} |
Oops, something went wrong.