Search in sources :

Example 11 with UastNode

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);
    }
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) List(java.util.List) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) UastNode(org.sonar.uast.UastNode) Collectors(java.util.stream.Collectors) Check(org.sonar.commonruleengine.checks.Check) Check(org.sonar.commonruleengine.checks.Check) UastNode(org.sonar.uast.UastNode)

Example 12 with UastNode

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);
    }
}
Also used : HashSet(java.util.HashSet) InputFile(org.sonar.api.batch.fs.InputFile) List(java.util.List) Uast.syntacticallyEquivalent(org.sonar.uast.Uast.syntacticallyEquivalent) Set(java.util.Set) UastNode(org.sonar.uast.UastNode) Rule(org.sonar.check.Rule) IfLike(org.sonar.uast.helpers.IfLike) ElseLike(org.sonar.uast.helpers.ElseLike) SwitchLike(org.sonar.uast.helpers.SwitchLike) CaseLike(org.sonar.uast.helpers.CaseLike) SwitchLike(org.sonar.uast.helpers.SwitchLike) UastNode(org.sonar.uast.UastNode)

Example 13 with UastNode

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

Example 14 with UastNode

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

Example 15 with UastNode

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

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