forked from mysticatea/eslint-plugin-node
-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathno-extraneous-import.js
116 lines (109 loc) · 3.25 KB
/
no-extraneous-import.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
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
"use strict"
const path = require("path")
const { Linter } = require("eslint")
const { RuleTester } = require("#test-helpers")
const rule = require("../../../lib/rules/no-extraneous-import")
const DynamicImportSupported = (() => {
const config = { languageOptions: { ecmaVersion: 2020 } }
const messages = new Linter().verify("import(s)", config)
return messages.length === 0
})()
if (!DynamicImportSupported) {
console.warn(
"[%s] Skip tests for 'import()'",
path.basename(__filename, ".js")
)
}
/**
* Makes a file path to a fixture.
* @param {string} name - A name.
* @returns {string} A file path to a fixture.
*/
function fixture(name) {
return path.resolve(__dirname, "../../fixtures/no-extraneous", name)
}
const ruleTester = new RuleTester({
languageOptions: { sourceType: "module" },
})
ruleTester.run("no-extraneous-import", rule, {
valid: [
{
filename: fixture("dependencies/a.js"),
code: "import bbb from './bbb'",
},
{
filename: fixture("dependencies/a.js"),
code: "import aaa from 'aaa'",
},
{
filename: fixture("dependencies/a.js"),
code: "import bbb from 'aaa/bbb'",
},
{
filename: fixture("dependencies/a.js"),
code: "import aaa from '@bbb/aaa'",
},
{
filename: fixture("dependencies/a.js"),
code: "import bbb from '@bbb/aaa/bbb'",
},
{
filename: fixture("devDependencies/a.js"),
code: "import aaa from 'aaa'",
},
{
filename: fixture("peerDependencies/a.js"),
code: "import aaa from 'aaa'",
},
{
filename: fixture("optionalDependencies/a.js"),
code: "import aaa from 'aaa'",
},
{
filename: fixture("import-map/a.js"),
code: "import '#b'",
},
// missing packages are warned by no-missing-import
{
filename: fixture("dependencies/a.js"),
code: "import ccc from 'ccc'",
},
],
invalid: [
{
filename: fixture("dependencies/a.js"),
code: "import bbb from 'bbb'",
errors: ['"bbb" is extraneous.'],
},
{
filename: fixture("devDependencies/a.js"),
code: "import bbb from 'bbb'",
errors: ['"bbb" is extraneous.'],
},
{
filename: fixture("peerDependencies/a.js"),
code: "import bbb from 'bbb'",
errors: ['"bbb" is extraneous.'],
},
{
filename: fixture("optionalDependencies/a.js"),
code: "import bbb from 'bbb'",
errors: ['"bbb" is extraneous.'],
},
// import()
...(DynamicImportSupported
? [
{
filename: fixture("dependencies/a.js"),
code: "function f() { import('bbb') }",
languageOptions: { ecmaVersion: 2020 },
errors: ['"bbb" is extraneous.'],
},
]
: []),
],
})