Skip to content

Commit

Permalink
Makes JSON output consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampfkarren committed Nov 5, 2020
1 parent 0cc16f3 commit 4776b96
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.9.1] - 2020-11-04
### Fixed
- Fixed `--display-style=json` giving an output incompatible with previous tooling.

## [0.9.0] - 2020-11-04
### Added
- Arguments that aren't required can now be filled with nil.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion selene-lib/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl Diagnostic {
code: Some(self.code.to_owned()),
labels,
message: self.message.to_owned(),
notes: self.notes.to_owned(),
notes: self.notes,
severity,
}
}
Expand Down
2 changes: 1 addition & 1 deletion selene/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "selene"
version = "0.9.0"
version = "0.9.1"
license = "MPL-2.0"
authors = ["Kampfkarren <[email protected]>"]
description = "A blazing-fast modern Lua linter written in Rust"
Expand Down
54 changes: 54 additions & 0 deletions selene/src/json_output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use codespan_reporting::diagnostic::{
Diagnostic as CodespanDiagnostic, Label as CodespanLabel, LabelStyle, Severity,
};
use serde::Serialize;

#[derive(Serialize)]
struct JsonDiagnostic {
severity: Severity,
code: Option<String>,
message: String,
primary_label: Label,
notes: Vec<String>,
secondary_labels: Vec<Label>,
}

#[derive(Serialize)]
struct Label {
span: Span,
message: String,
}

#[derive(Serialize)]
struct Span {
start: usize,
end: usize,
}

fn label_to_serializable<T>(label: &CodespanLabel<T>) -> Label {
Label {
message: label.message.to_owned(),
span: Span {
start: label.range.start,
end: label.range.end,
},
}
}

pub fn diagnostic_to_json(
diagnostic: &CodespanDiagnostic<codespan::FileId>,
) -> serde_json::Result<String> {
serde_json::to_string(&JsonDiagnostic {
code: diagnostic.code.to_owned(),
message: diagnostic.message.to_owned(),
severity: diagnostic.severity.to_owned(),
notes: diagnostic.notes.to_owned(),
primary_label: label_to_serializable(diagnostic.labels.first().expect("no labels passed")),
secondary_labels: diagnostic
.labels
.iter()
.filter(|label| label.style == LabelStyle::Secondary)
.map(label_to_serializable)
.collect(),
})
}
8 changes: 7 additions & 1 deletion selene/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use structopt::{clap, StructOpt};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use threadpool::ThreadPool;

mod json_output;
mod opts;
#[cfg(feature = "roblox")]
mod roblox;
Expand Down Expand Up @@ -112,7 +113,12 @@ fn emit_codespan(
};

if opts.display_style == opts::DisplayStyle::Json {
writeln!(writer, "{}", serde_json::to_string(&diagnostic).unwrap()).unwrap();
writeln!(
writer,
"{}",
json_output::diagnostic_to_json(diagnostic).unwrap()
)
.unwrap();
} else {
codespan_reporting::term::emit(writer, &config, files, diagnostic)
.expect("couldn't emit error to codespan");
Expand Down

0 comments on commit 4776b96

Please sign in to comment.