The SonarQube CSS / SCSS / Less Analyzer can be enhanced by writing custom rules through a plugin using SonarQube CSS / SCSS / Less API. This sample plugin is designed to help you get started writing your own plugin and custom rules.
- Download and install SonarQube 6.7 or greater
- Install the SonarQube CSS / SCSS / Less plugin (4.13 or greater) by a direct download
- Install this sample plugin by a direct download
- Start SonarQube
- Activate some of the custom rules implemented in this sample plugin. "Forbidden properties should not be used" for example.
- Install your favorite scanner (SonarQube Scanner, Maven, Ant, etc.)
- Analyze your code
- Browse the issues through the web interface
- Create a standard SonarQube plugin from scratch or start from this sample plugin
- Attach this plugin to the SonarQube CSS / SCSS / Less plugin through the POM:
- Add the dependency to the CSS / SCSS / Less plugin
- Base the plugin on the CSS / SCSS / Less plugin
- Require a minimal version of the CSS / SCSS / Less plugin
- Implement the following extension points:
- Declare the
RulesDefinition
implementation as an extension in thePlugin
extension point.
- Create a class to define the implementation of a rule. It should:
- Either extend
SubscriptionVisitorCheck
orDoubleDispatchVisitorCheck
. - Define the rule's attributes: key, name, priority, etc.
- Either extend
- Declare this class in the class implementing
RulesDefinition
There are two different ways to browse the AST:
To explore part of the AST, override a method from DoubleDispactchVisitor
.
For instance, if you want to explore property nodes, override DoubleDispactchVisitor#visitProperty
. This method is called each time a key node is encountered in the AST.
Note: When overriding a visit method, you must call the super method in order to allow the visitor to visit the children of the node.
See ForbiddenPropertiesCheck
for example.
To explore part of the AST, override SubscriptionVisitor#nodesToVisit
by returning the list of Tree#Kind
nodes you want to visit.
For instance, if you want to explore URI content nodes the method should return a list containing Tree#Kind#URI_CONTENT
.
See ForbiddenUrlCheck
for example.
Precise issue or file issue or line issue can be created by calling the related method in Issues.
Testing is made easy by the CssCheckVerifier. There are two ways to assert that an issue should be raised:
- Through comments directly in the .css test file
- Or using assertions in the check class test
Examples of coding rule implementation and testing can be found in the CSS plugin css-checks
module.
The same as "Implementing a CSS Rule" applies. But, instead, look at:
The same as "Implementing a CSS Rule" applies. But, instead, look at:
Just add the rule to MyCssCustomRulesDefinition
and MyScssCustomRulesDefinition
and MyLessCustomRulesDefinition