use of org.sonar.python.regex.RegexContext in project sonar-python by SonarSource.
the class SubscriptionVisitorTest method test_regex_cache.
@Test
public void test_regex_cache() {
PythonSubscriptionCheck check = new PythonSubscriptionCheck() {
@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.STRING_ELEMENT, ctx -> {
StringElement stringElement = (StringElement) ctx.syntaxNode();
RegexContext regexCtx = (RegexContext) ctx;
RegexParseResult resultWithNoFlags = regexCtx.regexForStringElement(stringElement, new FlagSet());
RegexParseResult resultWithFlags = regexCtx.regexForStringElement(stringElement, new FlagSet(Pattern.MULTILINE));
assertThat(resultWithNoFlags).isNotSameAs(resultWithFlags);
// When we retrieve them again, it will be the same instance retrieved from the cache.
assertThat(resultWithNoFlags).isSameAs(regexCtx.regexForStringElement(stringElement, new FlagSet()));
assertThat(resultWithFlags).isSameAs(regexCtx.regexForStringElement(stringElement, new FlagSet(Pattern.MULTILINE)));
});
}
};
FileInput fileInput = PythonTestUtils.parse("'.*'");
PythonVisitorContext context = new PythonVisitorContext(fileInput, PythonTestUtils.pythonFile("file"), null, null);
SubscriptionVisitor.analyze(Collections.singleton(check), context);
}
use of org.sonar.python.regex.RegexContext in project sonar-python by SonarSource.
the class AbstractRegexCheck method checkCall.
private void checkCall(SubscriptionContext ctx) {
regexContext = (RegexContext) ctx;
CallExpression callExpression = (CallExpression) ctx.syntaxNode();
Symbol calleeSymbol = callExpression.calleeSymbol();
if (calleeSymbol == null || calleeSymbol.fullyQualifiedName() == null) {
return;
}
String functionFqn = calleeSymbol.fullyQualifiedName();
if (functionFqn != null && lookedUpFunctions().containsKey(functionFqn)) {
FlagSet flagSet = getFlagSet(callExpression, functionFqn);
patternArgStringLiteral(callExpression).flatMap(l -> regexForStringLiteral(l, flagSet)).ifPresent(parseResult -> checkRegex(parseResult, callExpression));
}
}
Aggregations