Add avoid_build_context_in_blocs lint - #545
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new avoid_build_context_in_blocs warning lint to leancode_lint to discourage passing BuildContext into Bloc/Cubit APIs or declaring it inside Bloc/Cubit types, with accompanying docs and test coverage.
Changes:
- Implement
avoid_build_context_in_blocsanalysis rule (detect passingBuildContextinto Bloc/Cubit calls/constructors and declaring it as parameters/fields). - Register the new lint in the analyzer plugin and add documentation + changelog entry.
- Add targeted test cases and extend the mock
bloclibrary used by tests.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/leancode_lint/lib/src/lints/avoid_build_context_in_blocs.dart | Implements the new lint’s AST visitor and reporting behavior. |
| packages/leancode_lint/lib/plugin.dart | Registers the new lint as a warning rule. |
| packages/leancode_lint/test/test_cases/avoid_build_context_in_blocs_test.dart | Adds positive/negative test cases for the new lint. |
| packages/leancode_lint/test/mock_libraries/bloc.dart | Adds a mock Bloc.add method needed by new tests. |
| packages/leancode_lint/README.md | Documents the new lint, rationale, and examples. |
| packages/leancode_lint/CHANGELOG.md | Adds an Unreleased entry for the new lint and fixes a markdown link formatting issue. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| correctionMessage: | ||
| "Business logic shouldn't depend on the widget tree. Pass only the data the Bloc/Cubit needs.", |
There was a problem hiding this comment.
The correction message should be exactly that: What to do to make the code compliant with the lint. This current string is more of a description (at least the first part)
There was a problem hiding this comment.
Updated to Remove the 'BuildContext' and directly pass only the data the Bloc/Cubit needs instead.
| // The specific clause is supplied per report site via `arguments`. | ||
| '{0}', |
There was a problem hiding this comment.
Let's not supply the entire message as the arg. Make it a template here and substitute as little as possible. Also, reuse it in the super constructor
There was a problem hiding this comment.
Simplified to just Avoid using 'BuildContext' in a Bloc/Cubit.
There was a problem hiding this comment.
Hmm, I don't like that too much 🫥 Let's customize the message at least a little bit, to make it more relevant to the reported location:
Bloc/Cubit– we can use the detected type hereAvoid using– "using" is very vague; let's say what kind of usage we detected
PiotrRogulski
left a comment
There was a problem hiding this comment.
One last comment, other than that LGTM <3
| String _blocTypeName(BlocType type) => switch (type) { | ||
| .bloc => 'Bloc', | ||
| .cubit => 'Cubit', | ||
| }; |
There was a problem hiding this comment.
Use .name directly as an arg (no need to capitalize)
Adds a warning-level lint that flags
BuildContextcrossing into a Bloc/Cubit — either passed in (via a method likeadd, or a constructor) or declared inside one (parameter or field).