-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathno-relative-parent-imports.js
119 lines (115 loc) · 3.96 KB
/
no-relative-parent-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
import { RuleTester } from 'eslint'
import rule from 'rules/no-relative-parent-imports'
import { test as _test, testFilePath } from '../utils'
const test = def => _test(Object.assign(def, {
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'),
parser: 'babel-eslint',
}))
const ruleTester = new RuleTester()
ruleTester.run('no-relative-parent-imports', rule, {
valid: [
test({
code: 'import foo from "./internal.js"',
}),
test({
code: 'import foo from "./app/index.js"',
}),
test({
code: 'import foo from "package"',
}),
test({
code: 'require("./internal.js")',
options: [{ commonjs: true }],
}),
test({
code: 'require("./app/index.js")',
options: [{ commonjs: true }],
}),
test({
code: 'require("package")',
options: [{ commonjs: true }],
}),
test({
code: 'require.resolve("./internal.js")',
options: [{ commonjs: true, requireResolve: true }],
}),
test({
code: 'require.resolve("./app/index.js")',
options: [{ commonjs: false, requireResolve: { commonjs: true } }],
}),
test({
code: 'require.resolve("package")',
options: [{ requireResolve: { commonjs: true } }],
}),
test({
code: 'import("./internal.js")',
}),
test({
code: 'import("./app/index.js")',
}),
test({
code: 'import(".")',
}),
test({
code: 'import("path")',
}),
test({
code: 'import("package")',
}),
test({
code: 'import("@scope/package")',
}),
],
invalid: [
test({
code: 'import foo from "../plugin.js"',
errors: [ {
message: 'Relative imports from parent directories are not allowed. Please either pass what you\'re importing through at runtime (dependency injection), move `index.js` to same directory as `../plugin.js` or consider making `../plugin.js` a package.',
line: 1,
column: 17,
} ],
}),
test({
code: 'require("../plugin.js")',
options: [{ commonjs: true }],
errors: [ {
message: 'Relative imports from parent directories are not allowed. Please either pass what you\'re importing through at runtime (dependency injection), move `index.js` to same directory as `../plugin.js` or consider making `../plugin.js` a package.',
line: 1,
column: 9,
} ],
}),
test({
code: 'require.resolve("../plugin.js")',
options: [{ requireResolve: { commonjs: true } }],
errors: [ {
message: 'Relative imports from parent directories are not allowed. Please either pass what you\'re importing through at runtime (dependency injection), move `index.js` to same directory as `../plugin.js` or consider making `../plugin.js` a package.',
line: 1,
column: 17,
} ],
}),
test({
code: 'import("../plugin.js")',
errors: [ {
message: 'Relative imports from parent directories are not allowed. Please either pass what you\'re importing through at runtime (dependency injection), move `index.js` to same directory as `../plugin.js` or consider making `../plugin.js` a package.',
line: 1,
column: 8,
} ],
}),
test({
code: 'import foo from "./../plugin.js"',
errors: [ {
message: 'Relative imports from parent directories are not allowed. Please either pass what you\'re importing through at runtime (dependency injection), move `index.js` to same directory as `./../plugin.js` or consider making `./../plugin.js` a package.',
line: 1,
column: 17,
}],
}),
test({
code: 'import foo from "../../api/service"',
errors: [ {
message: 'Relative imports from parent directories are not allowed. Please either pass what you\'re importing through at runtime (dependency injection), move `index.js` to same directory as `../../api/service` or consider making `../../api/service` a package.',
line: 1,
column: 17,
}],
}),
],
})