Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: no-void add an option to allow void as a statement #12613

Merged
merged 2 commits into from Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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" }]
}
]
});