Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSLint to ignore undefined variables

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?

like image 440
Randomblue Avatar asked Oct 23 '11 15:10

Randomblue


People also ask

How do you handle undefined variables?

undefined variables can be corrected by DEFINING them. for instance: $place = ''; OR $place = null; defines the variable.

How do you handle undefined in node JS?

Accessing the variable evaluates to undefined . An efficient approach to solve the troubles of uninitialized variables is whenever possible assign an initial value. The less the variable exists in an uninitialized state, the better.

What are undefined undeclared variables?

Undefined variable means a variable has been declared but it does not have a value. Undeclared variable means that the variable does not exist in the program at all.


2 Answers

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.

like image 101
stivlo Avatar answered Sep 27 '22 18:09

stivlo


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"
}
like image 24
reergymerej Avatar answered Sep 27 '22 18:09

reergymerej