forked from import-js/eslint-plugin-import
/
first.js
127 lines (122 loc) · 3.5 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
117
118
119
120
121
122
123
124
125
126
127
import { test, getTSParsers, testVersion } from '../utils';
import fs from 'fs';
import path from 'path';
import { RuleTester } from 'eslint';
const ruleTester = new RuleTester();
const rule = require('rules/first');
ruleTester.run('first', rule, {
valid: [].concat(
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';",
}),
testVersion('>= 7', () => ({
// issue #2210
code: String(fs.readFileSync(path.join(__dirname, '../../files/component.html'))),
parser: require.resolve('@angular-eslint/template-parser'),
})),
),
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()
.forEach((parser) => {
const parserConfig = {
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');
`,
...parserConfig,
}),
],
invalid: [
test({
code: `
import { x } from './foo';
import y = require('bar');
`,
options: ['absolute-first'],
...parserConfig,
errors: [
{
message: 'Absolute imports should come before relative imports.',
},
],
}),
],
});
});
});