checkSource method

List<StyleViolation> checkSource(
  1. String path,
  2. String content
)

Parses content, the text of the file at path relative to the package root, and runs every rule on it. Violations suppressed by an // inspectra: ignore-style comment are left out.

The file is parsed with error recovery, so a file with syntax errors is still checked as far as it can be read.

Returns the violations sorted by line, column and rule.

Implementation

List<StyleViolation> checkSource(String path, String content) {
  final ParseStringResult parsed = parseString(
    content: content,
    path: path,
    throwIfDiagnostics: false,
  );
  final file = StyleFile(
    path: path,
    content: content,
    unit: parsed.unit,
    lineInfo: parsed.lineInfo,
  );
  final suppressions = StyleSuppressions.parse(content);
  final violations = <StyleViolation>[];
  for (final StyleRule rule in rules) {
    final reporter = StyleReporter(rule.id, file);
    rule.check(file, reporter);
    violations.addAll(
      reporter.violations.where((v) => !suppressions.suppresses(v)),
    );
  }
  return violations..sort(compareStyleViolations);
}