From efdcbfe10973bd859db1b53734a6996e6858ace7 Mon Sep 17 00:00:00 2001 From: mrb Date: Tue, 7 Jun 2016 10:19:28 -0400 Subject: [PATCH] Clean up Code Climate renderer --- .../pmd/renderers/CodeClimateRenderer.java | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java index 01d87dd414..64d4dbcfb7 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java @@ -34,7 +34,7 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { // Note: required by https://github.com/codeclimate/spec/blob/master/SPEC.md protected static final String NULL_CHARACTER = "\u0000"; - + private Rule rule; public CodeClimateRenderer() { @@ -48,7 +48,7 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { public void renderFileViolations(Iterator violations) throws IOException { Writer writer = getWriter(); Gson gson = new GsonBuilder().disableHtmlEscaping().create(); - + while (violations.hasNext()) { RuleViolation rv = violations.next(); rule = rv.getRule(); @@ -71,7 +71,7 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { issue.location = getLocation(rv); issue.remediation_points = getRemediationPoints(); issue.categories = getCategories(); - + switch(rule.getPriority()) { case HIGH: issue.severity = "critical"; @@ -85,7 +85,7 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { issue.severity = "info"; break; } - + return issue; } @@ -93,35 +93,35 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { public String defaultFileExtension() { return "json"; } - + private CodeClimateIssue.Location getLocation(RuleViolation rv) { CodeClimateIssue.Location result; - + String pathWithoutCcRoot = StringUtils.removeStartIgnoreCase(rv.getFilename(), "/code/"); - + if(rule.hasDescriptor(CODECLIMATE_REMEDIATION_MULTIPLIER) && !rule.getProperty(CODECLIMATE_BLOCK_HIGHLIGHTING)) { result = new CodeClimateIssue.Location(pathWithoutCcRoot, rv.getBeginLine(), rv.getBeginLine()); } else { result = new CodeClimateIssue.Location(pathWithoutCcRoot, rv.getBeginLine(), rv.getEndLine()); } - + return result; } - + private int getRemediationPoints() { int remediation_points = REMEDIATION_POINTS_DEFAULT; - + if(rule.hasDescriptor(CODECLIMATE_REMEDIATION_MULTIPLIER)) { remediation_points *= rule.getProperty(CODECLIMATE_REMEDIATION_MULTIPLIER); } - + return remediation_points; } - + private String[] getCategories() { String[] result; - + if(rule.hasDescriptor(CODECLIMATE_CATEGORIES)) { Object[] categories = rule.getProperty(CODECLIMATE_CATEGORIES); result = new String[categories.length]; @@ -132,10 +132,10 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { else { result = CODECLIMATE_DEFAULT_CATEGORIES; } - + return result; } - + private String getBody() { String result = "## " + rule.getName() + "\\n\\n" + "Since: PMD " + rule.getSince() + "\\n\\n" + @@ -143,22 +143,22 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { "[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): " + Arrays.toString(getCategories()).replaceAll("[\\[\\]]","") + "\\n\\n" + "[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): " + getRemediationPoints() + "\\n\\n" + cleaned(rule.getDescription()); - + if(!rule.getExamples().isEmpty()) { result += "\\n\\n### Example:\\n\\n"; - + for(String snippet : rule.getExamples()) { - snippet = snippet.replaceAll("\\n", "\\\\n"); - snippet = snippet.replaceAll("\\t", "\\\\t"); + snippet = snippet.replaceAll("\\n", "\\\\n"); + snippet = snippet.replaceAll("\\t", "\\\\t"); result += "```java\\n" + snippet + "\\n``` "; } } - + if(!rule.getPropertyDescriptors().isEmpty()) { result += "\\n\\n### [PMD properties](http://pmd.github.io/pmd-5.1.3/pmd-developer.html)\\n\\n"; result += "Name | Value | Description\\n"; result += "--- | --- | ---\\n"; - + for(PropertyDescriptor property : rule.getPropertyDescriptors()) { String propertyValue; try { @@ -167,22 +167,22 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { catch(Exception ignore) { propertyValue = rule.getProperty(property).toString(); } - - String porpertyName = property.name(); - porpertyName = porpertyName.replaceAll("\\_", "\\\\_"); - - result += porpertyName + " | " + propertyValue + " | " + property.description() + "\\n"; + + String propertyName = property.name(); + propertyName = propertyName.replaceAll("\\_", "\\\\_"); + + result += propertyName + " | " + propertyValue + " | " + property.description() + "\\n"; } } - - return result; + + return cleaned(result); } - + private String cleaned(String original) { String result = original.trim(); result = result.replaceAll("\\s+", " "); result = result.replaceAll("\\s*[\\r\\n]+\\s*", ""); - result = result.replaceAll("'", "`"); + result = result.replaceAll("\"","'"); return result; } }