Skip to content

Rename no-array-push-push to prefer-single-call, support Element#classList.{add,remove}() and importScripts(), Update options #2617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/deleted-and-deprecated-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

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

### no-array-push-push

Replaced by [`prefer-single-call`](rules/prefer-single-call.md) which covers more cases.

### no-length-as-slice-end

Replaced by [`no-unnecessary-slice-end`](rules/no-unnecessary-slice-end.md) which covers more cases.
Expand Down
70 changes: 0 additions & 70 deletions docs/rules/no-array-push-push.md

This file was deleted.

80 changes: 80 additions & 0 deletions docs/rules/prefer-single-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Enforce combining multiple `Array#push()`, `Element#classList.{add,remove}()`, and `importScripts()` into one call

💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config).

🔧💡 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).

<!-- end auto-generated rule header -->
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` -->

[`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.

## Examples

```js
// ❌
foo.push(1);
foo.push(2, 3);

// ✅
foo.push(1, 2, 3);
```

```js
// ❌
element.classList.add('foo');
element.classList.add('bar', 'baz');

// ✅
element.classList.add('foo', 'bar', 'baz');
```

```js
// ❌
importScripts("https://example.com/foo.js");
importScripts("https://example.com/bar.js");

// ✅
importScripts(
"https://example.com/foo.js",
"https://example.com/bar.js",
);
```

## Options

Type: `object`

### ignore

Type: `string[]`

Functions to ignore.

`stream.push`, `this.push`, and `this.stream.push` are ignored by default.

Example:

```js
{
'unicorn/prefer-single-call': [
'error',
{
ignore: [
'readable.push',
'foo.stream.push'
]
}
]
}
```

```js
// eslint unicorn/prefer-single-call: ["error", {"ignore": ["readable"]}]
import {Readable} from 'node:stream';

const readable = new Readable();
readable.push('one');
readable.push('another');
readable.push(null);
```
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
message: 'Replaced by `unicorn/no-unnecessary-slice-end` which covers more cases.',
replacedBy: ['unicorn/no-unnecessary-slice-end'],
},
'no-array-push-push': {
message: 'Replaced by `unicorn/prefer-single-call` which covers more cases.',
replacedBy: ['unicorn/prefer-single-call'],
},
});

const externalRules = {
Expand Down Expand Up @@ -63,7 +67,7 @@
recommended: createConfig(recommendedRules, 'unicorn/recommended'),
all: createConfig(allRules, 'unicorn/all'),

// TODO: Remove this at some point. Kept for now to avoid breaking users.

Check warning on line 70 in index.js

View workflow job for this annotation

GitHub Actions / lint-test (ubuntu-latest)

Unexpected 'todo' comment: 'TODO: Remove this at some point. Kept...'

Check warning on line 70 in index.js

View workflow job for this annotation

GitHub Actions / lint-test (windows-latest)

Unexpected 'todo' comment: 'TODO: Remove this at some point. Kept...'
'flat/recommended': createConfig(recommendedRules, 'unicorn/flat/recommended'),
'flat/all': createConfig(allRules, 'unicorn/flat/all'),
};
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export default [
| [no-array-callback-reference](docs/rules/no-array-callback-reference.md) | Prevent passing a function reference directly to iterator methods. | ✅ | | 💡 |
| [no-array-for-each](docs/rules/no-array-for-each.md) | Prefer `for…of` over the `forEach` method. | ✅ | 🔧 | 💡 |
| [no-array-method-this-argument](docs/rules/no-array-method-this-argument.md) | Disallow using the `this` argument in array methods. | ✅ | 🔧 | 💡 |
| [no-array-push-push](docs/rules/no-array-push-push.md) | Enforce combining multiple `Array#push()` into one call. | ✅ | 🔧 | 💡 |
| [no-array-reduce](docs/rules/no-array-reduce.md) | Disallow `Array#reduce()` and `Array#reduceRight()`. | ✅ | | |
| [no-await-expression-member](docs/rules/no-await-expression-member.md) | Disallow member access from await expression. | ✅ | 🔧 | |
| [no-await-in-promise-methods](docs/rules/no-await-in-promise-methods.md) | Disallow using `await` in `Promise` method parameters. | ✅ | | 💡 |
Expand Down Expand Up @@ -165,6 +164,7 @@ export default [
| [prefer-regexp-test](docs/rules/prefer-regexp-test.md) | Prefer `RegExp#test()` over `String#match()` and `RegExp#exec()`. | ✅ | 🔧 | 💡 |
| [prefer-set-has](docs/rules/prefer-set-has.md) | Prefer `Set#has()` over `Array#includes()` when checking for existence or non-existence. | ✅ | 🔧 | 💡 |
| [prefer-set-size](docs/rules/prefer-set-size.md) | Prefer using `Set#size` instead of `Array#length`. | ✅ | 🔧 | |
| [prefer-single-call](docs/rules/prefer-single-call.md) | Enforce combining multiple `Array#push()`, `Element#classList.{add,remove}()`, and `importScripts()` into one call. | ✅ | 🔧 | 💡 |
| [prefer-spread](docs/rules/prefer-spread.md) | Prefer the spread operator over `Array.from(…)`, `Array#concat(…)`, `Array#{slice,toSpliced}()` and `String#split('')`. | ✅ | 🔧 | 💡 |
| [prefer-string-raw](docs/rules/prefer-string-raw.md) | Prefer using the `String.raw` tag to avoid escaping `\`. | ✅ | 🔧 | |
| [prefer-string-replace-all](docs/rules/prefer-string-replace-all.md) | Prefer `String#replaceAll()` over regex searches with the global flag. | ✅ | 🔧 | |
Expand Down
4 changes: 2 additions & 2 deletions rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import noAnonymousDefaultExport from './no-anonymous-default-export.js';
import noArrayCallbackReference from './no-array-callback-reference.js';
import noArrayForEach from './no-array-for-each.js';
import noArrayMethodThisArgument from './no-array-method-this-argument.js';
import noArrayPushPush from './no-array-push-push.js';
import noArrayReduce from './no-array-reduce.js';
import noAwaitExpressionMember from './no-await-expression-member.js';
import noAwaitInPromiseMethods from './no-await-in-promise-methods.js';
Expand Down Expand Up @@ -110,6 +109,7 @@ import preferReflectApply from './prefer-reflect-apply.js';
import preferRegexpTest from './prefer-regexp-test.js';
import preferSetHas from './prefer-set-has.js';
import preferSetSize from './prefer-set-size.js';
import preferSingleCall from './prefer-single-call.js';
import preferSpread from './prefer-spread.js';
import preferStringRaw from './prefer-string-raw.js';
import preferStringReplaceAll from './prefer-string-replace-all.js';
Expand Down Expand Up @@ -156,7 +156,6 @@ const rules = {
'no-array-callback-reference': createRule(noArrayCallbackReference, 'no-array-callback-reference'),
'no-array-for-each': createRule(noArrayForEach, 'no-array-for-each'),
'no-array-method-this-argument': createRule(noArrayMethodThisArgument, 'no-array-method-this-argument'),
'no-array-push-push': createRule(noArrayPushPush, 'no-array-push-push'),
'no-array-reduce': createRule(noArrayReduce, 'no-array-reduce'),
'no-await-expression-member': createRule(noAwaitExpressionMember, 'no-await-expression-member'),
'no-await-in-promise-methods': createRule(noAwaitInPromiseMethods, 'no-await-in-promise-methods'),
Expand Down Expand Up @@ -242,6 +241,7 @@ const rules = {
'prefer-regexp-test': createRule(preferRegexpTest, 'prefer-regexp-test'),
'prefer-set-has': createRule(preferSetHas, 'prefer-set-has'),
'prefer-set-size': createRule(preferSetSize, 'prefer-set-size'),
'prefer-single-call': createRule(preferSingleCall, 'prefer-single-call'),
'prefer-spread': createRule(preferSpread, 'prefer-spread'),
'prefer-string-raw': createRule(preferStringRaw, 'prefer-string-raw'),
'prefer-string-replace-all': createRule(preferStringReplaceAll, 'prefer-string-replace-all'),
Expand Down
150 changes: 0 additions & 150 deletions rules/no-array-push-push.js

This file was deleted.

Loading
Loading