Search in sources :

Example 1 with UastNode

use of org.sonar.uast.UastNode in project sonar-go by SonarSource.

the class LiteralLikeTest method literal_nested_as_only_child.

@Test
void literal_nested_as_only_child() throws Exception {
    UastNode literal = Uast.from(new StringReader("{ \"kinds\": [], " + "\"children\": [{ \"kinds\": [\"LITERAL\"], \"token\": {\"value\": \"foo\" , \"line\": 1, \"column\": 1 } }]" + "}"));
    LiteralLike literalLike = LiteralLike.from(literal);
    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 2 with UastNode

use of org.sonar.uast.UastNode in project sonar-go by SonarSource.

the class UastGeneratorWrapper method createUast.

UastNode createUast(InputStream source) throws IOException, InterruptedException {
    Process process = processBuilder.start();
    errorConsumer.consumeStream(process.getErrorStream(), LOG::debug);
    try (OutputStream out = process.getOutputStream();
        InputStream in = process.getInputStream()) {
        copy(source, process.getOutputStream());
        out.close();
        UastNode uastNode = Uast.from(new InputStreamReader(in, StandardCharsets.UTF_8));
        boolean exited = process.waitFor(5, TimeUnit.SECONDS);
        if (exited && process.exitValue() != 0) {
            throw new IllegalStateException("Parser returned non-zero exit value: " + process.exitValue());
        }
        if (process.isAlive()) {
            process.destroyForcibly();
            throw new IllegalStateException("Took too long to parse. External process killed forcibly");
        }
        return uastNode;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) UastNode(org.sonar.uast.UastNode)

Example 3 with UastNode

use of org.sonar.uast.UastNode in project sonar-go by SonarSource.

the class CpdVisitorTest method test.

@Test
void test() throws IOException {
    String filename = "lets.go";
    String code = readTestResource(getClass(), filename);
    String codeJson = readTestResource(getClass(), filename + ".uast.json");
    InputFile inputFile = createInputFile("lets.go", code);
    sensorContext.fileSystem().add(inputFile);
    CpdVisitor cpdVisitor = new CpdVisitor(sensorContext, inputFile);
    UastNode node = Uast.from(new StringReader(codeJson));
    cpdVisitor.scan(node);
    cpdVisitor.save();
    List<TokensLine> tokensLines = sensorContext.cpdTokens("module:" + inputFile.filename());
    assertThat(tokensLines).isNotNull().hasSize(5);
    assertThat(tokensLines).extracting("value").isEqualTo(Arrays.asList("packagemain", "funcfun()string{", "a:=LITERAL", "returna", "}"));
    assertThat(tokensLines).extracting("startLine").isEqualTo(Arrays.asList(1, 3, 4, 5, 6));
}
Also used : StringReader(java.io.StringReader) TokensLine(org.sonar.duplications.internal.pmd.TokensLine) UastNode(org.sonar.uast.UastNode) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.jupiter.api.Test)

Example 4 with UastNode

use of org.sonar.uast.UastNode in project sonar-go by SonarSource.

the class TooManyParametersCheck method visitNode.

@Override
public void visitNode(UastNode node) {
    FunctionLike function = FunctionLike.from(node);
    if (function == null) {
        return;
    }
    List<UastNode> parameters = function.parameters();
    int parameterCount = 0;
    for (UastNode parameter : parameters) {
        List<UastNode> identifiers = new ArrayList<>();
        parameter.getDescendants(UastNode.Kind.IDENTIFIER, identifiers::add, UastNode.Kind.TYPE);
        parameterCount += identifiers.size();
    }
    if (parameterCount > maximum) {
        reportIssue(function.name(), String.format("Function has %d parameters, which is more than %d authorized.", parameterCount, maximum));
    }
}
Also used : ArrayList(java.util.ArrayList) UastNode(org.sonar.uast.UastNode) FunctionLike(org.sonar.uast.helpers.FunctionLike)

Example 5 with UastNode

use of org.sonar.uast.UastNode in project sonar-go by SonarSource.

the class WrongAssignmentOperatorCheck method visitNode.

@Override
public void visitNode(UastNode node) {
    AssignmentLike assignmentLike = AssignmentLike.from(node);
    if (assignmentLike == null) {
        return;
    }
    if (assignmentLike.isMultiple()) {
        List<AssignmentLike> couples = assignmentLike.assignmentsTuples();
        if (couples.size() != 1) {
            // not relevant for multiple assignment form
            return;
        }
        assignmentLike = couples.get(0);
    }
    UastNode.Token variableLastToken = assignmentLike.target().lastToken();
    UastNode operator = assignmentLike.operator();
    UastNode expression = assignmentLike.value();
    UastNode.Token expressionFirstToken = expression.firstToken();
    if (noSpacingBetween(operator.lastToken(), expressionFirstToken) && spacingBetween(variableLastToken, operator.firstToken()) && expression.is(UastNode.Kind.UNARY_MINUS, UastNode.Kind.UNARY_PLUS, UastNode.Kind.LOGICAL_COMPLEMENT)) {
        String msg;
        if (expression.is(UastNode.Kind.LOGICAL_COMPLEMENT)) {
            msg = "Add a space between \"=\" and \"!\" to avoid confusion.";
        } else {
            msg = String.format("Was \"%s=\" meant instead?", expressionFirstToken.value);
        }
        reportIssue(assignmentLike.node(), msg);
    }
}
Also used : AssignmentLike(org.sonar.uast.helpers.AssignmentLike) UastNode(org.sonar.uast.UastNode)

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