-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathrubocop.rs
159 lines (145 loc) · 4.7 KB
/
rubocop.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use super::Parser;
use anyhow::Result;
use qlty_types::analysis::v1::{Category, Issue, Level, Location, Range};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RubocopJson {
pub files: Vec<RubocopFile>,
metadata: Option<serde_json::Value>,
summary: Option<serde_json::Value>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RubocopFile {
pub path: String,
pub offenses: Vec<RubocopOffense>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RubocopOffense {
pub severity: String,
pub message: String,
pub cop_name: String,
pub corrected: bool,
pub correctable: Option<bool>,
pub location: RubocopLocation,
}
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub struct RubocopLocation {
pub start_line: i32,
pub start_column: i32,
pub last_line: i32,
pub last_column: i32,
pub length: i32,
pub line: i32,
pub column: i32,
}
#[derive(Debug, Default)]
pub struct Rubocop {}
impl Parser for Rubocop {
fn parse(&self, plugin_name: &str, output: &str) -> Result<Vec<Issue>> {
let mut issues = vec![];
for file in serde_json::from_str::<RubocopJson>(output)?.files {
for offense in file.offenses {
let issue = Issue {
tool: plugin_name.into(),
message: offense.message.trim().to_string(),
category: cop_name_to_category(&offense.cop_name).into(),
level: severity_to_level(&offense.severity).into(),
rule_key: offense.cop_name,
location: Some(Location {
path: file.path.clone(),
range: Some(Range {
start_line: offense.location.start_line as u32,
start_column: offense.location.start_column as u32,
end_line: offense.location.last_line as u32,
end_column: offense.location.last_column as u32,
..Default::default()
}),
}),
..Default::default()
};
issues.push(issue);
}
}
Ok(issues)
}
}
fn cop_name_to_category(cop_name: &str) -> Category {
if cop_name.to_lowercase().starts_with("layout") || cop_name.to_lowercase().starts_with("style")
{
Category::Style
} else if cop_name.to_lowercase().starts_with("performance") {
Category::Performance
} else {
Category::Lint
}
}
fn severity_to_level(severity: &str) -> Level {
match severity {
"fatal" => Level::High,
"error" => Level::High,
"warning" => Level::Medium,
"refactor" => Level::Medium,
"info" => Level::Low,
"convention" => Level::Medium,
_ => Level::Low,
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse() {
let input = r###"{
"metadata": {
"rubocop_version": "1.39.0",
"ruby_engine": "ruby",
"ruby_version": "3.2.0",
"ruby_patchlevel": "0",
"ruby_platform": "aarch64-linux-musl"
},
"files": [
{
"path": "/usr/local/bundle/gems/rubocop-1.39.0/lib/rubocop.rb",
"offenses": [
{
"severity": "warning",
"message": "Unnecessary disabling of `Naming/InclusiveLanguage`.",
"cop_name": "Lint/RedundantCopDisableDirective",
"corrected": false,
"correctable": true,
"location": {
"start_line": 68,
"start_column": 53,
"last_line": 68,
"last_column": 91,
"length": 39,
"line": 68,
"column": 53
}
}
]
}
],
"summary": {
"offense_count": 1,
"target_file_count": 1,
"inspected_file_count": 1
}
}"###;
let issues = Rubocop {}.parse("rubocop", input);
insta::assert_yaml_snapshot!(issues.unwrap(), @r#"
- tool: rubocop
ruleKey: Lint/RedundantCopDisableDirective
message: "Unnecessary disabling of `Naming/InclusiveLanguage`."
level: LEVEL_MEDIUM
category: CATEGORY_LINT
location:
path: /usr/local/bundle/gems/rubocop-1.39.0/lib/rubocop.rb
range:
startLine: 68
startColumn: 53
endLine: 68
endColumn: 91
"#);
}
}