forked from import-js/eslint-plugin-import
/
no-duplicates.js
88 lines (75 loc) · 2.99 KB
/
no-duplicates.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
import * as path from 'path'
import { test } from '../utils'
import { RuleTester } from 'eslint'
const ruleTester = new RuleTester()
, rule = require('rules/no-duplicates')
ruleTester.run('no-duplicates', rule, {
valid: [
test({ code: 'import "./malformed.js"' }),
test({ code: "import { x } from './foo'; import { y } from './bar'" }),
// #86: every unresolved module should not show up as 'null' and duplicate
test({ code: 'import foo from "234artaf";' +
'import { shoop } from "234q25ad"' }),
// #225: ignore duplicate if is a flow type import
test({
code: "import { x } from './foo'; import type { y } from './foo'",
parser: 'babel-eslint',
}),
// #1107: Using different query strings that trigger different webpack loaders.
test({
code: "import x from './bar?optionX'; import y from './bar?optionY';",
options: [{'considerQueryString': true}],
settings: { 'import/resolver': 'webpack' },
}),
],
invalid: [
test({
code: "import { x } from './foo'; import { y } from './foo'",
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),
test({
code: "import { x } from './foo'; import { y } from './foo'; import { z } from './foo'",
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),
// ensure resolved path results in warnings
test({
code: "import { x } from './bar'; import { y } from 'bar';",
settings: { 'import/resolve': {
paths: [path.join( process.cwd()
, 'tests', 'files'
)] }},
errors: 2, // path ends up hardcoded
}),
// #1107: Using different query strings that trigger different webpack loaders.
test({
code: "import x from './bar.js?optionX'; import y from './bar?optionX';",
settings: { 'import/resolver': 'webpack' },
errors: 2, // path ends up hardcoded
}),
test({
code: "import x from './bar?optionX'; import y from './bar?optionY';",
settings: { 'import/resolver': 'webpack' },
errors: 2, // path ends up hardcoded
}),
// #1107: Using same query strings that trigger the same loader.
test({
code: "import x from './bar?optionX'; import y from './bar.js?optionX';",
options: [{'considerQueryString': true}],
settings: { 'import/resolver': 'webpack' },
errors: 2, // path ends up hardcoded
}),
// #86: duplicate unresolved modules should be flagged
test({
code: "import foo from 'non-existent'; import bar from 'non-existent';",
errors: [
"'non-existent' imported multiple times.",
"'non-existent' imported multiple times.",
],
}),
test({
code: "import type { x } from './foo'; import type { y } from './foo'",
parser: 'babel-eslint',
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),
],
})