Search in sources :

Example 1 with SliceItem

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;
}
Also used : NumericLiteral(org.sonar.plugins.python.api.tree.NumericLiteral) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) HasSymbol(org.sonar.plugins.python.api.tree.HasSymbol) HashMap(java.util.HashMap) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) ArrayList(java.util.ArrayList) TreeUtils(org.sonar.python.tree.TreeUtils) Kind(org.sonar.plugins.python.api.tree.Tree.Kind) IssueLocation(org.sonar.plugins.python.api.IssueLocation) Name(org.sonar.plugins.python.api.tree.Name) Map(java.util.Map) Statement(org.sonar.plugins.python.api.tree.Statement) Expression(org.sonar.plugins.python.api.tree.Expression) Nullable(javax.annotation.Nullable) StatementList(org.sonar.plugins.python.api.tree.StatementList) SliceItem(org.sonar.plugins.python.api.tree.SliceItem) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) Token(org.sonar.plugins.python.api.tree.Token) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) Collectors(java.util.stream.Collectors) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) AbstractMap(java.util.AbstractMap) List(java.util.List) Stream(java.util.stream.Stream) SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) Tree(org.sonar.plugins.python.api.tree.Tree) Rule(org.sonar.check.Rule) CheckForNull(javax.annotation.CheckForNull) Symbol(org.sonar.plugins.python.api.symbols.Symbol) Token(org.sonar.plugins.python.api.tree.Token) SliceItem(org.sonar.plugins.python.api.tree.SliceItem) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) CheckForNull(javax.annotation.CheckForNull)

Example 2 with SliceItem

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();
}
Also used : SliceItem(org.sonar.plugins.python.api.tree.SliceItem) 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)

Aggregations

Name (org.sonar.plugins.python.api.tree.Name)2 SliceItem (org.sonar.plugins.python.api.tree.SliceItem)2 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1 Rule (org.sonar.check.Rule)1 IssueLocation (org.sonar.plugins.python.api.IssueLocation)1 PythonSubscriptionCheck (org.sonar.plugins.python.api.PythonSubscriptionCheck)1 SubscriptionContext (org.sonar.plugins.python.api.SubscriptionContext)1 Symbol (org.sonar.plugins.python.api.symbols.Symbol)1 AliasedName (org.sonar.plugins.python.api.tree.AliasedName)1 AssignmentStatement (org.sonar.plugins.python.api.tree.AssignmentStatement)1 Expression (org.sonar.plugins.python.api.tree.Expression)1