/
first.js
116 lines (111 loc) · 3.45 KB
/
first.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { test, getTSParsers } from '../utils';
import { RuleTester } from 'eslint';
const ruleTester = new RuleTester();
const rule = require('rules/first');
ruleTester.run('first', rule, {
valid: [
test({ code: "import { x } from './foo'; import { y } from './bar';\
export { x, y }" }),
test({ code: "import { x } from 'foo'; import { y } from './bar'" }),
test({ code: "import { x } from './foo'; import { y } from 'bar'" }),
test({ code: "import { x } from './foo'; import { y } from 'bar'",
options: ['disable-absolute-first'],
}),
test({ code: "'use directive';\
import { x } from 'foo';" }),
],
invalid: [
test({ code: "import { x } from './foo';\
export { x };\
import { y } from './bar';",
errors: 1,
output: "import { x } from './foo';\
import { y } from './bar';\
export { x };",
}),
test({ code: "import { x } from './foo';\
export { x };\
import { y } from './bar';\
import { z } from './baz';",
errors: 2,
output: "import { x } from './foo';\
import { y } from './bar';\
import { z } from './baz';\
export { x };",
}),
test({ code: "import { x } from './foo'; import { y } from 'bar'",
options: ['absolute-first'],
errors: 1,
}),
test({ code: "import { x } from 'foo';\
'use directive';\
import { y } from 'bar';",
errors: 1,
output: "import { x } from 'foo';\
import { y } from 'bar';\
'use directive';",
}),
test({ code: "var a = 1;\
import { y } from './bar';\
if (true) { x() };\
import { x } from './foo';\
import { z } from './baz';",
errors: 3,
output: "import { y } from './bar';\
var a = 1;\
if (true) { x() };\
import { x } from './foo';\
import { z } from './baz';",
}),
test({ code: "if (true) { console.log(1) }import a from 'b'",
errors: 1,
output: "import a from 'b'\nif (true) { console.log(1) }",
}),
],
});
context('TypeScript', function () {
getTSParsers()
.filter((parser) => parser !== require.resolve('typescript-eslint-parser'))
.forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
};
ruleTester.run('order', rule, {
valid: [
test(
{
code: `
import y = require('bar');
import { x } from 'foo';
import z = require('baz');
`,
parser,
},
parserConfig,
),
],
invalid: [
test(
{
code: `
import { x } from './foo';
import y = require('bar');
`,
options: ['absolute-first'],
parser,
errors: [
{
message: 'Absolute imports should come before relative imports.',
},
],
},
parserConfig,
),
],
});
});
});