Twitter GitHub Facebook Instagram dirv.me

Daniel Irvine on building software

Running JSLint across multiple files

7 August 2014

There’s no denying it: JSLint is a great tool. But it lacks the ability to test multiple files within one runtime context without producing undefined symbol errors.

JSLint can test multiple files at once, using a command line like this:

$ find . -name **/*.js -print0 | xargs -0 jslint

Unfortunately, that’s not enough: normally at runtime, JavaScript files are pulled together via an HTML file, which defines the runtime context. In this way, JavaScript files can reference each other’s type. But the HTML file is crucially to defining this link. Without it, each individual file has no awareness of the other types that will be defined within its runtime context. So the above JSLint command will generate errors of the following form.

#14 'NoughtsAndCrosses' was used before it was defined.
    NoughtsAndCrosses.Game.start(?args); // Line 95, Pos 5

One way this issue occurs is when linting both an implementation file and its corresponding spec file:

game.js // Game implementation
game_spec.js // Jasmine tests for game.js
 

Crucially, the Jasmine tests run via a third file, TestRunner.html, which defines the runtime context for the two JavaScript files. The specs in game_spec.js refer to functions within game.js, so TestRunner.html ensures both game.js and then game_spec.js are loaded in the same runtime context.

What I’d really like is if JSLint could do the same thing, and I could specify an HTML file to lint rather than a set of lose JavaScript files.

Workaround

The only workaround I’ve found so far is to add a global directive at the top of files which reference other constants. In game_spec.js I have the following line.

/*global Game: true*/

Not nice.

About the author

Daniel Irvine is a software craftsman at 8th Light, based in London. These days he prefers to code in Clojure and Ruby, despite having been a C++ and C# developer for the majority of his career.

For a longer bio please see danielirvine.com. To contact Daniel, send a tweet to @d_ir or use the comments section below.

Twitter GitHub Facebook Instagram dirv.me