Skip to content

Commit

Permalink
Update: no-void add an option to allow void as a statement (#12613)
Browse files Browse the repository at this point in the history
* Update: no-void add an option to allow void as a statement

* Test: add extra options test
  • Loading branch information
bradzacher authored and kaicataldo committed Dec 23, 2019
1 parent bb6cf50 commit 8f1020f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 18 deletions.
34 changes: 34 additions & 0 deletions docs/rules/no-void.md
Expand Up @@ -54,8 +54,42 @@ Examples of **incorrect** code for this rule:
/*eslint no-void: "error"*/

void foo
void someFunction();

var foo = void bar();
function baz() {
return void 0;
}
```

## Options

This rule has an object option:

* `allowAsStatement` set to `true` allows the void operator to be used as a statement (Default `false`).

### allowAsStatement

When `allowAsStatement` is set to true, the rule will not error on cases that the void operator is used as a statement, i.e. when it's not used in an expression position, like in a variable assignment or a function return.

Examples of **incorrect** code for `{ "allowAsStatement": true }`:

```js
/*eslint no-void: ["error", { "allowAsStatement": true }]*/

var foo = void bar();
function baz() {
return void 0;
}
```

Examples of **correct** code for `{ "allowAsStatement": true }`:

```js
/*eslint no-void: ["error", { "allowAsStatement": true }]*/

void foo;
void someFunction();
```

## When Not To Use It
Expand Down
34 changes: 29 additions & 5 deletions lib/rules/no-void.js
Expand Up @@ -19,22 +19,46 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-void"
},

schema: []
messages: {
noVoid: "Expected 'undefined' and instead saw 'void'."
},

schema: [
{
type: "object",
properties: {
allowAsStatement: {
type: "boolean",
default: false
}
},
additionalProperties: false
}
]
},

create(context) {
const allowAsStatement =
context.options[0] && context.options[0].allowAsStatement;

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------

return {
UnaryExpression(node) {
if (node.operator === "void") {
context.report({ node, message: "Expected 'undefined' and instead saw 'void'." });
'UnaryExpression[operator="void"]'(node) {
if (
allowAsStatement &&
node.parent &&
node.parent.type === "ExpressionStatement"
) {
return;
}
context.report({
node,
messageId: "noVoid"
});
}
};

}
};
42 changes: 29 additions & 13 deletions tests/lib/rules/no-void.js
Expand Up @@ -8,8 +8,8 @@
// Requirements
//------------------------------------------------------------------------------

const rule = require("../../../lib/rules/no-void"),
{ RuleTester } = require("../../../lib/rule-tester");
const rule = require("../../../lib/rules/no-void");
const { RuleTester } = require("../../../lib/rule-tester");

//------------------------------------------------------------------------------
// Tests
Expand All @@ -18,32 +18,48 @@ const rule = require("../../../lib/rules/no-void"),
const ruleTester = new RuleTester();

ruleTester.run("no-void", rule, {

valid: [
"var foo = bar()",
"foo.void()",
"foo.void = bar",
"delete foo;"
"delete foo;",
{
code: "void 0",
options: [{ allowAsStatement: true }]
},
{
code: "void(0)",
options: [{ allowAsStatement: true }]
}
],

invalid: [
{
code: "void 0",
errors: [{
message: "Expected 'undefined' and instead saw 'void'."
}]
errors: [{ messageId: "noVoid" }]
},
{
code: "void 0",
options: [{}],
errors: [{ messageId: "noVoid" }]
},
{
code: "void 0",
options: [{ allowAsStatement: false }],
errors: [{ messageId: "noVoid" }]
},
{
code: "void(0)",
errors: [{
message: "Expected 'undefined' and instead saw 'void'."
}]
errors: [{ messageId: "noVoid" }]
},
{
code: "var foo = void 0",
errors: [{ messageId: "noVoid" }]
},
{
code: "var foo = void 0",
errors: [{
message: "Expected 'undefined' and instead saw 'void'."
}]
options: [{ allowAsStatement: true }],
errors: [{ messageId: "noVoid" }]
}
]
});

0 comments on commit 8f1020f

Please sign in to comment.