Javascript Reference Errors

478 views
Skip to first unread message

ashwin....@gmail.com

unread,
Jun 21, 2017, 4:51:47 PM6/21/17
to SonarQube
I have just started with analyzing the results of the SonarJS scanner, so I apologize if this is a silly question. We have javascript in a js file that calls methods in another js file. The cshtml has includes to both these so we have no issues at run time. However Sonar Qube flags these as reference errors when scanning the js files.

Has anyone else encountered this and if so, how did you resolve it?

Here is an example which causes a reference error for alertNumber when File2.js is scanned.

Thank you.


File1.js

function alertNumber(number) {
    alert(number);
}

File2.js

function alertOne() {
     alertNumber("one");
}

HTML

<head>
....
    <script src="File1.js" type="text/javascript"></script> 
    <script src="File2.js" type="text/javascript"></script> 
....
</head>


Pierre-Yves Nicolas

unread,
Jun 22, 2017, 3:23:06 AM6/22/17
to ashwin....@gmail.com, SonarQube
Hi,

SonarJS does not parse HTML files and therefore cannot know that File2.js may depend on a global variable defined in File1.js.
If you read the description of the rule which raises the issues about ReferenceError, you will see that no issue is raised on "global variables which are defined with sonar.javascript.globals and sonar.javascript.environments properties".
These properties are mentioned in the documentation for SonarJS.

Hope this helps.

Pierre-Yves

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/a174c7e2-2a0a-4183-9cab-b40d84a8d95b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ashwin....@gmail.com

unread,
Jun 22, 2017, 7:44:04 AM6/22/17
to SonarQube, ashwin....@gmail.com
Thanks for the response Pierre. That is what I have done for now actually, so thanks for the confirmation. I tried an 'import from ..." and found that it worked for SonarJS, but gave me run time errors on any browsers not ECMA 6 compliant. (which is pretty much all of them except Edge).  I was hoping there was a comment or something similar to an "include" that I could use to let SonarJS know where the property or method was being referenced from.

Thanks again.

Ashwin
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.

m.bla...@gmail.com

unread,
Mar 1, 2018, 11:42:12 AM3/1/18
to SonarQube
Hello Pierre-Yves,

I have a follow up question based on your reply, hopefully I would not bother you that much with. 

I have configured a list of sonar.javascript.globals on Sonar side for a js project, however when triggering a sonar check from Atlassian's Bamboo CI server (with configured sonar-maven-plugin) I still get a ReferenceErrorCheck. After modifying the maven settings.xml (on bamboo side) to add that property no issue is raised. Can I keep the globals only on Sonar side? The list is very long and would like to keep it out of the settings.xml file.

Thank you in advance for your time on this.

To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.

Pierre-Yves Nicolas

unread,
Mar 20, 2018, 12:26:35 PM3/20/18
to m.bla...@gmail.com, SonarQube
Hi,

Sorry for answering late.
In the future, I suggest that you open a new thread instead of resurrecting an old one.

Your problem does not seem related to the rule itself but to the way you manage analysis properties.
The value of sonar.javascript.globals depends on your project and it doesn't make much sense to set a value for it in maven settings.xml.
If you use Maven (I guess your project contains Java, otherwise I don't see why you would use Maven), you can probably set sonar.javascript.globals inside your pom.xml. See the related documentation.
You can also set the property using the SonarQube UI by visiting your project and then going to Administration / General Settings / JavaScript if you have the required authorization.

Pierre-Yves


To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/e1a41ded-06e3-4bba-8aba-6079d1f597c9%40googlegroups.com.

Maria Blagoeva

unread,
Mar 21, 2018, 4:16:28 AM3/21/18
to Pierre-Yves Nicolas, SonarQube
Hi,

Thank you for your inputs. I do agree on all your points.

I am pushing to have the globals on SonarQube side, but for some reason SonarQube did not owner them, now I see that we are couple of versions behind the current one for the server itself and plugin, so I will schedule an update and see if this resolves the problem.

Thank you for your time on this too.


Reply all
Reply to author
Forward
0 new messages