-
Notifications
You must be signed in to change notification settings - Fork 24
Issue 190. Implement use nearest context rule #198
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
Open
Aygistov
wants to merge
6
commits into
solid-software:master
Choose a base branch
from
Aygistov:Issue-190-implement-use_nearest_context-rule
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4f4ff18
Added use_nearest_context rule
Aygistov 69f4c3f
Added use_nearest_context function test
Aygistov ec4651d
Update CHANGELOG.md
Aygistov c6a5969
Delete exclude parameter
Aygistov 9a7bd7c
Merge branch 'master' into Issue-190-implement-use_nearest_context-rule
15d5c8b
rm params class
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 hidden or 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 hidden or 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
65 changes: 65 additions & 0 deletions
65
lib/src/lints/use_nearest_context/fixes/use_nearest_context_fix.dart
This file contains hidden or 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,65 @@ | ||
| part of '../use_nearest_context_rule.dart'; | ||
|
|
||
| /// A Quick fix for `use_nearest_context` rule | ||
| /// Suggests to renaming the nearest BuildContext variable | ||
| /// to the one that is being used | ||
| class _UseNearestContextFix extends DartFix { | ||
| static const _replaceComment = "Rename the BuildContext variable"; | ||
|
|
||
| @override | ||
| void run( | ||
| CustomLintResolver resolver, | ||
| ChangeReporter reporter, | ||
| CustomLintContext context, | ||
| AnalysisError analysisError, | ||
| List<AnalysisError> others, | ||
| ) { | ||
| context.registry.addFunctionDeclaration((node) { | ||
| final statementInfo = analysisError.data as StatementInfo?; | ||
| if (statementInfo == null) return; | ||
| final parameterName = statementInfo.parameter.name; | ||
| if (parameterName == null) return; | ||
| if (node.sourceRange.intersects(parameterName.sourceRange)) { | ||
| _addReplacement( | ||
| reporter, | ||
| parameterName, | ||
| statementInfo.name, | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| void _addReplacement( | ||
| ChangeReporter reporter, | ||
| Token? token, | ||
| String correction, | ||
| ) { | ||
| if (token == null) return; | ||
| final changeBuilder = reporter.createChangeBuilder( | ||
| message: _replaceComment, | ||
| priority: 1, | ||
| ); | ||
|
|
||
| changeBuilder.addDartFileEdit((builder) { | ||
| builder.addSimpleReplacement( | ||
| token.sourceRange, | ||
| correction, | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /// Data class contains info required for fix | ||
| class StatementInfo { | ||
| /// Creates instance of an [StatementInfo] | ||
| const StatementInfo({ | ||
| required this.name, | ||
| required this.parameter, | ||
| }); | ||
|
|
||
| /// Variable name | ||
| final String name; | ||
|
|
||
| /// BuildContext parament | ||
| final SimpleFormalParameter parameter; | ||
| } | ||
124 changes: 124 additions & 0 deletions
124
lib/src/lints/use_nearest_context/use_nearest_context_rule.dart
This file contains hidden or 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,124 @@ | ||
| // ignore_for_file: avoid_print, lines_longer_than_80_chars | ||
|
|
||
| import 'package:analyzer/dart/ast/ast.dart'; | ||
| import 'package:analyzer/dart/ast/token.dart'; | ||
| import 'package:analyzer/error/error.dart'; | ||
| import 'package:analyzer/error/listener.dart'; | ||
| import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
| import 'package:solid_lints/src/models/rule_config.dart'; | ||
| import 'package:solid_lints/src/models/solid_lint_rule.dart'; | ||
| import 'package:solid_lints/src/utils/types_utils.dart'; | ||
|
|
||
| part 'fixes/use_nearest_context_fix.dart'; | ||
|
|
||
| /// A rule which checks that we use BuildContext from the nearest available | ||
| /// scope. | ||
| /// | ||
| /// ### Example: | ||
| /// #### BAD: | ||
| /// ```dart | ||
| /// class SomeWidget extends StatefulWidget { | ||
| /// ... | ||
| /// } | ||
| /// | ||
| /// class _SomeWidgetState extends State<SomeWidget> { | ||
| /// ... | ||
| /// void _showDialog() { | ||
| /// showModalBottomSheet( | ||
| /// context: context, | ||
| /// builder: (BuildContext _) { | ||
| /// final someProvider = context.watch<SomeProvider>(); // LINT, BuildContext is used not from the nearest available scope | ||
| /// | ||
| /// return const SizedBox.shrink(); | ||
| /// }, | ||
| /// ); | ||
| /// } | ||
| /// } | ||
| /// ``` | ||
| /// #### GOOD: | ||
| /// ```dart | ||
| /// class SomeWidget extends StatefulWidget { | ||
| /// ... | ||
| /// } | ||
| /// | ||
| /// class _SomeWidgetState extends State<SomeWidget> { | ||
| /// ... | ||
| /// void _showDialog() { | ||
| /// showModalBottomSheet( | ||
| /// context: context, | ||
| /// builder: (BuildContext context) | ||
| /// final someProvider = context.watch<SomeProvider>(); // OK | ||
| /// | ||
| /// return const SizedBox.shrink(); | ||
| /// }, | ||
| /// ); | ||
| /// } | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| class UseNearestContextRule extends SolidLintRule { | ||
| /// This lint rule represents the error if BuildContext is used not from the | ||
| /// nearest available scope | ||
| static const lintName = 'use_nearest_context'; | ||
|
|
||
| UseNearestContextRule._(super.rule); | ||
|
|
||
| /// Creates a new instance of [UseNearestContextRule] | ||
| /// based on the lint configuration. | ||
| factory UseNearestContextRule.createRule(CustomLintConfigs configs) { | ||
| final rule = RuleConfig( | ||
| configs: configs, | ||
| name: lintName, | ||
| problemMessage: (value) => | ||
| 'BuildContext is used not from the nearest available scope. ' | ||
| 'Consider renaming the nearest BuildContext parameter.', | ||
| ); | ||
|
|
||
| return UseNearestContextRule._(rule); | ||
| } | ||
|
|
||
| @override | ||
| void run( | ||
| CustomLintResolver resolver, | ||
| ErrorReporter reporter, | ||
| CustomLintContext context, | ||
| ) { | ||
| context.registry.addSimpleIdentifier((node) { | ||
| if (!isBuildContext(node.staticType)) return; | ||
|
|
||
| final closestBuildContext = _findClosestBuildContext(node); | ||
| if (closestBuildContext == null) return; | ||
| if (closestBuildContext.name?.lexeme != node.name) { | ||
| reporter.atNode( | ||
| node, | ||
| code, | ||
| data: StatementInfo( | ||
| name: node.name, | ||
| parameter: closestBuildContext, | ||
| ), | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| SimpleFormalParameter? _findClosestBuildContext(SimpleIdentifier node) { | ||
| AstNode? current = node.parent; | ||
|
|
||
| while (current != null) { | ||
| if (current is FunctionExpression) { | ||
| final functionParams = current.parameters?.parameters ?? []; | ||
| for (final param in functionParams) { | ||
| if (param is SimpleFormalParameter && | ||
| isBuildContext(param.declaredElement?.type)) { | ||
| return param; | ||
| } | ||
| } | ||
| } | ||
| current = current.parent; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @override | ||
| List<Fix> getFixes() => [_UseNearestContextFix()]; | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -81,3 +81,4 @@ custom_lint: | |
| - prefer_match_file_name | ||
| - proper_super_calls | ||
| - avoid_final_with_getter | ||
| - use_nearest_context | ||
This file contains hidden or 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,60 @@ | ||
| // ignore_for_file: avoid_unused_parameters, unused_local_variable | ||
| import 'package:flutter/material.dart'; | ||
|
|
||
| /// Check the `use_nearest_context` rule | ||
| void showDialog(BuildContext context) { | ||
| final outerContext = context; | ||
|
|
||
| showModalBottomSheet( | ||
| context: context, | ||
| builder: (BuildContext _) { | ||
| /// expect_lint: use_nearest_context | ||
| return SizedBox.fromSize(size: outerContext.size); | ||
| }, | ||
| ); | ||
|
|
||
| showModalBottomSheet( | ||
| context: context, | ||
| builder: (BuildContext _) { | ||
| /// expect_lint: use_nearest_context | ||
| return SizedBox.fromSize(size: context.size); | ||
| }, | ||
| ); | ||
|
|
||
| final fun = ({required BuildContext context}) { | ||
| /// expect_lint: use_nearest_context | ||
| outerContext.mounted; | ||
| }; | ||
|
|
||
| showModalBottomSheet( | ||
| context: context, | ||
| builder: (_) { | ||
| /// expect_lint: use_nearest_context | ||
| return SizedBox.fromSize(size: context.size); | ||
| }, | ||
| ); | ||
|
|
||
| showModalBottomSheet( | ||
| context: context, | ||
| builder: (BuildContext innerContext) { | ||
| /// expect_lint: use_nearest_context | ||
| return SizedBox.fromSize(size: context.size); | ||
| }, | ||
| ); | ||
|
|
||
| showModalBottomSheet( | ||
| context: context, | ||
| builder: (BuildContext innerContext) { | ||
| ///Allowed | ||
| return SizedBox.fromSize(size: innerContext.size); | ||
| }); | ||
|
|
||
| showModalBottomSheet( | ||
| ///Allowed | ||
| context: context, | ||
| builder: (BuildContext context) { | ||
| ///Allowed | ||
| return SizedBox.fromSize(size: context.size); | ||
| }, | ||
| ); | ||
| } |
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.
The documentation for the
StatementInfoclass could be more descriptive to enhance clarity for future maintainers.Specifically:
namefield, "Variable name" is a bit vague. It represents the identifier of theBuildContextthat was used (from an outer scope), and it's the target name for the renaming operation.parameterfield, "BuildContext parament" contains a typo and could better describe that this is the AST node of the nearestBuildContextparameter that will be renamed.Could we update these comments for better precision?