From 6abc7b72dfb824a372379708ca39340b2c7abc03 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Mon, 26 Aug 2019 19:24:55 +0200 Subject: [PATCH] Docs: Document the exception in no-unsafe-negation (#12161) --- docs/rules/no-unsafe-negation.md | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/docs/rules/no-unsafe-negation.md b/docs/rules/no-unsafe-negation.md index 8e1428ea959..880e9f64481 100644 --- a/docs/rules/no-unsafe-negation.md +++ b/docs/rules/no-unsafe-negation.md @@ -39,10 +39,36 @@ if (!(key in object)) { if (!(obj instanceof Ctor)) { // obj is not an instance of Ctor } +``` + +### Exception + +For rare situations when negating the left operand is intended, this rule allows an exception. +If the whole negation is explicitly wrapped in parentheses, the rule will not report a problem. + +Examples of **correct** code for this rule: + +```js +/*eslint no-unsafe-negation: "error"*/ + +if ((!foo) in object) { + // allowed, because the negation is explicitly wrapped in parentheses + // it is equivalent to (foo ? "false" : "true") in object + // this is allowed as an exception for rare situations when that is the intended meaning +} + +if(("" + !foo) in object) { + // you can also make the intention more explicit, with type conversion +} +``` + +Examples of **incorrect** code for this rule: + +```js +/*eslint no-unsafe-negation: "error"*/ -if(("" + !key) in object) { - // make operator precedence and type conversion explicit - // in a rare situation when that is the intended meaning +if (!(foo) in object) { + // this is not an allowed exception } ```