Skip to content

Commit

Permalink
New: no-instanceof-array rule
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Oct 8, 2016
1 parent 3be21b0 commit f0f4539
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ npm install --save-dev eslint eslint-plugin-mysticatea
"rules": {
"mysticatea/arrow-parens": "error",
"mysticatea/block-scoped-var": "error",
"mysticatea/no-instanceof-array": "error",
"mysticatea/no-instanceof-wrapper": "error",
"mysticatea/no-literal-call": "error",
"mysticatea/no-this-in-static": "error",
Expand All @@ -43,6 +44,7 @@ npm install --save-dev eslint eslint-plugin-mysticatea

- [arrow-parens](docs/rules/arrow-parens.md) - Enforce parens of argument lists (excludes too redundant parens) (fixable).
- [block-scoped-var](docs/rules/block-scoped-var.md) - The complete emulation of block-scoping for `var`.
- [no-instanceof-array](docs/rules/no-instanceof-array.md) - Disallow 'instanceof' for Array.
- [no-instanceof-wrapper](docs/rules/no-instanceof-wrapper.md) - Disallow 'instanceof' for wrapper objects.
- [no-literal-call](docs/rules/no-literal-call.md) - Disallow a call of a literal.
- [no-this-in-static](docs/rules/no-this-in-static.md) - Disallow `this`/`super` in static methods.
Expand Down
19 changes: 19 additions & 0 deletions docs/rules/no-instanceof-array.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Disallow 'instanceof' for Array (no-instanceof-array)

## Rule Details

Examples of **incorrect** code for this rule:

```js
/*eslint no-instanceof-array: "error"*/

x instanceof Array
```

Examples of **correct** code for this rule:

```js
/*eslint no-instanceof-array: "error"*/

Array.isArray(x)
```
72 changes: 72 additions & 0 deletions lib/rules/no-instanceof-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

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

module.exports = {
meta: {
docs: {
description: "disallow 'instanceof' for Array",
category: "Best Practices",
},
fixable: true,
schema: [],
},

create(context) {
const sourceCode = context.getSourceCode()

/**
* Checks whether the given node is RHS of instanceof.
*
* @param {ASTNode} node - The node to check.
* @returns {boolean} `true` if the node is RHS of instanceof.
*/
function isRhsOfInstanceof(node) {
return (
node.parent.type === "BinaryExpression" &&
node.parent.operator === "instanceof" &&
node.parent.right === node
)
}

return {
"Program:exit"() {
const globalScope = context.getScope()
const variable = globalScope.set.get("Array")

// Skip if undefined or shadowed
if (variable == null || variable.defs.length > 0) {
return
}

for (const reference of variable.references) {
const id = reference.identifier
const node = id.parent

// Skip if it's not instanceof
if (!isRhsOfInstanceof(id)) {
continue
}

// Report
context.report({
node,
loc: node.loc,
message: "Unexpected 'instanceof' operator. Use 'Array.isArray' instead.",
fix: (fixer) => fixer.replaceText(
node,
`Array.isArray(${sourceCode.getText(node.left)})`
),
})
}
},
}
},
}
34 changes: 34 additions & 0 deletions tests/lib/rules/no-instanceof-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"

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

const RuleTester = require("eslint").RuleTester
const rule = require("../../../lib/rules/no-instanceof-array")

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

const tester = new RuleTester()

tester.run("no-instanceof-array", rule, {
valid: [
"Array",
"Array.isArray(x)",
"function foo(Array) { x instanceof Array }",
],
invalid: [
{
code: "x instanceof Array",
output: "Array.isArray(x)",
errors: ["Unexpected 'instanceof' operator. Use 'Array.isArray' instead."],
},
],
})

0 comments on commit f0f4539

Please sign in to comment.