use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class Engine method visit.
private void visit(UastNode uast) {
metricsVisitor.visitNode(uast);
Set<Check> checks = uast.kinds.stream().flatMap(kind -> engineContext.registeredChecks(kind).stream()).collect(Collectors.toSet());
for (Check check : checks) {
check.visitNode(uast);
}
for (UastNode child : uast.children) {
visit(child);
}
for (Check check : checks) {
check.leaveNode(uast);
}
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class AllBranchesAreIdenticalCheck method handleSwitch.
private void handleSwitch(UastNode node) {
SwitchLike switchLike = SwitchLike.from(node);
if (switchLike == null) {
return;
}
List<UastNode> caseNodes = switchLike.caseNodes();
if (caseNodes.size() < 2) {
return;
}
UastNode firstCase = CaseLike.from(caseNodes.get(0)).body();
if (caseNodes.stream().noneMatch(UastNode.Kind.DEFAULT_CASE)) {
return;
}
boolean allEquivalent = caseNodes.stream().skip(1).map(caseNode -> CaseLike.from(caseNode).body()).allMatch(body -> syntacticallyEquivalent(firstCase, body));
if (allEquivalent) {
reportIssue(switchLike.switchKeyword(), MESSAGE);
}
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class BinaryExpressionLikeTest method test.
@Test
void test() {
UastNode leftOperand = node(UastNode.Kind.IDENTIFIER);
UastNode operator = new UastNode(Collections.emptySet(), "", new UastNode.Token(42, 7, "+"), Collections.emptyList());
UastNode rightOperand = node(UastNode.Kind.IDENTIFIER);
UastNode binaryExpression = node(UastNode.Kind.BINARY_EXPRESSION, leftOperand, operator, rightOperand);
BinaryExpressionLike assignmentLike = BinaryExpressionLike.from(binaryExpression);
assertEquals(assignmentLike.leftOperand(), leftOperand);
assertEquals(assignmentLike.operator(), operator);
assertEquals(assignmentLike.rightOperand(), rightOperand);
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class FunctionLikeTest method test.
@Test
void test() throws Exception {
UastNode node = Uast.from(new StringReader("" + "{\"kinds\": [\"FUNCTION\"], \"children\": [\n" + " {\"kinds\": [\"KEYWORD\"], \"token\": {\"value\":\"func\",\"line\":4,\"column\":1}},\n" + " {\"kinds\": [\"FUNCTION_NAME\",\"IDENTIFIER\"], \"token\": {\"value\":\"conv\",\"line\":4,\"column\":6}},\n" + " {\"children\": [\n" + " {\"kinds\": [\"PARAMETER_LIST\"], \"children\": [\n" + " {\"token\": {\"value\":\"(\",\"line\":4,\"column\":10}},\n" + " {\"children\": [\n" + " {\"children\": [\n" + " {\"kinds\": [\"PARAMETER\",\"IDENTIFIER\"], \"token\": {\"value\":\"a\",\"line\":4,\"column\":11}},\n" + " {\"token\": {\"value\":\",\",\"line\":4,\"column\":12}},\n" + " {\"kinds\": [\"PARAMETER\",\"IDENTIFIER\"], \"token\": {\"value\":\"b\",\"line\":4,\"column\":14}}\n" + " ]},\n" + " {\"kinds\": [\"TYPE\",\"IDENTIFIER\"], \"token\": {\"value\":\"int\",\"line\":4,\"column\":16}}\n" + " ]},\n" + " {\"token\": {\"value\":\")\",\"line\":4,\"column\":20}}\n" + " ]},\n" + " {\"kinds\": [\"RESULT_LIST\"], \"children\": [\n" + " {\"children\": [\n" + " {\"kinds\": [\"TYPE\",\"IDENTIFIER\"], \"token\": {\"value\":\"int\",\"line\":4,\"column\":22}}\n" + " ]}\n" + " ]}\n" + " ]},\n" + " {\"kinds\": [\"BLOCK\"], \"children\": [\n" + " {\"token\": {\"value\":\"{\",\"line\":4,\"column\":26}},\n" + " {\"token\": {\"value\":\"}\",\"line\":6,\"column\":1}}\n" + " ]}\n" + "]}\n"));
FunctionLike functionLike = FunctionLike.from(node);
assertThat(functionLike).isNotNull();
assertThat(functionLike.node().is(UastNode.Kind.FUNCTION)).isTrue();
assertThat(functionLike.name().joinTokens()).isEqualTo("conv");
assertThat(functionLike.parameters().size()).isEqualTo(2);
assertThat(functionLike.body().is(UastNode.Kind.BLOCK)).isTrue();
}
use of org.sonar.uast.UastNode in project sonar-go by SonarSource.
the class IfLikeTest method has_else.
@Test
void has_else() throws Exception {
UastNode node = Uast.from(new StringReader("{ kinds: ['IF'], " + "children: [" + "{kinds: ['IF_KEYWORD'], token: {value: 'if', line: 1, column: 1}}," + "{kinds: ['CONDITION'], token: {value: 'cond', line: 1, column: 1}}," + "{kinds: ['THEN'], token: {value: 'statement1', line: 1, column: 1}}," + "{kinds: ['ELSE_KEYWORD'], token: {value: 'else', line: 1, column: 1}}," + "{kinds: ['ELSE'], token: {value: 'statement2', line: 1, column: 1}}" + "] }"));
IfLike ifLike = IfLike.from(node);
assertThat(ifLike).isNotNull();
ElseLike elseLike = ifLike.elseLike();
assertThat(elseLike).isNotNull();
assertThat(elseLike.elseKeyword().joinTokens()).isEqualTo("else");
assertThat(elseLike.elseNode().joinTokens()).isEqualTo("statement2");
}
Aggregations