/
no-for-in-array.js
69 lines (63 loc) · 1.44 KB
/
no-for-in-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @fileoverview Disallow iterating over an array with a for-in loop
* @author Benjamin Lichtman
*/
'use strict';
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require('../../../lib/rules/no-for-in-array'),
RuleTester = require('eslint').RuleTester,
path = require('path');
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const rootDir = path.join(process.cwd(), 'tests/fixtures/');
const parserOptions = {
ecmaVersion: 2015,
tsconfigRootDir: rootDir,
project: './tsconfig.json'
};
const ruleTester = new RuleTester({
parserOptions,
parser: '@typescript-eslint/parser'
});
ruleTester.run('no-for-in-array', rule, {
valid: [
`
for (const x of [3, 4, 5]) {
console.log(x);
}`,
`
for (const x in { a: 1, b: 2, c: 3 }) {
console.log(x);
}`
],
invalid: [
{
code: `
for (const x in [3, 4, 5]) {
console.log(x);
}`,
errors: [
{
messageId: 'forInViolation',
type: 'ForInStatement'
}
]
},
{
code: `
const z = [3, 4, 5];
for (const x in z) {
console.log(x);
}`,
errors: [
{
messageId: 'forInViolation',
type: 'ForInStatement'
}
]
}
]
});