All files / eslint-plugin-craftsmanlint/src/rules/no-namespace-imports index.js

100% Statements 13/13
90.9% Branches 10/11
100% Functions 4/4
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51              1x                     9x   9x 11x     9x     4x 4x 4x   4x 2x     4x 3x                            
/**
 * Copyright (c) 2021-present, Matti Bar-Zeev.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
 
export default {
    name: 'no-namespace-imports',
    meta: {
        type: 'problem',
 
        docs: {
            description: 'disallow namespace imports',
            recommended: false,
        },
    },
    create: (context) => {
        return {
            ImportDeclaration: function (node) {
                const hasNamespaceSpecifier = node.specifiers.some((specifier) => {
                    return specifier.type === 'ImportNamespaceSpecifier';
                });
 
                if (hasNamespaceSpecifier) {
                    // If there are forbidden modules configuration, check if the
                    // source module is among them, and only if it is - report
                    let shouldReport = true;
                    const sourceModule = node.source.value;
                    const forbiddenModules = context?.options[0]?.forbiddenModules;
 
                    if (forbiddenModules) {
                        shouldReport = forbiddenModules.includes(sourceModule);
                    }
 
                    if (shouldReport) {
                        context.report({
                            node,
                            message:
                                'Importing a namespace is not allowed for "{{sourceModule}}". Please use a named import instead',
                            data: {
                                sourceModule,
                            },
                        });
                    }
                }
            },
        };
    },
};