]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-restricted-imports.js
import and build new upstream release 7.2.0
[pve-eslint.git] / eslint / tests / lib / rules / no-restricted-imports.js
1 /**
2 * @fileoverview Tests for no-restricted-imports.
3 * @author Guy Ellis
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-restricted-imports"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2020, sourceType: "module" } });
20
21 ruleTester.run("no-restricted-imports", rule, {
22 valid: [
23 "import os from \"os\";",
24 { code: "import os from \"os\";", options: ["osx"] },
25 { code: "import fs from \"fs\";", options: ["crypto"] },
26 { code: "import path from \"path\";", options: ["crypto", "stream", "os"] },
27 "import async from \"async\";",
28 { code: "import \"foo\"", options: ["crypto"] },
29 { code: "import \"foo/bar\";", options: ["foo"] },
30 { code: "import withPaths from \"foo/bar\";", options: [{ paths: ["foo", "bar"] }] },
31 { code: "import withPatterns from \"foo/bar\";", options: [{ patterns: ["foo/c*"] }] },
32 {
33 code: "import withPatternsAndPaths from \"foo/bar\";",
34 options: [{ paths: ["foo"], patterns: ["foo/c*"] }]
35 },
36 {
37 code: "import withGitignores from \"foo/bar\";",
38 options: [{ patterns: ["foo/*", "!foo/bar"] }]
39 },
40 {
41 code: "import AllowedObject from \"foo\";",
42 options: [{
43 paths: [{
44 name: "foo",
45 importNames: ["DisallowedObject"]
46 }]
47 }]
48 },
49 {
50 code: "import DisallowedObject from \"foo\";",
51 options: [{
52 paths: [{
53 name: "foo",
54 importNames: ["DisallowedObject"]
55 }]
56 }]
57 },
58 {
59 code: "import * as DisallowedObject from \"foo\";",
60 options: [{
61 paths: [{
62 name: "bar",
63 importNames: ["DisallowedObject"],
64 message: "Please import 'DisallowedObject' from /bar/ instead."
65 }]
66 }]
67 },
68 {
69 code: "import { AllowedObject } from \"foo\";",
70 options: [{
71 paths: [{
72 name: "foo",
73 importNames: ["DisallowedObject"],
74 message: "Please import 'DisallowedObject' from /bar/ instead."
75 }]
76 }]
77 },
78 {
79 code: "import { DisallowedObject } from \"foo\";",
80 options: [{
81 paths: [{
82 name: "bar",
83 importNames: ["DisallowedObject"],
84 message: "Please import 'DisallowedObject' from /bar/ instead."
85 }]
86 }]
87 },
88 {
89 code: "import { AllowedObject as DisallowedObject } from \"foo\";",
90 options: [{
91 paths: [{
92 name: "foo",
93 importNames: ["DisallowedObject"],
94 message: "Please import 'DisallowedObject' from /bar/ instead."
95 }]
96 }]
97 },
98 {
99 code: "import { AllowedObject, AllowedObjectTwo } from \"foo\";",
100 options: [{
101 paths: [{
102 name: "foo",
103 importNames: ["DisallowedObject"],
104 message: "Please import 'DisallowedObject' from /bar/ instead."
105 }]
106 }]
107 },
108 {
109 code: "import { AllowedObject, AllowedObjectTwo as DisallowedObject } from \"foo\";",
110 options: [{
111 paths: [{
112 name: "foo",
113 importNames: ["DisallowedObject"],
114 message: "Please import 'DisallowedObject' from /bar/ instead."
115 }]
116 }]
117 },
118 {
119 code: "import AllowedObjectThree, { AllowedObject as AllowedObjectTwo } from \"foo\";",
120 options: [{
121 paths: [{
122 name: "foo",
123 importNames: ["DisallowedObject"],
124 message: "Please import 'DisallowedObject' from /bar/ instead."
125 }]
126 }]
127 },
128 {
129 code: "import AllowedObject, { AllowedObjectTwo as DisallowedObject } from \"foo\";",
130 options: [{
131 paths: [{
132 name: "foo",
133 importNames: ["DisallowedObject"],
134 message: "Please import 'DisallowedObject' from /bar/ instead."
135 }]
136 }]
137 },
138 {
139 code: "import AllowedObject, { AllowedObjectTwo as DisallowedObject } from \"foo\";",
140 options: [{
141 paths: [{
142 name: "foo",
143 importNames: ["DisallowedObject", "DisallowedObjectTwo"],
144 message: "Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead."
145 }]
146 }]
147 },
148 {
149 code: "import AllowedObject, * as DisallowedObject from \"foo\";",
150 options: [{
151 paths: [{
152 name: "bar",
153 importNames: ["DisallowedObject"],
154 message: "Please import 'DisallowedObject' from /bar/ instead."
155 }]
156 }]
157 },
158 {
159 code: "import \"foo\";",
160 options: [{
161 paths: [{
162 name: "foo",
163 importNames: ["DisallowedObject", "DisallowedObjectTwo"],
164 message: "Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead."
165 }]
166 }]
167 },
168 {
169 code: "import {\nAllowedObject,\nDisallowedObject, // eslint-disable-line\n} from \"foo\";",
170 options: [{ paths: [{ name: "foo", importNames: ["DisallowedObject"] }] }]
171 },
172 {
173 code: "export * from \"foo\";",
174 options: ["bar"]
175 },
176 {
177 code: "export * from \"foo\";",
178 options: [{
179 name: "bar",
180 importNames: ["DisallowedObject"]
181 }]
182 }
183 ],
184 invalid: [{
185 code: "import \"fs\"",
186 options: ["fs"],
187 errors: [{
188 message: "'fs' import is restricted from being used.",
189 type: "ImportDeclaration",
190 line: 1,
191 column: 1,
192 endColumn: 12
193 }]
194 }, {
195 code: "import os from \"os \";",
196 options: ["fs", "crypto ", "stream", "os"],
197 errors: [{
198 message: "'os' import is restricted from being used.",
199 type: "ImportDeclaration",
200 line: 1,
201 column: 1,
202 endColumn: 22
203 }]
204 }, {
205 code: "import \"foo/bar\";",
206 options: ["foo/bar"],
207 errors: [{
208 message: "'foo/bar' import is restricted from being used.",
209 type: "ImportDeclaration",
210 line: 1,
211 column: 1,
212 endColumn: 18
213 }]
214 }, {
215 code: "import withPaths from \"foo/bar\";",
216 options: [{ paths: ["foo/bar"] }],
217 errors: [{
218 message: "'foo/bar' import is restricted from being used.",
219 type: "ImportDeclaration",
220 line: 1,
221 column: 1,
222 endColumn: 33
223 }]
224 }, {
225 code: "import withPatterns from \"foo/bar\";",
226 options: [{ patterns: ["foo"] }],
227 errors: [{
228 message: "'foo/bar' import is restricted from being used by a pattern.",
229 type: "ImportDeclaration",
230 line: 1,
231 column: 1,
232 endColumn: 36
233 }]
234 }, {
235 code: "import withPatterns from \"foo/bar\";",
236 options: [{ patterns: ["bar"] }],
237 errors: [{
238 message: "'foo/bar' import is restricted from being used by a pattern.",
239 type: "ImportDeclaration",
240 line: 1,
241 column: 1,
242 endColumn: 36
243 }]
244 }, {
245 code: "import withGitignores from \"foo/bar\";",
246 options: [{ patterns: ["foo/*", "!foo/baz"] }],
247 errors: [{
248 message: "'foo/bar' import is restricted from being used by a pattern.",
249 type: "ImportDeclaration",
250 line: 1,
251 column: 1,
252 endColumn: 38
253 }]
254 }, {
255 code: "export * from \"fs\";",
256 options: ["fs"],
257 errors: [{
258 message: "'fs' import is restricted from being used.",
259 type: "ExportAllDeclaration",
260 line: 1,
261 column: 1,
262 endColumn: 20
263 }]
264 }, {
265 code: "export * as ns from \"fs\";",
266 options: ["fs"],
267 errors: [{
268 message: "'fs' import is restricted from being used.",
269 type: "ExportAllDeclaration",
270 line: 1,
271 column: 1,
272 endColumn: 26
273 }]
274 }, {
275 code: "export {a} from \"fs\";",
276 options: ["fs"],
277 errors: [{
278 message: "'fs' import is restricted from being used.",
279 type: "ExportNamedDeclaration",
280 line: 1,
281 column: 1,
282 endColumn: 22
283 }]
284 }, {
285 code: "export {foo as b} from \"fs\";",
286 options: [{
287 paths: [{
288 name: "fs",
289 importNames: ["foo"],
290 message: "Don't import 'foo'."
291 }]
292 }],
293 errors: [{
294 message: "'foo' import from 'fs' is restricted. Don't import 'foo'.",
295 type: "ExportNamedDeclaration",
296 line: 1,
297 column: 9,
298 endColumn: 17
299 }]
300 }, {
301 code: "export * as ns from \"fs\";",
302 options: [{
303 paths: [{
304 name: "fs",
305 importNames: ["foo"],
306 message: "Don't import 'foo'."
307 }]
308 }],
309 errors: [{
310 message: "* import is invalid because 'foo' from 'fs' is restricted. Don't import 'foo'.",
311 type: "ExportAllDeclaration",
312 line: 1,
313 column: 8,
314 endColumn: 9
315 }]
316 }, {
317 code: "import withGitignores from \"foo\";",
318 options: [{
319 name: "foo",
320 message: "Please import from 'bar' instead."
321 }],
322 errors: [{
323 message: "'foo' import is restricted from being used. Please import from 'bar' instead.",
324 type: "ImportDeclaration",
325 line: 1,
326 column: 1,
327 endColumn: 34
328 }]
329 }, {
330 code: "import withGitignores from \"bar\";",
331 options: [
332 "foo",
333 {
334 name: "bar",
335 message: "Please import from 'baz' instead."
336 },
337 "baz"
338 ],
339 errors: [{
340 message: "'bar' import is restricted from being used. Please import from 'baz' instead.",
341 type: "ImportDeclaration",
342 line: 1,
343 column: 1,
344 endColumn: 34
345 }]
346 }, {
347 code: "import withGitignores from \"foo\";",
348 options: [{
349 paths: [{
350 name: "foo",
351 message: "Please import from 'bar' instead."
352 }]
353 }],
354 errors: [{
355 message: "'foo' import is restricted from being used. Please import from 'bar' instead.",
356 type: "ImportDeclaration",
357 line: 1,
358 column: 1,
359 endColumn: 34
360 }]
361 },
362 {
363 code: "import DisallowedObject from \"foo\";",
364 options: [{
365 paths: [{
366 name: "foo",
367 importNames: ["default"],
368 message: "Please import the default import of 'foo' from /bar/ instead."
369 }]
370 }],
371 errors: [{
372 message: "'default' import from 'foo' is restricted. Please import the default import of 'foo' from /bar/ instead.",
373 type: "ImportDeclaration",
374 line: 1,
375 column: 8,
376 endColumn: 24
377 }]
378 },
379 {
380 code: "import * as All from \"foo\";",
381 options: [{
382 paths: [{
383 name: "foo",
384 importNames: ["DisallowedObject"],
385 message: "Please import 'DisallowedObject' from /bar/ instead."
386 }]
387 }],
388 errors: [{
389 message: "* import is invalid because 'DisallowedObject' from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.",
390 type: "ImportDeclaration",
391 line: 1,
392 column: 8,
393 endColumn: 16
394 }]
395 },
396 {
397 code: "export * from \"foo\";",
398 options: [{
399 paths: [{
400 name: "foo",
401 importNames: ["DisallowedObject"],
402 message: "Please import 'DisallowedObject' from /bar/ instead."
403 }]
404 }],
405 errors: [{
406 message: "* import is invalid because 'DisallowedObject' from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.",
407 type: "ExportAllDeclaration",
408 line: 1,
409 column: 8,
410 endColumn: 9
411 }]
412 },
413 {
414 code: "export * from \"foo\";",
415 options: [{
416 name: "foo",
417 importNames: ["DisallowedObject1, DisallowedObject2"]
418 }],
419 errors: [{
420 message: "* import is invalid because 'DisallowedObject1, DisallowedObject2' from 'foo' is restricted.",
421 type: "ExportAllDeclaration",
422 line: 1,
423 column: 8,
424 endColumn: 9
425 }]
426 },
427 {
428 code: "import { DisallowedObject } from \"foo\";",
429 options: [{
430 paths: [{
431 name: "foo",
432 importNames: ["DisallowedObject"],
433 message: "Please import 'DisallowedObject' from /bar/ instead."
434 }]
435 }],
436 errors: [{
437 message: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.",
438 type: "ImportDeclaration",
439 line: 1,
440 column: 10,
441 endColumn: 26
442 }]
443 },
444 {
445 code: "import { DisallowedObject as AllowedObject } from \"foo\";",
446 options: [{
447 paths: [{
448 name: "foo",
449 importNames: ["DisallowedObject"],
450 message: "Please import 'DisallowedObject' from /bar/ instead."
451 }]
452 }],
453 errors: [{
454 message: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.",
455 type: "ImportDeclaration",
456 line: 1,
457 column: 10,
458 endColumn: 43
459 }]
460 },
461 {
462 code: "import { AllowedObject, DisallowedObject } from \"foo\";",
463 options: [{
464 paths: [{
465 name: "foo",
466 importNames: ["DisallowedObject"],
467 message: "Please import 'DisallowedObject' from /bar/ instead."
468 }]
469 }],
470 errors: [{
471 message: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.",
472 type: "ImportDeclaration",
473 line: 1,
474 column: 25,
475 endColumn: 41
476 }]
477 },
478 {
479 code: "import { AllowedObject, DisallowedObject as AllowedObjectTwo } from \"foo\";",
480 options: [{
481 paths: [{
482 name: "foo",
483 importNames: ["DisallowedObject"],
484 message: "Please import 'DisallowedObject' from /bar/ instead."
485 }]
486 }],
487 errors: [{
488 message: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.",
489 type: "ImportDeclaration",
490 line: 1,
491 column: 25,
492 endColumn: 61
493 }]
494 },
495 {
496 code: "import { AllowedObject, DisallowedObject as AllowedObjectTwo } from \"foo\";",
497 options: [{
498 paths: [{
499 name: "foo",
500 importNames: ["DisallowedObjectTwo", "DisallowedObject"],
501 message: "Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead."
502 }]
503 }],
504 errors: [{
505 message: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead.",
506 type: "ImportDeclaration",
507 line: 1,
508 column: 25,
509 endColumn: 61
510 }]
511 },
512 {
513 code: "import { AllowedObject, DisallowedObject as AllowedObjectTwo } from \"foo\";",
514 options: [{
515 paths: [{
516 name: "foo",
517 importNames: ["DisallowedObject", "DisallowedObjectTwo"],
518 message: "Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead."
519 }]
520 }],
521 errors: [{
522 message: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead.",
523 type: "ImportDeclaration",
524 line: 1,
525 column: 25,
526 endColumn: 61
527 }]
528 },
529 {
530 code: "import DisallowedObject, { AllowedObject as AllowedObjectTwo } from \"foo\";",
531 options: [{
532 paths: [{
533 name: "foo",
534 importNames: ["default"],
535 message: "Please import the default import of 'foo' from /bar/ instead."
536 }]
537 }],
538 errors: [{
539 message: "'default' import from 'foo' is restricted. Please import the default import of 'foo' from /bar/ instead.",
540 type: "ImportDeclaration",
541 line: 1,
542 column: 8,
543 endColumn: 24
544 }]
545 },
546 {
547 code: "import AllowedObject, { DisallowedObject as AllowedObjectTwo } from \"foo\";",
548 options: [{
549 paths: [{
550 name: "foo",
551 importNames: ["DisallowedObject"],
552 message: "Please import 'DisallowedObject' from /bar/ instead."
553 }]
554 }],
555 errors: [{
556 message: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.",
557 type: "ImportDeclaration",
558 line: 1,
559 column: 25,
560 endColumn: 61
561 }]
562 },
563 {
564 code: "import AllowedObject, * as AllowedObjectTwo from \"foo\";",
565 options: [{
566 paths: [{
567 name: "foo",
568 importNames: ["DisallowedObject"],
569 message: "Please import 'DisallowedObject' from /bar/ instead."
570 }]
571 }],
572 errors: [{
573 message: "* import is invalid because 'DisallowedObject' from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead.",
574 type: "ImportDeclaration",
575 line: 1,
576 column: 23,
577 endColumn: 44
578 }]
579 },
580 {
581 code: "import AllowedObject, * as AllowedObjectTwo from \"foo\";",
582 options: [{
583 paths: [{
584 name: "foo",
585 importNames: ["DisallowedObject", "DisallowedObjectTwo"],
586 message: "Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead."
587 }]
588 }],
589 errors: [{
590 message: "* import is invalid because 'DisallowedObject,DisallowedObjectTwo' from 'foo' is restricted. Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead.",
591 type: "ImportDeclaration",
592 line: 1,
593 column: 23,
594 endColumn: 44
595 }]
596 },
597 {
598 code: "import { DisallowedObjectOne, DisallowedObjectTwo, AllowedObject } from \"foo\";",
599 options: [{
600 paths: [{
601 name: "foo",
602 importNames: ["DisallowedObjectOne", "DisallowedObjectTwo"]
603 }]
604 }],
605 errors: [{
606 message: "'DisallowedObjectOne' import from 'foo' is restricted.",
607 type: "ImportDeclaration",
608 line: 1,
609 column: 10,
610 endColumn: 29
611 }, {
612 message: "'DisallowedObjectTwo' import from 'foo' is restricted.",
613 type: "ImportDeclaration",
614 line: 1,
615 column: 31,
616 endColumn: 50
617 }]
618 },
619 {
620 code: "import { DisallowedObjectOne, DisallowedObjectTwo, AllowedObject } from \"foo\";",
621 options: [{
622 paths: [{
623 name: "foo",
624 importNames: ["DisallowedObjectOne", "DisallowedObjectTwo"],
625 message: "Please import this module from /bar/ instead."
626 }]
627 }],
628 errors: [{
629 message: "'DisallowedObjectOne' import from 'foo' is restricted. Please import this module from /bar/ instead.",
630 type: "ImportDeclaration",
631 line: 1,
632 column: 10,
633 endColumn: 29
634 }, {
635 message: "'DisallowedObjectTwo' import from 'foo' is restricted. Please import this module from /bar/ instead.",
636 type: "ImportDeclaration",
637 line: 1,
638 column: 31,
639 endColumn: 50
640 }]
641 },
642 {
643 code: "import { AllowedObject, DisallowedObject as Bar } from \"foo\";",
644 options: [{
645 paths: [{
646 name: "foo",
647 importNames: ["DisallowedObject"]
648 }]
649 }],
650 errors: [{
651 message: "'DisallowedObject' import from 'foo' is restricted.",
652 type: "ImportDeclaration",
653 line: 1,
654 column: 25,
655 endColumn: 48
656 }]
657 },
658 {
659 code: "import foo, { bar } from 'mod';",
660 options: [{
661 paths: [{
662 name: "mod",
663 importNames: ["bar"]
664 }]
665 }],
666 errors: [{
667 message: "'bar' import from 'mod' is restricted.",
668 type: "ImportDeclaration",
669 line: 1,
670 column: 15,
671 endColumn: 18
672 }]
673 },
674 {
675 code: "import foo, { bar } from 'mod';",
676 options: [{
677 paths: [{
678 name: "mod",
679 importNames: ["default"]
680 }]
681 }],
682 errors: [{
683 message: "'default' import from 'mod' is restricted.",
684 type: "ImportDeclaration",
685 line: 1,
686 column: 8,
687 endColumn: 11
688 }]
689 },
690 {
691 code: "import foo, * as bar from 'mod';",
692 options: [{
693 paths: [{
694 name: "mod",
695 importNames: ["default"]
696 }]
697 }],
698 errors: [{
699 message: "'default' import from 'mod' is restricted.",
700 type: "ImportDeclaration",
701 line: 1,
702 column: 8,
703 endColumn: 11
704 }, {
705 message: "* import is invalid because 'default' from 'mod' is restricted.",
706 type: "ImportDeclaration",
707 line: 1,
708 column: 13,
709 endColumn: 21
710 }]
711 }, {
712 code: "import * as bar from 'foo';",
713 options: ["foo"],
714 errors: [{
715 message: "'foo' import is restricted from being used.",
716 type: "ImportDeclaration",
717 line: 1,
718 column: 1,
719 endColumn: 28
720 }]
721 }, {
722 code: "import { a, a as b } from 'mod';",
723 options: [{
724 paths: [{
725 name: "mod",
726 importNames: ["a"]
727 }]
728 }],
729 errors: [{
730 message: "'a' import from 'mod' is restricted.",
731 type: "ImportDeclaration",
732 line: 1,
733 column: 10,
734 endColumn: 11
735 }, {
736 message: "'a' import from 'mod' is restricted.",
737 type: "ImportDeclaration",
738 line: 1,
739 column: 13,
740 endColumn: 19
741 }]
742 }, {
743 code: "export { x as y, x as z } from 'mod';",
744 options: [{
745 paths: [{
746 name: "mod",
747 importNames: ["x"]
748 }]
749 }],
750 errors: [{
751 message: "'x' import from 'mod' is restricted.",
752 type: "ExportNamedDeclaration",
753 line: 1,
754 column: 10,
755 endColumn: 16
756 }, {
757 message: "'x' import from 'mod' is restricted.",
758 type: "ExportNamedDeclaration",
759 line: 1,
760 column: 18,
761 endColumn: 24
762 }]
763 }, {
764 code: "import foo, { default as bar } from 'mod';",
765 options: [{
766 paths: [{
767 name: "mod",
768 importNames: ["default"]
769 }]
770 }],
771 errors: [{
772 message: "'default' import from 'mod' is restricted.",
773 type: "ImportDeclaration",
774 line: 1,
775 column: 8,
776 endColumn: 11
777 }, {
778 message: "'default' import from 'mod' is restricted.",
779 type: "ImportDeclaration",
780 line: 1,
781 column: 15,
782 endColumn: 29
783 }]
784 }
785 ]
786 });