0

I know that I can type into Chrome or FF the following command:

Object.keys(window);

but this displays DHTMLX stuff and also function names in which I'm not interested in. And it doesn't display variables in functions that have not been executed yet. We have more than 20,000 lines of JavaScript codebase, so I would prefer static code analyis. I found JavsScript Lint. It is a great tool but I don't know how to use it for displaying global vars.

So I'm searching for memory leaks, forgotten var keywords, and so on...

user3719454
  • 994
  • 1
  • 9
  • 24
  • Have you tried [JSLint](http://www.jslint.com/)? It has detection for undeclared globals and a whole lot else. – JLRishe Mar 31 '15 at 12:13
  • yes, but I don't want to put our codebase to the internet.... – user3719454 Mar 31 '15 at 12:14
  • JSLint is executed entirely in the browser. You wouldn't be "putting your codebase to the internet". – JLRishe Mar 31 '15 at 12:17
  • 3
    Command-line adaptations of JSLint exist for a number of runtimes. The basic API is one function call, so you can even write your own if you feel that it's necessary: it'll work in Node, Rhino, or just about anything else. – The Spooniest Mar 31 '15 at 13:24

3 Answers3

2

To do [only] what you're asking, I think you're looking for this:

for each (obj in window) {
    if (window.hasOwnProperty(obj)) { 
        console.log(obj);
    }
}

I haven't linted that code, which is unlike me, but you get the idea. Try setting something first (var spam = "spam";) and you'll see it reported on your console, and not the cruft you asked about avoiding.

That said, JLRishe is right; JSLint executes JavaScript in your browser without "phoning home", so feel free to run it. There are also many offline tools for JSLinting your code. I use a plugin for Sublime Text, for instance.

If you'd like some simplest-case html/JavaScript code to "wrap" JSLint, I've got an example here. Just download the latest jslint.js file from Crockford's repository into the same directory, and poof, you're linting with a local copy of JSLint.js. Edit: Added code in a new answer here.

Though understand that you're linting locally with that wrapper or when you visit JSLint.com. Honestly, I can say with some confidence, Crockford would rather not see our code. ;^) You download JSLint.js (actually webjslint, a minified compilation of a few files) from JSLint.com, but you execute in the browser.

(Admittedly, you're technically right -- you never know when that site could be compromised, and to be completely on the up and up, you sh/c/ould vet jslint.js each time you grab a fresh copy. It is safer to run locally, but as of this writing, you appear safe to use JSLint.com. Just eyeball your favorite browser's Net tab while running some test, non-proprietary code, and see if any phoning happens. Or unplug your box's network cable!)

Rick's answer to use "use strict"; is another great suggestion.

Community
  • 1
  • 1
ruffin
  • 16,507
  • 9
  • 88
  • 138
0

A great way to catch undeclared variables is to add 'use strict' to your code.

The errors will appear in the console, or you could display them in a try ... catch block:

'use strict';
try {
  var i= 15;
  u= 25;
} catch(ee) {
  alert(ee.message);
}
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
0

I found a very good solution to list all the global variables with the jsl command line tool:

Here is the documentation

I just have to put /*jsl:option explicit*/ into each file that I want to check. Then it is enough to run ./jsl -process <someFile> | grep 'undeclared identifier'

It is also possible to use referenceFile that contains some intentional global variables /*jsl:import <referenceFile>*/ so these variables will not be listed.

user3719454
  • 994
  • 1
  • 9
  • 24