Skip to content

Commit e117783

Browse files
authored
Rename no-array-push-push to prefer-single-call, support Element#classList.{add,remove}() and importScripts() (#2617)
1 parent 7cd91b8 commit e117783

14 files changed

+2397
-1093
lines changed

docs/deleted-and-deprecated-rules.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
Replaced by [`no-instanceof-builtins`](rules/no-instanceof-builtins.md) which covers more cases.
88

9+
### no-array-push-push
10+
11+
Replaced by [`prefer-single-call`](rules/prefer-single-call.md) which covers more cases.
12+
913
### no-length-as-slice-end
1014

1115
Replaced by [`no-unnecessary-slice-end`](rules/no-unnecessary-slice-end.md) which covers more cases.

docs/rules/no-array-push-push.md

-70
This file was deleted.

docs/rules/prefer-single-call.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Enforce combining multiple `Array#push()`, `Element#classList.{add,remove}()`, and `importScripts()` into one call
2+
3+
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config).
4+
5+
🔧💡 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix) and manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
6+
7+
<!-- end auto-generated rule header -->
8+
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` -->
9+
10+
[`Array#push()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push), [`Element#classList.add()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList), [`Element#classList.remove()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList), and [`importScripts`](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts) accepts multiple arguments. Multiple calls should be combined into one.
11+
12+
## Examples
13+
14+
```js
15+
//
16+
foo.push(1);
17+
foo.push(2, 3);
18+
19+
//
20+
foo.push(1, 2, 3);
21+
```
22+
23+
```js
24+
//
25+
element.classList.add('foo');
26+
element.classList.add('bar', 'baz');
27+
28+
//
29+
element.classList.add('foo', 'bar', 'baz');
30+
```
31+
32+
```js
33+
//
34+
importScripts("https://example.com/foo.js");
35+
importScripts("https://example.com/bar.js");
36+
37+
//
38+
importScripts(
39+
"https://example.com/foo.js",
40+
"https://example.com/bar.js",
41+
);
42+
```
43+
44+
## Options
45+
46+
Type: `object`
47+
48+
### ignore
49+
50+
Type: `string[]`
51+
52+
Functions to ignore.
53+
54+
`stream.push`, `this.push`, and `this.stream.push` are ignored by default.
55+
56+
Example:
57+
58+
```js
59+
{
60+
'unicorn/prefer-single-call': [
61+
'error',
62+
{
63+
ignore: [
64+
'readable.push',
65+
'foo.stream.push'
66+
]
67+
}
68+
]
69+
}
70+
```
71+
72+
```js
73+
// eslint unicorn/prefer-single-call: ["error", {"ignore": ["readable"]}]
74+
import {Readable} from 'node:stream';
75+
76+
const readable = new Readable();
77+
readable.push('one');
78+
readable.push('another');
79+
readable.push(null);
80+
```

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const deprecatedRules = createDeprecatedRules({
1313
message: 'Replaced by `unicorn/no-unnecessary-slice-end` which covers more cases.',
1414
replacedBy: ['unicorn/no-unnecessary-slice-end'],
1515
},
16+
'no-array-push-push': {
17+
message: 'Replaced by `unicorn/prefer-single-call` which covers more cases.',
18+
replacedBy: ['unicorn/prefer-single-call'],
19+
},
1620
});
1721

1822
const externalRules = {

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ export default [
7979
| [no-array-callback-reference](docs/rules/no-array-callback-reference.md) | Prevent passing a function reference directly to iterator methods. || | 💡 |
8080
| [no-array-for-each](docs/rules/no-array-for-each.md) | Prefer `for…of` over the `forEach` method. || 🔧 | 💡 |
8181
| [no-array-method-this-argument](docs/rules/no-array-method-this-argument.md) | Disallow using the `this` argument in array methods. || 🔧 | 💡 |
82-
| [no-array-push-push](docs/rules/no-array-push-push.md) | Enforce combining multiple `Array#push()` into one call. || 🔧 | 💡 |
8382
| [no-array-reduce](docs/rules/no-array-reduce.md) | Disallow `Array#reduce()` and `Array#reduceRight()`. || | |
8483
| [no-await-expression-member](docs/rules/no-await-expression-member.md) | Disallow member access from await expression. || 🔧 | |
8584
| [no-await-in-promise-methods](docs/rules/no-await-in-promise-methods.md) | Disallow using `await` in `Promise` method parameters. || | 💡 |
@@ -165,6 +164,7 @@ export default [
165164
| [prefer-regexp-test](docs/rules/prefer-regexp-test.md) | Prefer `RegExp#test()` over `String#match()` and `RegExp#exec()`. || 🔧 | 💡 |
166165
| [prefer-set-has](docs/rules/prefer-set-has.md) | Prefer `Set#has()` over `Array#includes()` when checking for existence or non-existence. || 🔧 | 💡 |
167166
| [prefer-set-size](docs/rules/prefer-set-size.md) | Prefer using `Set#size` instead of `Array#length`. || 🔧 | |
167+
| [prefer-single-call](docs/rules/prefer-single-call.md) | Enforce combining multiple `Array#push()`, `Element#classList.{add,remove}()`, and `importScripts()` into one call. || 🔧 | 💡 |
168168
| [prefer-spread](docs/rules/prefer-spread.md) | Prefer the spread operator over `Array.from(…)`, `Array#concat(…)`, `Array#{slice,toSpliced}()` and `String#split('')`. || 🔧 | 💡 |
169169
| [prefer-string-raw](docs/rules/prefer-string-raw.md) | Prefer using the `String.raw` tag to avoid escaping `\`. || 🔧 | |
170170
| [prefer-string-replace-all](docs/rules/prefer-string-replace-all.md) | Prefer `String#replaceAll()` over regex searches with the global flag. || 🔧 | |

rules/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import noAnonymousDefaultExport from './no-anonymous-default-export.js';
2424
import noArrayCallbackReference from './no-array-callback-reference.js';
2525
import noArrayForEach from './no-array-for-each.js';
2626
import noArrayMethodThisArgument from './no-array-method-this-argument.js';
27-
import noArrayPushPush from './no-array-push-push.js';
2827
import noArrayReduce from './no-array-reduce.js';
2928
import noAwaitExpressionMember from './no-await-expression-member.js';
3029
import noAwaitInPromiseMethods from './no-await-in-promise-methods.js';
@@ -110,6 +109,7 @@ import preferReflectApply from './prefer-reflect-apply.js';
110109
import preferRegexpTest from './prefer-regexp-test.js';
111110
import preferSetHas from './prefer-set-has.js';
112111
import preferSetSize from './prefer-set-size.js';
112+
import preferSingleCall from './prefer-single-call.js';
113113
import preferSpread from './prefer-spread.js';
114114
import preferStringRaw from './prefer-string-raw.js';
115115
import preferStringReplaceAll from './prefer-string-replace-all.js';
@@ -156,7 +156,6 @@ const rules = {
156156
'no-array-callback-reference': createRule(noArrayCallbackReference, 'no-array-callback-reference'),
157157
'no-array-for-each': createRule(noArrayForEach, 'no-array-for-each'),
158158
'no-array-method-this-argument': createRule(noArrayMethodThisArgument, 'no-array-method-this-argument'),
159-
'no-array-push-push': createRule(noArrayPushPush, 'no-array-push-push'),
160159
'no-array-reduce': createRule(noArrayReduce, 'no-array-reduce'),
161160
'no-await-expression-member': createRule(noAwaitExpressionMember, 'no-await-expression-member'),
162161
'no-await-in-promise-methods': createRule(noAwaitInPromiseMethods, 'no-await-in-promise-methods'),
@@ -242,6 +241,7 @@ const rules = {
242241
'prefer-regexp-test': createRule(preferRegexpTest, 'prefer-regexp-test'),
243242
'prefer-set-has': createRule(preferSetHas, 'prefer-set-has'),
244243
'prefer-set-size': createRule(preferSetSize, 'prefer-set-size'),
244+
'prefer-single-call': createRule(preferSingleCall, 'prefer-single-call'),
245245
'prefer-spread': createRule(preferSpread, 'prefer-spread'),
246246
'prefer-string-raw': createRule(preferStringRaw, 'prefer-string-raw'),
247247
'prefer-string-replace-all': createRule(preferStringReplaceAll, 'prefer-string-replace-all'),

rules/no-array-push-push.js

-150
This file was deleted.

0 commit comments

Comments
 (0)