analyzeFile static method

Future<List<FunctionMetrics>> analyzeFile(
  1. String filePath, {
  2. MetricsThresholds thresholds = const MetricsThresholds(),
})

Analyzes a single file for metrics.

Returns FunctionMetrics for each function in the file, or an empty list if the file cannot be parsed.

filePath is the path to the Dart file. thresholds configures violation detection limits.

Example:

final functions = await Anteater.analyzeFile('lib/main.dart');
for (final func in functions) {
  print('${func.functionName}: CC=${func.result.cyclomaticComplexity}');
}

Implementation

static Future<List<FunctionMetrics>> analyzeFile(
  String filePath, {
  MetricsThresholds thresholds = const MetricsThresholds(),
}) async {
  final loader = SourceLoader(filePath);
  try {
    final result = await loader.resolveFile(filePath);
    if (result == null) return [];

    final aggregator = MetricsAggregator(thresholds: thresholds);
    aggregator.addFile(filePath, result.unit);

    final report = aggregator.generateReport();
    return report.worstFunctions;
  } finally {
    await loader.dispose();
  }
}