Skip to content

Commit 431db16

Browse files
author
James Allardice
committed
Added recommendations
1 parent 576ed21 commit 431db16

File tree

4 files changed

+130
-47
lines changed

4 files changed

+130
-47
lines changed

option-articles/bitwise-jshint.md

Lines changed: 0 additions & 47 deletions
This file was deleted.

option-articles/bitwise.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!---
2+
{
3+
"titles": [
4+
"bitwise"
5+
],
6+
"slugs": [
7+
"option-bitwise"
8+
],
9+
"linters": [
10+
"jslint",
11+
"jshint"
12+
],
13+
"author": "jallardice",
14+
"subject": "option"
15+
}
16+
-->
17+
18+
### What does this option do?
19+
20+
In JSLint the `bitwise` option is used to allow the usage of any bitwise
21+
operators. In JavaScript the available bitwise operators are `<<` (bitwise left
22+
shift), `>>` (bitwise right shift), `>>>` (unsigned bitwise right shift), `&`
23+
(bitwise AND), `|` (bitwise OR), `^` (bitwise XOR) and `~` (bitwise NOT). In the
24+
following example we are using the bitwise OR operator to round a number down to
25+
the closest integer which is a relatively common shorthand trick:
26+
27+
<!---
28+
{
29+
"linter": "jslint"
30+
}
31+
-->
32+
```javascript
33+
/*jslint bitwise: true */
34+
var x = 1.2345 | 0;
35+
```
36+
37+
The JSHint `bitwise` option is used to **disallow** the use of those operators.
38+
Here's the same example again:
39+
40+
<!---
41+
{
42+
"linter": "jshint"
43+
}
44+
-->
45+
```javascript
46+
/*jshint bitwise: true */
47+
var x = 1.2345 | 0;
48+
```
49+
50+
### When should I use this option?
51+
52+
With JSLint, if the `bitwise` option is not set, you'll get an "Unexpected
53+
'{a}'" error, where "{a}" is a bitwise operator, any time a bitwise operator is
54+
used. In JSHint the opposite is true and you'll receive an "Unexpected use of
55+
'{a}'" error for each bitwise operator occurence when the option is set.
56+
57+
If you require the use of bitwise operators for actual program logic then you
58+
cannot enable this option. However, if you do not need to use such operators and
59+
want to prevent tricks such as the one shown above, enabling this option is a
60+
good way to do so.
61+
62+
Note that in JSHint this is an *enforcing* option which means JSHint does not
63+
apply it by default. If you do not explicitly set this option to `true` JSHint
64+
will allow the use of bitwise operators anywhere in your code.
65+
66+
#### Recommendation
67+
68+
- **JSLint** - Set this option to `true` (you will be able to use bitwise
69+
operators).
70+
71+
- **JSHint** - Do not set this option (you will be able to use bitwise
72+
operators).

option-articles/camelcase-jshint.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,7 @@ project with multiple developers to help keep things consistent.
6565
Note that this is an *enforcing* option which means JSHint does not apply it by
6666
default. If you do not explicitly set this option to `true` JSHint will allow
6767
the use of bitwise operators anywhere in your code.
68+
69+
#### Recommendation
70+
71+
Set this option to `true` (enforces the use of camel case and constant case).

option-articles/curly-jshint.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!---
2+
{
3+
"titles": [
4+
"curly"
5+
],
6+
"slugs": [
7+
"option-jshint-curly"
8+
],
9+
"linters": [
10+
"jshint"
11+
],
12+
"author": "jallardice",
13+
"subject": "option"
14+
}
15+
-->
16+
17+
### What does this option do?
18+
19+
The JSHint `curly` option is used to enforce the use of block statements
20+
following the `if`, `for`, `while` and `do` statements. The language grammar
21+
shows that these statements must be followed by another statement which is why
22+
it's possible to omit the curly braces for single-statement bodies such as the
23+
following:
24+
25+
<!---
26+
{
27+
"linter": "jshint"
28+
}
29+
-->
30+
```javascript
31+
/*jshint curly: true */
32+
while (x)
33+
y();
34+
z(); // This is not inside the loop
35+
```
36+
37+
### When should I use this option?
38+
39+
The use of the `curly` JSHint option will cause an "Expected '{' and instead saw
40+
'{b}'" error, where "{b}" is the statement following the statement in question,
41+
any time it encounters a statement that doesn't match the rules discussed above.
42+
As demonstrated in the above example the omission of curly braces can make it
43+
easier to introduce bugs into the code. In general you should use curly braces
44+
where possible and leave it up to your minification or build process to remove
45+
them where necessary. Therefore it's usually sensible to enable this option
46+
unless your coding guidelines ask for the shorter form.
47+
48+
Note that this is an *enforcing* option which means JSHint does not apply it by
49+
default. If you do not explicitly set this option to `true` JSHint will allow
50+
the use of bitwise operators anywhere in your code.
51+
52+
#### Recommendation
53+
54+
Set this option to `true` (enforces the use of curly braces).

0 commit comments

Comments
 (0)