Search in sources :

Example 21 with UastNode

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

the class AssignmentLikeTest method test_malformed.

@Test
void test_malformed() {
    UastNode target = node(UastNode.Kind.ASSIGNMENT_TARGET);
    UastNode operator = node(UastNode.Kind.ASSIGNMENT_OPERATOR);
    UastNode value = node(UastNode.Kind.ASSIGNMENT_VALUE);
    assertNull(AssignmentLike.from(node(UastNode.Kind.ASSIGNMENT)));
    assertNull(AssignmentLike.from(node(UastNode.Kind.ASSIGNMENT, target, value)));
    assertNull(AssignmentLike.from(node(UastNode.Kind.ASSIGNMENT, target, operator)));
    assertNull(AssignmentLike.from(node(UastNode.Kind.ASSIGNMENT, operator, value)));
    UastNode targetList = node(UastNode.Kind.ASSIGNMENT_TARGET_LIST);
    UastNode valueList = node(UastNode.Kind.ASSIGNMENT_VALUE_LIST);
    assertNull(AssignmentLike.from(node(UastNode.Kind.ASSIGNMENT, target, operator, valueList)));
    assertNull(AssignmentLike.from(node(UastNode.Kind.ASSIGNMENT, targetList, operator, value)));
}
Also used : UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 22 with UastNode

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

the class AssignmentLikeTest method test_multipe_malformed.

@Test
void test_multipe_malformed() {
    UastNode target = node(UastNode.Kind.ASSIGNMENT_TARGET_LIST);
    UastNode operator = node(UastNode.Kind.ASSIGNMENT_OPERATOR);
    UastNode value = node(UastNode.Kind.ASSIGNMENT_VALUE_LIST, node(UastNode.Kind.ASSIGNMENT_VALUE));
    UastNode assignment = node(UastNode.Kind.ASSIGNMENT, target, operator, value);
    AssignmentLike assignmentLike = AssignmentLike.from(assignment);
    assertEquals(assignmentLike.node(), assignment);
    assertEquals(assignmentLike.target(), target);
    assertEquals(assignmentLike.operator(), operator);
    assertEquals(assignmentLike.value(), value);
    assertTrue(assignmentLike.isMultiple());
    List<AssignmentLike> assignmentsTuples = assignmentLike.assignmentsTuples();
    // not the same number of target and values
    assertTrue(assignmentsTuples.isEmpty());
    // same instance, not recomputed
    assertTrue(assignmentLike.assignmentsTuples() == assignmentsTuples);
}
Also used : UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 23 with UastNode

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

the class AssignmentLikeTest method test_multipe.

@Test
void test_multipe() {
    UastNode t1 = node(UastNode.Kind.ASSIGNMENT_TARGET);
    UastNode t2 = node(UastNode.Kind.ASSIGNMENT_TARGET);
    UastNode target = node(UastNode.Kind.ASSIGNMENT_TARGET_LIST, t1, t2);
    UastNode operator = node(UastNode.Kind.ASSIGNMENT_OPERATOR);
    UastNode v1 = node(UastNode.Kind.ASSIGNMENT_VALUE);
    UastNode v2 = node(UastNode.Kind.ASSIGNMENT_VALUE);
    UastNode value = node(UastNode.Kind.ASSIGNMENT_VALUE_LIST, v1, v2);
    UastNode assignment = node(UastNode.Kind.ASSIGNMENT, target, operator, value);
    AssignmentLike assignmentLike = AssignmentLike.from(assignment);
    assertEquals(assignmentLike.node(), assignment);
    assertEquals(assignmentLike.target(), target);
    assertEquals(assignmentLike.operator(), operator);
    assertEquals(assignmentLike.value(), value);
    assertTrue(assignmentLike.isMultiple());
    List<AssignmentLike> assignmentsTuples = assignmentLike.assignmentsTuples();
    assertFalse(assignmentsTuples.isEmpty());
    assertEquals(assignmentsTuples.size(), 2);
    // same instance, not recomputed
    assertTrue(assignmentLike.assignmentsTuples() == assignmentsTuples);
    AssignmentLike childAssignment1 = assignmentsTuples.get(0);
    // shared with parent multiple assignment
    assertEquals(childAssignment1.node(), assignment);
    assertEquals(childAssignment1.operator(), operator);
    // assignment is a single-assignment
    assertEquals(childAssignment1.target(), t1);
    assertEquals(childAssignment1.value(), v1);
    assertFalse(childAssignment1.isMultiple());
    AssignmentLike childAssignment2 = assignmentsTuples.get(1);
    // shared with parent multiple assignment
    assertEquals(childAssignment2.node(), assignment);
    assertEquals(childAssignment2.operator(), operator);
    // assignment is a single-assignment
    assertEquals(childAssignment2.target(), t2);
    assertEquals(childAssignment2.value(), v2);
    assertFalse(childAssignment2.isMultiple());
}
Also used : UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 24 with UastNode

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

the class MetricsVisitorTest method getMetrics.

private Metrics getMetrics(String source) throws IOException {
    Engine engine = new Engine(Collections.emptyList());
    UastNode node = Uast.from(new StringReader(source));
    InputFile inputFile = TestInputFileBuilder.create(".", "foo.go").setType(InputFile.Type.MAIN).build();
    return engine.scan(node, inputFile).metrics;
}
Also used : StringReader(java.io.StringReader) UastNode(org.sonar.uast.UastNode) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 25 with UastNode

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

the class RedundantBooleanLiteralCheck method visitNode.

@Override
public void visitNode(UastNode node) {
    BinaryExpressionLike binExpr = BinaryExpressionLike.from(node);
    if (binExpr == null) {
        return;
    }
    Optional<UastNode> booleanLiteralOperand = findBooleanLiteralOperand(binExpr);
    if (binExpr.operator().is(UastNode.Kind.OPERATOR_EQUAL, UastNode.Kind.OPERATOR_NOT_EQUAL, UastNode.Kind.OPERATOR_LOGICAL_AND, UastNode.Kind.OPERATOR_LOGICAL_OR) && booleanLiteralOperand.isPresent()) {
        reportIssue(booleanLiteralOperand.get(), "Remove this redundant boolean literal");
    }
}
Also used : UastNode(org.sonar.uast.UastNode) BinaryExpressionLike(org.sonar.uast.helpers.BinaryExpressionLike)

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