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

泪湿孤枕 提交于 2019-11-28 07:05:03

问题


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.



来源:https://stackoverflow.com/questions/14474227/how-to-tell-jslint-not-to-ask-for-on-single-line-if-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!