Search in sources :

Example 26 with UastNode

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() {}");
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) ByteArrayInputStream(java.io.ByteArrayInputStream) UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 27 with UastNode

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();
}
Also used : StringReader(java.io.StringReader) UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 28 with UastNode

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");
}
Also used : StringReader(java.io.StringReader) UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 29 with UastNode

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);
    }
}
Also used : UastNode(org.sonar.uast.UastNode)

Example 30 with UastNode

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);
}
Also used : ArrayList(java.util.ArrayList) UastNode(org.sonar.uast.UastNode) Engine(org.sonar.commonruleengine.Engine) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

UastNode (org.sonar.uast.UastNode)31 Test (org.junit.jupiter.api.Test)16 StringReader (java.io.StringReader)10 InputFile (org.sonar.api.batch.fs.InputFile)5 ArrayList (java.util.ArrayList)4 List (java.util.List)2 Set (java.util.Set)2 BinaryExpressionLike (org.sonar.uast.helpers.BinaryExpressionLike)2 CaseLike (org.sonar.uast.helpers.CaseLike)2 IfLike (org.sonar.uast.helpers.IfLike)2 SwitchLike (org.sonar.uast.helpers.SwitchLike)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1