use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class UastGeneratorWrapperTest method test.
@Test
void test() throws Exception {
SensorContextTester sensorContext = SensorContextTester.create(workDir);
sensorContext.fileSystem().setWorkDir(workDir.toPath());
UastGeneratorWrapper generator = new UastGeneratorWrapper(sensorContext);
ByteArrayInputStream in = new ByteArrayInputStream("package main\nfunc foo() {}".getBytes(StandardCharsets.UTF_8));
UastNode uast = generator.createUast(in);
assertThat(uast.joinTokens()).isEqualTo("package main\n" + "func foo() {}");
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class LiteralLikeTest method multiple_literal_children.
@Test
void multiple_literal_children() throws Exception {
UastNode literal = Uast.from(new StringReader("{ \"kinds\": [], " + "\"children\": [" + "{ \"kinds\": [\"LITERAL\"], \"token\": {\"value\": \"foo\" , \"line\": 1, \"column\": 1} }," + "{ \"kinds\": [\"LITERAL\"], \"token\": {\"value\": \"bar\" , \"line\": 1, \"column\": 1 } }" + "]" + "}"));
LiteralLike literalLike = LiteralLike.from(literal);
assertThat(literalLike).isNull();
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class LiteralLikeTest method test.
@Test
void test() throws Exception {
UastNode literal = Uast.from(new StringReader("{ \"kinds\": [\"LITERAL\"], \"token\": {\"value\": \"foo\" , \"line\": 1, \"column\": 1} }"));
LiteralLike literalLike = LiteralLike.from(literal);
assertThat(literalLike).isNotNull();
assertThat(literalLike.value()).isEqualTo("foo");
UastNode node = new UastNode(emptySet(), "", null, singletonList(literal));
literalLike = LiteralLike.from(node);
assertThat(literalLike).isNotNull();
assertThat(literalLike.value()).isEqualTo("foo");
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class CpdVisitor method scan.
public void scan(UastNode node) {
if (node.kinds.contains(UastNode.Kind.COMMENT) || node.kinds.contains(UastNode.Kind.EOF)) {
return;
}
UastNode.Token token = node.token;
if (token != null) {
String text = token.value;
if (node.kinds.contains(UastNode.Kind.LITERAL)) {
text = "LITERAL";
}
cpdTokens.addToken(newRange(inputFile, token), text);
}
for (UastNode child : node.children) {
scan(child);
}
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class GoSensor method execute.
@Override
public void execute(SensorContext context) {
Engine ruleEngine = new Engine(checks.all());
UastGeneratorWrapper uastGenerator;
try {
uastGenerator = new UastGeneratorWrapper(context);
} catch (Exception e) {
LOG.error("Error initializing UAST generator", e);
return;
}
List<InputFile> failedFiles = new ArrayList<>();
for (InputFile inputFile : getInputFiles(context)) {
try {
UastNode uast = uastGenerator.createUast(inputFile.inputStream());
// FIXME currently *_test.go files are MAIN and not TEST, see issue #140
if (inputFile.type() == InputFile.Type.MAIN) {
Engine.ScanResult scanResult = ruleEngine.scan(uast, inputFile);
scanResult.issues.forEach(issue -> reportIssue(issue, context, inputFile));
saveMetrics(scanResult.metrics, context, inputFile);
saveCpdTokens(uast, context, inputFile);
}
saveHighlighting(uast, context, inputFile);
} catch (Exception e) {
failedFiles.add(inputFile);
LOG.debug("Error analyzing file " + inputFile.toString(), e);
}
}
if (!failedFiles.isEmpty()) {
String failedFilesAsString = failedFiles.stream().map(InputFile::toString).collect(Collectors.joining("\n"));
LOG.error("Failed to analyze {} file(s). Turn on debug message to see the details. Failed files:\n{}", failedFiles.size(), failedFilesAsString);
}
saveCoverageReports(context, GoCoverageReport.GoContext.DEFAULT);
}
Aggregations