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

Commit

Permalink
Fix #105, more robust handling of tslint output (#106)
Browse files Browse the repository at this point in the history
* Makes handling of paths reported by tslint more robust to casing issues (on case-insensitive filesystems) and differences between relative and absolute path specification
  • Loading branch information
Pablissimo committed Feb 7, 2017
1 parent 0a6b9aa commit 34ce5b5
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/main/java/com/pablissimo/sonar/TsLintSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.batch.sensor.issue.NewIssue;
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.config.Settings;
import org.sonar.api.rule.RuleKey;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

public class TsLintSensor implements Sensor {
Expand All @@ -22,7 +28,7 @@ public class TsLintSensor implements Sensor {
private PathResolver resolver;
private TsLintExecutor executor;
private TsLintParser parser;

public TsLintSensor(Settings settings, PathResolver resolver, TsLintExecutor executor, TsLintParser parser) {
this.settings = settings;
this.resolver = resolver;
Expand Down Expand Up @@ -64,20 +70,16 @@ else if (config.getConfigFile() == null && config.getPathToTsConfig() == null) {
}

List<String> paths = new ArrayList<String>();
HashMap<String, InputFile> absoluteFileMap = new HashMap<String, InputFile>();
HashMap<String, InputFile> relativeFileMap = new HashMap<String, InputFile>();

for (InputFile file : ctx.fileSystem().inputFiles(ctx.fileSystem().predicates().hasLanguage(TypeScriptLanguage.LANGUAGE_KEY))) {
if (skipTypeDefFiles && file.file().getName().toLowerCase().endsWith("." + TypeScriptLanguage.LANGUAGE_DEFINITION_EXTENSION)) {
if (shouldSkipFile(file.file(), skipTypeDefFiles)) {
continue;
}

String pathAdjusted = file.absolutePath().replace('\\', '/');
String pathAdjusted = file.absolutePath();
paths.add(pathAdjusted);
absoluteFileMap.put(pathAdjusted, file);
relativeFileMap.put(file.relativePath().replace('\\', '/'), file);
}

List<String> jsonResults = this.executor.execute(config, paths);

Map<String, List<TsLintIssue>> issues = this.parser.parse(jsonResults);
Expand All @@ -95,15 +97,30 @@ else if (config.getConfigFile() == null && config.getPathToTsConfig() == null) {
continue;
}

InputFile matchingFile = absoluteFileMap.get(filePath);
if (matchingFile == null) {
matchingFile = relativeFileMap.get(filePath);
File matchingFile = ctx.fileSystem().resolvePath(filePath);
InputFile inputFile = null;

if (shouldSkipFile(matchingFile, skipTypeDefFiles)) {
continue;
}

if (matchingFile != null) {
try {
inputFile = ctx.fileSystem().inputFile(ctx.fileSystem().predicates().is(matchingFile));
}
catch (IllegalArgumentException e) {
LOG.error("Failed to resolve " + filePath + " to a single path", e);
continue;
}
}

if (matchingFile == null) {
LOG.warn("TsLint reported issues against a file that wasn't sent to it - will be ignored: " + filePath);
if (inputFile == null) {
LOG.warn("TsLint reported issues against a file that isn't in the analysis set - will be ignored: " + filePath);
continue;
}
else {
LOG.debug("Handling TsLint output for '" + filePath + "' reporting against '" + inputFile.absolutePath() + "'");
}

for (TsLintIssue issue : batchIssues) {
// Make sure the rule we're violating is one we recognise - if not, we'll
Expand All @@ -121,13 +138,17 @@ else if (config.getConfigFile() == null && config.getPathToTsConfig() == null) {
NewIssueLocation newIssueLocation =
newIssue
.newLocation()
.on(matchingFile)
.on(inputFile)
.message(issue.getFailure())
.at(matchingFile.selectLine(issue.getStartPosition().getLine() + 1));
.at(inputFile.selectLine(issue.getStartPosition().getLine() + 1));

newIssue.at(newIssueLocation);
newIssue.save();
}
}
}

private boolean shouldSkipFile(File f, boolean skipTypeDefFiles) {
return skipTypeDefFiles && f.getName().toLowerCase().endsWith("." + TypeScriptLanguage.LANGUAGE_DEFINITION_EXTENSION);
}
}

0 comments on commit 34ce5b5

Please sign in to comment.