Search in sources :

Example 16 with UastNode

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

the class IfLikeTest method must_have_condition.

@Test
void must_have_condition() 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}}" + "] }"));
    IfLike ifLike = IfLike.from(node);
    assertThat(ifLike).isNotNull();
    assertThat(ifLike.ifKeyword().joinTokens()).isEqualTo("if");
    assertThat(ifLike.condition().joinTokens()).isEqualTo("cond");
    assertThat(ifLike.thenNode().joinTokens()).isEqualTo("statement1");
    node = Uast.from(new StringReader("{ kinds: ['IF'] }"));
    assertThat(IfLike.from(node)).isNull();
}
Also used : StringReader(java.io.StringReader) UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 17 with UastNode

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

the class TestUtils method checkRuleOnGo.

public static void checkRuleOnGo(Check check, String filename) throws IOException {
    Path testFile = testFile("go", check.getClass(), filename);
    UastNode uast = goUast(testFile);
    checkRule(check, testFile, uast);
}
Also used : Path(java.nio.file.Path) UastNode(org.sonar.uast.UastNode)

Example 18 with UastNode

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

the class NestedSwitchCheck method checkNested.

public void checkNested(UastNode nestedSwitchNode) {
    if (!reported.contains(nestedSwitchNode)) {
        UastNode switchKeyword = SwitchLike.from(nestedSwitchNode).switchKeyword();
        reportIssue(switchKeyword, "Refactor the code to eliminate this nested \"switch\".");
        reported.add(nestedSwitchNode);
    }
}
Also used : UastNode(org.sonar.uast.UastNode)

Example 19 with UastNode

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

the class NoIdenticalConditionsCheck method handleIf.

private void handleIf(UastNode node) {
    IfLike ifLike = IfLike.from(node);
    if (ifLike == null) {
        return;
    }
    UastNode condition = ifLike.condition();
    IfLike elseIf = ifLike.elseIf();
    while (elseIf != null) {
        checkConditions(elseIf.condition(), condition);
        elseIf = elseIf.elseIf();
    }
}
Also used : IfLike(org.sonar.uast.helpers.IfLike) UastNode(org.sonar.uast.UastNode)

Example 20 with UastNode

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

the class NoIdenticalConditionsCheck method handleSwitch.

private void handleSwitch(UastNode node) {
    SwitchLike switchLike = SwitchLike.from(node);
    if (switchLike == null) {
        return;
    }
    List<UastNode> caseNodes = switchLike.caseNodes();
    List<UastNode> allConditions = new ArrayList<>();
    for (UastNode caseNode : caseNodes) {
        CaseLike caseLike = CaseLike.from(caseNode);
        List<UastNode> conditions = caseLike.conditions();
        for (UastNode condition : conditions) {
            for (UastNode prevCondition : allConditions) {
                checkConditions(condition, prevCondition);
            }
            allConditions.add(condition);
        }
    }
}
Also used : SwitchLike(org.sonar.uast.helpers.SwitchLike) ArrayList(java.util.ArrayList) UastNode(org.sonar.uast.UastNode) CaseLike(org.sonar.uast.helpers.CaseLike)

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