dartd 0.3.0
dartd: ^0.3.0 copied to clipboard
CLI to analyze and remove unused Dart classes or modules.
dartd #
CLI to analyze and remove unused Riverpod providers and modules in a Dart / Flutter project.
This tool is designed around the Riverpod codegen style:
@riverpod/@Riverpod-annotated functions in user code- Generated
*Providersymbols in.g.dartfiles
and performs safe, heuristic-based cleanup of unused modules and unused Dart files.
Features #
analyze- Reports unused Riverpod module groups (e.g.
@riverpod Foo+FooProvider). - Lists Dart files under
--rootthat can be safely deleted because they contain no used modules or top-level declarations.
- Reports unused Riverpod module groups (e.g.
fix- Removes unused Riverpod definitions in user code only (non-generated files).
- Deletes Dart files that contain:
- no module definitions, and
- no used top-level declarations (classes, enums, typedefs, extensions, etc.).
- Riverpod-aware grouping:
- Treats
@riverpod Foo()andFooProvider(and related generated providers) as a single module group. - If any symbol in the group is referenced from user code, the whole group is kept.
- Treats
- Generated file safety:
- Never modifies generated files (
*.g.dart,*.freezed.dart,*.gen.dart,*.gr.dart). - Generated files are used only as a source of usage information, not as edit targets.
- Never modifies generated files (
- Usage tracking:
- Understands usage through:
- direct identifier references (e.g.
fooProvider,RemoteConfigParameter), - type names (e.g.
extends LocationBasedUseCase), - extension method calls (e.g.
children.withSpaceBetween()), - typedef usage (e.g.
FutureCallback<T>), - enum usage (e.g.
RemoteConfigParameter.values).
- direct identifier references (e.g.
- Understands usage through:
Install #
dart pub global activate dartd
Usage #
# Analyze under lib/ by default
dartd analyze
# Analyze a specific root directory
dartd analyze --root lib
# Apply fixes under lib/ (remove unused modules and deletable files)
dartd fix
# Apply fixes to another directory
dartd fix --root packages/my_feature/lib
Options:
-r, --rootRoot directory to analyze (default:lib).
Notes and limitations #
- Heuristic detection:
Code referenced only via reflection, string-based lookups, or
dynamiccalls may be treated as unused. - Generated files are never modified:
Files like
*.g.dart,*.freezed.dart,*.gen.dart,*.gr.dartare not rewritten or deleted. - Riverpod-specific:
Module detection is centered around Riverpod’s
@riverpod/@Riverpodannotations and*Providernaming. Other DI / state management patterns are not specially handled. - Review changes:
Always review the
analyzeoutput and, if needed, use version control to inspect whatfixremoved.
Development #
dart pub get
dart format .
dart analyze .
Run from source:
dart run bin/dartd.dart analyze --root lib
dart run bin/dartd.dart fix --root lib
License #
MIT