Path: blob/main/src/rules/no-unassigned-import.js
829 views
import path from 'path';1import minimatch from 'minimatch';23import isStaticRequire from '../core/staticRequire';4import docsUrl from '../docsUrl';56function report(context, node) {7context.report({8node,9message: 'Imported module should be assigned',10});11}1213function testIsAllow(globs, filename, source) {14if (!Array.isArray(globs)) {15return false; // default doesn't allow any patterns16}1718let filePath;1920if (source[0] !== '.' && source[0] !== '/') { // a node module21filePath = source;22} else {23filePath = path.resolve(path.dirname(filename), source); // get source absolute path24}2526return globs.find(glob => (27minimatch(filePath, glob) ||28minimatch(filePath, path.join(process.cwd(), glob))29)) !== undefined;30}3132function create(context) {33const options = context.options[0] || {};34const filename = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();35const isAllow = source => testIsAllow(options.allow, filename, source);3637return {38ImportDeclaration(node) {39if (node.specifiers.length === 0 && !isAllow(node.source.value)) {40report(context, node);41}42},43ExpressionStatement(node) {44if (node.expression.type === 'CallExpression' &&45isStaticRequire(node.expression) &&46!isAllow(node.expression.arguments[0].value)) {47report(context, node.expression);48}49},50};51}5253module.exports = {54create,55meta: {56type: 'suggestion',57docs: {58url: docsUrl('no-unassigned-import'),59},60schema: [61{62'type': 'object',63'properties': {64'devDependencies': { 'type': ['boolean', 'array'] },65'optionalDependencies': { 'type': ['boolean', 'array'] },66'peerDependencies': { 'type': ['boolean', 'array'] },67'allow': {68'type': 'array',69'items': {70'type': 'string',71},72},73},74'additionalProperties': false,75},76],77},78};798081