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.pushDo not call Array#push() multiple times.("email is not valid");
queue.unshift(second);
queue.unshiftDo not call Array#unshift() multiple times.(first);
button.classList.add("active");
button.classList.addDo not call Element#classList.add() multiple times.("large");
importScripts("parser.js");
importScriptsDo not call importScripts() multiple times.("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#
| Option | Default | What 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.pushDo not call Array#push() multiple times.(body);example.jswith
{ "ignore": ["readable.push"] }No problemsreadable.push(header);
readable.push(body);