feat(bloc_lint): add avoid_duplicate_event_handlers - #4851
Open
TiwariAshuism wants to merge 1 commit into
Open
Conversation
Warn when a bloc registers more than one event handler for the same event type. `on<E>` supports a single handler per event type and throws a `StateError` when called twice for the same `E`, but because the check runs inside an assert it only surfaces in debug mode. The rule hooks `endTypeArguments` so the parser resolves the balanced type argument group, which keeps nested generics such as `on<MyEvent<String>>` working without hand-rolled `>>` handling. Closes felangel#4460
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_duplicate_event_handlerslint rule, which warns when a bloc registers more than one event handler for the same event type.A
Blocsupports a single handler per event type —on<E>throws aStateErrorwhen called twice for the sameE. Because that check lives inside anassert, it only surfaces in debug mode, so a duplicate registration can otherwise reach production unnoticed.Implementation notes
endTypeArgumentsso the parser resolves the balanced type argument group. Nested generics such ason<MyEvent<String>>work without hand-rolled>>splitting, andMyEvent<String>/MyEvent<int>are correctly treated as distinct event types.handleClassExtendsto classes whose superclass ends inBloc, which coversBloc,HydratedBlocandReplayBlocand avoids class-level type parameters confusing the extends lookup.registry.on<T>(...)) andonclauses intry/catch.Known limitation: handlers registered in mutually exclusive branches (
if (x) on<E>(a); else on<E>(b);) are flagged.// ignore: avoid_duplicate_event_handlerscovers that case.Two decisions I would like your call on
warning, matching the otheravoid_*rules.errormay be more appropriate given this is a guaranteed runtime failure rather than a style preference — happy to change it.recommended.yaml, following the precedent in feat(bloc_lint): addavoid_bloc_to_bloc_members#4665. It may be a reasonable recommended candidate since it catches code that is certain to throw.Verified locally against the CI gates:
dart format,dart analyze --fatal-warnings lib test, full suite (165 tests, 14 new) and 100% coverage.Closes #4460
Type of Change