use of org.sonar.plugins.python.api.tree.SliceItem in project sonar-python by SonarSource.
the class OverwrittenCollectionEntryCheck method key.
@CheckForNull
private static String key(Tree tree) {
if (tree.is(Kind.TOKEN)) {
return ((Token) tree).value();
} else if (tree.is(Kind.NUMERIC_LITERAL)) {
return ((NumericLiteral) tree).valueAsString();
} else if (tree.is(Kind.STRING_LITERAL)) {
return Expressions.unescape((StringLiteral) tree);
} else if (tree.is(Kind.NAME)) {
return ((Name) tree).name();
} else if (tree.is(Kind.SLICE_ITEM)) {
SliceItem sliceItem = (SliceItem) tree;
List<String> keyParts = Stream.of(sliceItem.lowerBound(), sliceItem.upperBound(), sliceItem.stride()).map(e -> e == null ? "" : key(e)).collect(Collectors.toList());
return keyParts.contains(null) ? null : String.join(":", keyParts);
} else if (tree.is(Kind.UNARY_MINUS)) {
String nested = key(((UnaryExpression) tree).expression());
return nested == null ? null : ("-" + nested);
}
return null;
}
use of org.sonar.plugins.python.api.tree.SliceItem in project sonar-python by SonarSource.
the class PythonTreeMakerTest method slice.
@Test
public void slice() {
setRootRule(PythonGrammar.SUBSCRIPT);
SliceItem slice = parse("a:b:c", treeMaker::sliceItem);
assertThat(((Name) slice.lowerBound()).name()).isEqualTo("a");
assertThat(((Name) slice.upperBound()).name()).isEqualTo("b");
assertThat(((Name) slice.stride()).name()).isEqualTo("c");
assertThat(slice.boundSeparator().value()).isEqualTo(":");
assertThat(slice.strideSeparator().value()).isEqualTo(":");
assertThat(slice.children()).hasSize(5);
SliceItem trivial = parse(":", treeMaker::sliceItem);
assertThat(trivial.lowerBound()).isNull();
assertThat(trivial.upperBound()).isNull();
assertThat(trivial.stride()).isNull();
assertThat(trivial.strideSeparator()).isNull();
SliceItem lowerBoundOnly = parse("a:", treeMaker::sliceItem);
assertThat(((Name) lowerBoundOnly.lowerBound()).name()).isEqualTo("a");
assertThat(lowerBoundOnly.upperBound()).isNull();
assertThat(lowerBoundOnly.stride()).isNull();
assertThat(lowerBoundOnly.strideSeparator()).isNull();
SliceItem upperBoundOnly = parse(":a", treeMaker::sliceItem);
assertThat(upperBoundOnly.lowerBound()).isNull();
assertThat(((Name) upperBoundOnly.upperBound()).name()).isEqualTo("a");
assertThat(upperBoundOnly.stride()).isNull();
assertThat(upperBoundOnly.strideSeparator()).isNull();
SliceItem strideOnly = parse("::a", treeMaker::sliceItem);
assertThat(strideOnly.lowerBound()).isNull();
assertThat(strideOnly.upperBound()).isNull();
assertThat(((Name) strideOnly.stride()).name()).isEqualTo("a");
assertThat(strideOnly.strideSeparator()).isNotNull();
SliceItem strideContainingOnlyColon = parse("::", treeMaker::sliceItem);
assertThat(strideContainingOnlyColon.lowerBound()).isNull();
assertThat(strideContainingOnlyColon.upperBound()).isNull();
assertThat(strideContainingOnlyColon.strideSeparator()).isNotNull();
}
Aggregations