]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-restricted-imports.js
2 * @fileoverview Tests for no-restricted-imports.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require ( "../../../lib/rules/no-restricted-imports" ),
13 { RuleTester
} = require ( "../../../lib/rule-tester" );
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester ({ parserOptions
: { ecmaVersion
: 2020 , sourceType
: "module" } });
21 ruleTester
. run ( "no-restricted-imports" , rule
, {
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*" ] }] },
33 code
: "import withPatternsAndPaths from \" foo/bar \" ;" ,
34 options
: [{ paths
: [ "foo" ], patterns
: [ "foo/c*" ] }]
37 code
: "import withGitignores from \" foo/bar \" ;" ,
38 options
: [{ patterns
: [ "foo/*" , "!foo/bar" ] }]
41 code
: "import AllowedObject from \" foo \" ;" ,
45 importNames
: [ "DisallowedObject" ]
50 code
: "import DisallowedObject from \" foo \" ;" ,
54 importNames
: [ "DisallowedObject" ]
59 code
: "import * as DisallowedObject from \" foo \" ;" ,
63 importNames
: [ "DisallowedObject" ],
64 message
: "Please import 'DisallowedObject' from /bar/ instead."
69 code
: "import { AllowedObject } from \" foo \" ;" ,
73 importNames
: [ "DisallowedObject" ],
74 message
: "Please import 'DisallowedObject' from /bar/ instead."
79 code
: "import { DisallowedObject } from \" foo \" ;" ,
83 importNames
: [ "DisallowedObject" ],
84 message
: "Please import 'DisallowedObject' from /bar/ instead."
89 code
: "import { AllowedObject as DisallowedObject } from \" foo \" ;" ,
93 importNames
: [ "DisallowedObject" ],
94 message
: "Please import 'DisallowedObject' from /bar/ instead."
99 code
: "import { AllowedObject, AllowedObjectTwo } from \" foo \" ;" ,
103 importNames
: [ "DisallowedObject" ],
104 message
: "Please import 'DisallowedObject' from /bar/ instead."
109 code
: "import { AllowedObject, AllowedObjectTwo as DisallowedObject } from \" foo \" ;" ,
113 importNames
: [ "DisallowedObject" ],
114 message
: "Please import 'DisallowedObject' from /bar/ instead."
119 code
: "import AllowedObjectThree, { AllowedObject as AllowedObjectTwo } from \" foo \" ;" ,
123 importNames
: [ "DisallowedObject" ],
124 message
: "Please import 'DisallowedObject' from /bar/ instead."
129 code
: "import AllowedObject, { AllowedObjectTwo as DisallowedObject } from \" foo \" ;" ,
133 importNames
: [ "DisallowedObject" ],
134 message
: "Please import 'DisallowedObject' from /bar/ instead."
139 code
: "import AllowedObject, { AllowedObjectTwo as DisallowedObject } from \" foo \" ;" ,
143 importNames
: [ "DisallowedObject" , "DisallowedObjectTwo" ],
144 message
: "Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead."
149 code
: "import AllowedObject, * as DisallowedObject from \" foo \" ;" ,
153 importNames
: [ "DisallowedObject" ],
154 message
: "Please import 'DisallowedObject' from /bar/ instead."
159 code
: "import \" foo \" ;" ,
163 importNames
: [ "DisallowedObject" , "DisallowedObjectTwo" ],
164 message
: "Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead."
169 code
: "import { \n AllowedObject, \n DisallowedObject, // eslint-disable-line \n } from \" foo \" ;" ,
170 options
: [{ paths
: [{ name
: "foo" , importNames
: [ "DisallowedObject" ] }] }]
173 code
: "export * from \" foo \" ;" ,
177 code
: "export * from \" foo \" ;" ,
180 importNames
: [ "DisallowedObject" ]
185 code
: "import \" fs \" " ,
188 message
: "'fs' import is restricted from being used." ,
189 type
: "ImportDeclaration" ,
195 code
: "import os from \" os \" ;" ,
196 options
: [ "fs" , "crypto " , "stream" , "os" ],
198 message
: "'os' import is restricted from being used." ,
199 type
: "ImportDeclaration" ,
205 code
: "import \" foo/bar \" ;" ,
206 options
: [ "foo/bar" ],
208 message
: "'foo/bar' import is restricted from being used." ,
209 type
: "ImportDeclaration" ,
215 code
: "import withPaths from \" foo/bar \" ;" ,
216 options
: [{ paths
: [ "foo/bar" ] }],
218 message
: "'foo/bar' import is restricted from being used." ,
219 type
: "ImportDeclaration" ,
225 code
: "import withPatterns from \" foo/bar \" ;" ,
226 options
: [{ patterns
: [ "foo" ] }],
228 message
: "'foo/bar' import is restricted from being used by a pattern." ,
229 type
: "ImportDeclaration" ,
235 code
: "import withPatterns from \" foo/bar \" ;" ,
236 options
: [{ patterns
: [ "bar" ] }],
238 message
: "'foo/bar' import is restricted from being used by a pattern." ,
239 type
: "ImportDeclaration" ,
245 code
: "import withGitignores from \" foo/bar \" ;" ,
246 options
: [{ patterns
: [ "foo/*" , "!foo/baz" ] }],
248 message
: "'foo/bar' import is restricted from being used by a pattern." ,
249 type
: "ImportDeclaration" ,
255 code
: "export * from \" fs \" ;" ,
258 message
: "'fs' import is restricted from being used." ,
259 type
: "ExportAllDeclaration" ,
265 code
: "export * as ns from \" fs \" ;" ,
268 message
: "'fs' import is restricted from being used." ,
269 type
: "ExportAllDeclaration" ,
275 code
: "export {a} from \" fs \" ;" ,
278 message
: "'fs' import is restricted from being used." ,
279 type
: "ExportNamedDeclaration" ,
285 code
: "export {foo as b} from \" fs \" ;" ,
289 importNames
: [ "foo" ],
290 message
: "Don't import 'foo'."
294 message
: "'foo' import from 'fs' is restricted. Don't import 'foo'." ,
295 type
: "ExportNamedDeclaration" ,
301 code
: "export * as ns from \" fs \" ;" ,
305 importNames
: [ "foo" ],
306 message
: "Don't import 'foo'."
310 message
: "* import is invalid because 'foo' from 'fs' is restricted. Don't import 'foo'." ,
311 type
: "ExportAllDeclaration" ,
317 code
: "import withGitignores from \" foo \" ;" ,
320 message
: "Please import from 'bar' instead."
323 message
: "'foo' import is restricted from being used. Please import from 'bar' instead." ,
324 type
: "ImportDeclaration" ,
330 code
: "import withGitignores from \" bar \" ;" ,
335 message
: "Please import from 'baz' instead."
340 message
: "'bar' import is restricted from being used. Please import from 'baz' instead." ,
341 type
: "ImportDeclaration" ,
347 code
: "import withGitignores from \" foo \" ;" ,
351 message
: "Please import from 'bar' instead."
355 message
: "'foo' import is restricted from being used. Please import from 'bar' instead." ,
356 type
: "ImportDeclaration" ,
363 code
: "import DisallowedObject from \" foo \" ;" ,
367 importNames
: [ "default" ],
368 message
: "Please import the default import of 'foo' from /bar/ instead."
372 message
: "'default' import from 'foo' is restricted. Please import the default import of 'foo' from /bar/ instead." ,
373 type
: "ImportDeclaration" ,
380 code
: "import * as All from \" foo \" ;" ,
384 importNames
: [ "DisallowedObject" ],
385 message
: "Please import 'DisallowedObject' from /bar/ instead."
389 message
: "* import is invalid because 'DisallowedObject' from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead." ,
390 type
: "ImportDeclaration" ,
397 code
: "export * from \" foo \" ;" ,
401 importNames
: [ "DisallowedObject" ],
402 message
: "Please import 'DisallowedObject' from /bar/ instead."
406 message
: "* import is invalid because 'DisallowedObject' from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead." ,
407 type
: "ExportAllDeclaration" ,
414 code
: "export * from \" foo \" ;" ,
417 importNames
: [ "DisallowedObject1, DisallowedObject2" ]
420 message
: "* import is invalid because 'DisallowedObject1, DisallowedObject2' from 'foo' is restricted." ,
421 type
: "ExportAllDeclaration" ,
428 code
: "import { DisallowedObject } from \" foo \" ;" ,
432 importNames
: [ "DisallowedObject" ],
433 message
: "Please import 'DisallowedObject' from /bar/ instead."
437 message
: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead." ,
438 type
: "ImportDeclaration" ,
445 code
: "import { DisallowedObject as AllowedObject } from \" foo \" ;" ,
449 importNames
: [ "DisallowedObject" ],
450 message
: "Please import 'DisallowedObject' from /bar/ instead."
454 message
: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead." ,
455 type
: "ImportDeclaration" ,
462 code
: "import { AllowedObject, DisallowedObject } from \" foo \" ;" ,
466 importNames
: [ "DisallowedObject" ],
467 message
: "Please import 'DisallowedObject' from /bar/ instead."
471 message
: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead." ,
472 type
: "ImportDeclaration" ,
479 code
: "import { AllowedObject, DisallowedObject as AllowedObjectTwo } from \" foo \" ;" ,
483 importNames
: [ "DisallowedObject" ],
484 message
: "Please import 'DisallowedObject' from /bar/ instead."
488 message
: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead." ,
489 type
: "ImportDeclaration" ,
496 code
: "import { AllowedObject, DisallowedObject as AllowedObjectTwo } from \" foo \" ;" ,
500 importNames
: [ "DisallowedObjectTwo" , "DisallowedObject" ],
501 message
: "Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead."
505 message
: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead." ,
506 type
: "ImportDeclaration" ,
513 code
: "import { AllowedObject, DisallowedObject as AllowedObjectTwo } from \" foo \" ;" ,
517 importNames
: [ "DisallowedObject" , "DisallowedObjectTwo" ],
518 message
: "Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead."
522 message
: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead." ,
523 type
: "ImportDeclaration" ,
530 code
: "import DisallowedObject, { AllowedObject as AllowedObjectTwo } from \" foo \" ;" ,
534 importNames
: [ "default" ],
535 message
: "Please import the default import of 'foo' from /bar/ instead."
539 message
: "'default' import from 'foo' is restricted. Please import the default import of 'foo' from /bar/ instead." ,
540 type
: "ImportDeclaration" ,
547 code
: "import AllowedObject, { DisallowedObject as AllowedObjectTwo } from \" foo \" ;" ,
551 importNames
: [ "DisallowedObject" ],
552 message
: "Please import 'DisallowedObject' from /bar/ instead."
556 message
: "'DisallowedObject' import from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead." ,
557 type
: "ImportDeclaration" ,
564 code
: "import AllowedObject, * as AllowedObjectTwo from \" foo \" ;" ,
568 importNames
: [ "DisallowedObject" ],
569 message
: "Please import 'DisallowedObject' from /bar/ instead."
573 message
: "* import is invalid because 'DisallowedObject' from 'foo' is restricted. Please import 'DisallowedObject' from /bar/ instead." ,
574 type
: "ImportDeclaration" ,
581 code
: "import AllowedObject, * as AllowedObjectTwo from \" foo \" ;" ,
585 importNames
: [ "DisallowedObject" , "DisallowedObjectTwo" ],
586 message
: "Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead."
590 message
: "* import is invalid because 'DisallowedObject,DisallowedObjectTwo' from 'foo' is restricted. Please import 'DisallowedObject' and 'DisallowedObjectTwo' from /bar/ instead." ,
591 type
: "ImportDeclaration" ,
598 code
: "import { DisallowedObjectOne, DisallowedObjectTwo, AllowedObject } from \" foo \" ;" ,
602 importNames
: [ "DisallowedObjectOne" , "DisallowedObjectTwo" ]
606 message
: "'DisallowedObjectOne' import from 'foo' is restricted." ,
607 type
: "ImportDeclaration" ,
612 message
: "'DisallowedObjectTwo' import from 'foo' is restricted." ,
613 type
: "ImportDeclaration" ,
620 code
: "import { DisallowedObjectOne, DisallowedObjectTwo, AllowedObject } from \" foo \" ;" ,
624 importNames
: [ "DisallowedObjectOne" , "DisallowedObjectTwo" ],
625 message
: "Please import this module from /bar/ instead."
629 message
: "'DisallowedObjectOne' import from 'foo' is restricted. Please import this module from /bar/ instead." ,
630 type
: "ImportDeclaration" ,
635 message
: "'DisallowedObjectTwo' import from 'foo' is restricted. Please import this module from /bar/ instead." ,
636 type
: "ImportDeclaration" ,
643 code
: "import { AllowedObject, DisallowedObject as Bar } from \" foo \" ;" ,
647 importNames
: [ "DisallowedObject" ]
651 message
: "'DisallowedObject' import from 'foo' is restricted." ,
652 type
: "ImportDeclaration" ,
659 code
: "import foo, { bar } from 'mod';" ,
667 message
: "'bar' import from 'mod' is restricted." ,
668 type
: "ImportDeclaration" ,
675 code
: "import foo, { bar } from 'mod';" ,
679 importNames
: [ "default" ]
683 message
: "'default' import from 'mod' is restricted." ,
684 type
: "ImportDeclaration" ,
691 code
: "import foo, * as bar from 'mod';" ,
695 importNames
: [ "default" ]
699 message
: "'default' import from 'mod' is restricted." ,
700 type
: "ImportDeclaration" ,
705 message
: "* import is invalid because 'default' from 'mod' is restricted." ,
706 type
: "ImportDeclaration" ,
712 code
: "import * as bar from 'foo';" ,
715 message
: "'foo' import is restricted from being used." ,
716 type
: "ImportDeclaration" ,
722 code
: "import { a, a as b } from 'mod';" ,
730 message
: "'a' import from 'mod' is restricted." ,
731 type
: "ImportDeclaration" ,
736 message
: "'a' import from 'mod' is restricted." ,
737 type
: "ImportDeclaration" ,
743 code
: "export { x as y, x as z } from 'mod';" ,
751 message
: "'x' import from 'mod' is restricted." ,
752 type
: "ExportNamedDeclaration" ,
757 message
: "'x' import from 'mod' is restricted." ,
758 type
: "ExportNamedDeclaration" ,
764 code
: "import foo, { default as bar } from 'mod';" ,
768 importNames
: [ "default" ]
772 message
: "'default' import from 'mod' is restricted." ,
773 type
: "ImportDeclaration" ,
778 message
: "'default' import from 'mod' is restricted." ,
779 type
: "ImportDeclaration" ,