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)));
}
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);
}
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());
}
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;
}
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");
}
}
Aggregations