Skip to content

Commit 2ba7fd5

Browse files
sgtpepper43benmosher
authored andcommitted
Check duplicates for normal imports and flow type imports separately (#334)
Fix #225
1 parent 1e9c371 commit 2ba7fd5

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/rules/no-duplicates.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,35 @@ import Set from 'es6-set'
44

55
import resolve from '../core/resolve'
66

7+
function checkImports(imported, context) {
8+
for (let [module, nodes] of imported.entries()) {
9+
if (nodes.size > 1) {
10+
for (let node of nodes) {
11+
context.report(node, `'${module}' imported multiple times.`)
12+
}
13+
}
14+
}
15+
}
16+
717
module.exports = function (context) {
818
const imported = new Map()
19+
const typesImported = new Map()
920
return {
1021
'ImportDeclaration': function (n) {
1122
// resolved path will cover aliased duplicates
12-
let resolvedPath = resolve(n.source.value, context) || n.source.value
23+
const resolvedPath = resolve(n.source.value, context) || n.source.value
24+
const importMap = n.importKind === 'type' ? typesImported : imported
1325

14-
if (imported.has(resolvedPath)) {
15-
imported.get(resolvedPath).add(n.source)
26+
if (importMap.has(resolvedPath)) {
27+
importMap.get(resolvedPath).add(n.source)
1628
} else {
17-
imported.set(resolvedPath, new Set([n.source]))
29+
importMap.set(resolvedPath, new Set([n.source]))
1830
}
1931
},
2032

2133
'Program:exit': function () {
22-
for (let [module, nodes] of imported.entries()) {
23-
if (nodes.size > 1) {
24-
for (let node of nodes) {
25-
context.report(node, `'${module}' imported multiple times.`)
26-
}
27-
}
28-
}
34+
checkImports(imported, context)
35+
checkImports(typesImported, context)
2936
},
3037
}
3138
}

tests/src/rules/no-duplicates.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ ruleTester.run('no-duplicates', rule, {
1515
// #86: every unresolved module should not show up as 'null' and duplicate
1616
test({ code: 'import foo from "234artaf";' +
1717
'import { shoop } from "234q25ad"' }),
18+
19+
// #225: ignore duplicate if is a flow type import
20+
test({
21+
code: "import { x } from './foo'; import type { y } from './foo'",
22+
parser: 'babel-eslint',
23+
}),
1824
],
1925
invalid: [
2026
test({
@@ -45,5 +51,11 @@ ruleTester.run('no-duplicates', rule, {
4551
"'non-existent' imported multiple times.",
4652
],
4753
}),
54+
55+
test({
56+
code: "import type { x } from './foo'; import type { y } from './foo'",
57+
parser: 'babel-eslint',
58+
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
59+
}),
4860
],
4961
})

0 commit comments

Comments
 (0)