Skip to content

Commit

Permalink
Initial vale integration
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-parasoft committed Dec 19, 2024
1 parent 0c2a702 commit e299d9c
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 1 deletion.
75 changes: 75 additions & 0 deletions src/main/java/edu/hm/hafner/analysis/parser/ValeParser.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public class ParserRegistry {
new TnsdlDescriptor(),
new TrivyDescriptor(),
new TsLintDescriptor(),
new ValeDescriptor(),
new ValgrindDescriptor(),
new VeraCodePipelineScannerDescriptor(),
new XlcDescriptor(),
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/edu/hm/hafner/analysis/registry/ValeDescriptor.java
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 src/test/java/edu/hm/hafner/analysis/parser/ValeParserTest.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class ParserRegistryTest extends ResourceTest {
// Note for parser developers: if you add a new parser,
// please check if you are using the correct type and increment the corresponding count
private static final long WARNING_PARSERS_COUNT = 127L;
private static final long WARNING_PARSERS_COUNT = 128L;
private static final long BUG_PARSERS_COUNT = 3L;
private static final long VULNERABILITY_PARSERS_COUNT = 7L;
private static final long DUPLICATION_PARSERS_COUNT = 3L;
Expand Down
234 changes: 234 additions & 0 deletions src/test/resources/edu/hm/hafner/analysis/parser/vale-report.json
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
}
]
}

0 comments on commit e299d9c

Please sign in to comment.