Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/tests/src/rules/no-restricted-paths.js
829 views
1
import { RuleTester } from 'eslint';
2
import rule from 'rules/no-restricted-paths';
3
4
import { test, testFilePath } from '../utils';
5
6
const ruleTester = new RuleTester();
7
8
ruleTester.run('no-restricted-paths', rule, {
9
valid: [
10
test({
11
code: 'import a from "../client/a.js"',
12
filename: testFilePath('./restricted-paths/server/b.js'),
13
options: [ {
14
zones: [ { target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/other' } ],
15
} ],
16
}),
17
test({
18
code: 'import a from "../client/a.js"',
19
filename: testFilePath('./restricted-paths/server/b.js'),
20
options: [ {
21
zones: [ { target: '**/*', from: './tests/files/restricted-paths/other' } ],
22
} ],
23
}),
24
test({
25
code: 'import a from "../client/a.js"',
26
filename: testFilePath('./restricted-paths/client/b.js'),
27
options: [ {
28
zones: [ {
29
target: './tests/files/restricted-paths/!(client)/**/*',
30
from: './tests/files/restricted-paths/client/**/*',
31
} ],
32
} ],
33
}),
34
test({
35
code: 'const a = require("../client/a.js")',
36
filename: testFilePath('./restricted-paths/server/b.js'),
37
options: [ {
38
zones: [ { target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/other' } ],
39
} ],
40
}),
41
test({
42
code: 'import b from "../server/b.js"',
43
filename: testFilePath('./restricted-paths/client/a.js'),
44
options: [ {
45
zones: [ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/other' } ],
46
} ],
47
}),
48
test({
49
code: 'import a from "./a.js"',
50
filename: testFilePath('./restricted-paths/server/one/a.js'),
51
options: [ {
52
zones: [ {
53
target: './tests/files/restricted-paths/server/one',
54
from: './tests/files/restricted-paths/server',
55
except: ['./one'],
56
} ],
57
} ],
58
}),
59
test({
60
code: 'import a from "../two/a.js"',
61
filename: testFilePath('./restricted-paths/server/one/a.js'),
62
options: [ {
63
zones: [ {
64
target: './tests/files/restricted-paths/server/one',
65
from: './tests/files/restricted-paths/server',
66
except: ['./two'],
67
} ],
68
} ],
69
}),
70
test({
71
code: 'import a from "../one/a.js"',
72
filename: testFilePath('./restricted-paths/server/two-new/a.js'),
73
options: [ {
74
zones: [ {
75
target: './tests/files/restricted-paths/server/two',
76
from: './tests/files/restricted-paths/server',
77
except: [],
78
} ],
79
} ],
80
}),
81
test({
82
code: 'import A from "../two/a.js"',
83
filename: testFilePath('./restricted-paths/server/one/a.js'),
84
options: [ {
85
zones: [ {
86
target: '**/*',
87
from: './tests/files/restricted-paths/server/**/*',
88
except: ['**/a.js'],
89
} ],
90
} ],
91
}),
92
93
// irrelevant function calls
94
test({ code: 'notrequire("../server/b.js")' }),
95
test({
96
code: 'notrequire("../server/b.js")',
97
filename: testFilePath('./restricted-paths/client/a.js'),
98
options: [ {
99
zones: [ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/server' } ],
100
} ] }),
101
102
// no config
103
test({ code: 'require("../server/b.js")' }),
104
test({ code: 'import b from "../server/b.js"' }),
105
106
// builtin (ignore)
107
test({ code: 'require("os")' }),
108
],
109
110
invalid: [
111
test({
112
code: 'import b from "../server/b.js"',
113
filename: testFilePath('./restricted-paths/client/a.js'),
114
options: [ {
115
zones: [ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/server' } ],
116
} ],
117
errors: [ {
118
message: 'Unexpected path "../server/b.js" imported in restricted zone.',
119
line: 1,
120
column: 15,
121
} ],
122
}),
123
test({
124
code: 'import b from "../server/b.js"',
125
filename: testFilePath('./restricted-paths/client/a.js'),
126
options: [ {
127
zones: [ { target: './tests/files/restricted-paths/client/**/*', from: './tests/files/restricted-paths/server' } ],
128
} ],
129
errors: [ {
130
message: 'Unexpected path "../server/b.js" imported in restricted zone.',
131
line: 1,
132
column: 15,
133
} ],
134
}),
135
test({
136
code: 'import a from "../client/a"\nimport c from "./c"',
137
filename: testFilePath('./restricted-paths/server/b.js'),
138
options: [ {
139
zones: [
140
{ target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/client' },
141
{ target: './tests/files/restricted-paths/server', from: './tests/files/restricted-paths/server/c.js' },
142
],
143
} ],
144
errors: [
145
{
146
message: 'Unexpected path "../client/a" imported in restricted zone.',
147
line: 1,
148
column: 15,
149
},
150
{
151
message: 'Unexpected path "./c" imported in restricted zone.',
152
line: 2,
153
column: 15,
154
},
155
],
156
}),
157
test({
158
code: 'import b from "../server/b.js"',
159
filename: testFilePath('./restricted-paths/client/a.js'),
160
options: [ {
161
zones: [ { target: './client', from: './server' } ],
162
basePath: testFilePath('./restricted-paths'),
163
} ],
164
errors: [ {
165
message: 'Unexpected path "../server/b.js" imported in restricted zone.',
166
line: 1,
167
column: 15,
168
} ],
169
}),
170
test({
171
code: 'const b = require("../server/b.js")',
172
filename: testFilePath('./restricted-paths/client/a.js'),
173
options: [ {
174
zones: [ { target: './tests/files/restricted-paths/client', from: './tests/files/restricted-paths/server' } ],
175
} ],
176
errors: [ {
177
message: 'Unexpected path "../server/b.js" imported in restricted zone.',
178
line: 1,
179
column: 19,
180
} ],
181
}),
182
test({
183
code: 'import b from "../two/a.js"',
184
filename: testFilePath('./restricted-paths/server/one/a.js'),
185
options: [ {
186
zones: [ {
187
target: './tests/files/restricted-paths/server/one',
188
from: './tests/files/restricted-paths/server',
189
except: ['./one'],
190
} ],
191
} ],
192
errors: [ {
193
message: 'Unexpected path "../two/a.js" imported in restricted zone.',
194
line: 1,
195
column: 15,
196
} ],
197
}),
198
test({
199
code: 'import b from "../two/a.js"',
200
filename: testFilePath('./restricted-paths/server/one/a.js'),
201
options: [ {
202
zones: [ {
203
target: './tests/files/restricted-paths/server/one',
204
from: './tests/files/restricted-paths/server',
205
except: ['./one'],
206
message: 'Custom message',
207
} ],
208
} ],
209
errors: [ {
210
message: 'Unexpected path "../two/a.js" imported in restricted zone. Custom message',
211
line: 1,
212
column: 15,
213
} ],
214
}),
215
test({
216
code: 'import b from "../two/a.js"',
217
filename: testFilePath('./restricted-paths/server/one/a.js'),
218
options: [ {
219
zones: [ {
220
target: './tests/files/restricted-paths/server/one',
221
from: './tests/files/restricted-paths/server',
222
except: ['../client/a'],
223
} ],
224
} ],
225
errors: [ {
226
message: 'Restricted path exceptions must be descendants of the configured ' +
227
'`from` path for that zone.',
228
line: 1,
229
column: 15,
230
} ],
231
}),
232
test({
233
code: 'import A from "../two/a.js"',
234
filename: testFilePath('./restricted-paths/server/one/a.js'),
235
options: [ {
236
zones: [ {
237
target: '**/*',
238
from: './tests/files/restricted-paths/server/**/*',
239
} ],
240
} ],
241
errors: [ {
242
message: 'Unexpected path "../two/a.js" imported in restricted zone.',
243
line: 1,
244
column: 15,
245
} ],
246
}),
247
test({
248
code: 'import A from "../two/a.js"',
249
filename: testFilePath('./restricted-paths/server/one/a.js'),
250
options: [ {
251
zones: [ {
252
target: '**/*',
253
from: './tests/files/restricted-paths/server/**/*',
254
except: ['a.js'],
255
} ],
256
} ],
257
errors: [ {
258
message: 'Restricted path exceptions must be glob patterns when`from` is a glob pattern',
259
line: 1,
260
column: 15,
261
} ],
262
}),
263
],
264
});
265
266