Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/tests/src/rules/first.js
829 views
1
import { test, getTSParsers, testVersion } from '../utils';
2
import fs from 'fs';
3
import path from 'path';
4
5
import { RuleTester } from 'eslint';
6
7
const ruleTester = new RuleTester();
8
const rule = require('rules/first');
9
10
ruleTester.run('first', rule, {
11
valid: [].concat(
12
test({
13
code: "import { x } from './foo'; import { y } from './bar';\
14
export { x, y }",
15
}),
16
test({ code: "import { x } from 'foo'; import { y } from './bar'" }),
17
test({ code: "import { x } from './foo'; import { y } from 'bar'" }),
18
test({
19
code: "import { x } from './foo'; import { y } from 'bar'",
20
options: ['disable-absolute-first'],
21
}),
22
test({
23
code: "'use directive';\
24
import { x } from 'foo';",
25
}),
26
testVersion('>= 7', () => ({
27
// issue #2210
28
code: String(fs.readFileSync(path.join(__dirname, '../../files/component.html'))),
29
parser: require.resolve('@angular-eslint/template-parser'),
30
})),
31
),
32
invalid: [
33
test({
34
code: "import { x } from './foo';\
35
export { x };\
36
import { y } from './bar';",
37
errors: 1,
38
output: "import { x } from './foo';\
39
import { y } from './bar';\
40
export { x };",
41
}),
42
test({
43
code: "import { x } from './foo';\
44
export { x };\
45
import { y } from './bar';\
46
import { z } from './baz';",
47
errors: 2,
48
output: "import { x } from './foo';\
49
import { y } from './bar';\
50
import { z } from './baz';\
51
export { x };",
52
}),
53
test({
54
code: "import { x } from './foo'; import { y } from 'bar'",
55
options: ['absolute-first'],
56
errors: 1,
57
}),
58
test({
59
code: "import { x } from 'foo';\
60
'use directive';\
61
import { y } from 'bar';",
62
errors: 1,
63
output: "import { x } from 'foo';\
64
import { y } from 'bar';\
65
'use directive';",
66
}),
67
test({
68
code: "var a = 1;\
69
import { y } from './bar';\
70
if (true) { x() };\
71
import { x } from './foo';\
72
import { z } from './baz';",
73
errors: 3,
74
output: "import { y } from './bar';\
75
var a = 1;\
76
if (true) { x() };\
77
import { x } from './foo';\
78
import { z } from './baz';",
79
}),
80
test({
81
code: "if (true) { console.log(1) }import a from 'b'",
82
errors: 1,
83
output: "import a from 'b'\nif (true) { console.log(1) }",
84
}),
85
],
86
});
87
88
context('TypeScript', function () {
89
getTSParsers()
90
.forEach((parser) => {
91
const parserConfig = {
92
parser,
93
settings: {
94
'import/parsers': { [parser]: ['.ts'] },
95
'import/resolver': { 'eslint-import-resolver-typescript': true },
96
},
97
};
98
99
ruleTester.run('order', rule, {
100
valid: [
101
test({
102
code: `
103
import y = require('bar');
104
import { x } from 'foo';
105
import z = require('baz');
106
`,
107
...parserConfig,
108
}),
109
],
110
invalid: [
111
test({
112
code: `
113
import { x } from './foo';
114
import y = require('bar');
115
`,
116
options: ['absolute-first'],
117
...parserConfig,
118
errors: [
119
{
120
message: 'Absolute imports should come before relative imports.',
121
},
122
],
123
}),
124
],
125
});
126
});
127
});
128
129