How to define the scope for a Java custom rule

Hey Thomas,

Sorry, I didn’t saw your question earlier.

Yes, there is. To do so you just have to register the rule differently in your RulesList class or your CheckRegistrar class, depending on how you implemented it.

If you followed our tutorial, by default all the rules are simply assigned to MAIN code. This makes me think that we should add an example of rules targeting TEST, at least for completeness (SONARJAVA-3818).

Now, the only thing you have to do is to add some new rules to be executed exclusively on TEST code, by modifying the list of rules assigned to each scope.


Now, regarding the scope field that you can see in the metadata json file. Currently, this field has no impact on what domain the (java) rules are going to be executed. As of today, it is even purely and simply ignored by the Java analyzer, and rules are exclusive: they target either MAIN, either TEST sources. Even rules having the ALL scope are currently only executed on MAIN.

This scope was introduced a while ago, while we were exploring the possibility to actually execute much more rules on TEST scope, in a more global wish to push for improved quality of tests… This effort is currently paused on our side, but we might get back to it at some point. You might want to have a look at its umbrella ticket (MMF-1451) or the related feature request expressed in this forum (Allow enabling sonar rules for test sources).

Hope this helps,
Michael

2 Likes