gjd6640 / sonar-text-plugin

A free and open-source plugin for SonarSource's Sonarqube product that lets you create rules to flag issues in text files.
Apache License 2.0
23 stars 4 forks source link

One issue and feature request with Multiline Regex Check template #10

Open leojosefm opened 5 years ago

leojosefm commented 5 years ago

I'm facing one issue with multi-line regex. It is working fine when I tested in https://regex101.com but not working as expected when the rule is executed.

Requirement : I want to find all the tabs (///$tab) except QDF & Main when succeeding line is not commented (//)

Expression : (?m)\/\/\/\$tab\s(?!(QDF|(?i)MAIN))(.*\n)(?!(\/\/.*))

Issue In below example, only tab1 should be captured as the succeeding line is not commented.

image

But when I executed it in Sonarqube, even if the subsequent line is commented, the code is captured as a code-smell.

image

Feature request: Only the first match is being captured. I would like to capture n issues if there are n-tabs in my text file with no comments (starting with //) in subsequent line.

I'm a beginner with both Regular expression and Sonar-qube, so could you please advise if it's something wrong with the expression/template that I use

gjd6640 commented 5 years ago

Thank you for your patience. I assume that you're using the "Multiline Regex Check" rule template here which is the correct choice. That rule type enables the "dotall" aka "(?s)" regex feature to make dots in provided expressions match to newlines which is causing the "(.*\n)" clause in your pattern to work incorrectly.

The easiest workaround here is to add "(?-s)" to your pattern to disable the "dotall" feature. I did some testing by adjusting this project's unit tests and based on that it looks like either of these expressions would work for you:

(?m-s)\/\/\/\$tab\s(?!(QDF|(?i)MAIN))(.*\n)(?!(\/\/.*)) (?m)\/\/\/\$tab\s(?!(QDF|(?i)MAIN))(?-s)(.*\n)(?!(\/\/.*))

I kind of like the second form because when you're trying to read it later you'll have some context about why the "(?-s)" is there as it immediately precedes the clause that needs it.

leojosefm commented 5 years ago

I tried both, it's working fine with online regex testers but when I execute sonar scanner they are not capturing the bugs. Below is the screen shot of the pattern captured using online regex testers and sonarqube analysis and rule defined

image

image

image