Uploaded image for project: 'SonarQube'
  1. SonarQube
  2. SONAR-4908

New API to declare coding rules

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 4.3
    • Component/s: Plugin API, Rules
    • Labels:
      None

      Description

      The way to declare rules (name, desc, parameters, SQALE requirement details,...) is not optimal and can be improved. There's a mix of distinct sources : l10n bundles in a standard location, sonar-check-api annotations, XML files for technical debt metadata... All these data are splitted among engine lib and SQ plugin. Moreover it assumes that a rule is a single check (and vice-versa), which is wrong, and it delegates the responsibility to instantiate checks to SQ core.

      For these reasons I suggest to provide a simple and lightweight library "sonar-rule-api" that would allow rule engine libraries to declare rule metadata without depending on sonar-plugin-api.

      Pattern has to be improved, but here is an example:

      • in engine library :
        public class CppRuleDefinitions implements org.sonar.api.rule.RuleDefinitions {
          @Override
          public void define(Context ctx) {
            NewRepository repository = ctx.newRepository("cpp");
            NewRule rule = repository.newRule("one") 
               .name("One")
               .htmlDescription("string or load file")
               .status(Status.BETA)
               .defaultSeverity(Severity.CRITICAL)
               .tags("database", "performance");
            rule.newParam("threshold")
              .name("threshold")
              .type(RuleParamType.INTEGER);
            repository.done();
          } 
        }
        
      • in language plugin :
        public class CppPlugin extends SonarPlugin {
          public List getExtensions() {
            return Arrays.asList(CppRuleDefinitions.class);
          }
        }
        

      Using XML file or classes with sonar-check-api can also be "natively" supported :

      public class CppRuleDefinitions implements RuleDefinitions {
        @Override
        public void define(Context ctx) {
          NewRepository repository = ctx.newRepository("cpp");
          repository.loadXml(getClass().getResource("/path/to/cpprules.xml"));
          repository.loadAnnotatedClasses(MyCppCheck.class, MyOtherCppCheck.class);
          repository.done();
        } 
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              julien.lancelot Julien Lancelot
              Reporter:
              simon.brandhof Simon Brandhof (Inactive)
              Votes:
              2 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: