-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c2a702
commit e299d9c
Showing
6 changed files
with
369 additions
and
1 deletion.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/main/java/edu/hm/hafner/analysis/parser/ValeParser.java
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,75 @@ | ||
package edu.hm.hafner.analysis.parser; | ||
|
||
import java.io.Serial; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import edu.hm.hafner.analysis.Issue; | ||
import edu.hm.hafner.analysis.IssueBuilder; | ||
import edu.hm.hafner.analysis.Report; | ||
import edu.hm.hafner.analysis.Severity; | ||
|
||
/** | ||
* Parser for vale reports. | ||
*/ | ||
public class ValeParser extends JsonIssueParser { | ||
// Constants for JSON keys | ||
private static final String CHECK = "Check"; | ||
private static final String LINE_KEY = "Line"; | ||
private static final String LINK_KEY = "Link"; | ||
private static final String MESSAGE_KEY = "Message"; | ||
private static final String SPAN_KEY = "Span"; | ||
private static final String SEVERITY_KEY = "Severity"; | ||
|
||
@Serial | ||
private static final long serialVersionUID = -4034450901865555017L; | ||
|
||
@Override | ||
protected void parseJsonObject(final Report report, final JSONObject jsonReport, final IssueBuilder issueBuilder) { | ||
JSONArray fileNames = jsonReport.names(); | ||
for (Object o : fileNames) { | ||
if (o instanceof String f) { | ||
JSONArray jsonArray = jsonReport.getJSONArray(f); | ||
for (Object data : jsonArray) { | ||
if (data instanceof JSONObject dataObject) { | ||
report.add(createIssue(issueBuilder, f, dataObject)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private Issue createIssue(final IssueBuilder issueBuilder, final String fileName, final JSONObject data) { | ||
String checker = data.getString(CHECK); | ||
String message = data.getString(MESSAGE_KEY); | ||
String severity = data.getString(SEVERITY_KEY); | ||
String link = data.getString(LINK_KEY); | ||
int line = data.getInt(LINE_KEY); | ||
JSONArray span = data.getJSONArray(SPAN_KEY); | ||
int startColumn = span.getInt(0); | ||
int endColumn = span.getInt(1); | ||
final Severity analysisSeverity; | ||
switch (severity) { | ||
case "error": | ||
analysisSeverity = Severity.ERROR; | ||
break; | ||
case "warning": | ||
analysisSeverity = Severity.WARNING_NORMAL; | ||
break; | ||
case "suggestion": | ||
analysisSeverity = Severity.WARNING_LOW; | ||
break; | ||
default: | ||
analysisSeverity = Severity.WARNING_NORMAL; | ||
break; | ||
} | ||
return issueBuilder.setFileName(fileName).setDescription(checker) | ||
.setMessage(message).setSeverity(analysisSeverity) | ||
.setReference(link) | ||
.setLineStart(line) | ||
.setLineEnd(line) | ||
.setColumnStart(startColumn).setColumnEnd(endColumn) | ||
.buildAndClean(); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
src/main/java/edu/hm/hafner/analysis/registry/ValeDescriptor.java
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,31 @@ | ||
package edu.hm.hafner.analysis.registry; | ||
|
||
import edu.hm.hafner.analysis.IssueParser; | ||
import edu.hm.hafner.analysis.parser.ValeParser; | ||
|
||
/** | ||
* Descriptor for the vale prose linter. | ||
*/ | ||
public class ValeDescriptor extends ParserDescriptor { | ||
private static final String ID = "vale"; | ||
private static final String NAME = "Vale"; | ||
|
||
ValeDescriptor() { | ||
super(ID, NAME); | ||
} | ||
|
||
@Override | ||
public IssueParser createParser(Option... options) { | ||
return new ValeParser(); | ||
} | ||
|
||
@Override | ||
public String getPattern() { | ||
return "**/vale-report.json"; | ||
} | ||
|
||
@Override | ||
public String getUrl() { | ||
return "https://vale.sh/"; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/edu/hm/hafner/analysis/parser/ValeParserTest.java
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,27 @@ | ||
package edu.hm.hafner.analysis.parser; | ||
|
||
import edu.hm.hafner.analysis.IssueParser; | ||
import edu.hm.hafner.analysis.Report; | ||
import edu.hm.hafner.analysis.Severity; | ||
import edu.hm.hafner.analysis.assertions.SoftAssertions; | ||
import edu.hm.hafner.analysis.registry.AbstractParserTest; | ||
|
||
class ValeParserTest extends AbstractParserTest { | ||
ValeParserTest() { | ||
super("vale-report.json"); | ||
} | ||
|
||
@Override | ||
protected void assertThatIssuesArePresent(final Report report, final SoftAssertions softly) { | ||
softly.assertThat(report).hasSize(12).hasDuplicatesSize(0); | ||
softly.assertThat(report.get(0)) | ||
.hasFileName("file3.adoc") | ||
.hasDescription("RedHat.SimpleWords") | ||
.hasSeverity(Severity.WARNING_LOW); | ||
} | ||
|
||
@Override | ||
protected IssueParser createParser() { | ||
return new ValeParser(); | ||
} | ||
} |
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
234 changes: 234 additions & 0 deletions
234
src/test/resources/edu/hm/hafner/analysis/parser/vale-report.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 |
---|---|---|
@@ -0,0 +1,234 @@ | ||
{ | ||
"file1.adoc": [ | ||
{ | ||
"Action": { | ||
"Name": "", | ||
"Params": null | ||
}, | ||
"Span": [ | ||
1, | ||
5 | ||
], | ||
"Check": "RedHat.SentenceLength", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/sentencelength/", | ||
"Message": "Try to keep sentences to an average of 32 words or fewer.", | ||
"Severity": "suggestion", | ||
"Match": "While", | ||
"Line": 10 | ||
} | ||
], | ||
"file2.adoc": [ | ||
{ | ||
"Action": { | ||
"Name": "replace", | ||
"Params": [ | ||
"might", | ||
"can" | ||
] | ||
}, | ||
"Span": [ | ||
143, | ||
145 | ||
], | ||
"Check": "RedHat.TermsWarnings", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/termswarnings/", | ||
"Message": "Consider using 'might' or 'can' rather than 'may' unless updating existing content that uses the term.", | ||
"Severity": "warning", | ||
"Match": "may", | ||
"Line": 39 | ||
}, | ||
{ | ||
"Action": { | ||
"Name": "edit", | ||
"Params": [ | ||
"regex", | ||
"(\\w+)( using)", | ||
"$1 by using" | ||
] | ||
}, | ||
"Span": [ | ||
44, | ||
64 | ||
], | ||
"Check": "RedHat.Using", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/using/", | ||
"Message": "Use 'by using' instead of 'using' when it follows a noun for clarity and grammatical correctness.", | ||
"Severity": "warning", | ||
"Match": "functionalities using", | ||
"Line": 51 | ||
}, | ||
{ | ||
"Action": { | ||
"Name": "replace", | ||
"Params": [ | ||
"might", | ||
"can" | ||
] | ||
}, | ||
"Span": [ | ||
24, | ||
26 | ||
], | ||
"Check": "RedHat.TermsWarnings", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/termswarnings/", | ||
"Message": "Consider using 'might' or 'can' rather than 'may' unless updating existing content that uses the term.", | ||
"Severity": "warning", | ||
"Match": "may", | ||
"Line": 65 | ||
} | ||
], | ||
"file3.adoc": [ | ||
{ | ||
"Action": { | ||
"Name": "replace", | ||
"Params": [ | ||
"give", | ||
"offer" | ||
] | ||
}, | ||
"Span": [ | ||
11, | ||
17 | ||
], | ||
"Check": "RedHat.SimpleWords", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/simplewords/", | ||
"Message": "Use simple language. Consider using 'give' or 'offer' rather than 'provide'.", | ||
"Severity": "suggestion", | ||
"Match": "provide", | ||
"Line": 170 | ||
}, | ||
{ | ||
"Action": { | ||
"Name": "replace", | ||
"Params": [ | ||
"complete", | ||
"finish" | ||
] | ||
}, | ||
"Span": [ | ||
29, | ||
36 | ||
], | ||
"Check": "RedHat.SimpleWords", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/simplewords/", | ||
"Message": "Use simple language. Consider using 'complete' or 'finish' rather than 'finalize'.", | ||
"Severity": "suggestion", | ||
"Match": "finalize", | ||
"Line": 212 | ||
}, | ||
{ | ||
"Action": { | ||
"Name": "replace", | ||
"Params": [ | ||
"complete", | ||
"finish" | ||
] | ||
}, | ||
"Span": [ | ||
5, | ||
12 | ||
], | ||
"Check": "RedHat.SimpleWords", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/simplewords/", | ||
"Message": "Use simple language. Consider using 'complete' or 'finish' rather than 'finalize'.", | ||
"Severity": "suggestion", | ||
"Match": "finalize", | ||
"Line": 451 | ||
}, | ||
{ | ||
"Action": { | ||
"Name": "replace", | ||
"Params": [ | ||
"give", | ||
"offer" | ||
] | ||
}, | ||
"Span": [ | ||
16, | ||
22 | ||
], | ||
"Check": "RedHat.SimpleWords", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/simplewords/", | ||
"Message": "Use simple language. Consider using 'give' or 'offer' rather than 'provide'.", | ||
"Severity": "suggestion", | ||
"Match": "provide", | ||
"Line": 530 | ||
}, | ||
{ | ||
"Action": { | ||
"Name": "", | ||
"Params": null | ||
}, | ||
"Span": [ | ||
1, | ||
4 | ||
], | ||
"Check": "RedHat.SentenceLength", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/sentencelength/", | ||
"Message": "Try to keep sentences to an average of 32 words or fewer.", | ||
"Severity": "suggestion", | ||
"Match": "When", | ||
"Line": 533 | ||
}, | ||
{ | ||
"Action": { | ||
"Name": "", | ||
"Params": null | ||
}, | ||
"Span": [ | ||
1, | ||
4 | ||
], | ||
"Check": "RedHat.SentenceLength", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/sentencelength/", | ||
"Message": "Try to keep sentences to an average of 32 words or fewer.", | ||
"Severity": "suggestion", | ||
"Match": "When", | ||
"Line": 535 | ||
}, | ||
{ | ||
"Action": { | ||
"Name": "", | ||
"Params": null | ||
}, | ||
"Span": [ | ||
1, | ||
3 | ||
], | ||
"Check": "RedHat.SentenceLength", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/sentencelength/", | ||
"Message": "Try to keep sentences to an average of 32 words or fewer.", | ||
"Severity": "suggestion", | ||
"Match": "The", | ||
"Line": 571 | ||
}, | ||
{ | ||
"Action": { | ||
"Name": "", | ||
"Params": null | ||
}, | ||
"Span": [ | ||
1, | ||
12 | ||
], | ||
"Check": "RedHat.SentenceLength", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/sentencelength/", | ||
"Message": "Try to keep sentences to an average of 32 words or fewer.", | ||
"Severity": "suggestion", | ||
"Match": "Additionally", | ||
"Line": 717 | ||
} | ||
] | ||
} |