Search in sources :

Example 1 with RegexContext

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);
}
Also used : PythonVisitorContext(org.sonar.plugins.python.api.PythonVisitorContext) RegexContext(org.sonar.python.regex.RegexContext) FlagSet(org.sonarsource.analyzer.commons.regex.ast.FlagSet) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) RegexContext(org.sonar.python.regex.RegexContext) StringElement(org.sonar.plugins.python.api.tree.StringElement) RegexParseResult(org.sonarsource.analyzer.commons.regex.RegexParseResult) FileInput(org.sonar.plugins.python.api.tree.FileInput) PythonVisitorContext(org.sonar.plugins.python.api.PythonVisitorContext) Test(org.junit.Test)

Example 2 with RegexContext

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));
    }
}
Also used : FlagSet(org.sonarsource.analyzer.commons.regex.ast.FlagSet) RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) RegexParseResult(org.sonarsource.analyzer.commons.regex.RegexParseResult) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) RegexIssueLocation(org.sonarsource.analyzer.commons.regex.RegexIssueLocation) HashMap(java.util.HashMap) FlagSet(org.sonarsource.analyzer.commons.regex.ast.FlagSet) TreeUtils(org.sonar.python.tree.TreeUtils) HashSet(java.util.HashSet) RegexSyntaxElement(org.sonarsource.analyzer.commons.regex.ast.RegexSyntaxElement) Locale(java.util.Locale) Name(org.sonar.plugins.python.api.tree.Name) Map(java.util.Map) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) Expression(org.sonar.plugins.python.api.tree.Expression) Nullable(javax.annotation.Nullable) Expressions(org.sonar.python.checks.Expressions) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) StringElement(org.sonar.plugins.python.api.tree.StringElement) Set(java.util.Set) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) RegexContext(org.sonar.python.regex.RegexContext) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) PythonRegexIssueLocation(org.sonar.python.regex.PythonRegexIssueLocation) List(java.util.List) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) Optional(java.util.Optional) Tree(org.sonar.plugins.python.api.tree.Tree) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Symbol(org.sonar.plugins.python.api.symbols.Symbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) CallExpression(org.sonar.plugins.python.api.tree.CallExpression)

Aggregations

PythonSubscriptionCheck (org.sonar.plugins.python.api.PythonSubscriptionCheck)2 StringElement (org.sonar.plugins.python.api.tree.StringElement)2 RegexContext (org.sonar.python.regex.RegexContext)2 RegexParseResult (org.sonarsource.analyzer.commons.regex.RegexParseResult)2 FlagSet (org.sonarsource.analyzer.commons.regex.ast.FlagSet)2 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1 PythonVisitorContext (org.sonar.plugins.python.api.PythonVisitorContext)1 SubscriptionContext (org.sonar.plugins.python.api.SubscriptionContext)1 Symbol (org.sonar.plugins.python.api.symbols.Symbol)1 BinaryExpression (org.sonar.plugins.python.api.tree.BinaryExpression)1