Skip to content

Commit 519c018

Browse files
authored
Merge pull request #783 from nyuszika7h/patch-3
RULES.md: Add more examples; minor fixes
2 parents 938e119 + 64d6f3f commit 519c018

File tree

1 file changed

+63
-48
lines changed

1 file changed

+63
-48
lines changed

RULES.md

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ your code.
570570

571571
```js
572572
eval( "var result = user." + propName ) // ✗ avoid
573+
var result = user[propName] // ✓ ok
573574
```
574575

575576
* **No reassigning exceptions in `catch` clauses.**
@@ -656,6 +657,14 @@ your code.
656657
case 2:
657658
doSomethingElse()
658659
}
660+
661+
switch (filter) {
662+
case 1:
663+
doSomething()
664+
// fallthrough // ✓ ok
665+
case 2:
666+
doSomethingElse()
667+
}
659668
```
660669

661670
* **No floating decimals.**
@@ -689,7 +698,8 @@ your code.
689698
eslint: [`no-implied-eval`](http://eslint.org/docs/rules/no-implied-eval)
690699

691700
```js
692-
setTimeout("alert('Hello world')") // ✗ avoid
701+
setTimeout("alert('Hello world')") // ✗ avoid
702+
setTimeout(function () { alert('Hello world') }) // ✓ ok
693703
```
694704

695705
* **No function declarations in nested blocks.**
@@ -716,7 +726,7 @@ your code.
716726
eslint: [`no-irregular-whitespace`](http://eslint.org/docs/rules/no-irregular-whitespace)
717727

718728
```js
719-
function myFunc() /*<NBSP>*/{} // ✗ avoid
729+
function myFunc () /*<NBSP>*/{} // ✗ avoid
720730
```
721731

722732
* **No using `__iterator__`.**
@@ -761,7 +771,7 @@ your code.
761771
}
762772

763773
function myFunc () {
764-
myOtherFunc() // ✓ ok
774+
myOtherFunc() // ✓ ok
765775
}
766776
```
767777

@@ -897,6 +907,8 @@ your code.
897907

898908
```js
899909
const regexp = /test value/ // ✗ avoid
910+
911+
const regexp = /test {3}value/ // ✓ ok
900912
const regexp = /test value/ // ✓ ok
901913
```
902914

@@ -1128,6 +1140,7 @@ your code.
11281140
}
11291141

11301142
const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' } // ✓ ok
1143+
11311144
const user = {
11321145
name: 'Jane Doe',
11331146
age: 30,
@@ -1160,7 +1173,7 @@ your code.
11601173
fn(...args) // ✓ ok
11611174
```
11621175

1163-
* **Semicolon's must have a space after and no space before.**
1176+
* **Semicolons must have a space after and no space before.**
11641177

11651178
eslint: [`semi-spacing`](http://eslint.org/docs/rules/semi-spacing)
11661179

@@ -1178,7 +1191,7 @@ your code.
11781191
if (admin) {...} // ✓ ok
11791192
```
11801193

1181-
* **No spaces inside parens.**
1194+
* **No spaces inside parentheses.**
11821195

11831196
eslint: [`space-in-parens`](http://eslint.org/docs/rules/space-in-parens)
11841197

@@ -1193,7 +1206,7 @@ your code.
11931206

11941207
```js
11951208
typeof!admin // ✗ avoid
1196-
typof !admin // ✓ ok
1209+
typeof !admin // ✓ ok
11971210
```
11981211

11991212
* **Use spaces inside comments.**
@@ -1203,6 +1216,9 @@ your code.
12031216
```js
12041217
//comment // ✗ avoid
12051218
// comment // ✓ ok
1219+
1220+
/*comment*/ // ✗ avoid
1221+
/* comment */ // ✓ ok
12061222
```
12071223

12081224
* **No spacing in template strings.**
@@ -1223,7 +1239,7 @@ your code.
12231239
if (isNaN(price)) { } // ✓ ok
12241240
```
12251241

1226-
* **`typeof` must be compared to valid string.**
1242+
* **`typeof` must be compared to a valid string.**
12271243

12281244
eslint: [`valid-typeof`](http://eslint.org/docs/rules/valid-typeof)
12291245

@@ -1232,14 +1248,15 @@ your code.
12321248
typeof name === 'undefined' // ✓ ok
12331249
```
12341250

1235-
* **Immediately Invoked Function Expressions (IFFE's) must be wrapped.**
1251+
* **Immediately Invoked Function Expressions (IIFEs) must be wrapped.**
12361252

12371253
eslint: [`wrap-iife`](http://eslint.org/docs/rules/wrap-iife)
12381254

12391255
```js
12401256
const getName = function () { }() // ✗ avoid
12411257

1242-
const getName = (function () { }) // ✓ ok
1258+
const getName = (function () { }()) // ✓ ok
1259+
const getName = (function () { })() // ✓ ok
12431260
```
12441261

12451262
* **The `*` in `yield*`expressions must have a space before and after.**
@@ -1314,7 +1331,7 @@ your code.
13141331
;[1, 2, 3].forEach(bar)
13151332
```
13161333

1317-
This is much preferred:
1334+
This is strongly preferred:
13181335

13191336
```js
13201337
var nums = [1, 2, 3]
@@ -1337,44 +1354,42 @@ in JavaScript).
13371354

13381355
##### Excerpt from *["An Open Letter to JavaScript Leaders Regarding Semicolons"][1]*:
13391356

1340-
[Relying on automatic semicolon insertion] is quite safe, and perfectly valid JS that every browser understands. Closure compiler, yuicompressor, packer, and jsmin all can properly minify it. There is no performance impact anywhere.
1341-
1342-
I am sorry that, instead of educating you, the leaders in this language community have given you lies and fear. That was shameful. I recommend learning how statements in JS are actually terminated (and in which cases they are not terminated), so that you can write code that you find beautiful.
1343-
1344-
In general, `\n` ends a statement unless:
1345-
1. The statement has an unclosed paren, array literal, or object literal or ends in some
1346-
other way that is not a valid way to end a statement. (For instance, ending with `.`
1347-
or `,`.)
1348-
2. The line is `--` or `++` (in which case it will decrement/increment the next token.)
1349-
3. It is a `for()`, `while()`, `do`, `if()`, or `else`, and there is no `{`
1350-
4. The next line starts with `[`, `(`, `+`, `*`, `/`, `-`, `,`, `.`, or some other
1351-
binary operator that can only be found between two tokens in a single expression.
1352-
1353-
The first is pretty obvious. Even JSLint is ok with `\n` chars in JSON and parenthesized constructs, and with `var` statements that span multiple lines ending in `,`.
1354-
1355-
The second is super weird. I’ve never seen a case (outside of these sorts of conversations) where you’d want to do write `i\n++\nj`, but, point of fact, that’s parsed as `i; ++j`, not `i++; j`.
1356-
1357-
The third is well understood, if generally despised. `if (x)\ny()` is equivalent to `if (x) { y() }`. The construct doesn’t end until it reaches either a block, or a statement.
1358-
1359-
`;` is a valid JavaScript statement, so `if(x);` is equivalent to `if(x){}` or, “If x, do nothing.” This is more commonly applied to loops where the loop check also is the update function. Unusual, but not unheard of.
1360-
1361-
The fourth is generally the fud-inducing “oh noes, you need semicolons!” case. But, as it turns out, it’s quite easy to *prefix* those lines with semicolons if you don’t mean them to be continuations of the previous line. For example, instead of this:
1362-
1363-
```js
1364-
foo();
1365-
[1,2,3].forEach(bar);
1366-
```
1367-
1368-
you could do this:
1369-
1370-
```js
1371-
foo()
1372-
;[1,2,3].forEach(bar)
1373-
```
1374-
1375-
The advantage is that the prefixes are easier to notice, once you are accustomed to never seeing lines starting with `(` or `[` without semis.
1376-
1377-
*End quote from "An Open Letter to JavaScript Leaders Regarding Semicolons".*
1357+
> [Relying on automatic semicolon insertion] is quite safe, and perfectly valid JS that every browser understands. Closure compiler, yuicompressor, packer, and jsmin all can properly minify it. There is no performance impact anywhere.
1358+
>
1359+
> I am sorry that, instead of educating you, the leaders in this language community have given you lies and fear. That was shameful. I recommend learning how statements in JS are actually terminated (and in which cases they are not terminated), so that you can write code that you find beautiful.
1360+
>
1361+
> In general, `\n` ends a statement unless:
1362+
> 1. The statement has an unclosed paren, array literal, or object literal or ends in some
1363+
> other way that is not a valid way to end a statement. (For instance, ending with `.`
1364+
> or `,`.)
1365+
> 2. The line is `--` or `++` (in which case it will decrement/increment the next token.)
1366+
> 3. It is a `for()`, `while()`, `do`, `if()`, or `else`, and there is no `{`
1367+
> 4. The next line starts with `[`, `(`, `+`, `*`, `/`, `-`, `,`, `.`, or some other
1368+
> binary operator that can only be found between two tokens in a single expression.
1369+
>
1370+
> The first is pretty obvious. Even JSLint is ok with `\n` chars in JSON and parenthesized constructs, and with `var` statements that span multiple lines ending in `,`.
1371+
>
1372+
> The second is super weird. I’ve never seen a case (outside of these sorts of conversations) where you’d want to do write `i\n++\nj`, but, point of fact, that’s parsed as `i; ++j`, not `i++; j`.
1373+
>
1374+
> The third is well understood, if generally despised. `if (x)\ny()` is equivalent to `if (x) { y() }`. The construct doesn’t end until it reaches either a block, or a statement.
1375+
>
1376+
> `;` is a valid JavaScript statement, so `if(x);` is equivalent to `if(x){}` or, “If x, do nothing.” This is more commonly applied to loops where the loop check also is the update function. Unusual, but not unheard of.
1377+
>
1378+
> The fourth is generally the fud-inducing “oh noes, you need semicolons!” case. But, as it turns out, it’s quite easy to *prefix* those lines with semicolons if you don’t mean them to be continuations of the previous line. For example, instead of this:
1379+
>
1380+
> ```js
1381+
> foo();
1382+
> [1,2,3].forEach(bar);
1383+
> ```
1384+
>
1385+
> you could do this:
1386+
>
1387+
> ```js
1388+
> foo()
1389+
> ;[1,2,3].forEach(bar)
1390+
> ```
1391+
>
1392+
> The advantage is that the prefixes are easier to notice, once you are accustomed to never seeing lines starting with `(` or `[` without semis.
13781393
13791394
[1]: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding
13801395
[2]: http://inimino.org/~inimino/blog/javascript_semicolons

0 commit comments

Comments
 (0)