UNPKG

eslint-plugin-kisszaya-fsd-plugin

Version:

Plugin to format code by fsd principles

65 lines (64 loc) 2.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.isLayer = isLayer; exports.isAbsolute = isAbsolute; exports.getPattern = getPattern; exports.getFilenameElems = getFilenameElems; exports.normalizePath = normalizePath; exports.getAbsoluteImportPathElems = getAbsoluteImportPathElems; const path_1 = __importDefault(require("path")); function isLayer({ path, projectStructure, }) { return new Set(Object.keys(projectStructure)).has(path); } function isAbsolute({ alias, importPath, projectStructure, }) { if (!importPath.startsWith(alias)) { return false; } const layer = importPath.slice(alias.length).split("/")[0]; return isLayer({ path: layer, projectStructure }); } /** * Get passed filename Pattern * @example filenameElems: ["pages", "page-name", "ui", "index.ts"] --> ["pages", "**"] */ function getPattern({ elems, projectStructure, }) { const pattern = []; let structure = projectStructure; for (let elem of elems) { if (structure[elem]) { pattern.push(elem); if (structure[elem] === 1) { break; } else { structure = structure[elem]; } } else if (Boolean(structure["**"])) { pattern.push("**"); if ((structure === null || structure === void 0 ? void 0 : structure["**"]) === 1) { break; } else { structure = structure[elem]; } } } return pattern; } function getFilenameElems({ filename, srcPath }) { var _a, _b; const normalizedFilename = normalizePath(filename); return (_b = (_a = normalizedFilename.split(srcPath)[1]) === null || _a === void 0 ? void 0 : _a.split("/")) !== null && _b !== void 0 ? _b : []; } function normalizePath(filename) { const normalized = path_1.default.normalize(filename).replace(/\\/g, "/"); return normalized.split(path_1.default.sep).join(path_1.default.posix.sep); } function getAbsoluteImportPathElems({ importPath, alias, }) { const importPathWithoutAlias = importPath.slice(alias.length); return importPathWithoutAlias.split("/"); }