Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper, Javascript: "Use of implicitly declared global variable 'X'"

I'm using Resharper 6 and ASP.NET Web Methods and have an irritating warning in my Javascript files:

"Use of implicitly declared global variable 'X'"

The reason is that the web method is created in Javascript as:

new X.example().webMethod(arg1, arg2, successCallback, failureCallback);

And X...is implicitly defined. I'm wondering if there is a solution to explicitly define this? It's defined in some auto-generated JS file, created by the ASP.NET web method framework stuff.

My question is: how do I get rid of the error for this situation, without getting rid of it for legitimately wrong situations?

Thanks!

like image 857
rythos42 Avatar asked Sep 29 '11 23:09

rythos42


4 Answers

When using symbols (functions, constants, global variables) defined in other JavaScript files, I pass them to the current file's "scoping function" (top-level, usually anonymous, function that prevents global namespace pollution) as parameters:

multiple containers

As you can see from the screenshot, ReSharper (6.0.2202.688) is happy with jQuery, ContainerA and ContainerB even though they are not defined anywhere in the current file. The comment in line 1 is only there for JSLint (no errors).

This technique assumes that all the other JavaScript files follow the JavaScript best practice of minimally polluting the global namespace by defining a single top-level object that contains all the exported (public) symbols (i.e. jQuery is the only global object for jQuery library and its plugins, ContainerA is the only global object for LibraryA, ContainerB is the only global object for LibraryB, etc).

Because you clearly don't have control over ASP.NET Web Methods generating constructor functions into global namespace, in your case you have to resort to the ultimate container, window:

window as a container

This is a slight variation on the technique suggested by @sethobrien in his comment. An important (IMHO) advantage is that you're not hard-coding window.X into your code. Instead, your code is instantiating classes from the aspNet container (that at the moment happens to be a synonym for window, but that might change in the future). Also, having aspNet.X in the code declares your intention more clearly for people who will read your code in the future. Finally, local variables can be shortened by JavaScript minimizers yielding slightly smaller file transmitted to client browsers.

like image 99
Milan Gardian Avatar answered Nov 19 '22 09:11

Milan Gardian


Adding following to the top of your script file ///<reference path="my.js" /> (my.js is the file where X is defined) will likely fix this warning since ReSharper start seeing this global variable.

Otherwise to minimize changes you can add var X = window.X; near the top of the file. Try to make it not to polute global namespace and make sure it will not confuse code that actually instantiates X on the window.

like image 25
Alexei Levenkov Avatar answered Nov 19 '22 08:11

Alexei Levenkov


Got exactly the same problem after moving Jasmine to an external Bower package and excluding Jasmine's code from VS project. Resharper immediately started complaining on Use of an implicitly declared global variable 'describe' and so on.

I solved this by adding to the project another file named workaround.js dummy definitions for the variables. In your case it would be:

// This is a workaround for R# complaining on undefined global variables.
// In practice they come from and are defined by external frameworks, so 
// this is not a real issue.

var X = function () { };

And this is a file in my project - https://gist.github.com/barahilia/62871d9219cee825d82e.

like image 36
Ilia Barahovsky Avatar answered Nov 19 '22 09:11

Ilia Barahovsky


If it is an error that can be ignored you can use

// ReSharper disable once UseOfImplicitGlobalInFunctionScope
like image 1
floralGhost Avatar answered Nov 19 '22 08:11

floralGhost