Skip to content

Commit 3740084

Browse files
authored
Update style for rule fail/pass examples (3/5) (#2755)
1 parent a3b2e90 commit 3740084

25 files changed

+526
-424
lines changed

docs/rules/no-unreadable-iife.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,28 @@
77

88
[IIFE](https://en.wikipedia.org/wiki/Immediately_invoked_function_expression) with parenthesized arrow function body is considered unreadable.
99

10-
## Fail
10+
## Examples
1111

1212
```js
13+
//
1314
const foo = (bar => (bar ? bar.baz : baz))(getBar());
14-
```
1515

16-
```js
17-
const foo = ((bar, baz) => ({bar, baz}))(bar, baz);
18-
```
19-
20-
## Pass
21-
22-
```js
16+
//
2317
const bar = getBar();
2418
const foo = bar ? bar.baz : baz;
25-
```
2619

27-
```js
20+
//
2821
const getBaz = bar => (bar ? bar.baz : baz);
2922
const foo = getBaz(getBar());
3023
```
3124

3225
```js
26+
//
27+
const foo = ((bar, baz) => ({bar, baz}))(bar, baz);
28+
```
29+
30+
```js
31+
//
3332
const foo = (bar => {
3433
return bar ? bar.baz : baz;
3534
})(getBar());

docs/rules/no-unused-properties.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,32 @@ const ClassName = {
3232
};
3333
```
3434

35-
## Fail
35+
## Examples
3636

3737
```js
38-
const enum = {
38+
//
39+
const myEnum = {
3940
used: 1,
40-
unused: 2 // <- Property `unused` is defined but never used.
41+
unused: 2, // <- Property `unused` is defined but never used.
4142
};
4243

43-
console.log(enum.used);
44+
console.log(myEnum.used);
4445

45-
const {used} = enum;
46+
const {used} = myEnum;
4647
```
4748

48-
## Pass
49-
5049
```js
51-
const enum = {
50+
//
51+
const myEnum = {
5252
used: 1,
53-
usedToo: 2
53+
usedToo: 2,
5454
};
5555

56-
console.log(enum); // The whole object is used
56+
console.log(myEnum); // The whole object is used
5757

58-
console.log(enum.used, enum.usedToo); // Every property is used individually
58+
console.log(myEnum.used, myEnum.usedToo); // Every property is used individually
5959

60-
enum[x] // Unpredictable, all properties are considered to be used
60+
myEnum[x] // Unpredictable, all properties are considered to be used
6161

6262
// Objects with methods are skipped too, all properties are considered used
6363
const foo = {

docs/rules/no-useless-error-capture-stack-trace.md

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,54 @@ Calling [`Error.captureStackTrace(…)`](https://developer.mozilla.org/en-US/doc
1212
## Examples
1313

1414
```js
15+
//
1516
class MyError extends Error {
1617
constructor() {
17-
//
1818
Error.captureStackTrace(this, MyError);
19-
//
19+
}
20+
}
21+
```
22+
23+
```js
24+
//
25+
class MyError extends Error {
26+
constructor() {
2027
Error.captureStackTrace?.(this, MyError);
21-
//
28+
}
29+
}
30+
```
31+
32+
```js
33+
//
34+
class MyError extends Error {
35+
constructor() {
2236
Error.captureStackTrace(this, this.constructor);
23-
//
37+
}
38+
}
39+
```
40+
41+
```js
42+
//
43+
class MyError extends Error {
44+
constructor() {
2445
Error.captureStackTrace?.(this, this.constructor);
25-
//
46+
}
47+
}
48+
```
49+
50+
```js
51+
//
52+
class MyError extends Error {
53+
constructor() {
2654
Error.captureStackTrace(this, new.target);
27-
//
55+
}
56+
}
57+
```
58+
59+
```js
60+
//
61+
class MyError extends Error {
62+
constructor() {
2863
Error.captureStackTrace?.(this, new.target);
2964
}
3065
}

docs/rules/no-useless-fallback-in-spread.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,25 @@
99

1010
Spreading [falsy values](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) in object literals won't add any unexpected properties, so it's unnecessary to add an empty object as fallback.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
const object = {...(foo || {})};
16-
```
1717

18-
```js
18+
//
1919
const object = {...(foo ?? {})};
20-
```
2120

22-
## Pass
23-
24-
```js
21+
//
2522
const object = {...foo};
2623
```
2724
2825
```js
26+
//
2927
const object = {...(foo && {})};
3028
```
3129
3230
```js
31+
//
3332
const array = [...(foo || [])];
3433
```

docs/rules/no-useless-length-check.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,48 @@
1212

1313
We only check `.length === 0`, `.length !== 0`, and `.length > 0`. These zero and non-zero length check styles are allowed in the [`unicorn/explicit-length-check`](./explicit-length-check.md#options) rule. It is recommended to use them together.
1414

15-
## Fail
15+
## Examples
1616

1717
```js
18+
//
1819
if (array.length === 0 || array.every(Boolean));
1920
```
2021

2122
```js
23+
//
2224
if (array.length !== 0 && array.some(Boolean));
23-
```
24-
25-
```js
26-
if (array.length > 0 && array.some(Boolean));
27-
```
2825

29-
```js
30-
const isAllTrulyOrEmpty = array.length === 0 || array.every(Boolean);
31-
```
32-
33-
## Pass
34-
35-
```js
26+
//
3627
if (array.every(Boolean));
3728
```
3829

3930
```js
31+
//
32+
if (array.length > 0 && array.some(Boolean));
33+
34+
//
4035
if (array.some(Boolean));
4136
```
4237

4338
```js
39+
//
40+
const isAllTrulyOrEmpty = array.length === 0 || array.every(Boolean);
41+
42+
//
4443
const isAllTrulyOrEmpty = array.every(Boolean);
4544
```
4645

4746
```js
47+
//
4848
if (array.length === 0 || anotherCheck() || array.every(Boolean));
4949
```
5050

5151
```js
52+
//
5253
const isNonEmptyAllTrulyArray = array.length > 0 && array.every(Boolean);
5354
```
5455

5556
```js
57+
//
5658
const isEmptyArrayOrAllTruly = array.length === 0 || array.some(Boolean);
5759
```

docs/rules/no-useless-promise-resolve-reject.md

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
Wrapping a return value in `Promise.resolve` in an async function or a `Promise#then`/`catch`/`finally` callback is unnecessary as all return values in async functions and promise callback functions are already wrapped in a `Promise`. Similarly, returning an error wrapped in `Promise.reject` is equivalent to simply `throw`ing the error. This is the same for `yield`ing in async generators as well.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
const main = async foo => {
1617
if (foo > 4) {
1718
return Promise.reject(new Error('🤪'));
@@ -20,40 +21,43 @@ const main = async foo => {
2021
return Promise.resolve(result);
2122
};
2223

24+
//
25+
const main = async foo => {
26+
if (foo > 4) {
27+
throw new Error('🤪');
28+
}
29+
30+
return result;
31+
};
32+
```
33+
34+
```js
35+
//
2336
async function * generator() {
2437
yield Promise.resolve(result);
2538
yield Promise.reject(error);
2639
}
2740

41+
//
42+
async function * generator() {
43+
yield result;
44+
throw error;
45+
}
46+
```
47+
48+
```js
49+
//
2850
promise
2951
.then(x => {
3052
if (x % 2 == 0) {
3153
return Promise.resolve(x / 2);
3254
}
3355

3456
return Promise.reject(new Error('odd number'));
35-
});
57+
})
3658
.catch(error => Promise.reject(new FancyError(error)));
3759

38-
promise.finally(() => Promise.reject(new Error('oh no')));
39-
```
40-
41-
## Pass
42-
43-
```js
44-
const main = async foo => {
45-
if (foo > 4) {
46-
throw new Error('🤪');
47-
}
48-
49-
return result;
50-
};
51-
52-
async function * generator() {
53-
yield result;
54-
throw error;
55-
}
56-
60+
//
5761
promise
5862
.then(x => {
5963
if (x % 2 == 0) {
@@ -65,7 +69,13 @@ promise
6569
.catch(error => {
6670
throw new FancyError(error);
6771
});
72+
```
73+
74+
```js
75+
//
76+
promise.finally(() => Promise.reject(new Error('oh no')));
6877

78+
//
6979
promise.finally(() => {
7080
throw new Error('oh no');
7181
});

0 commit comments

Comments
 (0)