Search in sources :

Example 6 with StringElement

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

the class CommentedCodeCheck method isMultilineComment.

private static boolean isMultilineComment(StringLiteral stringLiteral) {
    Tree parent = stringLiteral.parent();
    StringElement firstElement = stringLiteral.stringElements().get(0);
    return firstElement.isTripleQuoted() && parent.is(Tree.Kind.EXPRESSION_STMT);
}
Also used : StringElement(org.sonar.plugins.python.api.tree.StringElement) Tree(org.sonar.plugins.python.api.tree.Tree)

Example 7 with StringElement

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

the class ConfusingWalrusCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.ASSIGNMENT_EXPRESSION, ConfusingWalrusCheck::checkAssignmentExpression);
    context.registerSyntaxNodeConsumer(Tree.Kind.STRING_ELEMENT, ctx -> {
        StringElement stringElement = (StringElement) ctx.syntaxNode();
        for (FormattedExpression formattedExpression : stringElement.formattedExpressions()) {
            checkNestedWalrus(ctx, formattedExpression.expression(), String.format(MOVE_MESSAGE, "interpolated expression"));
        }
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.PARAMETER, ctx -> {
        Parameter parameter = (Parameter) ctx.syntaxNode();
        checkNestedWalrus(ctx, parameter, String.format(MOVE_MESSAGE, "function definition"));
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.ARG_LIST, ctx -> {
        ArgList argList = (ArgList) ctx.syntaxNode();
        if (hasKeywordArguments(argList)) {
            checkNestedWalrus(ctx, argList, String.format(MOVE_MESSAGE, "argument list"));
        }
    });
}
Also used : StringElement(org.sonar.plugins.python.api.tree.StringElement) FormattedExpression(org.sonar.plugins.python.api.tree.FormattedExpression) Parameter(org.sonar.plugins.python.api.tree.Parameter) ArgList(org.sonar.plugins.python.api.tree.ArgList)

Example 8 with StringElement

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

the class BackslashInStringCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.STRING_ELEMENT, ctx -> {
        StringElement pyStringLiteralTree = ((StringElement) ctx.syntaxNode());
        String string = pyStringLiteralTree.value();
        int length = string.length();
        boolean isEscaped = false;
        boolean inPrefix = true;
        boolean isThreeQuotes = length > 5 && "\"\"".equals(string.substring(1, 3));
        for (int i = 0; i < length; i++) {
            char c = string.charAt(i);
            inPrefix = isInPrefix(inPrefix, c);
            if (isRawStringLiteral(inPrefix, c)) {
                return;
            } else {
                if (isEscaped && VALID_ESCAPED_CHARACTERS.indexOf(c) == -1 && !isBackslashedSpaceAfterInlineMarkup(isThreeQuotes, string, i, c)) {
                    ctx.addIssue(pyStringLiteralTree, MESSAGE);
                }
                isEscaped = c == '\\' && !isEscaped;
            }
        }
    });
}
Also used : StringElement(org.sonar.plugins.python.api.tree.StringElement)

Example 9 with StringElement

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

the class PythonTreeMaker method stringLiteral.

private Expression stringLiteral(AstNode astNode) {
    List<StringElement> elements = new ArrayList<>();
    for (AstNode elementNode : astNode.getChildren(PythonTokenType.STRING)) {
        com.sonar.sslr.api.Token token = elementNode.getToken();
        StringElementImpl element = new StringElementImpl(toPyToken(token));
        if (element.isInterpolated()) {
            F_STRING_PARSER.fStringExpressions(token).forEach(expressionNode -> element.addFormattedExpression(formattedExpression(expressionNode)));
        }
        elements.add(element);
    }
    return new StringLiteralImpl(elements);
}
Also used : StringElement(org.sonar.plugins.python.api.tree.StringElement) ArrayList(java.util.ArrayList) AstNode(com.sonar.sslr.api.AstNode)

Example 10 with StringElement

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

the class ClearTextProtocolsCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.STRING_ELEMENT, ctx -> {
        Tree node = ctx.syntaxNode();
        String value = Expressions.unescape((StringElement) node);
        unsafeProtocol(value).map(protocol -> protocol.substring(0, protocol.length() - 3)).ifPresent(protocol -> ctx.addIssue(node, message(protocol)));
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, ctx -> {
        Symbol symbol = ((CallExpression) ctx.syntaxNode()).calleeSymbol();
        isUnsafeLib(symbol).ifPresent(protocol -> ctx.addIssue(ctx.syntaxNode(), message(protocol)));
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.ASSIGNMENT_STMT, ctx -> handleAssignmentStatement((AssignmentStatement) ctx.syntaxNode(), ctx));
}
Also used : Arrays(java.util.Arrays) StringElement(org.sonar.plugins.python.api.tree.StringElement) URISyntaxException(java.net.URISyntaxException) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) HasSymbol(org.sonar.plugins.python.api.tree.HasSymbol) HashMap(java.util.HashMap) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) TreeUtils(org.sonar.python.tree.TreeUtils) List(java.util.List) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) Map(java.util.Map) Optional(java.util.Optional) Expression(org.sonar.plugins.python.api.tree.Expression) Tree(org.sonar.plugins.python.api.tree.Tree) URI(java.net.URI) Pattern(java.util.regex.Pattern) Rule(org.sonar.check.Rule) Nullable(javax.annotation.Nullable) Symbol(org.sonar.plugins.python.api.symbols.Symbol) Expressions(org.sonar.python.checks.Expressions) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) HasSymbol(org.sonar.plugins.python.api.tree.HasSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) Tree(org.sonar.plugins.python.api.tree.Tree) CallExpression(org.sonar.plugins.python.api.tree.CallExpression)

Aggregations

StringElement (org.sonar.plugins.python.api.tree.StringElement)15 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)8 Expression (org.sonar.plugins.python.api.tree.Expression)8 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)7 StringLiteral (org.sonar.plugins.python.api.tree.StringLiteral)7 SubscriptionExpression (org.sonar.plugins.python.api.tree.SubscriptionExpression)7 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)6 AwaitExpression (org.sonar.plugins.python.api.tree.AwaitExpression)6 BinaryExpression (org.sonar.plugins.python.api.tree.BinaryExpression)6 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)6 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)6 DictCompExpression (org.sonar.plugins.python.api.tree.DictCompExpression)6 EllipsisExpression (org.sonar.plugins.python.api.tree.EllipsisExpression)6 InExpression (org.sonar.plugins.python.api.tree.InExpression)6 IsExpression (org.sonar.plugins.python.api.tree.IsExpression)6 LambdaExpression (org.sonar.plugins.python.api.tree.LambdaExpression)6 NoneExpression (org.sonar.plugins.python.api.tree.NoneExpression)6 ParenthesizedExpression (org.sonar.plugins.python.api.tree.ParenthesizedExpression)6 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)6 ReprExpression (org.sonar.plugins.python.api.tree.ReprExpression)6