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