How to tell JSLint not to ask for { on single line

2019-01-27 13:55发布

问题:

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?

回答1:

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.