Library API
This page shows practical examples. For every public class, member, and signature, see the generated API reference on pub.dev, the canonical API documentation.
Import the package’s stable public surface:
import 'package:hyena_dart/hyena_dart.dart';Run both analyzers
Section titled “Run both analyzers”import 'dart:io';
import 'package:hyena_dart/hyena_dart.dart';
Future<void> main() async { const target = 'lib'; final config = await AnalyzerConfig.load(null, targetPath: target); final stopwatch = Stopwatch()..start();
final deadCode = await DeadCodeAnalyzer(config).analyze(target); final complexity = await ComplexityAnalyzer(config).analyze(target);
stopwatch.stop(); final result = AnalysisResult( targetPath: target, duration: stopwatch.elapsed, deadCodeReport: deadCode, complexityReport: complexity, );
final output = await JsonReporter().generate(result); await File('hyena-report.json').writeAsString(output);}AnalyzerConfig.load() applies the same explicit-file or upward-discovery behavior as the CLI. Construct AnalyzerConfig directly when all values come from code:
final config = AnalyzerConfig( excludePatterns: ['**/fixtures/**'], cyclomaticThreshold: 15, maxNestingLevel: 4, maxParameters: 6, ignoreMain: true, ignoreExports: true, ignorePrivate: false,);Use copyWith() to change a subset of an existing config.
Analyzer return values
Section titled “Analyzer return values”DeadCodeAnalyzer
Section titled “DeadCodeAnalyzer”final report = await DeadCodeAnalyzer(config).analyze('lib');DeadCodeReport exposes:
unusedEntitiesand derivedunusedCount;totalDeclarationsanddeadCodePercentage;groupedByTypeandgroupedByFile;skippedFiles;analyzedAt;toJson().
Each CodeEntity includes its name, EntityType, file, line, column, optional parent, visibility, and export status. fullName and typeLabel provide display values. For a display path, use package:path:
import 'package:path/path.dart' as p;
for (final entity in report.unusedEntities) { print('${entity.typeLabel} ${entity.fullName} (${p.relative(entity.filePath)})');}ComplexityAnalyzer
Section titled “ComplexityAnalyzer”final report = await ComplexityAnalyzer(config).analyze('lib');ComplexityReport exposes file and function totals, configured thresholds, high-complexity/nesting/parameter lists, the combined thresholdViolations list, skippedFiles, and toJson().
The nested models are:
FileMetrics: line counts, functions, average complexity, and maximum complexity;FunctionMetrics: location, complexity metrics, Halstead volume, and calculated maintainability index.
For the complete field list of every model, see the API reference.
Skipped files
Section titled “Skipped files”Dead-code analysis records non-generated files with resolution failures, ERROR diagnostics, or visitor failures in skippedFiles. Complexity analysis records read failures, syntax errors, and visitor failures. Each entry is a SkippedFile with an absolute filePath and a single-line reason:
for (final skipped in report.skippedFiles) { stderr.writeln('Skipped ${p.relative(skipped.filePath)}: ${skipped.reason}');}Missing references can cause dead-code findings, and generated resolution failures are absent from skippedFiles; see Analysis flow.
The analyzers also accept optional resolveUnit (DeadCodeAnalyzer) and readSource (ComplexityAnalyzer) constructor hooks, intended mainly for testing failure handling.
Combine results
Section titled “Combine results”AnalysisResult can contain either or both report types:
final result = AnalysisResult( targetPath: target, duration: elapsed, deadCodeReport: deadCode, complexityReport: null,);Its toJson() omits absent analyzer sections. AnalysisResult.skippedFiles aggregates skipped files from every included report, including workspace packages.
Generate output
Section titled “Generate output”Built-in reporters are:
final reporters = <Reporter>[ ConsoleReporter(useColors: false), JsonReporter(prettyPrint: true), MarkdownReporter(), HtmlReporter(), SarifReporter(),];
for (final reporter in reporters) { final content = await reporter.generate(result); // Store or publish content as appropriate.}Analyzers do not print. Reporters return a Future<String>, leaving file I/O and transport to the caller.
To add a format, implement Reporter:
class SummaryReporter implements Reporter { @override Future<String> generate(AnalysisResult result) async { final unused = result.deadCodeReport?.unusedCount ?? 0; final violations = result.complexityReport?.thresholdViolations.length ?? 0; return 'unused=$unused violations=$violations'; }}Analyze a package or workspace
Section titled “Analyze a package or workspace”AnalysisRunner is the entry point the CLI uses. It loads configuration, handles single packages and Dart workspaces, and returns one AnalysisResult:
final result = await const AnalysisRunner().analyze('.');print('Packages: ${result.packageAnalyses.length}');