scan library

Programmatic scan API for saropa_lints.

Use this when you want to run a scan from code (e.g. from another package or script) without invoking the CLI. The types and functions here are part of the public API and follow semver.

Example:

import 'package:saropa_lints/scan.dart';

void main() {
  final runner = ScanRunner(
    targetPath: '/path/to/project',
    tier: 'recommended',           // optional: override config with a tier
    dartFiles: ['lib/main.dart'],   // optional: scan only these files
    messageSink: (msg) => log(msg), // optional: redirect/suppress output
  );
  final diagnostics = runner.run();
  if (diagnostics != null) {
    // process diagnostics or serialize with scanDiagnosticsToJson(diagnostics)
  }
}

ScanRunner.run is a fast syntactic pass; rules on instance-creation expressions and type-based rules under-report there. Use the async ScanRunner.runResolved to scan with full resolution when those rules matter (slower, and the target must have had pub get run).

Classes

AnalysisContextCollection
A collection of analysis contexts.
JobsDecision
The final worker count for a run and messages explaining it.
MachineInfo
A snapshot of the machine's capacity, with container limits applied.
ParallelScanReport
What happened during the scan (for summaries and the extension).
ScanConfig
Rule configuration loaded from a project's config files.
ScanDiagnostic
A single lint diagnostic found during scanning.
ScanParallelConfig
Tunables for ScanRunner.runParallel beyond the worker count.
ScanRunner
Scans Dart files using saropa_lints rules without the plugin framework.
SlowFile
A file whose scan exceeded the slow-file threshold.
StaleIgnore
A single stale ignore finding: an ignore directive whose suppressed rule did not produce a diagnostic on the target line.
StaleIgnoreCoverage
What one stale-ignore scan covered. Every check answers "may this comment be judged?"; anything not positively covered is kept.
StaleIgnoreFixResult
Result of fixing stale ignores in a single file: tracks the count of removed ignore directives and the rewritten file content.
StaleIgnoreScan
Diagnostics and coverage from runStaleIgnoreScan.
ThrottleEvent
One throttle transition, for the report.

Enums

ProgressStyle
How progress is reported.

Functions

buildSarifReport(List<Map<String, dynamic>> diagnostics, {required String rootPath, required String toolVersion}) → Map<String, dynamic>
Builds a full SARIF 2.1.0 log for diagnostics (enriched diagnostic maps as produced by scanDiagnosticsToJson + audit's tier/category enrichment). rootPath anchors the relative artifactLocation.uri values. toolVersion should be saropaLintsVersion from package:saropa_lints/saropa_lints.dart — passed in rather than read here so this module has no dependency on the package's own export graph.
categoryForRule(String ruleName) → String?
Returns the category slug for ruleName (e.g. core, security, packages), derived from the source file's parent directory. Returns null if the rule is not in the generated category map.
categoryIndexForRules(Set<String> ruleNames) → Map<String, String>
Returns the category for every rule in ruleNames, keyed by rule name. Rules not in the category map are omitted from the result.
detectMachine({Map<String, String>? env}) → MachineInfo
Detects the machine's capacity. Reads cgroup limits on Linux so containers are sized by their quota, not the host.
detectStaleIgnores({required List<ScanDiagnostic> diagnostics, required List<String> files, StaleIgnoreCoverage? coverage}) → List<StaleIgnore>
Detects stale // ignore: comments across the given files by comparing them against diagnostics from a scan run.
filterIgnoredDiagnostics({required List<ScanDiagnostic> diagnostics, required List<String> files}) → List<ScanDiagnostic>
Removes diagnostics that are suppressed by // ignore: or // ignore_for_file: directives in the source files.
fixStaleIgnores(List<StaleIgnore> staleIgnores) → List<StaleIgnoreFixResult>
Removes stale ignore directives from the source files they appear in.
gitChangedDartFiles(String repoPath, String ref) → List<String>
Returns the list of .dart files changed between ref and HEAD in the repository at repoPath.
hasCommentedOutPluginBlock(String projectRoot) → bool
True when the project's analysis_options.yaml carries the marker written around a commented-out plugins: block. The marker alone does NOT mean saropa_lints is off: "Lint integration" off, the language server replacing the analyzer plugin, and the init default for a new project all write the same text. Use isScanIntegrationDisabled to ask whether it is off.
isScanIntegrationDisabled(String projectRoot) → bool
True when saropa_lints was switched off for this project: the plugin block is commented out AND a file on disk records the "off" choice. A plain scan without --tier then reports "scan skipped" and exits 0, so hooks do not fail on a switched-off project.
loadAnalyzerExcludes(String scanRoot) → List<String>
Reads analyzer: exclude: for a scan of scanRoot (#403).
loadScanConfig(String projectRoot, {bool includeDisabledBlock = true}) → ScanConfig?
Attempts to load rule config from the project at projectRoot.
recommendJobs({required int fileCount, required int cores, int? availableMemoryMb, bool isCi = false, bool resolved = false}) → JobsRecommendation
Computes a safe recommended worker count.
resolveJobs({required int? requested, required int fileCount, required MachineInfo machine, bool resolved = false, String? incompatibleReason}) → JobsDecision
Decides the worker count from the user's requested value.
runStaleIgnoreScan(ScanRunner runner, {bool resolve = true}) → Future<StaleIgnoreScan?>
Scans with runner in this process with the StaleIgnoreProbe armed.
sarifLevelForSeverity(String? severity) → String
Maps this project's three-level LintImpact/severity scale (error, warning, info — see saropa_lint_rule.dart) onto SARIF's level enum (error, warning, note, none). There is no finer-grained scale in the codebase to draw from, so this is a direct 1:1 mapping, not an invented scale. Unrecognized values fall back to warning rather than none, because none means "not evaluated as a problem" in the SARIF spec — a worse default for an unknown diagnostic than surfacing it visibly.
sarifReportToJsonString(List<Map<String, dynamic>> diagnostics, {required String rootPath, required String toolVersion}) → String
Encodes diagnostics as a pretty-printed SARIF 2.1.0 JSON string. Mirrors scanDiagnosticsToJsonString's encoding convention (2-space indent) so both output formats look consistent to a human reading --output files side by side.
scanDiagnosticsToJson(List<ScanDiagnostic> diagnostics, {Map<String, Object>? failOn}) → Map<String, Object>
Serializes diagnostics to the same JSON structure used by dart run saropa_lints scan --format json.
scanDiagnosticsToJsonString(List<ScanDiagnostic> diagnostics, {Map<String, Object>? failOn}) → String
Encodes diagnostics to a JSON string (pretty-printed).
staleIgnoresToJsonString(List<StaleIgnore> staleIgnores) → String
Serializes staleIgnores to the JSON format compatible with the scan CLI's --format json output convention.
tierForRule(String ruleName) → String?
Returns the tier name for ruleName, or null if the rule is not registered in any tier set.
tierIndexForRules(Set<String> ruleNames) → Map<String, String>
Returns the tier name for every rule in ruleNames, keyed by rule name. Rules not in any tier are omitted from the result.

Typedefs

ScanMessageSink = void Function(String message)
Optional sink for progress and error messages. When null, output goes to stdout (messages) and stderr (progress). When set, all output is sent to this callback so callers can suppress or redirect.