Skip to content

Commit ba8a125

Browse files
authored
Update style for rule fail/pass examples (5/5) (#2757)
1 parent dc8c14b commit ba8a125

24 files changed

+261
-297
lines changed

docs/rules/prefer-regexp-test.md

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,25 @@
99

1010
When you want to know whether a pattern is found in a string, use [`RegExp#test()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test) instead of [`String#match()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) and [`RegExp#exec()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec), as it exclusively returns a boolean and therefore is more efficient.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
if (string.match(/unicorn/)) {}
16-
```
1717

18-
```js
18+
//
1919
if (/unicorn/.exec(string)) {}
20-
```
2120

22-
```vue
23-
<template>
24-
<div v-if="/unicorn/.exec(string)">Vue</div>
25-
</template>
26-
```
27-
28-
## Pass
29-
30-
```js
21+
//
3122
if (/unicorn/.test(string)) {}
3223
```
3324

3425
```vue
3526
<template>
27+
<!-- ❌ -->
28+
<div v-if="/unicorn/.exec(string)">Vue</div>
29+
30+
<!-- ✅ -->
3631
<div v-if="/unicorn/.test(string)">Vue</div>
3732
</template>
3833
```

docs/rules/prefer-set-has.md

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

1010
[`Set#has()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/has) is faster than [`Array#includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes).
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
const array = [1, 2, 3];
1617
const hasValue = value => array.includes(value);
17-
```
18-
19-
## Pass
2018

21-
```js
19+
//
2220
const set = new Set([1, 2, 3]);
2321
const hasValue = value => set.has(value);
2422
```
2523

2624
```js
25+
//
2726
// This array is not only checking existence.
2827
const array = [1, 2];
2928
const hasValue = value => array.includes(value);
3029
array.push(3);
3130
```
3231

3332
```js
33+
//
3434
// This array is only checked once.
3535
const array = [1, 2, 3];
3636
const hasOne = array.includes(1);

docs/rules/prefer-set-size.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@
99

1010
Prefer using `Set#size` directly instead of first converting it to an array and then using its `.length` property.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
function isUnique(array) {
1617
return [...new Set(array)].length === array.length;
1718
}
18-
```
19-
20-
## Pass
2119

22-
```js
20+
//
2321
function isUnique(array) {
2422
return new Set(array).size === array.length;
2523
}

docs/rules/prefer-spread.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,51 @@ Enforces the use of [the spread operator (`...`)](https://developer.mozilla.org/
3737

3838
To enforce the spread operator over `Object#assign()`, use the built-in [`prefer-object-spread` rule](https://eslint.org/docs/rules/prefer-object-spread).
3939

40-
## Fail
40+
## Examples
4141

4242
```js
43+
//
4344
Array.from(set).map(element => foo(element));
45+
46+
//
47+
[...set].map(element => foo(element));
4448
```
4549

4650
```js
51+
//
4752
const array = array1.concat(array2);
53+
54+
//
55+
const array = [...array1, ...array2];
4856
```
4957

5058
```js
59+
//
5160
const copy = array.slice();
52-
```
5361

54-
```js
62+
//
5563
const copy = array.slice(0);
56-
```
5764

58-
```js
65+
//
5966
const copy = array.toSpliced();
60-
```
6167

62-
```js
63-
const characters = string.split('');
68+
//
69+
const copy = [...array];
6470
```
6571

66-
## Pass
67-
6872
```js
69-
[...set].map(element => foo(element));
70-
```
73+
//
74+
const characters = string.split('');
7175

72-
```js
73-
const array = [...array1, ...array2];
76+
//
77+
const characters = [...string];
7478
```
7579

7680
```js
81+
//
7782
const tail = array.slice(1);
7883
```
7984

80-
```js
81-
const copy = [...array];
82-
```
83-
84-
```js
85-
const characters = [...string];
86-
```
87-
8885
## With the `unicorn/no-useless-spread` rule
8986

9087
Some cases are fixed using extra spread syntax. Therefore we recommend enabling the [`unicorn/no-useless-spread`](./no-useless-spread.md) rule to fix it.

docs/rules/prefer-string-raw.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@
99

1010
[`String.raw`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw) can be used to avoid escaping `\`.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
const file = "C:\\windows\\style\\path\\to\\file.js";
16-
```
17-
18-
```js
19-
const regexp = new RegExp('foo\\.bar');
20-
```
2117

22-
## Pass
23-
24-
```js
18+
//
2519
const file = String.raw`C:\windows\style\path\to\file.js`;
2620
```
2721

2822
```js
23+
//
24+
const regexp = new RegExp('foo\\.bar');
25+
26+
//
2927
const regexp = new RegExp(String.raw`foo\.bar`);
3028
```

docs/rules/prefer-string-replace-all.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,59 @@
99

1010
The [`String#replaceAll()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll) method is both faster and safer as you don't have to use a regex and remember to escape it if the string is not a literal. And when used with a regex, it makes the intent clearer.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
string.replace(/RegExp with global flag/igu, '');
17+
18+
//
19+
string.replaceAll(/RegExp with global flag/igu, '');
1620
```
1721

1822
```js
23+
//
1924
string.replace(/RegExp without special symbols/g, '');
25+
26+
//
27+
string.replaceAll('RegExp without special symbols', '');
2028
```
2129

2230
```js
31+
//
2332
string.replace(/\(It also checks for escaped regex symbols\)/g, '');
33+
34+
//
35+
string.replaceAll('(It also checks for escaped regex symbols)', '');
2436
```
2537

2638
```js
39+
//
2740
string.replace(/Works for u flag too/gu, '');
41+
42+
//
43+
string.replaceAll('Works for u flag too', '');
2844
```
2945

3046
```js
47+
//
3148
string.replaceAll(/foo/g, 'bar');
32-
```
3349

34-
## Pass
50+
//
51+
string.replaceAll('foo', 'bar');
52+
```
3553

3654
```js
55+
//
3756
string.replace(/Non-global regexp/iu, '');
3857
```
3958

4059
```js
60+
//
4161
string.replace('Not a regex expression', '')
4262
```
4363

4464
```js
45-
string.replaceAll('string', '');
46-
```
47-
48-
```js
65+
//
4966
string.replaceAll(/\s/g, '');
5067
```
51-
52-
```js
53-
string.replaceAll('foo', 'bar');
54-
```

docs/rules/prefer-string-slice.md

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

1010
[`String#substr()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) and [`String#substring()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) are the two lesser known legacy ways to slice a string. It's better to use [`String#slice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) as it's a more popular option with clearer behavior that has a consistent [`Array` counterpart](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice).
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
foo.substr(start, length);
16-
foo.substring(indexStart, indexEnd);
17-
```
1817

19-
## Pass
18+
//
19+
foo.substring(indexStart, indexEnd);
2020

21-
```js
21+
//
2222
foo.slice(beginIndex, endIndex);
2323
```

docs/rules/prefer-string-starts-ends-with.md

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,25 @@ Prefer [`String#startsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScr
1111

1212
This rule is fixable, unless the matching object is known not a string.
1313

14-
## Fail
14+
## Examples
1515

1616
```js
17+
//
1718
const foo = /^bar/.test(baz);
18-
```
19-
20-
```js
21-
const foo = /bar$/.test(baz);
22-
```
2319

24-
## Pass
25-
26-
```js
20+
//
2721
const foo = baz.startsWith('bar');
2822
```
2923

3024
```js
31-
const foo = baz.endsWith('bar');
32-
```
33-
34-
```js
35-
const foo = baz?.startsWith('bar');
36-
```
37-
38-
```js
39-
const foo = (baz ?? '').startsWith('bar');
40-
```
25+
//
26+
const foo = /bar$/.test(baz);
4127

42-
```js
43-
const foo = String(baz).startsWith('bar');
28+
//
29+
const foo = baz.endsWith('bar');
4430
```
4531

4632
```js
33+
//
4734
const foo = /^bar/i.test(baz);
4835
```

docs/rules/prefer-string-trim-start-end.md

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

1010
[`String#trimLeft()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimLeft) and [`String#trimRight()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimRight) are aliases of [`String#trimStart()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart) and [`String#trimEnd()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd). This is to ensure consistency and use [direction](https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Handling_different_text_directions)-independent wording.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
const foo = bar.trimLeft();
16-
const foo = bar.trimRight();
17-
```
1817

19-
## Pass
18+
//
19+
const foo = bar.trimStart();
20+
```
2021

2122
```js
22-
const foo = bar.trimStart();
23+
//
24+
const foo = bar.trimRight();
25+
26+
//
2327
const foo = bar.trimEnd();
2428
```

docs/rules/prefer-structured-clone.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,16 @@
99

1010
[`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) is the modern way to create a deep clone of a value.
1111

12-
## Fail
12+
## Examples
1313

1414
```js
15+
//
1516
const clone = JSON.parse(JSON.stringify(foo));
16-
```
1717

18-
```js
18+
//
1919
const clone = _.cloneDeep(foo);
20-
```
2120

22-
## Pass
23-
24-
```js
21+
//
2522
const clone = structuredClone(foo);
2623
```
2724

@@ -55,5 +52,6 @@ Example:
5552

5653
```js
5754
// eslint unicorn/prefer-structured-clone: ["error", {"functions": ["utils.clone"]}]
58-
const clone = utils.clone(foo); // Fails
55+
//
56+
const clone = utils.clone(foo);
5957
```

0 commit comments

Comments
 (0)