3

I have JSLint Plugin installed in Sublime Text 2. But i don't agree with some of the rules imposed by the JSLint specially this error on declaring variables inside a loop.

Move 'var' declarations to the top of the function.
    for (var i = 0; i < 100; i++) { // Line 12, Pos 10

My question is how can i override or disable this rule in JSLint on Sublime Text 2.

chanHXC
  • 611
  • 1
  • 7
  • 17
  • Some plugins allow you to specify codes/patterns to ignore. Your best bet would be to look over the README in the plugin. If it doesn't perhaps make a feature request for the plugin, or find one that allows you to do it. I did a quick search and it seems there are two JSLint plugins, so make sure you look up the proper repo. – skuroda May 03 '13 at 18:41

2 Answers2

3

If you have Darren DeRidder's plugin (there are two JSLint plugins for Sublime Text), you do this:

You can set any of jslint's options under preference -> package settings -> jslint -> advanced built settings. See http://www.jslint.com/lint.html#options for a list of options in JSLint.

Now you're going to have a hard time disabling just var declarations within loops. You can turn the vars option to true, but then JSLint will let you have as many var declarations as you want, anywhere on the page. That can be a misleading practice, as JavaScript has what some call Function Scope and "hoists" declarations to the top of their scope.

EDIT: Argh, I lied. vars only allows multiple var declaration statements, but they still have to be at the top of the function. It only allows you to do this:

function fnTest() {
  var i;
  var j;  // Oh boy! Two var statements at the TOP of the same function
  for (i = 0; i < 100; i++) {
    j++;
  }
}

and not

function fnTest() {
  var j;
  for (var i = 0; i < 100; i++) { // still can't do this.
    j++;
  }
}

Though I'm surprised Crockford doesn't let you do this, I think you're out of luck, and have to use JSHint instead (there seems to be a Sublime plugin for JSHint here, though I haven't used it).

Community
  • 1
  • 1
ruffin
  • 16,507
  • 9
  • 88
  • 138
  • +1 for using JSHint, it's the cleanest solution. JSHint is included in SublimeLinter plugin. – Mikko Ohtamaa May 04 '13 at 06:09
  • The reason that it doesn't want you declaring variables inside is because Javascript 'lifts' functions. It's not what you expect, so it's try to get you to guarantee you see what JS will do anyways. – Jono Aug 20 '15 at 14:59
0

@chanHXC with the new default options in sublime-jslint the var declaration warnings are skipped.

Darren
  • 1,846
  • 15
  • 22