Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Code Climate output format #937

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/typos-cli/src/bin/typos-cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum Format {
#[default]
Long,
Json,
CodeClimate,
}

impl Format {
Expand All @@ -19,6 +20,7 @@ impl Format {
Format::Brief => Box::new(crate::report::PrintBrief),
Format::Long => Box::new(crate::report::PrintLong),
Format::Json => Box::new(crate::report::PrintJson),
Format::CodeClimate => Box::new(crate::report::PrintCodeClimate),
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions crates/typos-cli/src/bin/typos-cli/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,98 @@ impl Report for PrintJson {
}
}

pub struct PrintCodeClimate;

impl Report for PrintCodeClimate {
fn report(&self, msg: Message) -> Result<(), std::io::Error> {
let mut climate: CodeClimate = Default::default();
climate.r#type = "issue";
climate.check_name = "typos";
match msg {
Message::BinaryFile(_msg) => {
climate.severity = "info";
}
Message::Typo(msg) => {
match msg.corrections {
typos::Status::Valid => {}
typos::Status::Invalid => {
climate.description = format!("`{}` is disallowed", msg.typo,);
}
typos::Status::Corrections(corrections) => {
climate.description = format!(
"`{}` should be {}",
msg.typo,
itertools::join(corrections.iter().map(|s| format!("`{}`", s)), ", ")
);
}
}
climate.categories = "Clarity";
climate.severity = "minor";
climate.location = Location::from(msg.context.unwrap());
}
Message::Error(msg) => {
climate.severity = "error";
climate.description = context_display(&msg.context).to_string();
}
_ => {
climate.severity = "minor";
}
}
writeln!(
stdout().lock(),
"{}",
serde_json::to_string(&climate).unwrap()
)?;
Ok(())
}
}

#[derive(Default, serde::Serialize)]
struct CodeClimate<'l> {
check_name: &'l str,
description: String,
categories: &'l str,
location: Location<'l>,
severity: &'l str,
r#type: &'l str,
}

#[derive(serde::Serialize)]
struct Location<'l> {
path: &'l std::path::Path,
lines: Lines,
}

impl<'l> Default for Location<'l> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
lines: Lines { begin: 0 },
}
}
}

impl<'l> From<Context<'l>> for Location<'l> {
fn from(context: Context<'l>) -> Self {
match context {
Context::File(c) => Self {
path: c.path,
lines: Lines { begin: c.line_num },
},
Context::Path(c) => Self {
path: c.path,
..Default::default()
},
_ => Default::default(),
}
}
}

#[derive(Default, serde::Serialize)]
struct Lines {
begin: usize,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down