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