1

I'm taking on a task to find issues in a large number of short Javascript codes. One of the things I'm looking for is instance of a variable usage before explicit assignment, like so:

var a;
var b = a + 10; // a is not explicitly assigned a value

I've tried a few JS analysis tools like JSLint but they don't catch it. I'm hoping for a ready to use tool, as I'm pressed for time, but if there's a library that can be used for this with a bit of coding, then that's also helpful.

Mansour
  • 1,787
  • 2
  • 20
  • 33
  • That’s not necessarily an issue when it comes to JavaScript, though… – Ry- Jun 26 '14 at 06:24
  • 1
    True, but it does cause problems in the way my scripts are run as the data leaks from previous run of a script to the next invocation. – Mansour Jun 26 '14 at 06:27
  • That seems odd. How *are* your scripts run? – Ry- Jun 26 '14 at 06:27
  • You shouldn't be able to have such leaks unless you're using global variables (i.e. no `var` at all). Specifically, in JavaScript, this does not leak: `var b = a; var a;` (as long as it is in a function). – Amadan Jun 26 '14 at 06:28
  • So how to you determine the value that *a* should be initialised to? – RobG Jun 26 '14 at 06:29
  • The scripts are run in Pentaho Data Integration suite. It seems that the script is run per row of data without reinitialization of the environment. So `var a;` initializes `a` to `undefined` the first time. But any subsequent times, it does nothing (try it). – Mansour Jun 26 '14 at 06:30
  • 1
    Not sure about ready made solutions, but it shouldn't be too hard to create your own using http://esprima.org/. – Felix Kling Jun 26 '14 at 06:31
  • @Amadan it's evidently not encapsulated in a function. – Mansour Jun 26 '14 at 06:34

0 Answers0