Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-instanceof-array: Simplify fix, improve report location #1389

Merged
merged 2 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.