From 8f1020ff711b0c57d590bf666e2841f64186d083 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Tue, 24 Dec 2019 07:21:53 +1030 Subject: [PATCH] Update: no-void add an option to allow void as a statement (#12613) * Update: no-void add an option to allow void as a statement * Test: add extra options test --- docs/rules/no-void.md | 34 ++++++++++++++++++++++++++++++ lib/rules/no-void.js | 34 +++++++++++++++++++++++++----- tests/lib/rules/no-void.js | 42 ++++++++++++++++++++++++++------------ 3 files changed, 92 insertions(+), 18 deletions(-) diff --git a/docs/rules/no-void.md b/docs/rules/no-void.md index 856c934c6e1..6b7774805db 100644 --- a/docs/rules/no-void.md +++ b/docs/rules/no-void.md @@ -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 diff --git a/lib/rules/no-void.js b/lib/rules/no-void.js index d2b5d2f9631..99c83785552 100644 --- a/lib/rules/no-void.js +++ b/lib/rules/no-void.js @@ -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" + }); } }; - } }; diff --git a/tests/lib/rules/no-void.js b/tests/lib/rules/no-void.js index 2d12718d8f7..2a1ee1d9a4f 100644 --- a/tests/lib/rules/no-void.js +++ b/tests/lib/rules/no-void.js @@ -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 @@ -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" }] } ] });