Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/tests/src/rules/no-internal-modules.js
829 views
1
import { RuleTester } from 'eslint';
2
import flatMap from 'array.prototype.flatmap';
3
import rule from 'rules/no-internal-modules';
4
5
import { test, testFilePath, getTSParsers } from '../utils';
6
7
const ruleTester = new RuleTester();
8
9
ruleTester.run('no-internal-modules', rule, {
10
valid: [
11
// imports
12
test({
13
code: 'import a from "./plugin2"',
14
filename: testFilePath('./internal-modules/plugins/plugin.js'),
15
options: [],
16
}),
17
test({
18
code: 'const a = require("./plugin2")',
19
filename: testFilePath('./internal-modules/plugins/plugin.js'),
20
}),
21
test({
22
code: 'const a = require("./plugin2/")',
23
filename: testFilePath('./internal-modules/plugins/plugin.js'),
24
}),
25
test({
26
code: 'const dynamic = "./plugin2/"; const a = require(dynamic)',
27
filename: testFilePath('./internal-modules/plugins/plugin.js'),
28
}),
29
test({
30
code: 'import b from "./internal.js"',
31
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'),
32
}),
33
test({
34
code: 'import get from "lodash.get"',
35
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'),
36
}),
37
test({
38
code: 'import b from "@org/package"',
39
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
40
}),
41
test({
42
code: 'import b from "../../api/service"',
43
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
44
options: [ {
45
allow: [ '**/api/*' ],
46
} ],
47
}),
48
test({
49
code: 'import "jquery/dist/jquery"',
50
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
51
options: [ {
52
allow: [ 'jquery/dist/*' ],
53
} ],
54
}),
55
test({
56
code: 'import "./app/index.js";\nimport "./app/index"',
57
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
58
options: [ {
59
allow: [ '**/index{.js,}' ],
60
} ],
61
}),
62
test({
63
code: 'import a from "./plugin2/thing"',
64
filename: testFilePath('./internal-modules/plugins/plugin.js'),
65
options: [ {
66
forbid: [ '**/api/*' ],
67
} ],
68
}),
69
test({
70
code: 'const a = require("./plugin2/thing")',
71
filename: testFilePath('./internal-modules/plugins/plugin.js'),
72
options: [ {
73
forbid: [ '**/api/*' ],
74
} ],
75
}),
76
test({
77
code: 'import b from "app/a"',
78
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
79
options: [ {
80
forbid: [ 'app/**/**' ],
81
} ],
82
}),
83
test({
84
code: 'import b from "@org/package"',
85
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
86
options: [ {
87
forbid: [ '@org/package/*' ],
88
} ],
89
}),
90
// exports
91
test({
92
code: 'export {a} from "./internal.js"',
93
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'),
94
}),
95
test({
96
code: 'export * from "lodash.get"',
97
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'),
98
}),
99
test({
100
code: 'export {b} from "@org/package"',
101
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
102
}),
103
test({
104
code: 'export {b} from "../../api/service"',
105
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
106
options: [ {
107
allow: [ '**/api/*' ],
108
} ],
109
}),
110
test({
111
code: 'export * from "jquery/dist/jquery"',
112
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
113
options: [ {
114
allow: [ 'jquery/dist/*' ],
115
} ],
116
}),
117
test({
118
code: 'export * from "./app/index.js";\nexport * from "./app/index"',
119
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
120
options: [ {
121
allow: [ '**/index{.js,}' ],
122
} ],
123
}),
124
test({
125
code: `
126
export class AuthHelper {
127
128
static checkAuth(auth) {
129
}
130
}
131
`,
132
}),
133
...flatMap(getTSParsers(), (parser) => [
134
test({
135
code: `
136
export class AuthHelper {
137
138
public static checkAuth(auth?: string): boolean {
139
}
140
}
141
`,
142
parser,
143
}),
144
]),
145
test({
146
code: 'export * from "./plugin2/thing"',
147
filename: testFilePath('./internal-modules/plugins/plugin.js'),
148
options: [ {
149
forbid: [ '**/api/*' ],
150
} ],
151
}),
152
test({
153
code: 'export * from "app/a"',
154
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
155
options: [ {
156
forbid: [ 'app/**/**' ],
157
} ],
158
}),
159
test({
160
code: 'export { b } from "@org/package"',
161
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
162
options: [ {
163
forbid: [ '@org/package/*' ],
164
} ],
165
}),
166
test({
167
code: 'export * from "./app/index.js";\nexport * from "./app/index"',
168
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
169
options: [ {
170
forbid: [ '**/index.ts' ],
171
} ],
172
}),
173
],
174
175
invalid: [
176
// imports
177
test({
178
code: 'import "./plugin2/index.js";\nimport "./plugin2/app/index"',
179
filename: testFilePath('./internal-modules/plugins/plugin.js'),
180
options: [ {
181
allow: [ '*/index.js' ],
182
} ],
183
errors: [ {
184
message: 'Reaching to "./plugin2/app/index" is not allowed.',
185
line: 2,
186
column: 8,
187
} ],
188
}),
189
test({
190
code: 'import "./app/index.js"',
191
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
192
errors: [ {
193
message: 'Reaching to "./app/index.js" is not allowed.',
194
line: 1,
195
column: 8,
196
} ],
197
}),
198
test({
199
code: 'import b from "./plugin2/internal"',
200
filename: testFilePath('./internal-modules/plugins/plugin.js'),
201
errors: [ {
202
message: 'Reaching to "./plugin2/internal" is not allowed.',
203
line: 1,
204
column: 15,
205
} ],
206
}),
207
test({
208
code: 'import a from "../api/service/index"',
209
filename: testFilePath('./internal-modules/plugins/plugin.js'),
210
options: [ {
211
allow: [ '**/internal-modules/*' ],
212
} ],
213
errors: [
214
{
215
message: 'Reaching to "../api/service/index" is not allowed.',
216
line: 1,
217
column: 15,
218
},
219
],
220
}),
221
test({
222
code: 'import b from "@org/package/internal"',
223
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
224
errors: [
225
{
226
message: 'Reaching to "@org/package/internal" is not allowed.',
227
line: 1,
228
column: 15,
229
},
230
],
231
}),
232
test({
233
code: 'import get from "debug/node"',
234
filename: testFilePath('./internal-modules/plugins/plugin.js'),
235
errors: [
236
{
237
message: 'Reaching to "debug/node" is not allowed.',
238
line: 1,
239
column: 17,
240
},
241
],
242
}),
243
test({
244
code: 'import "./app/index.js"',
245
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
246
options: [ {
247
forbid: [ '*/app/*' ],
248
} ],
249
errors: [ {
250
message: 'Reaching to "./app/index.js" is not allowed.',
251
line: 1,
252
column: 8,
253
} ],
254
}),
255
test({
256
code: 'import b from "@org/package"',
257
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
258
options: [ {
259
forbid: [ '@org/**' ],
260
} ],
261
errors: [ {
262
message: 'Reaching to "@org/package" is not allowed.',
263
line: 1,
264
column: 15,
265
} ],
266
}),
267
test({
268
code: 'import b from "app/a/b"',
269
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
270
options: [ {
271
forbid: [ 'app/**/**' ],
272
} ],
273
errors: [ {
274
message: 'Reaching to "app/a/b" is not allowed.',
275
line: 1,
276
column: 15,
277
} ],
278
}),
279
test({
280
code: 'import get from "lodash.get"',
281
filename: testFilePath('./internal-modules/plugins/plugin2/index.js'),
282
options: [ {
283
forbid: [ 'lodash.*' ],
284
} ],
285
errors: [ {
286
message: 'Reaching to "lodash.get" is not allowed.',
287
line: 1,
288
column: 17,
289
} ],
290
}),
291
test({
292
code: 'import "./app/index.js";\nimport "./app/index"',
293
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
294
options: [ {
295
forbid: [ '**/index{.js,}' ],
296
} ],
297
errors: [ {
298
message: 'Reaching to "./app/index.js" is not allowed.',
299
line: 1,
300
column: 8,
301
}, {
302
message: 'Reaching to "./app/index" is not allowed.',
303
line: 2,
304
column: 8,
305
} ],
306
}),
307
// exports
308
test({
309
code: 'export * from "./plugin2/index.js";\nexport * from "./plugin2/app/index"',
310
filename: testFilePath('./internal-modules/plugins/plugin.js'),
311
options: [ {
312
allow: [ '*/index.js' ],
313
} ],
314
errors: [ {
315
message: 'Reaching to "./plugin2/app/index" is not allowed.',
316
line: 2,
317
column: 15,
318
} ],
319
}),
320
test({
321
code: 'export * from "./app/index.js"',
322
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
323
errors: [ {
324
message: 'Reaching to "./app/index.js" is not allowed.',
325
line: 1,
326
column: 15,
327
} ],
328
}),
329
test({
330
code: 'export {b} from "./plugin2/internal"',
331
filename: testFilePath('./internal-modules/plugins/plugin.js'),
332
errors: [ {
333
message: 'Reaching to "./plugin2/internal" is not allowed.',
334
line: 1,
335
column: 17,
336
} ],
337
}),
338
test({
339
code: 'export {a} from "../api/service/index"',
340
filename: testFilePath('./internal-modules/plugins/plugin.js'),
341
options: [ {
342
allow: [ '**/internal-modules/*' ],
343
} ],
344
errors: [
345
{
346
message: 'Reaching to "../api/service/index" is not allowed.',
347
line: 1,
348
column: 17,
349
},
350
],
351
}),
352
test({
353
code: 'export {b} from "@org/package/internal"',
354
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
355
errors: [
356
{
357
message: 'Reaching to "@org/package/internal" is not allowed.',
358
line: 1,
359
column: 17,
360
},
361
],
362
}),
363
test({
364
code: 'export {get} from "debug/node"',
365
filename: testFilePath('./internal-modules/plugins/plugin.js'),
366
errors: [
367
{
368
message: 'Reaching to "debug/node" is not allowed.',
369
line: 1,
370
column: 19,
371
},
372
],
373
}),
374
test({
375
code: 'export * from "./plugin2/thing"',
376
filename: testFilePath('./internal-modules/plugins/plugin.js'),
377
options: [ {
378
forbid: [ '**/plugin2/*' ],
379
} ],
380
errors: [
381
{
382
message: 'Reaching to "./plugin2/thing" is not allowed.',
383
line: 1,
384
column: 15,
385
},
386
],
387
}),
388
test({
389
code: 'export * from "app/a"',
390
filename: testFilePath('./internal-modules/plugins/plugin2/internal.js'),
391
options: [ {
392
forbid: [ '**' ],
393
} ],
394
errors: [
395
{
396
message: 'Reaching to "app/a" is not allowed.',
397
line: 1,
398
column: 15,
399
},
400
],
401
}),
402
],
403
});
404
405