Skip to content

Commit 4c759b0

Browse files
authored
include resolved autofixes in --report output (#915)
1 parent cae7d59 commit 4c759b0

File tree

4 files changed

+108
-3
lines changed

4 files changed

+108
-3
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ dscanner -S source/
9191
dscanner --report source/
9292
```
9393

94+
The `--report` switch includes all information, plus cheap to compute autofixes
95+
that are already resolved ahead of time, as well as the names for the autofixes
96+
that need to be resolved using the `--resolveMessage` switch like described
97+
below.
98+
9499
You can also specify custom formats using `-f` / `--errorFormat`, where there
95100
are also built-in formats for GitHub Actions:
96101

@@ -101,7 +106,7 @@ dscanner -S -f github source/
101106
dscanner -S -f '{filepath}({line}:{column})[{type}]: {message}' source/
102107
```
103108

104-
To collect automatic issue fixes for a given location use
109+
To resolve automatic issue fixes for a given location use
105110

106111
```sh
107112
# collecting automatic issue fixes

src/dscanner/reports.d

+24
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ class DScannerJsonReporter
5555

5656
private static JSONValue toJson(Issue issue)
5757
{
58+
import std.sumtype : match;
59+
import dscanner.analysis.base : AutoFix;
60+
5861
// dfmt off
5962
JSONValue js = JSONValue([
6063
"key": JSONValue(issue.message.key),
@@ -80,6 +83,27 @@ class DScannerJsonReporter
8083
"message": JSONValue(a.message),
8184
])
8285
).array
86+
),
87+
"autofixes": JSONValue(
88+
issue.message.autofixes.map!(a =>
89+
JSONValue([
90+
"name": JSONValue(a.name),
91+
"replacements": a.replacements.match!(
92+
(const AutoFix.CodeReplacement[] replacements) => JSONValue(
93+
replacements.map!(r => JSONValue([
94+
"range": JSONValue([
95+
JSONValue(r.range[0]),
96+
JSONValue(r.range[1])
97+
]),
98+
"newText": JSONValue(r.newText)
99+
])).array
100+
),
101+
(const AutoFix.ResolveContext _) => JSONValue(
102+
"resolvable"
103+
)
104+
)
105+
])
106+
).array
83107
)
84108
]);
85109
// dfmt on

tests/it/source_autofix.d

+6
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@ struct S
22
{
33
int myProp() @property
44
{
5+
static if (a)
6+
{
7+
}
8+
else if (b)
9+
{
10+
}
511
}
612
}

tests/it/source_autofix.report.json

+72-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,81 @@
1515
"message": "Zero-parameter '@property' function should be marked 'const', 'inout', or 'immutable'.",
1616
"name": "function_attribute_check",
1717
"supplemental": [],
18+
"type": "warn",
19+
"autofixes": [
20+
{
21+
"name": "Mark function `const`",
22+
"replacements": [
23+
{
24+
"newText": " const",
25+
"range": [
26+
24,
27+
24
28+
]
29+
}
30+
]
31+
},
32+
{
33+
"name": "Mark function `inout`",
34+
"replacements": [
35+
{
36+
"newText": " inout",
37+
"range": [
38+
24,
39+
24
40+
]
41+
}
42+
]
43+
},
44+
{
45+
"name": "Mark function `immutable`",
46+
"replacements": [
47+
{
48+
"newText": " immutable",
49+
"range": [
50+
24,
51+
24
52+
]
53+
}
54+
]
55+
}
56+
]
57+
},
58+
{
59+
"autofixes": [
60+
{
61+
"name": "Insert `static`",
62+
"replacements": [
63+
{
64+
"newText": "static ",
65+
"range": [
66+
69,
67+
69
68+
]
69+
}
70+
]
71+
},
72+
{
73+
"name": "Wrap '{}' block around 'if'",
74+
"replacements": "resolvable"
75+
}
76+
],
77+
"column": 3,
78+
"endColumn": 10,
79+
"endIndex": 71,
80+
"endLine": 8,
81+
"fileName": "it/source_autofix.d",
82+
"index": 64,
83+
"key": "dscanner.suspicious.static_if_else",
84+
"line": 8,
85+
"message": "Mismatched static if. Use 'else static if' here.",
86+
"name": "static_if_else_check",
87+
"supplemental": [],
1888
"type": "warn"
1989
}
2090
],
21-
"lineOfCodeCount": 0,
22-
"statementCount": 0,
91+
"lineOfCodeCount": 3,
92+
"statementCount": 4,
2393
"structCount": 1,
2494
"templateCount": 0,
2595
"undocumentedPublicSymbols": 0

0 commit comments

Comments
 (0)