Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 40 Next »

Table of Contents

There are three ways to extend coding rules:

  1. Adding XPath rules directly in the Sonar web interface.
  2. Extending an existing Sonar plugin. For example Checkstyle and PMD plugins accept definition of custom checks.
  3. Embedding and executing a code analyzer. For example the Checkstyle plugin configures and executes the library Checkstyle. 

Solution #1: Adding XPath Rules

Sonar provides a quick and easy way to add new coding rules directly via the web interface.

The rules have to be written in XPath to navigate the Abstract Syntax Tree (AST).

Tools (SSDK) are also provided for each language to navigate the AST.

So, knowing the XPath language is the only prerequisite.

You'll find below a detailed tutorial explaining how to add XPath rules.


Let's take the following JavaScript source code sample:


While parsing the source code, Sonar builds an Abstract Syntax Tree (AST).

A SSDK is provided by each language supporting XPath to get a representation of this AST. Here's the AST for our JavaScript sample:

The XPath language provides a way to write coding rules by navigating this AST.

To create a new rule:

  1. Login as an administrator
  2. Go to Configuration > Quality Profile
  3. Select one of the quality profiles whose language you want to add a new rule
  4. Look for the XPath rule template:
  5. Copy this template to create a new rule:
  6. Write your XPath rule (it should comply to XPath 1.0):

    Here are two examples of JavaScript XPath rules:

    Do not use document.write

    Always use curly braces for if/else statements: TODO



  7. Once written, activate those rules and run a Sonar analysis.
  8. Violations on those XPath rules are now logged:

Language Plugins Supporting XPath

Through SSLR:

  • Cobol
  • JavaScript
  • Flex
  • PL/SQL
Eventually, every language based on SSLR will support XPath.

Through external tools:

  • Java through PMD

Solution #2: Extending Sonar Plugins

The following languages can be extended with new rules:

Solution #3: Executing a Code Analyzer

A code analyzer plugin executes the following steps:

  1. Register definitions of coding rules, when the server is started.
  2. Optionally define some templates of quality profiles, when the server is started.
  3. Analyze source code and inject results in database 

1. Registering coding rules

This step relates to the extension point org.sonar.api.rules.RuleRepository. A RuleRepository defines a set of coding rules. It usually loads data from a XML file:

The XML file is available in the plugin classloader and looks like :

2. Defining quality profiles

This step relates to the extension point org.sonar.api.profiles.ProfileDefinition. Profiles provided by plugins are registered at server startup  and can't be edited by users:

3. Analyzing source code

This step relates to the extension point org.sonar.api.batch.Sensor.

  • No labels