Skip to content

Commit cd7b0da

Browse files
authored
Update style for rule fail/pass examples (1/5) (#2753)
1 parent e5a7f54 commit cd7b0da

25 files changed

+486
-373
lines changed

docs/rules/better-regex.md

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,62 @@
99

1010
Note: This rule uses [`regexp-tree`](https://github.com/DmitrySoshnikov/regexp-tree) and [`clean-regexp`](https://github.com/samverschueren/clean-regexp) under the hood.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
const regex = /[0-9]/;
16-
const regex = /[^0-9]/;
17-
const regex = /[a-zA-Z0-9_]/;
18-
const regex = /[a-z0-9_]/i;
19-
const regex = /[^a-zA-Z0-9_]/;
20-
const regex = /[^a-z0-9_]/i;
21-
const regex = /[0-9]\.[a-zA-Z0-9_]\-[^0-9]/i;
22-
```
2317

24-
## Pass
18+
//
19+
const regex = /\d/;
20+
```
2521

2622
```js
27-
const regex = /\d/;
23+
//
24+
const regex = /[^0-9]/;
25+
26+
//
2827
const regex = /\D/;
28+
```
29+
30+
```js
31+
//
32+
const regex = /[a-zA-Z0-9_]/;
33+
34+
//
2935
const regex = /\w/;
36+
```
37+
38+
```js
39+
//
40+
const regex = /[a-z0-9_]/i;
41+
42+
//
3043
const regex = /\w/i;
44+
```
45+
46+
```js
47+
//
48+
const regex = /[^a-zA-Z0-9_]/;
49+
50+
//
3151
const regex = /\W/;
52+
```
53+
54+
```js
55+
//
56+
const regex = /[^a-z0-9_]/i;
57+
58+
//
3259
const regex = /\W/i;
33-
const regex = /\d\.\w\-\D/i;
60+
```
61+
62+
```js
63+
//
64+
const regex = /[0-9]\.[a-zA-Z0-9_]\-[^0-9]/i;
65+
66+
//
67+
const regex = /\d\.\w-\D/i;
3468
```
3569

3670
## Options

docs/rules/catch-error-name.md

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,52 @@ The following names are ignored:
2121
- Descriptive names, for example, `fsError` or `authError`.
2222
- Names matching [`options.ignore`](#ignore).
2323

24-
## Fail
24+
## Examples
2525

2626
```js
27+
//
2728
try {} catch (badName) {}
28-
```
2929

30-
```js
31-
// `_` is not allowed if it's used
32-
try {} catch (_) {
33-
console.log(_);
34-
}
30+
//
31+
try {} catch (error) {}
3532
```
3633

3734
```js
35+
//
3836
promise.catch(badName => {});
39-
```
40-
41-
```js
42-
promise.then(undefined, badName => {});
43-
```
44-
45-
## Pass
4637

47-
```js
48-
try {} catch (error) {}
49-
```
50-
51-
```js
38+
//
5239
promise.catch(error => {});
5340
```
5441

5542
```js
43+
//
44+
promise.then(undefined, badName => {});
45+
46+
//
5647
promise.then(undefined, error => {});
5748
```
5849

5950
```js
60-
// `_` is allowed when it's not used
51+
//
52+
try {} catch (_) {
53+
console.log(_);
54+
}
55+
56+
//
6157
try {} catch (_) {
6258
console.log(foo);
6359
}
6460
```
6561

6662
```js
63+
//
6764
// Descriptive name is allowed
6865
try {} catch (fsError) {}
6966
```
7067

7168
```js
69+
//
7270
// `error_` is allowed because of shadowed variables
7371
try {} catch (error_) {
7472
const error = new Error('🦄');

docs/rules/consistent-assert.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ assert.strictEqual(actual, expected);
1818
assert.deepStrictEqual(actual, expected);
1919

2020
//
21-
assert(divide(10, 2) === 5); // Inconsistent with other API styles
21+
assert(divide(10, 2) === 5);
2222

2323
//
2424
assert.ok(divide(10, 2) === 5);
@@ -31,7 +31,7 @@ assert.strictEqual(actual, expected);
3131
assert.deepStrictEqual(actual, expected);
3232

3333
//
34-
assert(divide(10, 2) === 5); // Inconsistent with other API styles
34+
assert(divide(10, 2) === 5);
3535

3636
//
3737
assert.ok(divide(10, 2) === 5);
@@ -44,7 +44,7 @@ assert.strictEqual(actual, expected);
4444
assert.deepStrictEqual(actual, expected);
4545

4646
//
47-
assert(divide(10, 2) === 5); // Inconsistent with other API styles
47+
assert(divide(10, 2) === 5);
4848

4949
//
5050
assert.ok(divide(10, 2) === 5);

docs/rules/consistent-destructuring.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,57 @@ Enforces the use of already destructured objects and their variables over access
1111

1212
This rule is partly fixable. It does not fix nested destructuring.
1313

14-
## Fail
14+
## Examples
1515

1616
```js
17+
//
1718
const {a} = foo;
1819
console.log(a, foo.b);
20+
21+
//
22+
const {a, b} = foo;
23+
console.log(a, b);
24+
25+
//
26+
console.log(foo.a, foo.b);
1927
```
2028

2129
```js
30+
//
2231
const {a} = foo;
2332
console.log(foo.a);
33+
34+
//
35+
const {a} = foo;
36+
console.log(a);
2437
```
2538

2639
```js
40+
//
2741
const {
28-
a: {
29-
b
30-
}
42+
a: {b},
3143
} = foo;
3244
console.log(foo.a.c);
3345
```
3446

3547
```js
48+
//
3649
const {bar} = foo;
3750
const {a} = foo.bar;
38-
```
39-
40-
## Pass
4151

42-
```js
43-
const {a} = foo;
44-
console.log(a);
45-
```
46-
47-
```js
48-
console.log(foo.a, foo.b);
52+
//
53+
const {bar} = foo;
54+
const {a} = bar;
4955
```
5056

5157
```js
58+
//
5259
const {a} = foo;
5360
console.log(a, foo.b());
5461
```
5562

5663
```js
64+
//
5765
const {a} = foo.bar;
5866
console.log(foo.bar);
5967
```

docs/rules/consistent-empty-array-spread.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,28 @@
99

1010
When spreading a ternary in an array, we can use both `[]` and `''` as fallbacks, but it's better to have consistent types in both branches.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
const array = [
1617
a,
1718
...(foo ? [b, c] : ''),
1819
];
19-
```
2020

21-
```js
21+
//
2222
const array = [
2323
a,
2424
...(foo ? 'bc' : []),
2525
];
26-
```
27-
28-
## Pass
2926

30-
```js
27+
//
3128
const array = [
3229
a,
3330
...(foo ? [b, c] : []),
3431
];
35-
```
3632

37-
```js
33+
//
3834
const array = [
3935
a,
4036
...(foo ? 'bc' : ''),

docs/rules/consistent-function-scoping.md

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

88
A function definition should be placed as close to the top-level scope as possible without breaking its captured values. This improves readability, [directly improves performance](https://stackoverflow.com/a/81329/207247) and allows JavaScript engines to [better optimize performance](https://ponyfoo.com/articles/javascript-performance-pitfalls-v8#optimization-limit).
99

10-
## Fail
10+
## Examples
1111

1212
```js
13+
//
1314
export function doFoo(foo) {
1415
// Does not capture anything from the scope, can be moved to the outer scope
1516
function doBar(bar) {
@@ -19,24 +20,34 @@ export function doFoo(foo) {
1920
return doBar;
2021
}
2122

23+
//
24+
function doBar(bar) {
25+
return bar === 'bar';
26+
}
27+
28+
export function doFoo(foo) {
29+
return doBar;
30+
}
31+
```
32+
33+
```js
34+
//
2235
function doFoo(foo) {
2336
const doBar = bar => {
2437
return bar === 'bar';
2538
};
2639
}
27-
```
2840

29-
## Pass
30-
31-
```js
32-
function doBar(bar) {
41+
//
42+
const doBar = bar => {
3343
return bar === 'bar';
34-
}
44+
};
3545

36-
export function doFoo(foo) {
37-
return doBar;
38-
}
46+
function doFoo(foo) {}
47+
```
3948

49+
```js
50+
//
4051
export function doFoo(foo) {
4152
function doBar(bar) {
4253
return bar === 'bar' && foo.doBar(bar);

0 commit comments

Comments
 (0)