0

I could successfully integrate Sahi automation tool to SonarQube and could get the results but the issue is to define set of rules to report issues related to sahi script and find duplicate functions.

Here is the code snippet of Sahi

File name: sample1.sah

function eno_selectFromCombo($Field_Name, $Value) //Function name
{
    if($Value!="")
    {   
        _setStrictVisibilityCheck(true);
        if(_exists(_select($Field_Name)))
        {
            _assertEqual(false, _select($Field_Name).disabled)
            _setSelected(_select($Field_Name),$Value);
        }
        else
        {
            onScriptError($Field_Name + " doesn't exist ")
        }
        _setStrictVisibilityCheck(false);
    }
    else
    {   
        _log($Field_Name + " is set with default value")
    }
}

File name: sample2.sah

function eno_selectCombo($Field_Name, $Value) //Different function name but same set of code
    {
        if($Value!="")
        {   
            _setStrictVisibilityCheck(true);
            if(_exists(_select($Field_Name)))
            {
                _assertEqual(false, _select($Field_Name).disabled)
                _setSelected(_select($Field_Name),$Value);
            }
            else
            {
                onScriptError($Field_Name + " doesn't exist ")
            }
            _setStrictVisibilityCheck(false);
        }
        else
        {   
            _log($Field_Name + " is set with default value")
        }
    }

Same set of code copied over but with different name

For the above example,

  • How do I define rules to identify issues, if any?

    How do I find the duplicate function with same set of code but with different function name? This will help to reduce redundant functions

sridattas
  • 459
  • 1
  • 6
  • 21
  • How is that different from JavaScript code? – Pierre-Yves Nov 18 '16 at 08:19
  • @Pierre-Yves-SonarSourceTeam It is a Sahi script and not exactly the JS. Whatever we see with underscore are built-in sahi APIs. I want to check basically below in a sahi script file. How many functions are defined? What is the number of arguments it is taking? Indentation? Duplicate lines of code? whether try...catch has proper exception? if loop should have _isVisible API instead of _exists? etc., – sridattas Nov 22 '16 at 10:38
  • See this Q/A for discussion on finding code clones in Java and JavaScript: http://stackoverflow.com/q/40799220/120163 – Ira Baxter Nov 25 '16 at 10:19

1 Answers1

0

Even if these files have references to external APIs, they seem to use a standard JavaScript syntax. You should be able to analyse such files with the SonarQube JavaScript plugin. If your files names end with "sah", you should have the following line in your sonar-project.properties:

sonar.javascript.file.suffixes=.js,.sah
Pierre-Yves
  • 1,476
  • 10
  • 15
  • Thanks for your suggestion, it helped. But I want to define few new rules and hide/delete the rules. Is it possible? – sridattas Dec 20 '16 at 05:58
  • You may [write custom rules on top of the JavaScript plugin](http://docs.sonarqube.org/display/PLUG/Custom+Rules+for+JavaScript). You can de-activate existing rules in your [quality profile](http://docs.sonarqube.org/display/SONAR/Quality+Profiles). – Pierre-Yves Dec 20 '16 at 10:50