unicorn/prefer-single-call

Require one call instead of several in a row to push(), unshift(), classList.add() and the like

fixable

push(), unshift(), classList.add(), classList.remove() and importScripts() take any number of arguments. Two calls in a row on the same object do the same thing as one call, with more lines.

--fix merges the two calls. It leaves them alone when there's a comment between them, or when one of the second call's arguments reads the object, as in items.push(items.length).

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/prefer-single-call" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"unicorn/prefer-single-call" = ["error", {
  ignore = [], # list of strings
}]

Examples#

example.js4 problems
errors.push("name is required");
errors.push("email is not valid");

queue.unshift(second);
queue.unshift(first);

button.classList.add("active");
button.classList.add("large");

importScripts("parser.js");
importScripts("worker-utils.js");
example.jsNo problems
errors.push("name is required", "email is not valid");

queue.unshift(first, second);

button.classList.add("active", "large");

importScripts("parser.js", "worker-utils.js");

push() on a stream is a different method that takes one chunk. push() and unshift() are never reported on stream, this, this.stream, process.stdin, process.stdout and process.stderr:

example.jsNo problems
stream.push(header);
stream.push(body);

Options#

OptionDefaultWhat it does
ignore[]More push() and unshift() calls to allow, written the way they appear in the code, like "readable.push".
example.js1 problem
readable.push(header);
readable.push(body);
example.jswith { "ignore": ["readable.push"] }No problems
readable.push(header);
readable.push(body);