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