Skip to content

Commit

Permalink
Code Refactoring for Enhanced Readability and Maintainability in Modu…
Browse files Browse the repository at this point in the history
…le Resolution Logic
  • Loading branch information
SpEXterXD committed Oct 6, 2024
1 parent 67cc798 commit 4ceee4e
Showing 1 changed file with 31 additions and 46 deletions.
77 changes: 31 additions & 46 deletions src/core/importType.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,72 @@
import { isAbsolute as nodeIsAbsolute, relative, resolve as nodeResolve } from 'path';
import isCoreModule from 'is-core-module';

import resolve from 'eslint-module-utils/resolve';
import { getContextPackagePath } from './packagePath';

const scopedRegExp = /^@[^/]+\/?[^/]+/;
const moduleRegExp = /^\w/;
const moduleMainRegExp = /^[\w]((?!\/).)*$/;
const indexFiles = ['.', './', './index', './index.js'];

export function isScoped(name) {
return name && scopedRegExp.test(name);
}

function baseModule(name) {
if (isScoped(name)) {
const [scope, pkg] = name.split('/');
return `${scope}/${pkg}`;
}
const [pkg] = name.split('/');
return pkg;
const parts = name.split('/');
return isScoped(name) ? `${parts[0]}/${parts[1]}` : parts[0];
}

function isInternalRegexMatch(name, settings) {
const internalScope = settings && settings['import/internal-regex'];
const internalScope = settings?.['import/internal-regex'];
return internalScope && new RegExp(internalScope).test(name);
}

export function isAbsolute(name) {
return typeof name === 'string' && nodeIsAbsolute(name);
}

// path is defined only when a resolver resolves to a non-standard path
export function isBuiltIn(name, settings, path) {
if (path || !name) { return false; }
if (path || !name) return false;
const base = baseModule(name);
const extras = settings && settings['import/core-modules'] || [];
return isCoreModule(base) || extras.indexOf(base) > -1;
const extras = settings?.['import/core-modules'] || [];
return isCoreModule(base) || extras.includes(base);
}

const moduleRegExp = /^\w/;
function isModule(name) {
return name && moduleRegExp.test(name);
}
const isModule = (name) => name && moduleRegExp.test(name);

const moduleMainRegExp = /^[\w]((?!\/).)*$/;
function isModuleMain(name) {
return name && moduleMainRegExp.test(name);
}
const isModuleMain = (name) => name && moduleMainRegExp.test(name);

function isRelativeToParent(name) {
return (/^\.\.$|^\.\.[\\/]/).test(name);
return /^\.\.$|^\.\.[\\/]/.test(name);
}
const indexFiles = ['.', './', './index', './index.js'];

function isIndex(name) {
return indexFiles.indexOf(name) !== -1;
return indexFiles.includes(name);
}

function isRelativeToSibling(name) {
return (/^\.[\\/]/).test(name);
return /^\.[\\/]/.test(name);
}

function isExternalPath(path, context) {
if (!path) {
return false;
}
if (!path) return false;

const { settings } = context;
const packagePath = getContextPackagePath(context);

if (relative(packagePath, path).startsWith('..')) {
return true;
}

const folders = settings && settings['import/external-module-folders'] || ['node_modules'];
const folders = settings?.['import/external-module-folders'] || ['node_modules'];
return folders.some((folder) => {
const folderPath = nodeResolve(packagePath, folder);
const relativePath = relative(folderPath, path);
return !relativePath.startsWith('..');
return !relative(folderPath, path).startsWith('..');
});
}

function isInternalPath(path, context) {
if (!path) {
return false;
}
if (!path) return false;
const packagePath = getContextPackagePath(context);
return !relative(packagePath, path).startsWith('../');
}
Expand All @@ -91,15 +77,15 @@ function isExternalLookingName(name) {

function typeTest(name, context, path) {
const { settings } = context;
if (isInternalRegexMatch(name, settings)) { return 'internal'; }
if (isAbsolute(name, settings, path)) { return 'absolute'; }
if (isBuiltIn(name, settings, path)) { return 'builtin'; }
if (isRelativeToParent(name, settings, path)) { return 'parent'; }
if (isIndex(name, settings, path)) { return 'index'; }
if (isRelativeToSibling(name, settings, path)) { return 'sibling'; }
if (isExternalPath(path, context)) { return 'external'; }
if (isInternalPath(path, context)) { return 'internal'; }
if (isExternalLookingName(name)) { return 'external'; }
if (isInternalRegexMatch(name, settings)) return 'internal';
if (isAbsolute(name)) return 'absolute';
if (isBuiltIn(name, settings, path)) return 'builtin';
if (isRelativeToParent(name)) return 'parent';
if (isIndex(name)) return 'index';
if (isRelativeToSibling(name)) return 'sibling';
if (isExternalPath(path, context)) return 'external';
if (isInternalPath(path, context)) return 'internal';
if (isExternalLookingName(name)) return 'external';
return 'unknown';
}

Expand All @@ -112,14 +98,13 @@ export function isExternalModule(name, path, context) {

export function isExternalModuleMain(name, path, context) {
if (arguments.length < 3) {
throw new TypeError('isExternalModule: name, path, and context are all required');
throw new TypeError('isExternalModuleMain: name, path, and context are all required');
}
return isModuleMain(name) && typeTest(name, context, path) === 'external';
}

const scopedMainRegExp = /^@[^/]+\/?[^/]+$/;
export function isScopedMain(name) {
return name && scopedMainRegExp.test(name);
return name && scopedRegExp.test(name);
}

export default function resolveImportType(name, context) {
Expand Down

0 comments on commit 4ceee4e

Please sign in to comment.