97 lines
2.7 KiB
Plaintext

== Why is this an issue?
The `void` operator evaluates its argument and always returns `undefined`. `void` allows using any expression where an `undefined` is expected. However, using `void` makes code more difficult to understand, as the intent is often unclear.
[source,javascript,diff-id=1,diff-type=noncompliant]
----
if (parameter === void 42) { // Noncompliant
// ...
}
doSomethingElse(void doSomething()); // Noncompliant
----
Instead of using `void` to get the `undefined` value, use the `undefined` global property. In ECMAScript5 and newer environments, `undefined` cannot be reassigned. In other cases, remove the `void` operator to avoid confusion for maintainers.
[source,javascript,diff-id=1,diff-type=compliant]
----
if (parameter === undefined) {
// ...
}
doSomething();
doSomethingElse();
----
=== Exceptions
* `void 0` (or the equivalent `void(0)`) is allowed as it was a conventional way to obtain the `undefined` value in environments before ECMAScript 5.
[source,javascript]
----
if (parameter === void 0) {
// ...
}
----
* `void` is allowed with immediately invoked function expressions.
[source,javascript]
----
void function() {
// ...
}();
----
* `void` is allowed with Promise-like objects to mark a promise as intentionally not awaited, as advised by https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/no-floating-promises.md[@typescript-eslint/no-floating-promises].
[source,javascript]
----
const runPromise = () => Promise.resolve();
void runPromise();
----
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void[`void` operator]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined[`undefined`]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Glossary/IIFE[IIFE (Immediately Invoked Function Expression)]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this use of the 'void' operator.
=== Highlighting
``++void arg++``
'''
== Comments And Links
(visible only on this page)
=== on 30 Sep 2016, 10:14:28 Elena Vilchik wrote:
\[~ann.campbell.2] Hi! Could you finish this ticket? Thanks!
(I'm struggling to define severity)
=== on 30 Sep 2016, 16:47:30 Ann Campbell wrote:
\[~elena.vilchik] could you supply some code samples?
=== on 30 Sep 2016, 17:13:13 Elena Vilchik wrote:
\[~ann.campbell.2] done
=== on 14 Mar 2017, 10:33:27 Elena Vilchik wrote:
\[~ann.campbell.2] Hi! we added exclusion to this rule: ``++void 0++`` used as ``++undefined++``. Could you update main part of description, as it's outdated now. Thanks!
endif::env-github,rspecator-view[]