-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rdjson foramt support for lint result, then we can use reviewdog.
- Loading branch information
Showing
6 changed files
with
229 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! AutoCorrect Lint JSON | ||
use crate::LintResult; | ||
|
||
#[doc(hidden)] | ||
pub fn to_lint_results_json(lint_results: Vec<LintResult>) -> String { | ||
format!( | ||
r#"{{"count": {},"messages": [{}]}}"#, | ||
lint_results.len(), | ||
lint_results | ||
.iter() | ||
.map(|r| r.to_json()) | ||
.collect::<Vec<_>>() | ||
.join(",") | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
pub(crate) fn crate_test_lint_results() -> Vec<LintResult> { | ||
use crate::result::{LineResult, Results, Severity}; | ||
|
||
let mut lint_result = LintResult::new("hello你好.\n这是第2行"); | ||
lint_result.filepath = "test/foo/bar.rs".to_owned(); | ||
lint_result.push(LineResult { | ||
line: 1, | ||
col: 1, | ||
new: "hello 你好。".to_owned(), | ||
old: "hello你好.".to_owned(), | ||
severity: Severity::Error, | ||
}); | ||
lint_result.push(LineResult { | ||
line: 2, | ||
col: 1, | ||
new: "这是第 2 行".to_owned(), | ||
old: "这是第2行".to_owned(), | ||
severity: Severity::Error, | ||
}); | ||
|
||
vec![lint_result] | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_to_lint_results_json() { | ||
let json = super::to_lint_results_json(crate::result::json::crate_test_lint_results()); | ||
|
||
let expected = r#"{"count": 1,"messages": [{"filepath":"test/foo/bar.rs","lines":[{"l":1,"c":1,"new":"hello 你好。","old":"hello你好.","severity":1},{"l":2,"c":1,"new":"这是第 2 行","old":"这是第2行","severity":1}],"error":""}]}"#; | ||
if expected != json { | ||
println!("--------------- json:\n{}", json); | ||
} | ||
assert_json_eq!(expected, json); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod json; | ||
pub mod rdjson; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_repr::*; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
//! Rdjson format for reviewdog | ||
//! https://github.com/reviewdog/reviewdog/tree/master/proto/rdf | ||
use super::LintResult; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
struct RdfJson { | ||
source: RdfSource, | ||
severity: String, | ||
diagnostics: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
struct RdfSource { | ||
name: String, | ||
url: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
struct RdfDiagnostic { | ||
message: String, | ||
severity: String, | ||
code: RdfCode, | ||
location: RdfLocation, | ||
suggestions: Vec<RdfSuggetion>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
struct RdfLocation { | ||
path: String, | ||
range: RdfRange, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
struct RdfRange { | ||
start: Option<RdfLineColumn>, | ||
end: Option<RdfLineColumn>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
struct RdfLineColumn { | ||
line: usize, | ||
column: usize, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
struct RdfSuggetion { | ||
text: String, | ||
range: RdfRange, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
struct RdfCode { | ||
value: Option<String>, | ||
url: String, | ||
} | ||
|
||
fn to_severity_str(severity: super::Severity) -> String { | ||
match severity { | ||
super::Severity::Error => "ERROR".to_owned(), | ||
super::Severity::Warning => "WARNING".to_owned(), | ||
super::Severity::Pass => "PASS".to_owned(), | ||
} | ||
} | ||
|
||
/// RDF JSONSchema | ||
/// https://github.com/reviewdog/reviewdog/blob/master/proto/rdf/jsonschema/Diagnostic.jsonschema | ||
#[doc(hidden)] | ||
pub(crate) fn to_rdjson_diagnostic(lint_result: &LintResult, pretty: bool) -> String { | ||
let range = RdfRange { | ||
start: Some(RdfLineColumn { | ||
line: lint_result.line, | ||
column: lint_result.col, | ||
}), | ||
end: None, | ||
}; | ||
|
||
let mut rdf_diagnostic: RdfDiagnostic = RdfDiagnostic { | ||
message: "".to_owned(), | ||
location: RdfLocation { | ||
path: lint_result.filepath.clone(), | ||
range: range.clone(), | ||
}, | ||
severity: "UNKNOWN_SEVERITY".to_owned(), | ||
code: RdfCode { | ||
value: Some("AutoCorrect".to_owned()), | ||
url: "https://github.com/huacnlee/autocorrect".to_owned(), | ||
}, | ||
suggestions: vec![], | ||
}; | ||
|
||
lint_result.lines.iter().for_each(|line_result| { | ||
if rdf_diagnostic.severity == "UNKNOWN_SEVERITY" { | ||
rdf_diagnostic.severity = to_severity_str(line_result.severity); | ||
} | ||
|
||
let suggestion = RdfSuggetion { | ||
text: line_result.new.clone(), | ||
range: RdfRange { | ||
start: Some(RdfLineColumn { | ||
line: line_result.line, | ||
column: line_result.col, | ||
}), | ||
end: Some(RdfLineColumn { | ||
line: line_result.line + line_result.old.split("\n").count() - 1, | ||
column: line_result.col | ||
+ line_result | ||
.old | ||
.split("\n") | ||
.last() | ||
.unwrap_or("") | ||
.chars() | ||
.count(), | ||
}), | ||
}, | ||
}; | ||
|
||
rdf_diagnostic.suggestions.push(suggestion); | ||
}); | ||
|
||
if pretty { | ||
serde_json::to_string_pretty(&rdf_diagnostic).unwrap() | ||
} else { | ||
serde_json::to_string(&rdf_diagnostic).unwrap() | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
pub fn to_lint_results_rdjson(lint_results: Vec<LintResult>) -> String { | ||
let diagnostics = lint_results | ||
.iter() | ||
.map(|r| to_rdjson_diagnostic(r, false)) | ||
.collect::<Vec<_>>() | ||
.join(","); | ||
format!( | ||
r#"{{"source":{{"name":"AutoCorrect Lint","url": "https://github.com/huacnlee/autocorrect"}},"diagnostics": [{diagnostics}]}}"#, | ||
) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_to_lint_results_rdjson() { | ||
let rdjson = super::to_lint_results_rdjson(crate::result::json::crate_test_lint_results()); | ||
|
||
let expected = r#"{"source":{"name":"AutoCorrect Lint","url": "https://github.com/huacnlee/autocorrect"},"diagnostics": [{"message":"","severity":"ERROR","code":{"value":"autocorrect","url":"https://github.com/huacnlee/autocorrect"},"location":{"path":"test/foo/bar.rs","range":{"start":{"line":1,"column":1},"end":{"line":1,"column":25}}},"suggestions":[{"text":"hello 你好。","range":{"start":{"line":1,"column":1},"end":{"line":0,"column":11}}},{"text":"这是第 2 行","range":{"start":{"line":2,"column":1},"end":{"line":0,"column":12}}}]}]}"#; | ||
if expected != rdjson { | ||
println!("--------------- rdjson:\n{}", rdjson); | ||
} | ||
assert_json_eq!(expected, rdjson); | ||
} | ||
} |