jshint/jshint — Issue #344: Issue with blocks
Author: cowboy
Created: 2011-11-17 13:39:35+00:00
State: open

When I validate this code at jshint.com, there are no complaints:

```javascript
var i = 0;
do {
++i;
} while (i < 10);
console.log(i);
```

This code, however, does not validate. Presumably because the Warn "When blocks omit {}" option is checked.

```javascript
var i = 0;
while (++i < 10);
console.log(i);
```

These are the reported errors:

```
Line 2: while (++i < 10);
Expected '{' and instead saw ';'.

Line 2: while (++i < 10);
Unnecessary semicolon.
```

Now, when I disable the Warn "When blocks omit {}" option and revalidate the same code, I get this error:

```
Line 2: while (++i < 10);
Unnecessary semicolon.
```

This error is wrong. In fact, the terminology used when describing the `curly` option in [the documentation](http://www.jshint.com/options/) is also wrong.

Blocks aren't surrounded by curly braces. A block is a statement, delimited by curly braces, that contains zero or more statements. In the first code example, `{ ++i }` is a block statement. The block is not the statements inside the block.

Anyways, if I were to follow JSHint's advice upon disabling the `curly` option, and removed the semicolon, my program would have a very different behavior:

```javascript
var i = 0;
while (++i < 10)
console.log(i);
```

Because semicolon is used to terminate statements, it can be used instead of an empty block `{}` to explicitly specify that zero statements exist. It can be argued that using a block in this situation is smarter, because a block can contain zero _or more_ statements, but while you probably wouldn't do `if (foo); else;`, using a semicolon (basically, an empty statement) can make while or do-while loops much more concise.

Either way, JSHint seems to not understand how `;` actually works as a statement terminator in this situation, and gives me bad advice when I disable the `curly` option.