analyzeWithDart function

Future<List<ReportedDiagnostic>> analyzeWithDart(
  1. String root
)

Runs dart analyze --format=json with the running SDK, so the package's own plugin configuration, disabled rules, and ignore comments apply.

Implementation

Future<List<ReportedDiagnostic>> analyzeWithDart(String root) async {
  final result = await Process.run(Platform.resolvedExecutable, [
    'analyze',
    '--format=json',
    root,
  ], workingDirectory: root);
  final output = result.stdout as String;
  final start = output.indexOf('{');
  if (start < 0) {
    throw StateError(
      'dart analyze produced no JSON (exit ${result.exitCode}): '
      '${result.stderr}',
    );
  }
  final json = jsonDecode(output.substring(start)) as Map<String, Object?>;
  return [
    for (final diagnostic in json['diagnostics'] as List<Object?>)
      if (diagnostic case {
        'code': final String code,
        'location': {
          'file': final String path,
          'range': {
            'start': {'offset': final int start},
            'end': {'offset': final int end},
          },
        },
      })
        ReportedDiagnostic(code.toLowerCase(), path, start, end - start),
  ];
}