forked from eslint/eslint
/
no-duplicate-imports.js
142 lines (136 loc) · 6.07 KB
/
no-duplicate-imports.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @fileoverview Tests for no-duplicate-imports.
* @author Simen Bekkhus
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-duplicate-imports"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 12, sourceType: "module" } });
ruleTester.run("no-duplicate-imports", rule, {
valid: [
"import os from \"os\";\nimport fs from \"fs\";",
"import { merge } from \"lodash-es\";",
"import _, { merge } from \"lodash-es\";",
"import * as Foobar from \"async\";",
"import \"foo\"",
"import os from \"os\";\nexport { something } from \"os\";",
"import * as bar from \"os\";\nimport { baz } from \"os\";",
"import foo, * as bar from \"os\";\nimport { baz } from \"os\";",
"import foo, { bar } from \"os\";\nimport * as baz from \"os\";",
{
code: "import os from \"os\";\nexport { hello } from \"hello\";",
options: [{ includeExports: true }]
},
{
code: "import os from \"os\";\nexport * from \"hello\";",
options: [{ includeExports: true }]
},
{
code: "import os from \"os\";\nexport { hello as hi } from \"hello\";",
options: [{ includeExports: true }]
},
{
code: "import os from \"os\";\nexport default function(){};",
options: [{ includeExports: true }]
},
{
code: "import { merge } from \"lodash-es\";\nexport { merge as lodashMerge }",
options: [{ includeExports: true }]
},
{
code: "export { something } from \"os\";\nexport * as os from \"os\";",
options: [{ includeExports: true }]
},
{
code: "import { something } from \"os\";\nexport * as os from \"os\";",
options: [{ includeExports: true }]
},
{
code: "import * as os from \"os\";\nexport { something } from \"os\";",
options: [{ includeExports: true }]
},
{
code: "import os from \"os\";\nexport * from \"os\";",
options: [{ includeExports: true }]
},
{
code: "export { something } from \"os\";\nexport * from \"os\";",
options: [{ includeExports: true }]
}
],
invalid: [
{
code: "import \"fs\";\nimport \"fs\"",
errors: [{ messageId: "import", data: { module: "fs" }, type: "ImportDeclaration" }]
},
{
code: "import { merge } from \"lodash-es\";\nimport { find } from \"lodash-es\";",
errors: [{ messageId: "import", data: { module: "lodash-es" }, type: "ImportDeclaration" }]
},
{
code: "import { merge } from \"lodash-es\";\nimport _ from \"lodash-es\";",
errors: [{ messageId: "import", data: { module: "lodash-es" }, type: "ImportDeclaration" }]
},
{
code: "import os from \"os\";\nimport { something } from \"os\";\nimport * as foobar from \"os\";",
errors: [
{ messageId: "import", data: { module: "os" }, type: "ImportDeclaration" },
{ messageId: "import", data: { module: "os" }, type: "ImportDeclaration" }
]
},
{
code: "import * as modns from \"lodash-es\";\nimport { merge } from \"lodash-es\";\nimport { baz } from \"lodash-es\";",
errors: [{ messageId: "import", data: { module: "lodash-es" }, type: "ImportDeclaration" }]
},
{
code: "export { os } from \"os\";\nexport { something } from \"os\";",
options: [{ includeExports: true }],
errors: [{ messageId: "export", data: { module: "os" }, type: "ExportNamedDeclaration" }]
},
{
code: "import os from \"os\";\nexport { os as foobar } from \"os\";\nexport { something } from \"os\";",
options: [{ includeExports: true }],
errors: [
{ messageId: "exportAs", data: { module: "os" }, type: "ExportNamedDeclaration" },
{ messageId: "export", data: { module: "os" }, type: "ExportNamedDeclaration" },
{ messageId: "exportAs", data: { module: "os" }, type: "ExportNamedDeclaration" }
]
},
{
code: "import os from \"os\";\nexport { something } from \"os\";",
options: [{ includeExports: true }],
errors: [{ messageId: "exportAs", data: { module: "os" }, type: "ExportNamedDeclaration" }]
},
{
code: "import os from \"os\";\nexport * as os from \"os\";",
options: [{ includeExports: true }],
errors: [{ messageId: "exportAs", data: { module: "os" }, type: "ExportAllDeclaration" }]
},
{
code: "export * as os from \"os\";\nimport os from \"os\";",
options: [{ includeExports: true }],
errors: [{ messageId: "importAs", data: { module: "os" }, type: "ImportDeclaration" }]
},
{
code: "import * as modns from \"mod\";\nexport * as modns from \"mod\";",
options: [{ includeExports: true }],
errors: [{ messageId: "exportAs", data: { module: "mod" }, type: "ExportAllDeclaration" }]
},
{
code: "export * from \"os\";\nexport * from \"os\";",
options: [{ includeExports: true }],
errors: [{ messageId: "export", data: { module: "os" }, type: "ExportAllDeclaration" }]
},
{
code: "import \"os\";\nexport * from \"os\";",
options: [{ includeExports: true }],
errors: [{ messageId: "exportAs", data: { module: "os" }, type: "ExportAllDeclaration" }]
}
]
});