Skip to content

feat(bloc_lint): add prefer_multi_bloc_provider - #4912

Open
shubhransh-gupta wants to merge 1 commit into
felangel:masterfrom
shubhransh-gupta:feat/prefer-multi-bloc-provider
Open

shubhransh-gupta wants to merge 1 commit into
felangel:masterfrom
shubhransh-gupta:feat/prefer-multi-bloc-provider

Conversation

@shubhransh-gupta

Copy link
Copy Markdown

Status

READY

Breaking Changes

NO

Description

Adds the prefer_multi_bloc_provider lint rule requested in #4453. It warns when BlocProvider widgets are nested directly through the child argument, recommending a single MultiBlocProvider widget instead for improved readability and reduced widget nesting.

// BAD
BlocProvider<BlocA>(
  create: (context) => BlocA(),
  child: BlocProvider<BlocB>(
    create: (context) => BlocB(),
    child: const ChildA(),
  ),
);

// GOOD
MultiBlocProvider(
  providers: [
    BlocProvider<BlocA>(create: (context) => BlocA()),
    BlocProvider<BlocB>(create: (context) => BlocB()),
  ],
  child: const ChildA(),
);

Implementation Details

  • Identifies BlocProvider(...) and BlocProvider<T>(...), including named constructors like BlocProvider.value(...) and BlocProvider<T>.value(...).
  • Resolves type arguments and constructor names to detect whether the receiver is BlocProvider.
  • Detects direct child: argument nesting while ignoring intervening widgets (e.g., Padding, Builder), callback closures, or other providers.
  • Only reports the outermost BlocProvider in a contiguous chain of nested providers.
  • Severity is info, consistent with other prefer_* stylistic lint rules.
  • Registered in allRules, exported in bloc_lint.dart and rules.dart, added to all.yaml, listed in packages/bloc_lint/README.md, and documented with examples under docs/src/content/docs/lint-rules/prefer_multi_bloc_provider.mdx.

Verification

  • 10 lintTest test cases covering: direct nesting, chain of 3+ nested providers (only outermost reported), .value constructor, const constructor in child, single BlocProvider (quiet), MultiBlocProvider (quiet), indirect non-child nesting (quiet), different widget nested (quiet), callback closure (quiet), and // ignore: prefer_multi_bloc_provider.
  • dart test: all 160 tests pass in packages/bloc_lint.
  • Coverage: 100% line coverage (41/41 lines) in prefer_multi_bloc_provider.dart.
  • dart analyze --fatal-infos --fatal-warnings .: 0 issues found.
  • dart format --set-exit-if-changed .: 0 files changed.
  • Documentation site built and formatted cleanly (npm run format:check and npm run build pass with 0 errors).

Closes #4453

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

Adds the `prefer_multi_bloc_provider` lint rule which warns when
`BlocProvider` widgets are nested directly through the `child` argument,
recommending a single `MultiBlocProvider` instead for improved readability.

Closes felangel#4453

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(bloc_lint): prefer_multi_bloc_provider

1 participant