forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Added no-negated-instanceof-lhs rule, largely based on no-negate…
…d-in-lhs. (fixes eslint#2716)
- Loading branch information
1 parent
52c42bf
commit b83fc1c
Showing
5 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}] | ||
}] | ||
}); |