Search in sources :

Example 1 with SliceExpression

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

the class OverwrittenCollectionEntryCheck method collectionWrite.

@CheckForNull
private static CollectionWrite collectionWrite(AssignmentStatement assignment, Expression expression) {
    if (expression.is(Kind.SLICE_EXPR)) {
        SliceExpression sliceExpression = (SliceExpression) expression;
        String key = key(sliceExpression.sliceList().children());
        return collectionWrite(assignment, sliceExpression.object(), key, sliceExpression.leftBracket(), sliceExpression.rightBracket());
    } else if (expression.is(Kind.SUBSCRIPTION)) {
        SubscriptionExpression subscription = (SubscriptionExpression) expression;
        String key = key(subscription.subscripts().children());
        return collectionWrite(assignment, subscription.object(), key, subscription.leftBracket(), subscription.rightBracket());
    } else {
        return null;
    }
}
Also used : SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) CheckForNull(javax.annotation.CheckForNull)

Example 2 with SliceExpression

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

the class PythonTreeMakerTest method slice_expressions.

@Test
public void slice_expressions() {
    setRootRule(PythonGrammar.TEST);
    SliceExpression expr = (SliceExpression) parse("x[a:b:c]", treeMaker::expression);
    assertThat(expr.getKind()).isEqualTo(Tree.Kind.SLICE_EXPR);
    assertThat(expr.object().getKind()).isEqualTo(Tree.Kind.NAME);
    assertThat(expr.leftBracket().value()).isEqualTo("[");
    assertThat(expr.rightBracket().value()).isEqualTo("]");
    assertThat(expr.children()).hasSize(4);
    assertThat(expr.sliceList().getKind()).isEqualTo(Tree.Kind.SLICE_LIST);
    assertThat(expr.sliceList().children()).hasSize(1);
    assertThat(expr.sliceList().slices().get(0).getKind()).isEqualTo(Tree.Kind.SLICE_ITEM);
    SliceExpression multipleSlices = (SliceExpression) parse("x[a, b:c, :]", treeMaker::expression);
    List<Tree> slices = multipleSlices.sliceList().slices();
    assertThat(slices).extracting(Tree::getKind).containsExactly(Tree.Kind.NAME, Tree.Kind.SLICE_ITEM, Tree.Kind.SLICE_ITEM);
    assertThat(multipleSlices.sliceList().separators()).extracting(Token::value).containsExactly(",", ",");
}
Also used : SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) Tree(org.sonar.plugins.python.api.tree.Tree) Test(org.junit.Test) RuleTest(org.sonar.python.parser.RuleTest)

Aggregations

SliceExpression (org.sonar.plugins.python.api.tree.SliceExpression)2 CheckForNull (javax.annotation.CheckForNull)1 Test (org.junit.Test)1 SubscriptionExpression (org.sonar.plugins.python.api.tree.SubscriptionExpression)1 Tree (org.sonar.plugins.python.api.tree.Tree)1 RuleTest (org.sonar.python.parser.RuleTest)1