Skip to content

Commit

Permalink
New: Added no-negated-instanceof-lhs rule, largely based on no-negate…
Browse files Browse the repository at this point in the history
…d-in-lhs. (fixes eslint#2716)
  • Loading branch information
platinumazure committed Aug 31, 2015
1 parent 52c42bf commit b83fc1c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"no-multiple-empty-lines": [0, {"max": 2}],
"no-native-reassign": 0,
"no-negated-in-lhs": 2,
"no-negated-instanceof-lhs": 0,
"no-nested-ternary": 0,
"no-new": 0,
"no-new-func": 0,
Expand Down
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The following rules point out areas where you might have made mistakes.
* [no-invalid-regexp](no-invalid-regexp.md) - disallow invalid regular expression strings in the `RegExp` constructor (recommended)
* [no-irregular-whitespace](no-irregular-whitespace.md) - disallow irregular whitespace outside of strings and comments (recommended)
* [no-negated-in-lhs](no-negated-in-lhs.md) - disallow negation of the left operand of an `in` expression (recommended)
* [no-negated-instanceof-lhs](no-negated-instanceof-lhs.md) - disallow negation of the left operand of an `instanceof` expression
* [no-obj-calls](no-obj-calls.md) - disallow the use of object properties of the global object (`Math` and `JSON`) as functions (recommended)
* [no-regex-spaces](no-regex-spaces.md) - disallow multiple spaces in a regular expression literal (recommended)
* [no-sparse-arrays](no-sparse-arrays.md) - disallow sparse arrays (recommended)
Expand Down
25 changes: 25 additions & 0 deletions docs/rules/no-negated-instanceof-lhs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Disallow negated left operand of `instanceof` operator (no-negated-instanceof-lhs)

## Rule Details

This error is raised to highlight a potential error. Commonly, when a developer intends to write

```js
if(!(a instanceof b)) // do something
```

they will instead write

```js
if(!a instanceof b) // do something
```

This is very often not what the developer wants and should be avoided.

## When Not To Use It

Never.

## Further Reading

None.
25 changes: 25 additions & 0 deletions lib/rules/no-negated-instanceof-lhs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @fileoverview A rule to disallow negated left operands of the `in` operator
* @author Kevin Partington
*/

"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

return {

"BinaryExpression": function(node) {
if (node.operator === "instanceof" && node.left.type === "UnaryExpression" && node.left.operator === "!") {
context.report(node, "The `instanceof` expression's left operand is negated");
}
}
};

};

module.exports.schema = [];
32 changes: 32 additions & 0 deletions tests/lib/rules/no-negated-instanceof-lhs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @fileoverview Tests for the no-negated-instanceof-lhs rule
* @author Kevin Partington
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/no-negated-instanceof-lhs"),
RuleTester = require("../../../lib/testers/rule-tester");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run("no-negated-instanceof-lhs", rule, {
valid: [
"a instanceof b",
"!(a instanceof b)"
],
invalid: [{
code: "!a instanceof b",
errors: [{
message: "The `instanceof` expression's left operand is negated",
type: "BinaryExpression"
}]
}]
});

0 comments on commit b83fc1c

Please sign in to comment.