feat(bloc_lint): add avoid_mutable_events - #4853
Open
TiwariAshuism wants to merge 2 commits into
Open
Conversation
Warn when a bloc event exposes mutable state via a non-final instance field or a setter. Events are queued and processed asynchronously, and an EventTransformer can delay them further, so mutating an event after it has been added makes the data seen by the handler depend on timing. Mutable fields also break the value equality blocs and tests rely on when comparing events. A class is treated as an event when its own name or the name of its superclass ends in `Event`, matching how the existing rules identify blocs and cubits. Static and `const` fields, `late final` fields and getters are left alone. Closes felangel#4452
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Status
READY
Breaking Changes
NO
Description
Adds the
avoid_mutable_eventslint rule, which warns when a bloc event exposes mutable state through a non-final instance field or a setter.Rationale
Events are not handled the moment they are added — they are queued and processed asynchronously, and an
EventTransformersuch asdebounceorthrottlecan delay them further. Mutating an event after it has been added means the handler observes different data than the caller intended, and the behavior depends on timing. Mutable fields also break the value equality blocs and tests rely on when comparing events.Implementation notes
Event(the sealed base) or its superclass name ends inEvent(the subclasses), matching how the existing rules identify blocs and cubits viaendsWith('Bloc')/endsWith('Cubit').varand barelate) and setters. Static fields,constfields,late finalfields and getters are left alone.endClassDeclaration, so a plain class or top-level variable following an event class is not affected — covered by tests.Scoped to
extendsand the class name, deliberately matching the other rules.implementsis not considered; happy to add it if you'd prefer.The sibling rule
avoid_mutable_states(#4451) would be close to a copy of this with a different suffix — I left it out since that issue is assigned.Verified locally against the CI gates:
dart format,dart analyze --fatal-warnings lib test, full suite (165 tests, 14 new) and 100% coverage.Closes #4452
Type of Change