mod escape;
mod group;
mod human;
mod json;
mod line;
mod listing;
mod name;
mod order;
mod sarif;
mod total;
mod value;
pub(crate) use escape::string as json_string;
pub(crate) use listing::{Listed, Listing, SetKind, listing};
pub(crate) use name::codepoint;
use crate::charset::CharSet;
use crate::fix::Rewrite;
use crate::lint::{Finding, Level, Lint};
use crate::rules::{LevelChoice, Origin, Rule, Sourced};
use crate::scan::Unreadable;
use crate::tokens::Count;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Format {
Human,
Json,
Sarif,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Violation<'a> {
pub path: &'a str,
pub finding: Finding,
pub set: &'a str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Skipped<'a> {
pub path: &'a str,
pub reason: Unreadable,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Change<'a> {
pub path: &'a str,
pub rewrite: &'a Rewrite,
pub set: &'a str,
pub lint: Lint,
pub level: Level,
}
#[cfg(test)]
impl<'a> Change<'a> {
pub(crate) const fn plain(path: &'a str, rewrite: &'a Rewrite) -> Self {
Self {
path,
rewrite,
set: "ascii",
lint: crate::lint::OUTSIDE_SET,
level: Level::Deny,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct FileStats<'a> {
pub path: &'a str,
pub outside: u64,
pub bytes: u64,
pub now: Count,
pub after: Count,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Explanation<'a> {
pub path: Option<&'a str>,
pub set: &'a CharSet,
pub rule: Option<&'a Rule>,
pub in_force: InForce<'a>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct InForce<'a> {
pub family: &'a str,
pub family_origin: Option<&'a Origin>,
pub level: Option<Sourced<'a, Level>>,
pub levels: Vec<Sourced<'a, LevelChoice>>,
}
#[cfg(test)]
pub(crate) fn check(
format: Format,
violations: &[Violation<'_>],
skipped: &[Skipped<'_>],
) -> String {
match format {
Format::Human => human::check(violations, skipped),
Format::Json => json::check(violations, skipped),
Format::Sarif => sarif::check(violations, skipped),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Batch<'a> {
pub path: &'a str,
pub set: &'a str,
pub findings: &'a [Finding],
pub remedies: &'a [Remedy],
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Remedy {
pub to: Option<String>,
pub fixable: bool,
}
pub(crate) fn check_batches(
format: Format,
batches: &[Batch<'_>],
skipped: &[Skipped<'_>],
shape: Shape,
) -> String {
let lines = order::batches(batches);
if shape.summary && format != Format::Sarif {
let groups = group::groups(lines);
return match format {
Format::Json => json::summary(&groups, skipped),
_ => human::summary(&groups, skipped, shape.max),
};
}
match format {
Format::Human => human::check_bounded(lines, skipped, shape.max),
Format::Json => json::check_lines(lines, skipped),
Format::Sarif => sarif::check_lines(lines, skipped),
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct Shape {
pub summary: bool,
pub max: Option<usize>,
}
pub(crate) fn check_note(format: Format, batches: &[Batch<'_>]) -> String {
match format {
Format::Human => group::tally(order::batches(batches)),
Format::Json | Format::Sarif => String::new(),
}
}
pub(crate) fn fix(
format: Format,
changes: &[Change<'_>],
unmapped: &[Batch<'_>],
skipped: &[Skipped<'_>],
) -> String {
let unmapped = order::batches(unmapped);
match format {
Format::Human => human::fix(changes, unmapped, skipped),
Format::Json | Format::Sarif => json::fix(changes, unmapped, skipped),
}
}
pub(crate) fn fix_note(
format: Format,
rewrote: usize,
left: usize,
wrote: bool,
) -> String {
match format {
Format::Human => human::fix_note(rewrote, left, wrote),
Format::Json | Format::Sarif => String::new(),
}
}
pub(crate) fn stats(
format: Format,
files: &[FileStats<'_>],
skipped: &[Skipped<'_>],
) -> String {
match format {
Format::Human => human::stats(files, skipped),
Format::Json | Format::Sarif => json::stats(files, skipped),
}
}
pub(crate) fn explain(format: Format, explanation: &Explanation<'_>) -> String {
match format {
Format::Human => human::explain(explanation),
Format::Json | Format::Sarif => json::explain(explanation),
}
}
pub(crate) fn error(verb: &str, message: &str) -> String {
json::error(verb, message)
}
pub(crate) fn sets(format: Format, sets: &[CharSet]) -> String {
match format {
Format::Human => human::sets(sets),
Format::Json | Format::Sarif => json::sets(sets),
}
}
#[cfg(test)]
mod tests {
use super::{Format, check, sets};
use crate::charset::CharSet;
#[test]
fn the_format_chooses_the_form() {
assert_eq!(check(Format::Human, &[], &[]), "");
assert!(check(Format::Json, &[], &[]).starts_with('{'));
}
#[test]
fn every_verb_answers_in_both_forms() {
let ascii = CharSet {
name: String::from("ascii"),
ranges: vec![],
};
let one = std::slice::from_ref(&ascii);
assert_eq!(sets(Format::Human, one), "ascii none");
assert!(sets(Format::Json, one).starts_with("{\"schema\":1,\"verb\""));
}
#[test]
fn only_check_has_a_sarif_shape() {
let log = check(Format::Sarif, &[], &[]);
assert!(log.contains("\"version\":\"2.1.0\""), "{log}");
assert_eq!(sets(Format::Sarif, &[]), sets(Format::Json, &[]));
}
}
#[cfg(test)]
mod spelled_once {
use super::{Format, Violation, check};
use crate::lint::{Finding, Level};
use crate::scan::{Hit, Position};
const ODD: &str = "b\u{202e}.md";
fn at(path: &str, byte: usize) -> Violation<'_> {
let (line, column, character) = (1, byte, '\u{2014}');
let position = Position { line, column, byte };
let hit = Hit {
position,
character,
};
let (lint, level) = (crate::lint::OUTSIDE_SET, Level::Deny);
let (finding, set) = (Finding { hit, lint, level }, "ascii");
Violation { path, finding, set }
}
fn report(format: Format) -> String {
let items = [at(ODD, 2), at("a.md", 1), at(ODD, 1), at("c.md", 1)];
check(format, &items, &[])
}
#[test]
fn every_human_line_names_its_own_path() {
let expected = concat!(
"a.md:1:1 U+2014 ascii\n",
"b<U+202E>.md:1:1 U+2014 ascii\n",
"b<U+202E>.md:1:2 U+2014 ascii\n",
"c.md:1:1 U+2014 ascii"
);
assert_eq!(report(Format::Human), expected);
}
#[test]
fn every_json_violation_names_its_own_path() {
let said = report(Format::Json);
let paths: Vec<&str> = said
.split("\"path\":")
.skip(1)
.filter_map(|rest| rest.split(',').next())
.collect();
let odd = "\"b\\u202e.md\"";
assert_eq!(paths, ["\"a.md\"", odd, odd, "\"c.md\""]);
}
#[test]
fn every_sarif_result_names_its_own_uri() {
let said = report(Format::Sarif);
let uris: Vec<&str> = said
.split("\"uri\":")
.skip(1)
.filter_map(|rest| rest.split('}').next())
.collect();
let odd = "\"b%E2%80%AE.md\"";
assert_eq!(uris, ["\"a.md\"", odd, odd, "\"c.md\""]);
}
}
#[cfg(test)]
mod batched {
use super::{Batch, Format, Violation, check, check_batches};
use crate::lint::{Finding, Level};
use crate::scan::{Hit, Position};
fn finding(byte: usize) -> Finding {
let (line, column, character) = (1, byte, '\u{2014}');
let position = Position { line, column, byte };
let hit = Hit {
position,
character,
};
let lint = crate::lint::OUTSIDE_SET;
let level = Level::Deny;
Finding { hit, lint, level }
}
fn batch<'a>(
path: &'a str,
set: &'a str,
findings: &'a [Finding],
) -> Batch<'a> {
Batch {
path,
set,
findings,
remedies: &[],
}
}
fn expanded<'a>(batches: &[Batch<'a>]) -> Vec<Violation<'a>> {
let one = |b: &Batch<'a>| {
let (path, set) = (b.path, b.set);
b.findings.iter().map(move |f| Violation {
path,
finding: f.clone(),
set,
})
};
batches.iter().flat_map(one).collect()
}
fn same_report(batches: &[Batch<'_>]) {
let flat = expanded(batches);
for format in [Format::Human, Format::Json, Format::Sarif] {
let said =
check_batches(format, batches, &[], super::Shape::default());
assert_eq!(said, check(format, &flat, &[]), "{format:?}");
}
}
#[test]
fn batches_in_any_path_order_report_as_their_violations() {
let (a, b) = ([finding(1), finding(4)], [finding(2), finding(9)]);
let hazard = [finding(3)];
same_report(&[
batch("b.md", "ascii", &b),
batch("a.md", "ascii", a.get(..1).unwrap_or_default()),
batch("a.md", "hazard", &hazard),
batch("a.md", "ascii", a.get(1..).unwrap_or_default()),
]);
}
#[test]
fn interleaved_batches_of_one_path_report_as_their_violations() {
let (one, two) = ([finding(1), finding(7)], [finding(3), finding(5)]);
same_report(&[
batch("a.md", "ascii", &one),
batch("a.md", "any", &two),
]);
}
}