Search in sources :

Example 1 with AssignmentExpression

use of org.sonar.plugins.python.api.tree.AssignmentExpression in project sonar-python by SonarSource.

the class PythonTreeMakerTest method assignement_expression.

@Test
public void assignement_expression() {
    setRootRule(PythonGrammar.NAMED_EXPR_TEST);
    AstNode astNode = p.parse("b := 12");
    Expression expression = treeMaker.expression(astNode);
    assertThat(expression.is(Kind.ASSIGNMENT_EXPRESSION)).isTrue();
    AssignmentExpression assignmentExpression = (AssignmentExpression) expression;
    Name name = assignmentExpression.lhsName();
    Token walrus = assignmentExpression.operator();
    Expression walrusExpression = assignmentExpression.expression();
    assertThat(name.name()).isEqualTo("b");
    assertThat(walrus.value()).isEqualTo(":=");
    assertThat(walrusExpression.is(Kind.NUMERIC_LITERAL)).isTrue();
    assertThat(((NumericLiteral) walrusExpression).valueAsString()).isEqualTo("12");
    assertThat(assignmentExpression.children()).containsExactly(name, walrus, walrusExpression);
    setRootRule(PythonGrammar.IF_STMT);
    astNode = p.parse("if a or (b := foo()):\n" + "  print(b)");
    IfStatement ifStatement = treeMaker.ifStatement(astNode);
    BinaryExpression condition = (BinaryExpression) ifStatement.condition();
    ParenthesizedExpression parenthesized = ((ParenthesizedExpression) condition.rightOperand());
    assignmentExpression = (AssignmentExpression) parenthesized.expression();
    name = assignmentExpression.lhsName();
    walrus = assignmentExpression.operator();
    walrusExpression = assignmentExpression.expression();
    assertThat(name.name()).isEqualTo("b");
    assertThat(walrus.value()).isEqualTo(":=");
    assertThat(walrusExpression.is(Kind.CALL_EXPR)).isTrue();
    Expression callee = ((CallExpression) walrusExpression).callee();
    assertThat(callee.is(Kind.NAME)).isTrue();
    assertThat(((Name) callee).name()).isEqualTo("foo");
    assertThat(assignmentExpression.children()).containsExactly(name, walrus, walrusExpression);
    setRootRule(PythonGrammar.EXPR);
    astNode = p.parse("foo(a:=42)");
    expression = treeMaker.expression(astNode);
    assertThat(expression.is(Kind.CALL_EXPR)).isTrue();
    Expression argumentExpression = ((RegularArgument) ((CallExpression) expression).arguments().get(0)).expression();
    assertThat(argumentExpression.is(Kind.ASSIGNMENT_EXPRESSION)).isTrue();
    assignmentExpression = (AssignmentExpression) argumentExpression;
    assertThat(assignmentExpression.lhsName().name()).isEqualTo("a");
    assertThat(assignmentExpression.expression().is(Kind.NUMERIC_LITERAL)).isTrue();
    setRootRule(PythonGrammar.NAMED_EXPR_TEST);
    try {
        astNode = p.parse("a.b := 12");
        treeMaker.expression(astNode);
        fail("Expected RecognitionException");
    } catch (RecognitionException e) {
        assertThat(e.getLine()).isEqualTo(1);
    }
}
Also used : ParenthesizedExpression(org.sonar.plugins.python.api.tree.ParenthesizedExpression) IfStatement(org.sonar.plugins.python.api.tree.IfStatement) AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) NumericLiteral(org.sonar.plugins.python.api.tree.NumericLiteral) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) 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) Token(org.sonar.plugins.python.api.tree.Token) RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) AstNode(com.sonar.sslr.api.AstNode) RecognitionException(com.sonar.sslr.api.RecognitionException) 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 2 with AssignmentExpression

use of org.sonar.plugins.python.api.tree.AssignmentExpression in project sonar-python by SonarSource.

the class PythonTreeMakerTest method subscription_expressions.

@Test
public void subscription_expressions() {
    setRootRule(PythonGrammar.TEST);
    SubscriptionExpression expr = (SubscriptionExpression) parse("x[a]", treeMaker::expression);
    assertThat(expr.getKind()).isEqualTo(Tree.Kind.SUBSCRIPTION);
    assertThat(((Name) expr.object()).name()).isEqualTo("x");
    assertThat(((Name) expr.subscripts().expressions().get(0)).name()).isEqualTo("a");
    assertThat(expr.leftBracket().value()).isEqualTo("[");
    assertThat(expr.rightBracket().value()).isEqualTo("]");
    assertThat(expr.children()).hasSize(4);
    SubscriptionExpression multipleSubscripts = (SubscriptionExpression) parse("x[a, 42]", treeMaker::expression);
    ExpressionList subscripts = multipleSubscripts.subscripts();
    assertThat(subscripts.children()).hasSize(3);
    assertThat(subscripts.children().get(1)).isSameAs(subscripts.commas().get(0));
    assertThat(subscripts.expressions()).extracting(Tree::getKind).containsExactly(Tree.Kind.NAME, Tree.Kind.NUMERIC_LITERAL);
    SubscriptionExpression subscriptionExpression = (SubscriptionExpression) parse("a[b:=1]", treeMaker::expression);
    assertThat(subscriptionExpression.subscripts().expressions()).hasSize(1);
    AssignmentExpression assignmentExpression = ((AssignmentExpression) subscriptionExpression.subscripts().expressions().get(0));
    assertThat(assignmentExpression.lhsName().name()).isEqualTo("b");
    assertThat(((NumericLiteral) assignmentExpression.expression()).valueAsLong()).isEqualTo(1);
}
Also used : AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) NumericLiteral(org.sonar.plugins.python.api.tree.NumericLiteral) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) ExpressionList(org.sonar.plugins.python.api.tree.ExpressionList) 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 3 with AssignmentExpression

use of org.sonar.plugins.python.api.tree.AssignmentExpression in project sonar-python by SonarSource.

the class BuiltinShadowingAssignmentCheck method checkAssignmentExpression.

private void checkAssignmentExpression(SubscriptionContext ctx) {
    AssignmentExpression assignmentExpression = (AssignmentExpression) ctx.syntaxNode();
    Name lhsName = assignmentExpression.lhsName();
    if (shouldReportIssue(lhsName)) {
        raiseIssueForNonGlobalVariable(ctx, lhsName);
    }
}
Also used : AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) Name(org.sonar.plugins.python.api.tree.Name)

Example 4 with AssignmentExpression

use of org.sonar.plugins.python.api.tree.AssignmentExpression in project sonar-python by SonarSource.

the class ConfusingWalrusCheck method checkAssignmentExpression.

private static void checkAssignmentExpression(SubscriptionContext ctx) {
    AssignmentExpression assignmentExpression = (AssignmentExpression) ctx.syntaxNode();
    Optional<Tree> parentTree = Optional.ofNullable(TreeUtils.firstAncestor(assignmentExpression, a -> !a.is(Tree.Kind.PARENTHESIZED)));
    parentTree.ifPresent(parent -> {
        if (parent.is(Tree.Kind.ASSIGNMENT_STMT)) {
            ctx.addIssue(assignmentExpression, MESSAGE);
        }
        if (parent.is(Tree.Kind.EXPRESSION_STMT)) {
            ctx.addIssue(assignmentExpression, MESSAGE);
        }
    });
}
Also used : RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) StringElement(org.sonar.plugins.python.api.tree.StringElement) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) ArrayList(java.util.ArrayList) ArgList(org.sonar.plugins.python.api.tree.ArgList) TreeUtils(org.sonar.python.tree.TreeUtils) List(java.util.List) FormattedExpression(org.sonar.plugins.python.api.tree.FormattedExpression) Argument(org.sonar.plugins.python.api.tree.Argument) Optional(java.util.Optional) Tree(org.sonar.plugins.python.api.tree.Tree) Rule(org.sonar.check.Rule) BaseTreeVisitor(org.sonar.plugins.python.api.tree.BaseTreeVisitor) Parameter(org.sonar.plugins.python.api.tree.Parameter) AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) Tree(org.sonar.plugins.python.api.tree.Tree)

Example 5 with AssignmentExpression

use of org.sonar.plugins.python.api.tree.AssignmentExpression in project sonar-python by SonarSource.

the class PythonTreeMaker method argument.

public Argument argument(AstNode astNode) {
    AstNode compFor = astNode.getFirstChild(PythonGrammar.COMP_FOR);
    if (compFor != null) {
        Expression expression = expression(astNode.getFirstChild());
        ComprehensionExpression comprehension = new ComprehensionExpressionImpl(Tree.Kind.GENERATOR_EXPR, null, expression, compFor(compFor), null);
        return new RegularArgumentImpl(comprehension);
    }
    AstNode walrusOperator = astNode.getFirstChild(PythonPunctuator.WALRUS_OPERATOR);
    if (walrusOperator != null) {
        AssignmentExpression assignmentExpression = (AssignmentExpression) assignmentExpression(astNode);
        return new RegularArgumentImpl(assignmentExpression);
    }
    AstNode assign = astNode.getFirstChild(PythonPunctuator.ASSIGN);
    Token star = astNode.getFirstChild(PythonPunctuator.MUL) == null ? null : toPyToken(astNode.getFirstChild(PythonPunctuator.MUL).getToken());
    if (star == null) {
        star = astNode.getFirstChild(PythonPunctuator.MUL_MUL) == null ? null : toPyToken(astNode.getFirstChild(PythonPunctuator.MUL_MUL).getToken());
    }
    Expression arg = expression(astNode.getLastChild(PythonGrammar.TEST));
    if (assign != null) {
        // Keyword in argument list must be an identifier.
        AstNode nameNode = astNode.getFirstChild(PythonGrammar.TEST).getFirstChild(PythonGrammar.ATOM).getFirstChild(PythonGrammar.NAME);
        return new RegularArgumentImpl(name(nameNode), toPyToken(assign.getToken()), arg);
    }
    return star == null ? new RegularArgumentImpl(arg) : new UnpackingExpressionImpl(star, arg);
}
Also used : AssignmentExpression(org.sonar.plugins.python.api.tree.AssignmentExpression) 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) ComprehensionExpression(org.sonar.plugins.python.api.tree.ComprehensionExpression) AstNode(com.sonar.sslr.api.AstNode)

Aggregations

AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)6 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)4 Name (org.sonar.plugins.python.api.tree.Name)4 AstNode (com.sonar.sslr.api.AstNode)3 AliasedName (org.sonar.plugins.python.api.tree.AliasedName)3 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)3 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)3 Expression (org.sonar.plugins.python.api.tree.Expression)3 ImportName (org.sonar.plugins.python.api.tree.ImportName)3 LambdaExpression (org.sonar.plugins.python.api.tree.LambdaExpression)3 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)3 Token (org.sonar.plugins.python.api.tree.Token)3 YieldExpression (org.sonar.plugins.python.api.tree.YieldExpression)3 Test (org.junit.Test)2 NumericLiteral (org.sonar.plugins.python.api.tree.NumericLiteral)2 RegularArgument (org.sonar.plugins.python.api.tree.RegularArgument)2 SubscriptionExpression (org.sonar.plugins.python.api.tree.SubscriptionExpression)2 RuleTest (org.sonar.python.parser.RuleTest)2 RecognitionException (com.sonar.sslr.api.RecognitionException)1 ArrayList (java.util.ArrayList)1