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