Skip to content

Commit 3b22917

Browse files
authored
no-instanceof-array: Support Vue SFC (#1410)
1 parent 33aa97d commit 3b22917

File tree

5 files changed

+104
-10
lines changed

5 files changed

+104
-10
lines changed

rules/fix/replace-node-or-token-and-spaces-before.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22
const {getParentheses} = require('../utils/parentheses.js');
33

4-
function * replaceNodeOrTokenAndSpacesBefore(nodeOrToken, replacement, fixer, sourceCode) {
5-
const tokens = getParentheses(nodeOrToken, sourceCode);
4+
function * replaceNodeOrTokenAndSpacesBefore(nodeOrToken, replacement, fixer, sourceCode, tokenStore = sourceCode) {
5+
const tokens = getParentheses(nodeOrToken, tokenStore);
66

77
for (const token of tokens) {
8-
yield * replaceNodeOrTokenAndSpacesBefore(token, '', fixer, sourceCode);
8+
yield * replaceNodeOrTokenAndSpacesBefore(token, '', fixer, sourceCode, tokenStore);
99
}
1010

1111
let [start, end] = nodeOrToken.range;

rules/no-instanceof-array.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const {checkVueTemplate} = require('./utils/rule.js');
23
const {getParenthesizedRange} = require('./utils/parentheses.js');
34
const {replaceNodeOrTokenAndSpacesBefore} = require('./fix/index.js');
45

@@ -21,27 +22,32 @@ const create = context => {
2122
return {
2223
[selector](node) {
2324
const {left, right} = node;
24-
const instanceofToken = sourceCode.getTokenAfter(left, isInstanceofToken);
25+
let tokenStore = sourceCode;
26+
let instanceofToken = tokenStore.getTokenAfter(left, isInstanceofToken);
27+
if (!instanceofToken && context.parserServices.getTemplateBodyTokenStore) {
28+
tokenStore = context.parserServices.getTemplateBodyTokenStore();
29+
instanceofToken = tokenStore.getTokenAfter(left, isInstanceofToken);
30+
}
2531

2632
return {
2733
node: instanceofToken,
2834
messageId: MESSAGE_ID,
2935
/** @param {import('eslint').Rule.RuleFixer} fixer */
3036
* fix(fixer) {
31-
const range = getParenthesizedRange(left, sourceCode);
37+
const range = getParenthesizedRange(left, tokenStore);
3238
yield fixer.insertTextBeforeRange(range, 'Array.isArray(');
3339
yield fixer.insertTextAfterRange(range, ')');
3440

35-
yield * replaceNodeOrTokenAndSpacesBefore(instanceofToken, '', fixer, sourceCode);
36-
yield * replaceNodeOrTokenAndSpacesBefore(right, '', fixer, sourceCode);
41+
yield * replaceNodeOrTokenAndSpacesBefore(instanceofToken, '', fixer, sourceCode, tokenStore);
42+
yield * replaceNodeOrTokenAndSpacesBefore(right, '', fixer, sourceCode, tokenStore);
3743
}
3844
};
3945
}
4046
};
4147
};
4248

4349
module.exports = {
44-
create,
50+
create: checkVueTemplate(create),
4551
meta: {
4652
type: 'suggestion',
4753
docs: {

test/no-instanceof-array.mjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import outdent from 'outdent';
2-
import {getTester} from './utils/test.mjs';
2+
import {getTester, parsers} from './utils/test.mjs';
33

44
const {test} = getTester(import.meta);
55

@@ -56,6 +56,14 @@ test.snapshot({
5656
)
5757
5858
// comment
59-
`
59+
`,
60+
...[
61+
'<template><div v-if="array instanceof Array" v-for="element of array"></div></template>',
62+
'<template><div v-if="(( (( array )) instanceof (( Array )) ))" v-for="element of array"></div></template>',
63+
'<template><div>{{(( (( array )) instanceof (( Array )) )) ? array.join(" | ") : array}}</div></template>',
64+
'<script>const foo = array instanceof Array</script>',
65+
'<script>const foo = (( (( array )) instanceof (( Array )) ))</script>'
66+
].map(code => ({code, parser: parsers.vue}))
6067
]
6168
});
69+

test/snapshots/no-instanceof-array.mjs.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,83 @@ Generated by [AVA](https://avajs.dev).
226226
33 |␊
227227
34 | // comment␊
228228
`
229+
230+
## Invalid #9
231+
1 | <template><div v-if="array instanceof Array" v-for="element of array"></div></template>
232+
233+
> Output
234+
235+
`␊
236+
1 | <template><div v-if="Array.isArray(array)" v-for="element of array"></div></template>␊
237+
`
238+
239+
> Error 1/1
240+
241+
`␊
242+
> 1 | <template><div v-if="array instanceof Array" v-for="element of array"></div></template>␊
243+
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
244+
`
245+
246+
## Invalid #10
247+
1 | <template><div v-if="(( (( array )) instanceof (( Array )) ))" v-for="element of array"></div></template>
248+
249+
> Output
250+
251+
`␊
252+
1 | <template><div v-if="(( Array.isArray((( array ))) ))" v-for="element of array"></div></template>␊
253+
`
254+
255+
> Error 1/1
256+
257+
`␊
258+
> 1 | <template><div v-if="(( (( array )) instanceof (( Array )) ))" v-for="element of array"></div></template>␊
259+
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
260+
`
261+
262+
## Invalid #11
263+
1 | <template><div>{{(( (( array )) instanceof (( Array )) )) ? array.join(" | ") : array}}</div></template>
264+
265+
> Output
266+
267+
`␊
268+
1 | <template><div>{{(( Array.isArray((( array ))) )) ? array.join(" | ") : array}}</div></template>␊
269+
`
270+
271+
> Error 1/1
272+
273+
`␊
274+
> 1 | <template><div>{{(( (( array )) instanceof (( Array )) )) ? array.join(" | ") : array}}</div></template>␊
275+
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
276+
`
277+
278+
## Invalid #12
279+
1 | <script>const foo = array instanceof Array</script>
280+
281+
> Output
282+
283+
`␊
284+
1 | <script>const foo = Array.isArray(array)</script>␊
285+
`
286+
287+
> Error 1/1
288+
289+
`␊
290+
> 1 | <script>const foo = array instanceof Array</script>␊
291+
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
292+
`
293+
294+
## Invalid #13
295+
1 | <script>const foo = (( (( array )) instanceof (( Array )) ))</script>
296+
297+
> Output
298+
299+
`␊
300+
1 | <script>const foo = (( Array.isArray((( array ))) ))</script>␊
301+
`
302+
303+
> Error 1/1
304+
305+
`␊
306+
> 1 | <script>const foo = (( (( array )) instanceof (( Array )) ))</script>␊
307+
| ^^^^^^^^^^ Use \`Array.isArray()\` instead of \`instanceof Array\`.␊
308+
`
365 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)