Search in sources :

Example 1 with ConditionalExpression

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);
}
Also used : ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression)

Example 2 with ConditionalExpression

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);
    }
}
Also used : ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression) Expression(org.sonar.plugins.python.api.tree.Expression) ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression)

Example 3 with ConditionalExpression

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);
}
Also used : AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) Expression(org.sonar.plugins.python.api.tree.Expression) FormattedExpression(org.sonar.plugins.python.api.tree.FormattedExpression) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression) LambdaExpression(org.sonar.plugins.python.api.tree.LambdaExpression) Token(org.sonar.plugins.python.api.tree.Token) AstNode(com.sonar.sslr.api.AstNode)

Example 4 with ConditionalExpression

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();
}
Also used : StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) ReprExpression(org.sonar.plugins.python.api.tree.ReprExpression) IsExpression(org.sonar.plugins.python.api.tree.IsExpression) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) EllipsisExpression(org.sonar.plugins.python.api.tree.EllipsisExpression) FormattedExpression(org.sonar.plugins.python.api.tree.FormattedExpression) UnpackingExpression(org.sonar.plugins.python.api.tree.UnpackingExpression) NoneExpression(org.sonar.plugins.python.api.tree.NoneExpression) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) DictCompExpression(org.sonar.plugins.python.api.tree.DictCompExpression) AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) Expression(org.sonar.plugins.python.api.tree.Expression) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) ParenthesizedExpression(org.sonar.plugins.python.api.tree.ParenthesizedExpression) InExpression(org.sonar.plugins.python.api.tree.InExpression) AwaitExpression(org.sonar.plugins.python.api.tree.AwaitExpression) ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression) LambdaExpression(org.sonar.plugins.python.api.tree.LambdaExpression) StringElement(org.sonar.plugins.python.api.tree.StringElement) AliasedName(org.sonar.plugins.python.api.tree.AliasedName) Name(org.sonar.plugins.python.api.tree.Name) ImportName(org.sonar.plugins.python.api.tree.ImportName) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Example 5 with ConditionalExpression

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);
}
Also used : ReprExpression(org.sonar.plugins.python.api.tree.ReprExpression) IsExpression(org.sonar.plugins.python.api.tree.IsExpression) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) EllipsisExpression(org.sonar.plugins.python.api.tree.EllipsisExpression) FormattedExpression(org.sonar.plugins.python.api.tree.FormattedExpression) UnpackingExpression(org.sonar.plugins.python.api.tree.UnpackingExpression) NoneExpression(org.sonar.plugins.python.api.tree.NoneExpression) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) DictCompExpression(org.sonar.plugins.python.api.tree.DictCompExpression) AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) Expression(org.sonar.plugins.python.api.tree.Expression) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) ParenthesizedExpression(org.sonar.plugins.python.api.tree.ParenthesizedExpression) InExpression(org.sonar.plugins.python.api.tree.InExpression) AwaitExpression(org.sonar.plugins.python.api.tree.AwaitExpression) ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression) LambdaExpression(org.sonar.plugins.python.api.tree.LambdaExpression) ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Aggregations

ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)5 Expression (org.sonar.plugins.python.api.tree.Expression)4 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)3 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)3 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)3 LambdaExpression (org.sonar.plugins.python.api.tree.LambdaExpression)3 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)3 YieldExpression (org.sonar.plugins.python.api.tree.YieldExpression)3 Test (org.junit.Test)2 AwaitExpression (org.sonar.plugins.python.api.tree.AwaitExpression)2 BinaryExpression (org.sonar.plugins.python.api.tree.BinaryExpression)2 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)2 DictCompExpression (org.sonar.plugins.python.api.tree.DictCompExpression)2 EllipsisExpression (org.sonar.plugins.python.api.tree.EllipsisExpression)2 InExpression (org.sonar.plugins.python.api.tree.InExpression)2 IsExpression (org.sonar.plugins.python.api.tree.IsExpression)2 NoneExpression (org.sonar.plugins.python.api.tree.NoneExpression)2 ParenthesizedExpression (org.sonar.plugins.python.api.tree.ParenthesizedExpression)2 ReprExpression (org.sonar.plugins.python.api.tree.ReprExpression)2 SliceExpression (org.sonar.plugins.python.api.tree.SliceExpression)2