Skip to content
This repository has been archived by the owner on Jul 8, 2019. It is now read-only.

Fix NullPointerException when tslint json is empty. #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
<groupId>com.pablissimo.sonar</groupId>
<artifactId>sonar-typescript-plugin</artifactId>
<packaging>sonar-plugin</packaging>
<version>0.96-SNAPSHOT</version>
<version>0.96.1-SNAPSHOT</version>

<name>TypeScript</name>
<description>Analyse TypeScript projects</description>
<inceptionYear>2013</inceptionYear>
<url>https://github.com/pablissimo/SonarTsPlugin</url>
<organisation>

<organisation>
<name>Paul O'Neill (pablissimo) and contributors</name>
<url>https://github.com/pablissimo/SonarTsPlugin</url>
</organisation>
<developers>

<developers>
<developer>
<id>pablissimo</id>
<name>Paul O'Neill</name>
Expand All @@ -39,11 +39,11 @@
<system>Travis</system>
<url>https://travis-ci.org/pablissimo/SonarTsPlugin</url>
</ciManagement>
<properties>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<sonar.buildVersion>5.6</sonar.buildVersion>

<sonar.buildVersion>5.6</sonar.buildVersion>
<jdk.min.version>1.8</jdk.min.version>
<sslr.version>1.21</sslr.version>
</properties>
Expand All @@ -61,8 +61,8 @@
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
Expand Down Expand Up @@ -135,8 +135,8 @@
<configuration>
<source>${jdk.min.version}</source>
<target>${jdk.min.version}</target>
<showDeprecation>true</showDeprecation>

<showDeprecation>true</showDeprecation>
<compilerArgs>
<arg>-Xlint:all</arg>
<arg>-Werror</arg>
Expand Down
61 changes: 24 additions & 37 deletions src/main/java/com/pablissimo/sonar/TsLintParserImpl.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,43 @@
package com.pablissimo.sonar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import com.pablissimo.sonar.model.TsLintIssue;

import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class TsLintParserImpl implements TsLintParser {
private static final Logger LOG = LoggerFactory.getLogger(TsLintParserImpl.class);


public Map<String, List<TsLintIssue>> parse(List<String> toParse) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();

List<TsLintIssue> allIssues = new ArrayList<TsLintIssue>();

for (String batch : toParse) {
TsLintIssue[] batchIssues = gson.fromJson(getFixedUpOutput(batch), TsLintIssue[].class);
for (TsLintIssue batchIssue : batchIssues) {
allIssues.add(batchIssue);
}
}

// Remap by filename
Map<String, List<TsLintIssue>> toReturn = new HashMap<String, List<TsLintIssue>>();
for (TsLintIssue issue : allIssues) {
List<TsLintIssue> issuesByFile = toReturn.get(issue.getName());
if (issuesByFile == null) {
issuesByFile = new ArrayList<TsLintIssue>();
toReturn.put(issue.getName(), issuesByFile);
}

issuesByFile.add(issue);
}
List<TsLintIssue> allIssues = toParse.stream()
.map(batch -> gson.fromJson(getFixedUpOutput(batch), TsLintIssue[].class))
.filter(Objects::nonNull)
.flatMap(Arrays::stream)
.collect(Collectors.toList());

return toReturn;
// Remap by filename
return allIssues.stream()
.collect(Collectors.groupingBy(TsLintIssue::getName));
}

private String getFixedUpOutput(String toParse) {
if (toParse.contains("][")) {
// Pre 4.0.0-versions of TsLint return nonsense for its JSON output
// when faced with multiple files so we need to fix it up before we
if (StringUtils.contains(toParse, ("]["))) {
// Pre 4.0.0-versions of TsLint return nonsense for its JSON output
// when faced with multiple files so we need to fix it up before we
// do anything else
return toParse.replaceAll("\\]\\[", ",");
}

return toParse;
}
}
}
46 changes: 26 additions & 20 deletions src/test/java/com/pablissimo/sonar/TsLintParserTest.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
package com.pablissimo.sonar;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.pablissimo.sonar.model.TsLintIssue;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.sonar.api.Properties;
import org.sonar.api.Property;

import com.pablissimo.sonar.model.TsLintIssue;
import static org.junit.Assert.assertEquals;

public class TsLintParserTest {


@Test
public void parsesValidTsLintRecordIntoObject() {
String parseRow1 = "[{\"endPosition\":{\"character\":44,\"line\":23,\"position\":658},\"failure\":\"for statements must be braced\",\"name\":\"Tools.ts\",\"ruleName\":\"curly\",\"startPosition\":{\"character\":6,\"line\":22,\"position\":587}}]";
String
parseRow1 =
"[{\"endPosition\":{\"character\":44,\"line\":23,\"position\":658},\"failure\":\"for statements must be braced\",\"name\":\"Tools.ts\",\"ruleName\":\"curly\",\"startPosition\":{\"character\":6,\"line\":22,\"position\":587}}]";
List<String> toParse = new ArrayList<String>();
toParse.add(parseRow1);

Map<String, List<TsLintIssue>> issues = new TsLintParserImpl().parse(toParse);

assertEquals(1, issues.size());

List<TsLintIssue> fileIssues = issues.get("Tools.ts");

assertEquals(1, fileIssues.size());

TsLintIssue issue = fileIssues.get(0);
assertEquals(44, issue.getEndPosition().getCharacter());
assertEquals(23, issue.getEndPosition().getLine());
Expand All @@ -44,26 +41,35 @@ public void parsesValidTsLintRecordIntoObject() {
assertEquals(22, issue.getStartPosition().getLine());
assertEquals(587, issue.getStartPosition().getPosition());
}


@Test
public void parsesEmptyJson() {
List<String> toParse = new ArrayList<>();
toParse.add("");

new TsLintParserImpl().parse(toParse);
}

@Test
public void parsesIssuesWithSameNameIntoSameBucket() {
List<String> toParse = new ArrayList<String>();
toParse.add("[{\"name\":\"Tools.ts\",\"ruleName\":\"tools1\"}]");
toParse.add("[{\"name\":\"Tools.ts\",\"ruleName\":\"tools2\"}]");

Map<String, List<TsLintIssue>> issues = new TsLintParserImpl().parse(toParse);

assertEquals(1, issues.size());
assertEquals(2, issues.get("Tools.ts").size());
}

@Test
public void fixesUpBrokenBatchedOutputFromTsLintPriorTo_4_0_0() {
List<String> toParse = new ArrayList<String>();
toParse.add("[{\"name\":\"Tools.ts\",\"ruleName\":\"tools1\"}][{\"name\":\"Tools.ts\",\"ruleName\":\"tools2\"}]");

toParse
.add("[{\"name\":\"Tools.ts\",\"ruleName\":\"tools1\"}][{\"name\":\"Tools.ts\",\"ruleName\":\"tools2\"}]");

Map<String, List<TsLintIssue>> issues = new TsLintParserImpl().parse(toParse);

assertEquals(1, issues.size());
assertEquals(2, issues.get("Tools.ts").size());
}
Expand Down