Path: blob/main/src/rules/no-relative-parent-imports.js
829 views
import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisitor';1import docsUrl from '../docsUrl';2import { basename, dirname, relative } from 'path';3import resolve from 'eslint-module-utils/resolve';45import importType from '../core/importType';67module.exports = {8meta: {9type: 'suggestion',10docs: {11url: docsUrl('no-relative-parent-imports'),12},13schema: [makeOptionsSchema()],14},1516create: function noRelativePackages(context) {17const myPath = context.getPhysicalFilename ? context.getPhysicalFilename() : context.getFilename();18if (myPath === '<text>') return {}; // can't check a non-file1920function checkSourceValue(sourceNode) {21const depPath = sourceNode.value;2223if (importType(depPath, context) === 'external') { // ignore packages24return;25}2627const absDepPath = resolve(depPath, context);2829if (!absDepPath) { // unable to resolve path30return;31}3233const relDepPath = relative(dirname(myPath), absDepPath);3435if (importType(relDepPath, context) === 'parent') {36context.report({37node: sourceNode,38message: 'Relative imports from parent directories are not allowed. ' +39`Please either pass what you're importing through at runtime ` +40`(dependency injection), move \`${basename(myPath)}\` to same ` +41`directory as \`${depPath}\` or consider making \`${depPath}\` a package.`,42});43}44}4546return moduleVisitor(checkSourceValue, context.options[0]);47},48};495051