Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSLint, else and Expected exactly one space between '}' and 'else' error

Why JSLint report in code:

function cos(a) {
    var b = 0;
    if (a) {
        b = 1;
    }
    else {
        b = 2;
    }

    return b;
}

error:

Problem at line 6 character 5: Expected exactly one space between '}' and 'else'.

This error can be turned off by disabling Tolerate messy white space option of JSLint.

Or in other words -- why syntax: } else { is better then

...
}
else {
...

Google also uses syntax with } else { form.

But I don't understand why. Google mentioned ''implicit semicolon insertion'', but in context of opening {, not closing one.

Can Javascript insert semicolon after closing } of if block even if next token is else instruction?

Sorry that my question is a bit chaotic -- I tried to think loud.

like image 445
Grzegorz Gierlik Avatar asked Nov 15 '11 00:11

Grzegorz Gierlik


4 Answers

JSLint is based on Crockford's preferences (which I share in this case).

It's a matter of opinion which is "better".

(Although clearly his opinion is right ;)

like image 152
Dave Newton Avatar answered Nov 02 '22 12:11

Dave Newton


It's not a matter of style. It's how ECMAScript works.

For better or for worse, it will automatically insert semicolons at the end of statements where it feels necessary.

JavaScript would interpret this:

function someFunc {
    return
    {
        something: 'My Value'
    };
}

As this:

function someFunc {
    return;
    {
        something: 'My Value'
    };
}

Which is certainly what you don't want.

If you always put the bracket on the same line as the if and if else statement, you won't run into a problem like this.

As with any coding language, the coding style chosen should be the one that minimizes potential risk the most.

Mozilla Developer Network also promotes same line bracketing: https://developer.mozilla.org/en-US/docs/User:GavinSharp_JS_Style_Guidelines#Brackets

like image 35
PhillipKregg Avatar answered Nov 02 '22 12:11

PhillipKregg


JSLint is being very picky here, just enforcing a style that you might not share.

Try JSHint instead:

The project originally started as an effort to make a more configurable version of JSLint—the one that doesn't enforce one particular coding style on its users [...]

like image 3
Jordão Avatar answered Nov 02 '22 12:11

Jordão


JSLint is just being picky here. The guy who wrote it also baked in many stylistic suggestions in order to keep his own code more consistent.

As for semicolon insertion, you shouldn't need to worry here. Inserting a semicolon before the else clause would lead to a syntax error and automatic semicolon insertion only occurs in situations where the resulting code would still be syntactically valid.

If you want to read more on semicolon insertion, I recommend this nice reference

Basically if you insert semicolons everywhere you only need be careful about putting the argument to "return" or "throw" (or the label for "break" and "continue") on the same line.

And when you accidentally forget a semicolon, the only common cases that are likely to bite you are if you start the next line with an array literal (it might parsed as the subscript operator) or a parenthsised expression (it might be parsed as a function call)

Conclusion

Should you omit optional semicolons or not? The answer is a matter of personal preference, but should be made on the basis of informed choice rather than nebulous fears of unknown syntactical traps or nonexistent browser bugs. If you remember the rules given here, you are equipped to make your own choices, and to read any JavaScript easily.

If you choose to omit semicolons where possible, my advice is to insert them immediately before the opening parenthesis or square bracket in any statement that begins with one of those tokens, or any which begins with one of the arithmetic operator tokens "/", "+", or "-" if you should happen to write such a statement.

Whether you omit semicolons or not, you must remember the restricted productions (return, break, continue, throw, and the postfix increment and decrement operators), and you should feel free to use linebreaks everywhere else to improve the readability of your code.


By the way, I personally think that the } else { version is prettier. Stop insisting in your evil ways and joins us on the light side of the force :P

like image 3
hugomg Avatar answered Nov 02 '22 11:11

hugomg