detect_unused_code library
detect_unused_code
A fast, zero-dependency Dart and Flutter dead code & unused code detector and remediation engine. Detects and cleans unused classes, widgets, mixins, enums, commented-out files, dead code blocks, unused imports, variables, fields, and pending TODOs with automatic backups and rollbacks.
Classes
- AnalysisReport
- Comprehensive report containing all findings from an audit run.
- AssetAnalysisReport
- Comprehensive audit report of assets across all packages in the workspace.
- AssetAuditor
- Primary auditor engine that scans Dart and Flutter workspaces for unused assets, images, and fonts.
- AssetCleaner
-
Surgical cleaner for deleting unused assets and updating corresponding
pubspec.yamlfiles. - AuditorOptions
- Configuration options for UnusedCodeAuditor.
- BackupFileEntry
- Metadata entry representing a single file preserved in a backup snapshot.
- BackupManager
- Manages snapshot creation, archiving, and restoration for safe code cleanup operations.
- BackupManifest
- Metadata describing a complete backup snapshot created before a code cleanup operation.
- CleanAssetsResult
- Result summary of an asset cleaning operation.
- CleanDependenciesResult
- Result summary of a dependency cleaning operation.
- CleanResult
- Summary report produced after executing a code cleanup operation.
- CleanupStage
- Represents an isolated, staged cleanup phase during remediation.
- CliRunner
- Command-line interface runner for auditing and cleaning unused code.
- CodeCleaner
- Orchestrates surgical code cleanup, dead element removal, and safe refactorings.
- CommentBlockResult
- Represents a detected continuous block of commented-out code.
- CommentedFileResult
- Represents the analysis result of a file with significant commented-out content.
- DeclaredClassItem
- Metadata describing a declared class, enum, mixin, or extension in a Dart file.
- DeclaredDependency
-
Represents a raw declared dependency extracted from a
pubspec.yamlfile. - DependencyAuditor
-
Primary auditor engine that scans projects for unused and duplicate external dependencies across all
pubspec.yamlfiles. - DependencyCleaner
-
Surgical cleaner that removes unused and duplicate dependencies from targeted
pubspec.yamlfiles. - DiagnosticIssueResult
- Represents a diagnostic issue identified by the Dart analyzer (e.g. unused variable or dead code).
- HealthCheckResult
- Comprehensive health check result after running Dart analysis.
- HealthIssue
- Represents a compiler or analyzer diagnostic issue found during post-cleanup health checks.
- InteractiveTui
- Interactive Terminal UI (TUI) control dashboard for detect_unused_code.
- MarkdownReporter
- Formats and generates GitHub Flavored Markdown (GFM) reports for CI/CD environments.
- PackageDependenciesReport
-
Comprehensive report of dependency auditing across all
pubspec.yamlfiles. - PackageDescriptor
- Represents a package within a single or multi-package / monorepo workspace.
- PubspecScanner
-
Scanner that flexibly discovers and parses all
pubspec.yamlfiles across any workspace layout. - RemovedDirectiveItem
- Describes a dangling directive (import/export/part) that was safely removed.
- RestoreResult
- Result details following a backup restoration attempt.
- StagedCleanupRunner
- Orchestrates multi-stage, health-verified remediation of unused code, assets, and packages.
- TodoItemResult
- Represents a detected TODO comment or pending task in the source code.
- UnusedAssetItem
- Represents an unused asset file discovered during auditing.
- UnusedClassResult
- Represents the audit finding for an unused or dead class.
- UnusedCodeAuditor
- Primary auditor engine that scans Dart projects for dead code, unused classes, and commented files.
- UnusedDependencyItem
- Represents an unused or duplicate dependency issue found during audit.
Enums
- AssetCategory
- Categories of assets tracked by the auditor.
- DependencyIssueType
- The type of issue detected for a declared dependency.
- DependencyScope
-
Scope of a package dependency inside
pubspec.yaml.
Constants
- packageVersion → const String
- Current version of the detect_unused_code package.
Functions
-
findProjectOrWorkspaceRoot(
[Directory? startDir]) → Directory - Automatically searches for and resolves the project root directory.
-
formatByteSize(
int bytes) → String - Formats a byte size into a human-readable string (B, KB, MB, GB).
-
hasDependencyInjectionAnnotation(
List< String> rawLines, int classLineIndex) → bool -
Checks if preceding lines before
classLineIndexcontain a Dependency Injection annotation. -
hasFileKeepUnusedDirective(
String fileContent) → bool - Checks whether the file header contains a file-level directive to ignore unused code.
-
hasKeepUnusedDirective(
List< String> rawLines, int classLineIndex) → bool -
Checks if preceding lines before
classLineIndexcontain a keep-unused directive or annotation. -
isGeneratedDartFile(
String path) → bool - Checks whether the given file path represents a generated Dart file.
-
loadPubspecExcludes(
Directory projectRoot) → List< String> -
Reads custom exclusion patterns from the project's
pubspec.yamlfile underdetect_unused_code:ordetect_unused:(keysexclude:orignore:). -
matchesCustomExclude(
String path, List< String> excludes) → bool - Checks if a file path matches any custom exclude pattern or substring.
-
resolveTargetPath(
String? input, Directory projectRoot, {bool isJson = false}) → FileSystemEntity -
Resolves user input into a target directory or file entity within
projectRoot. -
walkDartFiles(
FileSystemEntity entity) → List< File> -
Recursively scans and collects all
.dartfiles underentity.
Typedefs
- AssetProgressCallback = void Function(int percent, String message)
- Callback signature for asset auditing progress.
- AuditorProgressCallback = void Function(int percent, String message)
- Callback signature for progress monitoring during an audit.
- DepProgressCallback = void Function(int percent, String message)
- Callback signature for dependency auditing progress.