Javascript - reference error for Objects declared in another files

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
  • what are you trying to achieve
  • what have you tried so far to achieve this


Version information regarding our environment
SonarQube :
Javascript scanner (SonarJS) : 6.2.1 (build 12157)

Description of my problem

It seems that the Javascript scanner don’t recognize some Javascript object during parsing and so when the object is analyzed in another javascript file, the system detect a problem by indicate that the reference is unknown

In one Javascript file we have the declaration of the object , like

var GSMCheckArticlesBusiness = {};
GSMCheckArticlesBusiness.addFaultsItems = function(controller){
    var ctrl = controller;
    var xmlInput = '<GSMArtWithFaultUpdate>';
    var articleRecords = ctrl.getTask().getAllArticleRecords();

In another files we use the object

if(ctrl.getTask().getType() == TASK_TYPE.CHECK_ARTICLES){
     xmlInput += GSMCheckArticlesBusiness.addFaultsItems(ctrl); 

During the analyze of this last javascript files , Sonar indicate an error
“GSMCheckArticlesBusiness” does not exist. Change its name or declare it so that its usage doesn’t result in a “ReferenceError”

Do you have an idea , why the object is not recognized ?

Hello @jerome_chevalier,

Welcome to SonarSource community, and thank you for your message!

The problem you are facing is due to a limitation of SonarJS. Currently, files are parsed and analyzed one after another without sharing states. This also includes global variables declared with var in one file and referenced in another without explicit import statements.

Because of this limitation, S3827 raises an issue for GSMCheckArticlesBusiness when referenced outside of the file it is declared. We might change that in the future, but that would require some changes in the architecture of our JavaScript analyzer that we don’t consider to be a priority for now.

In the meantime, you can add GSMCheckArticlesBusiness in the list of the known global variables with sonar.javascript.globals. Otherwise, I am afraid that you have no other choice but to ignore that issue when resolving it or disable the rule if it turns out to be too noisy for the particular case of your project.

Hope this helps,