feat: add use_nearest_context rule#293
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the use_nearest_context lint rule along with two quick fixes (RenameNearestContextParameterFix and ReplaceWithNearestContextParameterFix) to ensure BuildContext is accessed from the nearest available scope. It also updates isBuildContext to support nullable types and includes comprehensive unit tests. The review feedback highlights two important improvements: first, ReplaceWithNearestContextParameterFix should be extended to handle direct usage of context (not just property accesses on this); second, the visitor can be simplified by using element.nameOffset directly to avoid an unnecessary null check.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces the use_nearest_context lint rule, which encourages using the BuildContext from the nearest available scope. It includes the rule implementation, a visitor, utility functions, unit tests, and two quick fixes: RenameNearestContextParameterFix and ReplaceWithNearestContextParameterFix. The feedback highlights an issue in the ReplaceWithNearestContextParameterFix where plain SimpleIdentifiers (like plain context usage) are not replaced, and suggests adding a fallback. Additionally, it recommends simplifying the scope check in UseNearestContextVisitor by using element.nameOffset directly to avoid null checks.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| class BuildContext { | ||
| Object? get size => null; | ||
| } | ||
| class Widget {} | ||
| void showModalBottomSheet({required BuildContext context, required Widget Function(BuildContext?) builder}) {} |
There was a problem hiding this comment.
we can extract that from all tests
@override
void setUp() {
final flutter = newPackage('flutter');
flutter.addFile('lib/material.dart', r'''
class BuildContext {
Object? get size => null;
}
class Widget {}
void showModalBottomSheet({required BuildContext context, required Widget Function(BuildContext?) builder}) {}
''');And there are few other things that can be extracted there too scattered across other tests, like Future.microtask, etc
and then in each test we only need one line:
import 'package:flutter/material.dart';| ); | ||
| } | ||
| ''', | ||
| [lint(345, 12)], |
There was a problem hiding this comment.
let's use this instead
e.g.
| if (closestBuildContext.name?.lexeme != node.name) { | ||
| if (_isDeclaredInNearestScope(node, closestBuildContext)) return; | ||
|
|
||
| _rule.reportAtNode(node); | ||
| } |
There was a problem hiding this comment.
| if (closestBuildContext.name?.lexeme != node.name) { | |
| if (_isDeclaredInNearestScope(node, closestBuildContext)) return; | |
| _rule.reportAtNode(node); | |
| } | |
| if (closestBuildContext.name?.lexeme == node.name) return; | |
| if (_isDeclaredInNearestScope(node, closestBuildContext)) return; | |
| _rule.reportAtNode(node); |
…o issue-190-implement-use_nearest_context
solid-danylosafonov
left a comment
There was a problem hiding this comment.
LGTM, two optional suggestions
| /// Returns `true` if [node] is a property accessed on another object | ||
| /// (e.g. `state.context`), but not on `this` (e.g. `this.context`). | ||
| bool _isPropertyOfOtherObject(SimpleIdentifier node) { | ||
| final parent = node.parent; | ||
| if (parent is PrefixedIdentifier && node == parent.identifier) { | ||
| return true; | ||
| } | ||
| if (parent is PropertyAccess && node == parent.propertyName) { | ||
| var target = parent.target; | ||
| while (target is ParenthesizedExpression) { | ||
| target = target.expression; | ||
| } | ||
| return target is! ThisExpression && target is! SuperExpression; | ||
| } | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Nit - maybe we should extract it to utils/node_utils.dart? - seems rather generic with potential for future reuse
| /// Returns `true` if [node] refers to a variable declared inside the body | ||
| /// of the function that owns [closestParam] (i.e. a local variable | ||
| /// in the same scope, like `final localCtx = innerContext;`). | ||
| bool _isDeclaredInNearestScope( |
There was a problem hiding this comment.
Nit:
- We probably can extract it to utils as well
- But, I'm not sure if the name reflects what this function does in the best way - maybe it should be something like
isDeclaredInSameFunction? - that naming also leads me to think that it would look better as an extension for node so the call would look likenode.isDeclaredInSameFunction(as: param)? WDYT?
There was a problem hiding this comment.
I think that's a great idea! I've updated it accordingly
…impleIdentifier extension
Closes #190