1 | const {docsUrl} = require('../utilities');
|
2 |
|
3 | module.exports = {
|
4 | meta: {
|
5 | docs: {
|
6 | description: 'Prevent namespace import declarations.',
|
7 | category: 'Stylistic Issues',
|
8 | recommended: false,
|
9 | uri: docsUrl('no-namespace-imports'),
|
10 | },
|
11 | schema: [
|
12 | {
|
13 | type: 'object',
|
14 | properties: {
|
15 | allow: {
|
16 | type: 'array',
|
17 | items: {type: 'string'},
|
18 | },
|
19 | },
|
20 | additionalProperties: false,
|
21 | },
|
22 | ],
|
23 | messages: {
|
24 | namespaceImport: `The namespace import declaration for {{name}} should be converted to a default or named import.`,
|
25 | },
|
26 | },
|
27 | create(context) {
|
28 | const options = context.options[0] || {};
|
29 | const allowed = options.allow || [];
|
30 |
|
31 | function report(node) {
|
32 | const name = node.source.value;
|
33 |
|
34 | context.report({
|
35 | node,
|
36 | messageId: 'namespaceImport',
|
37 | data: {name},
|
38 | });
|
39 | }
|
40 |
|
41 | return {
|
42 | ImportDeclaration(node) {
|
43 | if (allowed.length && ignoreModule(node, allowed)) {
|
44 | return;
|
45 | }
|
46 |
|
47 | const namespaceSpecifiers = getNamespaceSpecifiers(node);
|
48 |
|
49 | if (namespaceSpecifiers.length === 0) {
|
50 | return;
|
51 | }
|
52 |
|
53 | report(node);
|
54 | },
|
55 | };
|
56 | },
|
57 | };
|
58 |
|
59 | function getNamespaceSpecifiers(node) {
|
60 | return node.specifiers.filter((specifier) => {
|
61 | return specifier.type === 'ImportNamespaceSpecifier';
|
62 | });
|
63 | }
|
64 |
|
65 | function ignoreModule(node, allowed) {
|
66 | const regex = new RegExp(allowed.join('|'));
|
67 |
|
68 | return regex.test(node.source.value);
|
69 | }
|