15

I have chopped my js program into many pieces for development. Now when I run one piece through JSLint, I get a lot of errors of the type:

Problem at line 48 character 42: 'XXXXXXX' was used before it was defined.

I've looked for an option "Tolerate undefined variables" but haven't found any such option. What can I do so that JSLint ignores undefined variables?

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • 1
    I'll note that if you are getting "lots of errors" on a type in JSLint, that's a good sign that you are constantly referring to a global variables. Take a look at http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth (specifically the Global Import section). If you follow the module pattern and import globals into your local scope, each external reference should be mentioned by JSLint exactly once in any file. – Elliot Nelson Oct 23 '11 at 15:18

4 Answers4

45

From JSLint documentation:

JSLint also recognizes a /*global */ directive that can indicate to JSLint that variables used in this file were defined in other files. The comment can contain a comma separated list of names. Each name can optionally be followed by a colon and either true or false, true indicating that the variable may be assigned to by this file, and false indicating that assignment is not allowed (which is the default).

Example:

/*global var1, var2, var3 */

However I'd advise you to not do that, and instead write a simple script that re-assembles all the files and check the resulting file with JSLint.

stivlo
  • 83,644
  • 31
  • 142
  • 199
  • 2
    Also, if I'm not mistaken you can associate a boolean value to each variable name, to indicate whether the variable is writable. False implies read-only. So, `/*global var1:true, var2:false */` implies that var1 is writable, var2 is readonly. – Cheeso Oct 23 '11 at 15:17
  • @Cheeso, yes it's written in the quoted text above, thanks for putting an example. – stivlo Oct 23 '11 at 15:19
  • To add to @Cheeso's useful comment, the default is false (i.e. readonly) – Nick Feb 22 '13 at 15:50
  • this only worked for me as `/*globals a, b, c */ ` ie as a plural – dcsan Jul 28 '15 at 18:27
4

The answer "use a different program" is not acceptable.

Go to Preferences > Package Settings > JSLint > Advanced Build Settings.

Within the array for "cmd", add "--undef". It should look something like

{
    "cmd": [
      "node", 
      "${packages}/JSLint/linter.js",
      // tolerate missing 'use strict' pragma
      "--sloppy",
      // suggest an indent level of two spaces
      "--indent", "2",
      // assume node.js to predefine node globals
      "--node",
      // tolerate unfiltered for in
      //"--forin",
      // tolerate dangling _ in identifiers
      "--nomen",
      // tolerate many var statements per function
      "--vars",
      // tolerate ++ and --
      "--plusplus",
      // tolerate Douglas Crockford
      "--stupid",
      "--todo",
      // -----------------------------------------------
      // tolerate undefined variables
      "--undef",
      // -----------------------------------------------
      "$file"
    ],
    "file_regex": "^\\/.*\\/([^\\/]*)$",
    "line_regex": ".*\/\/ Line ([0-9]*), Pos ([0-9]*)$",
    "selector": "source.js, source.css, source.json, source.sass, source.less, source.html"
}
reergymerej
  • 2,371
  • 2
  • 26
  • 32
2

use the predefined option in your settings. You will see I added "NAMESP" at the end of the array.

Note also that I fixed the typo for jQuery (from JQuery to jQuery).

    // examples using predef flag.
    "--predef", "['angular', 'document', '\\$', '_', 'jQuery', 'NAMESP']"
haltersweb
  • 3,001
  • 2
  • 13
  • 14
-9

JSHint is a nice alternative to JSLint, and it has an option to turn on/off warnings for all undefined variables at once.

Red Orca
  • 4,755
  • 1
  • 15
  • 12
  • 1
    @stivlo Very true. I wouldn't recommend disabling it either, but I can see how someone could want to do so to check for other problems first, without JSLint stopping at every undefined variable. – Red Orca Oct 23 '11 at 15:43
  • 2
    An [interesting comparison of JSLint vs JSHint](http://www.scottlogic.co.uk/2011/03/jslint-vs-jshint/). – stivlo Oct 23 '11 at 15:50
  • This is not an answer to the question. – tivnet Mar 23 '17 at 19:00