15

Right now I have the following code:

if (c > last) break;

And jslint complains with

jslint:crud.js:69:19:Expected '{' and instead saw 'break'.

There are several ways to overcome it:

if (c > last) { break; }

or

if (c > last) { 
  break; 
}

But I'd like jslint not to complain when the if statement is on the same line.

Is there some way to configure it?

BTW: I'm working with sublime text and the sublime-jslint plugin, with the following configuration:

{
    // Path to the jslint jar.
    // Leave blank to use bundled jar.
    "jslint_jar": "",

    // Options pass to jslint.
    "jslint_options": "--indent 2 --white --maxerr 10000 --nomen --browser --plusplus --es5",

    // Ignore errors, regex.
    "ignore_errors":
    [
        "Combine this with the previous 'var' statement.",
        "It is not necessary to initialize"
    ],

    // run jslint on save.
    "run_on_save": false,

    // debug flag.
    "debug": false
}

Any idea how to configure it?

opensas
  • 60,462
  • 79
  • 252
  • 386

1 Answers1

17

Unfortunately, JSLint does not provide a configuration option that will tell it to tolerate this. In the eyes of JSLint it is good practice to always follow a conditional or iteration statement with a block statement, even when said block would contain only a single statement.

If you switch to JSHint, which is far more configurable, you can use the curly option to:

/*jshint curly: false */

This configures JSHint to allow a single statement without curly braces, whether or not it's on the same line as the preceding conditional or iteration statement.

If your Sublime plugin only supports JSLint, I can highly recommend SublimeLinter, which is what I use. It supports both JSLint and JSHint.

CODE-REaD
  • 2,819
  • 3
  • 33
  • 60
James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • thanks a lot, I'll give it a try... do you know if it works with jslint4java? at work I don't know if I'll be able to install node – opensas Jan 23 '13 at 14:54
  • @opensas - I'm afraid it won't... jslint4java is just a Java wrapper around JSLint. JSHint started life as a straight fork of JSLint, but they are hugely different now and someone would need to write a separate Java wrapper for it. – James Allardice Jan 23 '13 at 14:57
  • 2
    To ignore single-line if-statements use `curly: false` – dev Jan 20 '15 at 18:22
  • 1
    it works, but unfortunately also allows two-line `if` without curly braces – snovity Feb 26 '15 at 14:07
  • 2
    Downvoted for stating this debatable truth as fact: "Good practice is to always follow a conditional or iteration statement with a block statement." Please predicate these types of statements with "Some coders recommend..." – Aaron Jul 07 '15 at 05:19