Future _analyzePackage()

in lib/src/report/static_analysis.dart [73:130]


Future<_AnalysisResult> _analyzePackage(PackageContext context) async {
  Issue issueFromCodeProblem(CodeProblem codeProblem) {
    return Issue(
      '${codeProblem.severity}: ${codeProblem.description}',
      suggestion:
          'To reproduce make sure you are using the [lints_core](https://pub.dev/packages/lints) and '
          'run `${context.usesFlutter ? 'flutter analyze' : 'dart analyze'} ${codeProblem.file}`',
      spanFn: () {
        final sourceFile = SourceFile.fromString(
            File(p.join(context.packageDir, codeProblem.file))
                .readAsStringSync(),
            url: p.join(context.packageDir, codeProblem.file));
        try {
          // SourceSpans are 0-based, so we subtract 1 from line and column.
          final startOffset =
              sourceFile.getOffset(codeProblem.line - 1, codeProblem.col - 1);
          // Limit the maximum length of the source span.
          final length = math.min(codeProblem.length, maxSourceSpanLength);
          return sourceFile.span(startOffset, startOffset + length);
        } on RangeError {
          // Note: This happens if the file contains CR as line terminators.
          // If the range is invalid, then we just return null.
          return null;
        }
      },
    );
  }

  final dirs = await listFocusDirs(context.packageDir);

  try {
    final list = await context.staticAnalysis();

    return _AnalysisResult(
        list
            .where((element) => element.isError)
            .map(issueFromCodeProblem)
            .toList(),
        list
            .where((element) => element.isWarning)
            .map(issueFromCodeProblem)
            .toList(),
        list
            .where((element) => element.isInfo)
            .map(issueFromCodeProblem)
            .toList(),
        'dart analyze ${dirs.join(' ')}');
  } on ToolException catch (e) {
    return _AnalysisResult(
      [
        Issue('Failed to run `dart analyze`:\n```\n${e.message}\n```\n'),
      ],
      [],
      [],
      'dart analyze ${dirs.join(' ')}',
    );
  }
}