UNPKG

eslint-plugin-no-barrel-files

Version:

ESLint plugin for reducing barrel-file usage in two ways:

71 lines 3.35 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOptionsCacheKey = getOptionsCacheKey; exports.isRelativePath = isRelativePath; exports.resolveModuleFile = resolveModuleFile; exports.normalizeModulePath = normalizeModulePath; exports.toRelativeImportSpecifier = toRelativeImportSpecifier; exports.matchesAliasPattern = matchesAliasPattern; exports.applyAliasTarget = applyAliasTarget; exports.buildAliasSpecifier = buildAliasSpecifier; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const SOURCE_FILE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.mts', '.cts', '.mjs', '.cjs']; const moduleFileResolutionCache = new Map(); function getOptionsCacheKey(options, cwd) { return JSON.stringify({ cwd, options: options !== null && options !== void 0 ? options : {}, }); } function isRelativePath(value) { return value.startsWith('.'); } function resolveModuleFile(importerFilename, specifier) { var _a; const cacheKey = `${importerFilename}\0${specifier}`; const cachedResult = moduleFileResolutionCache.get(cacheKey); if (cachedResult !== undefined) { return cachedResult; } const resolvedBase = node_path_1.default.resolve(node_path_1.default.dirname(importerFilename), specifier); const candidatePaths = [ resolvedBase, ...SOURCE_FILE_EXTENSIONS.map(extension => `${resolvedBase}${extension}`), ...SOURCE_FILE_EXTENSIONS.map(extension => node_path_1.default.join(resolvedBase, `index${extension}`)), ]; const resolvedFilePath = (_a = candidatePaths.find(candidatePath => node_fs_1.default.existsSync(candidatePath) && node_fs_1.default.statSync(candidatePath).isFile())) !== null && _a !== void 0 ? _a : null; moduleFileResolutionCache.set(cacheKey, resolvedFilePath); return resolvedFilePath; } function normalizeModulePath(filePath) { const normalizedPath = filePath.split(node_path_1.default.sep).join('/'); return normalizedPath.replace(/\/index(?=(\.[^./]+)?$)/, '').replace(/\.[^./]+$/, ''); } function toRelativeImportSpecifier(importerFilename, resolvedFilePath) { const relativePath = node_path_1.default.relative(node_path_1.default.dirname(importerFilename), resolvedFilePath); const normalizedPath = normalizeModulePath(relativePath); return normalizedPath.startsWith('.') ? normalizedPath : `./${normalizedPath}`; } function matchesAliasPattern(specifier, pattern) { if (!pattern.includes('*')) { return specifier === pattern ? '' : null; } const [rawPrefix = '', rawSuffix = ''] = pattern.split('*'); const prefix = rawPrefix; const suffix = rawSuffix; if (!specifier.startsWith(prefix) || !specifier.endsWith(suffix)) { return null; } return specifier.slice(prefix.length, specifier.length - suffix.length); } function applyAliasTarget(target, wildcardValue) { return target.includes('*') ? target.replace('*', wildcardValue) : target; } function buildAliasSpecifier(pattern, wildcardValue) { return pattern.includes('*') ? pattern.replace('*', wildcardValue) : pattern; } //# sourceMappingURL=path-utils.js.map