unicorn/prefer-single-call
Require one call instead of several in a row to push(), unshift(), classList.add() and the like
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 two push() or unshift() calls if the file shows that the object is an array: a variable that's declared with [], new Array(), Array.from() or Array.of() and never assigned again. Other objects have a push() too. The second argument of a stream's push() is the encoding. The arguments have to be plain values, because rows.push(nextId()) can depend on the push() before it. It merges two importScripts() calls if the arguments are strings.
--fix-dangerously merges the other calls as well. No flag merges them 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:
[lint.rules]
"unicorn/prefer-single-call" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"unicorn/prefer-single-call" = ["error", {
ignore = [], # list of strings
}]Examples#
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");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:
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". |
readable.push(header);
readable.pushDo not call Array#push() multiple times.(body);{ "ignore": ["readable.push"] }No problemsreadable.push(header);
readable.push(body);