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.
- ScanConfig
- Rule configuration loaded from a project's config files.
- ScanDiagnostic
- A single lint diagnostic found during scanning.
- ScanRunner
- Scans Dart files using saropa_lints rules without the plugin framework.
- StaleIgnore
- A single stale ignore finding: an ignore directive whose suppressed rule did not produce a diagnostic on the target line.
- StaleIgnoreFixResult
- Result of fixing stale ignores in a single file: tracks the count of removed ignore directives and the rewritten file content.
Functions
-
buildSarifReport(
List< Map< diagnostics, {required String rootPath, required String toolVersion}) → Map<String, dynamic> >String, dynamic> -
Builds a full SARIF 2.1.0 log for
diagnostics(enriched diagnostic maps as produced byscanDiagnosticsToJson+ audit's tier/category enrichment).rootPathanchors the relativeartifactLocation.urivalues.toolVersionshould besaropaLintsVersionfrompackage: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. Returnsnullif 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. -
detectStaleIgnores(
{required List< ScanDiagnostic> diagnostics, required List<String> files}) → List<StaleIgnore> -
Detects stale
// ignore:comments across the givenfilesby comparing them againstdiagnosticsfrom a scan run. -
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
.dartfiles changed betweenrefand HEAD in the repository atrepoPath. -
loadScanConfig(
String projectRoot) → ScanConfig? -
Attempts to load rule config from the project at
projectRoot. -
sarifLevelForSeverity(
String? severity) → String -
Maps this project's three-level LintImpact/severity scale (error,
warning, info — see
saropa_lint_rule.dart) onto SARIF'slevelenum (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 towarningrather thannone, becausenonemeans "not evaluated as a problem" in the SARIF spec — a worse default for an unknown diagnostic than surfacing it visibly. -
sarifReportToJsonString(
List< Map< diagnostics, {required String rootPath, required String toolVersion}) → StringString, dynamic> > -
Encodes
diagnosticsas a pretty-printed SARIF 2.1.0 JSON string. MirrorsscanDiagnosticsToJsonString's encoding convention (2-space indent) so both output formats look consistent to a human reading--outputfiles side by side. -
scanDiagnosticsToJson(
List< ScanDiagnostic> diagnostics, {Map<String, Object> ? failOn}) → Map<String, Object> -
Serializes
diagnosticsto the same JSON structure used bydart run saropa_lints scan --format json. -
scanDiagnosticsToJsonString(
List< ScanDiagnostic> diagnostics, {Map<String, Object> ? failOn}) → String -
Encodes
diagnosticsto a JSON string (pretty-printed). -
staleIgnoresToJsonString(
List< StaleIgnore> staleIgnores) → String -
Serializes
staleIgnoresto the JSON format compatible with the scan CLI's--format jsonoutput convention. -
tierForRule(
String ruleName) → String? -
Returns the tier name for
ruleName, ornullif 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.