use of org.sonar.plugins.python.api.tree.ConditionalExpression in project sonar-python by SonarSource.
the class UselessStatementCheck method checkConditionalExpression.
public static void checkConditionalExpression(SubscriptionContext ctx) {
ConditionalExpression conditionalExpression = (ConditionalExpression) ctx.syntaxNode();
if (TreeUtils.hasDescendant(conditionalExpression, t -> t.is(Kind.CALL_EXPR))) {
return;
}
checkNode(ctx);
}
use of org.sonar.plugins.python.api.tree.ConditionalExpression in project sonar-python by SonarSource.
the class AllBranchesAreIdenticalCheck method addSecondaryLocations.
private static void addSecondaryLocations(PreciseIssue issue, Expression expression) {
Expression unwrappedExpression = Expressions.removeParentheses(expression);
if (unwrappedExpression.is(Tree.Kind.CONDITIONAL_EXPR)) {
ConditionalExpression conditionalExpression = (ConditionalExpression) unwrappedExpression;
ignoreList.add(conditionalExpression);
addSecondaryLocations(issue, conditionalExpression.trueExpression());
addSecondaryLocations(issue, conditionalExpression.falseExpression());
} else {
issue.secondary(unwrappedExpression, null);
}
}
use of org.sonar.plugins.python.api.tree.ConditionalExpression in project sonar-python by SonarSource.
the class PythonTreeMaker method conditionalExpression.
private ConditionalExpression conditionalExpression(AstNode astNode) {
List<AstNode> children = astNode.getChildren();
Expression trueExpression = expression(children.get(0));
Token ifToken = toPyToken(astNode.getFirstChild(PythonKeyword.IF).getToken());
Expression condition = expression(children.get(2));
Token elseToken = toPyToken(astNode.getFirstChild(PythonKeyword.ELSE).getToken());
Expression falseExpression = expression(children.get(4));
return new ConditionalExpressionImpl(trueExpression, ifToken, condition, elseToken, falseExpression);
}
use of org.sonar.plugins.python.api.tree.ConditionalExpression in project sonar-python by SonarSource.
the class PythonTreeMakerTest method string_interpolation.
@Test
public void string_interpolation() {
setRootRule(PythonGrammar.ATOM);
Expression expr = parseInterpolated("{x}").expression();
assertThat(expr.is(Tree.Kind.NAME)).isTrue();
assertThat(((Name) expr).name()).isEqualTo("x");
expr = parseInterpolated("{x if p != 0 else 0}").expression();
assertThat(expr.is(Kind.CONDITIONAL_EXPR)).isTrue();
assertThat(((ConditionalExpression) expr).trueExpression().is(Kind.NAME)).isTrue();
assertThat(((ConditionalExpression) expr).falseExpression().is(Kind.NUMERIC_LITERAL)).isTrue();
expr = parseInterpolated("{\"}\" + value6}").expression();
assertThat(expr.is(Tree.Kind.PLUS)).isTrue();
expr = parseInterpolated("{\"}}\" + value6}").expression();
assertThat(expr.is(Tree.Kind.PLUS)).isTrue();
expr = parseInterpolated("{{{\"}\" + value6}}}").expression();
assertThat(expr.is(Tree.Kind.PLUS)).isTrue();
Expression exp = parse("F'{{bar}}'", treeMaker::expression);
StringLiteral stringLiteral = (StringLiteral) exp;
assertThat(stringLiteral.stringElements()).hasSize(1);
StringElement elmt = stringLiteral.stringElements().get(0);
assertThat(elmt.isInterpolated()).isTrue();
assertThat(elmt.formattedExpressions()).isEmpty();
exp = parse("f'Some nested {f\"string \\ \n" + "interpolation {x}\"}'", treeMaker::expression);
stringLiteral = (StringLiteral) exp;
assertThat(stringLiteral.stringElements()).hasSize(1);
elmt = stringLiteral.stringElements().get(0);
assertThat(elmt.isInterpolated()).isTrue();
assertThat(elmt.formattedExpressions()).hasSize(1);
StringLiteral interpolation = (StringLiteral) elmt.formattedExpressions().get(0).expression();
StringElement stringElement = interpolation.stringElements().get(0);
assertThat(stringElement.isInterpolated()).isTrue();
Expression nestedInterpolation = stringElement.formattedExpressions().get(0).expression();
assertThat(nestedInterpolation.is(Tree.Kind.NAME)).isTrue();
assertThat(((Name) nestedInterpolation).name()).isEqualTo("x");
assertThat(nestedInterpolation.firstToken().line()).isEqualTo(2);
assertThat(nestedInterpolation.firstToken().column()).isEqualTo(15);
// interpolated expression contains curly braces
stringLiteral = (StringLiteral) parse("f'{ {element for element in [1, 2]} }'", treeMaker::expression);
assertThat(stringLiteral.stringElements()).hasSize(1);
elmt = stringLiteral.stringElements().get(0);
assertThat(elmt.isInterpolated()).isTrue();
assertThat(elmt.formattedExpressions()).hasSize(1);
assertThat(elmt.formattedExpressions().get(0).expression().is(Kind.SET_COMPREHENSION)).isTrue();
stringLiteral = (StringLiteral) parse("f\"{x if p != 0 else 0}\"", treeMaker::expression);
assertThat(stringLiteral.stringElements()).hasSize(1);
elmt = stringLiteral.stringElements().get(0);
assertThat(elmt.isInterpolated()).isTrue();
assertThat(elmt.formattedExpressions()).hasSize(1);
assertThat(elmt.formattedExpressions().get(0).expression().is(Kind.CONDITIONAL_EXPR)).isTrue();
}
use of org.sonar.plugins.python.api.tree.ConditionalExpression in project sonar-python by SonarSource.
the class PythonTreeMakerTest method conditional_expression.
@Test
public void conditional_expression() {
setRootRule(PythonGrammar.TEST);
ConditionalExpression tree = (ConditionalExpression) parse("1 if condition else 2", treeMaker::expression);
assertThat(tree.firstToken().value()).isEqualTo("1");
assertThat(tree.lastToken().value()).isEqualTo("2");
assertThat(tree.ifKeyword().value()).isEqualTo("if");
assertThat(tree.elseKeyword().value()).isEqualTo("else");
assertThat(tree.condition().getKind()).isEqualTo(Tree.Kind.NAME);
assertThat(tree.trueExpression().getKind()).isEqualTo(Tree.Kind.NUMERIC_LITERAL);
assertThat(tree.falseExpression().getKind()).isEqualTo(Tree.Kind.NUMERIC_LITERAL);
assertThat(tree.children()).containsExactly(tree.trueExpression(), tree.ifKeyword(), tree.condition(), tree.elseKeyword(), tree.falseExpression());
ConditionalExpression nestedConditionalExpressionTree = (ConditionalExpression) parse("1 if x else 2 if y else 3", treeMaker::expression);
assertThat(nestedConditionalExpressionTree.condition().getKind()).isEqualTo(Tree.Kind.NAME);
assertThat(nestedConditionalExpressionTree.trueExpression().getKind()).isEqualTo(Tree.Kind.NUMERIC_LITERAL);
Expression nestedConditionalExpr = nestedConditionalExpressionTree.falseExpression();
assertThat(nestedConditionalExpr.firstToken().value()).isEqualTo("2");
assertThat(nestedConditionalExpr.lastToken().value()).isEqualTo("3");
assertThat(nestedConditionalExpr.getKind()).isEqualTo(Tree.Kind.CONDITIONAL_EXPR);
}
Aggregations