Skip to content

Add avoid_build_context_in_blocs lint - #545

Open
cupofme wants to merge 6 commits into
masterfrom
feature/avoid_build_context_in_blocs-lint
Open

Add avoid_build_context_in_blocs lint#545
cupofme wants to merge 6 commits into
masterfrom
feature/avoid_build_context_in_blocs-lint

Conversation

@cupofme

@cupofme cupofme commented Jul 14, 2026

Copy link
Copy Markdown

Adds a warning-level lint that flags BuildContext crossing into a Bloc/Cubit — either passed in (via a method like add, or a constructor) or declared inside one (parameter or field).

@cupofme
cupofme requested a review from mchudy as a code owner July 14, 2026 15:15
@github-actions github-actions Bot added the p: leancode_lint Related to the leancode_lint package label Jul 14, 2026
@cupofme
cupofme requested review from Copilot and removed request for mchudy July 14, 2026 15:21
@cupofme
cupofme requested a review from PiotrRogulski July 14, 2026 15:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_blocs analysis rule (detect passing BuildContext into 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 bloc library 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.

Comment thread packages/leancode_lint/lib/src/lints/avoid_build_context_in_blocs.dart Outdated
Comment on lines +30 to +31
correctionMessage:
"Business logic shouldn't depend on the widget tree. Pass only the data the Bloc/Cubit needs.",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated to Remove the 'BuildContext' and directly pass only the data the Bloc/Cubit needs instead.

Comment on lines +28 to +29
// The specific clause is supplied per report site via `arguments`.
'{0}',

@PiotrRogulski PiotrRogulski Jul 14, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Simplified to just Avoid using 'BuildContext' in a Bloc/Cubit.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 here
  • Avoid using – "using" is very vague; let's say what kind of usage we detected

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Made it more context-aware

@cupofme
cupofme requested a review from PiotrRogulski July 15, 2026 08:57
@cupofme cupofme added the enhancement New feature or request label Jul 16, 2026
Comment thread packages/leancode_lint/lib/src/lints/avoid_build_context_in_blocs.dart Outdated
Comment thread packages/leancode_lint/lib/src/lints/avoid_build_context_in_blocs.dart Outdated
Comment thread packages/leancode_lint/lib/src/lints/avoid_build_context_in_blocs.dart Outdated
@cupofme
cupofme requested a review from PiotrRogulski July 27, 2026 09:03

@PiotrRogulski PiotrRogulski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One last comment, other than that LGTM <3

Comment on lines +218 to +221
String _blocTypeName(BlocType type) => switch (type) {
.bloc => 'Bloc',
.cubit => 'Cubit',
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use .name directly as an arg (no need to capitalize)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request p: leancode_lint Related to the leancode_lint package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants