Skip to content

Commit

Permalink
no-instanceof-array: Simplify fix, improve report location (#1389)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jun 30, 2021
1 parent d139ae8 commit 6512fbd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 95 deletions.
39 changes: 18 additions & 21 deletions rules/no-instanceof-array.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const {isParenthesized, isOpeningParenToken, isClosingParenToken} = require('eslint-utils');
const replaceNodeOrTokenAndSpacesBefore = require('./utils/replace-node-or-token-and-spaces-before.js');
const {getParenthesizedRange} = require('./utils/parentheses.js');

const isInstanceofToken = token => token.value === 'instanceof' && token.type === 'Keyword';

Expand All @@ -19,27 +19,24 @@ const create = context => {
const sourceCode = context.getSourceCode();

return {
[selector]: node => ({
node,
messageId: MESSAGE_ID,
* fix(fixer) {
const {left, right} = node;

let leftStartNodeOrToken = left;
let leftEndNodeOrToken = left;
if (isParenthesized(left, sourceCode)) {
leftStartNodeOrToken = sourceCode.getTokenBefore(left, isOpeningParenToken);
leftEndNodeOrToken = sourceCode.getTokenAfter(left, isClosingParenToken);
[selector](node) {
const {left, right} = node;
const instanceofToken = sourceCode.getTokenAfter(left, isInstanceofToken);

return {
node: instanceofToken,
messageId: MESSAGE_ID,
/** @param {import('eslint').Rule.RuleFixer} fixer */
* fix(fixer) {
const range = getParenthesizedRange(left, sourceCode);
yield fixer.insertTextBeforeRange(range, 'Array.isArray(');
yield fixer.insertTextAfterRange(range, ')');

yield * replaceNodeOrTokenAndSpacesBefore(instanceofToken, '', fixer, sourceCode);
yield * replaceNodeOrTokenAndSpacesBefore(right, '', fixer, sourceCode);
}

yield fixer.insertTextBefore(leftStartNodeOrToken, 'Array.isArray(');
yield fixer.insertTextAfter(leftEndNodeOrToken, ')');

const instanceofToken = sourceCode.getTokenAfter(left, isInstanceofToken);
yield * replaceNodeOrTokenAndSpacesBefore(instanceofToken, '', fixer, sourceCode);
yield * replaceNodeOrTokenAndSpacesBefore(right, '', fixer, sourceCode);
}
})
};
}
};
};

Expand Down
117 changes: 43 additions & 74 deletions test/snapshots/no-instanceof-array.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Generated by [AVA](https://avajs.dev).
`␊
> 1 | arr instanceof Array␊
| ^^^^^^^^^^^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
`

## Invalid #2
Expand All @@ -33,7 +33,7 @@ Generated by [AVA](https://avajs.dev).
`␊
> 1 | [] instanceof Array␊
| ^^^^^^^^^^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
`

## Invalid #3
Expand All @@ -49,7 +49,7 @@ Generated by [AVA](https://avajs.dev).
`␊
> 1 | [1,2,3] instanceof Array === true␊
| ^^^^^^^^^^^^^^^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
`

## Invalid #4
Expand All @@ -65,7 +65,7 @@ Generated by [AVA](https://avajs.dev).
`␊
> 1 | fun.call(1, 2, 3) instanceof Array␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
`

## Invalid #5
Expand All @@ -81,7 +81,7 @@ Generated by [AVA](https://avajs.dev).
`␊
> 1 | obj.arr instanceof Array␊
| ^^^^^^^^^^^^^^^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
`

## Invalid #6
Expand All @@ -97,7 +97,7 @@ Generated by [AVA](https://avajs.dev).
`␊
> 1 | foo.bar[2] instanceof Array␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
`

## Invalid #7
Expand All @@ -113,7 +113,7 @@ Generated by [AVA](https://avajs.dev).
`␊
> 1 | (0, array) instanceof Array␊
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
`

## Invalid #8
Expand Down Expand Up @@ -155,19 +155,19 @@ Generated by [AVA](https://avajs.dev).
> Output
`␊
1 | (␊
1 | Array.isArray((␊
2 | // comment␊
3 | ((␊
4 | // comment␊
5 | Array.isArray((␊
5 | (␊
6 | // comment␊
7 | foo␊
8 | // comment␊
9 | ))
9 | )␊
10 | // comment␊
11 | ))␊
12 | // comment␊
13 | )␊
13 | ))
14 | // comment before instanceof␊
15 |␊
16 | // comment after instanceof␊
Expand All @@ -190,70 +190,39 @@ Generated by [AVA](https://avajs.dev).
> Error 1/1
`␊
> 1 | (␊
| ^␊
> 2 | // comment␊
| ^^^^^^^^^^^␊
> 3 | ((␊
| ^^^^^^^^^^^␊
> 4 | // comment␊
| ^^^^^^^^^^^␊
> 5 | (␊
| ^^^^^^^^^^^␊
> 6 | // comment␊
| ^^^^^^^^^^^␊
> 7 | foo␊
| ^^^^^^^^^^^␊
> 8 | // comment␊
| ^^^^^^^^^^^␊
> 9 | )␊
| ^^^^^^^^^^^␊
> 10 | // comment␊
| ^^^^^^^^^^^␊
> 11 | ))␊
| ^^^^^^^^^^^␊
> 12 | // comment␊
| ^^^^^^^^^^^␊
> 13 | )␊
| ^^^^^^^^^^^␊
> 14 | // comment before instanceof␊
| ^^^^^^^^^^^␊
1 | (␊
2 | // comment␊
3 | ((␊
4 | // comment␊
5 | (␊
6 | // comment␊
7 | foo␊
8 | // comment␊
9 | )␊
10 | // comment␊
11 | ))␊
12 | // comment␊
13 | )␊
14 | // comment before instanceof␊
> 15 | instanceof␊
| ^^^^^^^^^^^␊
> 16 |␊
| ^^^^^^^^^^^␊
> 17 | // comment after instanceof␊
| ^^^^^^^^^^^␊
> 18 |␊
| ^^^^^^^^^^^␊
> 19 | (␊
| ^^^^^^^^^^^␊
> 20 | // comment␊
| ^^^^^^^^^^^␊
> 21 |␊
| ^^^^^^^^^^^␊
> 22 | (␊
| ^^^^^^^^^^^␊
> 23 |␊
| ^^^^^^^^^^^␊
> 24 | // comment␊
| ^^^^^^^^^^^␊
> 25 |␊
| ^^^^^^^^^^^␊
> 26 | Array␊
| ^^^^^^^^^^^␊
> 27 |␊
| ^^^^^^^^^^^␊
> 28 | // comment␊
| ^^^^^^^^^^^␊
> 29 | )␊
| ^^^^^^^^^^^␊
> 30 |␊
| ^^^^^^^^^^^␊
> 31 | // comment␊
| ^^^^^^^^^^^␊
> 32 | )␊
| ^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
16 |␊
17 | // comment after instanceof␊
18 |␊
19 | (␊
20 | // comment␊
21 |␊
22 | (␊
23 |␊
24 | // comment␊
25 |␊
26 | Array␊
27 |␊
28 | // comment␊
29 | )␊
30 |␊
31 | // comment␊
32 | )␊
33 |␊
34 | // comment␊
`
Binary file modified test/snapshots/no-instanceof-array.mjs.snap
Binary file not shown.

0 comments on commit 6512fbd

Please sign in to comment.