Skip to content

Commit 383d294

Browse files
author
Chris Clearwater
authored
Use a python parser for rubocop (#352)
1 parent 90b8546 commit 383d294

3 files changed

Lines changed: 61 additions & 22 deletions

File tree

linters/rubocop/plugin.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ lint:
1010
commands:
1111
- name: lint
1212
# Custom parser type defined in the trunk cli to handle rubocop's JSON output.
13-
output: rubocop
13+
output: sarif
1414
run: rubocop --format json ${target}
15-
success_codes: [0, 1, 2]
15+
success_codes: [0, 1]
1616
batch: true
17+
parser:
18+
runtime: python
19+
run: python3 ${plugin}/linters/rubocop/rubocop_to_sarif.py
1720
- name: fix-layout
1821
formatter: true
1922
output: rewrite
2023
run: rubocop --fix-layout ${target}
21-
success_codes: [0, 1, 2]
24+
success_codes: [0, 1]
2225
in_place: true
2326
batch: true
2427
issue_url_format: https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/{}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
3+
import json
4+
import sys
5+
6+
7+
def map_severity(severity):
8+
if severity in ["convention", "refactor", "info"]:
9+
return "note"
10+
if severity in ["warning"]:
11+
return "warning"
12+
if severity in ["error", "fatal"]:
13+
return "error"
14+
return "none"
15+
16+
17+
results = []
18+
19+
for file in json.load(sys.stdin)["files"]:
20+
for offense in file["offenses"]:
21+
parse = {
22+
"level": map_severity(offense["severity"]),
23+
"locations": [
24+
{
25+
"physicalLocation": {
26+
"artifactLocation": {
27+
"uri": file["path"],
28+
},
29+
"region": {
30+
"startLine": offense["location"]["start_line"],
31+
"startColumn": offense["location"]["start_column"],
32+
"endLine": offense["location"]["last_line"],
33+
"endColumn": offense["location"]["last_column"],
34+
},
35+
}
36+
}
37+
],
38+
"message": {"text": offense["message"]},
39+
"ruleId": offense["cop_name"],
40+
}
41+
results.append(parse)
42+
43+
sarif = {
44+
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
45+
"version": "2.1.0",
46+
"runs": [{"results": results}],
47+
}
48+
49+
print(json.dumps(sarif, indent=2))

linters/rubocop/test_data/rubocop_v1.39.0_basic.check.shot

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exports[`Testing linter rubocop test basic 1`] = `
1515
"ranges": [
1616
{
1717
"filePath": "test_data/basic.rb",
18-
"length": "20",
18+
"length": "19",
1919
},
2020
],
2121
"targetType": "ruby",
@@ -29,12 +29,6 @@ exports[`Testing linter rubocop test basic 1`] = `
2929
"line": "1",
3030
"linter": "rubocop",
3131
"message": "Missing frozen string literal comment.",
32-
"ranges": [
33-
{
34-
"filePath": "test_data/basic.rb",
35-
"length": "1",
36-
},
37-
],
3832
"targetType": "ruby",
3933
},
4034
{
@@ -49,7 +43,7 @@ exports[`Testing linter rubocop test basic 1`] = `
4943
"ranges": [
5044
{
5145
"filePath": "test_data/basic.rb",
52-
"length": "7",
46+
"length": "6",
5347
"offset": "5",
5448
},
5549
],
@@ -71,7 +65,7 @@ exports[`Testing linter rubocop test basic 1`] = `
7165
"ranges": [
7266
{
7367
"filePath": "test_data/basic.rb",
74-
"length": "44",
68+
"length": "43",
7569
"offset": "59",
7670
},
7771
],
@@ -89,7 +83,7 @@ exports[`Testing linter rubocop test basic 1`] = `
8983
"ranges": [
9084
{
9185
"filePath": "test_data/basic.rb",
92-
"length": "5",
86+
"length": "4",
9387
"offset": "69",
9488
},
9589
],
@@ -107,7 +101,7 @@ exports[`Testing linter rubocop test basic 1`] = `
107101
"ranges": [
108102
{
109103
"filePath": "test_data/basic.rb",
110-
"length": "5",
104+
"length": "4",
111105
"offset": "87",
112106
},
113107
],
@@ -212,13 +206,6 @@ exports[`Testing linter rubocop test basic 1`] = `
212206
"line": "9",
213207
"linter": "rubocop",
214208
"message": "Use 2 (not 1) spaces for indentation.",
215-
"ranges": [
216-
{
217-
"filePath": "test_data/basic.rb",
218-
"length": "1",
219-
"offset": "37",
220-
},
221-
],
222209
"targetType": "ruby",
223210
},
224211
{
@@ -233,7 +220,7 @@ exports[`Testing linter rubocop test basic 1`] = `
233220
"ranges": [
234221
{
235222
"filePath": "test_data/basic.rb",
236-
"length": "15",
223+
"length": "14",
237224
"offset": "38",
238225
},
239226
],

0 commit comments

Comments
 (0)