0

In a previous question Run JSLint on a .js file from debugging console in chrome or firefox I learned how to run JSLint from console or from node.

Is there a way to run JSLint (from the JavaScript code like in the answer given to the above mentioned question) but with custom options, for example for code that contains jQuery (JSLint validation for Javascript with JQuery) with the options Assume a browser and the option Predefined section with jQuery and $ as parameters?

Community
  • 1
  • 1
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
  • 1
    From the docs: *"When JSLINT is called as a function, it accepts an `option` object parameter that allows you to determine the subset of JavaScript that is acceptable to you."*. The source code also contains information about how to call the function (v see the link below v). – Felix Kling Aug 27 '12 at 20:14
  • 1
    https://github.com/douglascrockford/JSLint/blob/master/jslint.js – Matt Aug 27 '12 at 20:16
  • @FelixKling I found the quote that you gave at http://www.jslint.com/lint.html#options but no sample code and I don't see no code example, from what I read I guess the object should be simple `parameters ={"browser":"true", "predifined:":["jQuery","$"]}` ? How do I give this to JSLINT function? – Eduard Florinescu Aug 27 '12 at 20:25
  • @FelixKling I think I need to read much more on JSLint, maybe a book or something – Eduard Florinescu Aug 27 '12 at 20:48
  • Or maybe you like JSHint more, which has a better documentation IMHO: http://www.jshint.com/docs/ (and is often less frustrating). – Felix Kling Aug 27 '12 at 20:53
  • @FelixKling Does it have the assign protection feature you mentioned in the answer? – Eduard Florinescu Aug 27 '12 at 20:55

1 Answers1

1

Pass an object with the options as second parameter to JSLINT. Global variables can be defined as an array of strings and assigned to the predef property of said options object.

From the JSLint source code:

JSLINT is a global function. It takes two parameters.

var myResult = JSLINT(source, option);

The first parameter is either a string or an array of strings. If it is a string, it will be split on '\n' or '\r'. If it is an array of strings, it is assumed that each string represents one line. The source can be a JavaScript text, or HTML text, or a JSON text, or a CSS text.

The second parameter is an optional object of options that control the operation of JSLINT. Most of the options are booleans: They are all optional and have a default value of false. One of the options, predef, can be an array of names, which will be used to declare global variables, or an object whose keys are used as global names, with a boolean value that determines if they are assignable.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • So `option ={"browser":"true", "predef:":["jQuery","$"]}` should be correct? Also please can you expand a little * or an object whose keys are used as global names, with a boolean value that determines if they are assignable* ? this means if `"mylibrary":"false"` given to `predef` gives an warning if I try to do something like this `lib = mylibrary`? – Eduard Florinescu Aug 27 '12 at 20:39
  • 1
    Yes, that should work... try it ;) You are close with the second one, it means that you cannot overwrite the variables with some other value, i.e. you cannot do `mylibrary = someOtherValue`. – Felix Kling Aug 27 '12 at 20:42
  • Thanks this is what I looked for some time, I'll try it? how do i put the last option in the array `"predef":["jQuery","$", {"mylibrary":"false"}]`? – Eduard Florinescu Aug 27 '12 at 20:46
  • 1
    It appears to have to be either an array or object. So you could do `"predef": {"jQuery":true, ,"$":true, "mylibrary":"false"}`. – Felix Kling Aug 27 '12 at 20:48