UNPKG

eslint-plugin-sonarjs

Version:
41 lines (40 loc) • 1.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDependencyTopDir = getDependencyTopDir; const node_fs_1 = __importDefault(require("node:fs")); const files_js_1 = require("../files.js"); const sonar_runtime_js_1 = require("../sonar-runtime.js"); const cache_js_1 = require("../cache.js"); const repositoryRootCache = new cache_js_1.ComputedCache((startDir) => { let dir = startDir; while (true) { // A worktree or submodule has a .git file rather than a directory. if (node_fs_1.default.existsSync((0, files_js_1.joinPaths)(dir, '.git'))) { return dir; } if ((0, files_js_1.isRoot)(dir)) { return null; } dir = (0, files_js_1.dirnamePath)(dir); } }); /** Bounds standalone dependency lookup to the repository containing the linted file. */ function getDependencyTopDir(context, filePath) { const cwd = (0, files_js_1.normalizeToAbsolutePath)(context.cwd); if ((0, sonar_runtime_js_1.isSonarRuntime)(context)) { return cwd; } const fileDir = (0, files_js_1.dirnamePath)(filePath); const repositoryRoot = repositoryRootCache.get(fileDir); if (repositoryRoot) { // Keep the existing cwd boundary when linting a nested repository or submodule // from a parent workspace; otherwise, include manifests up to the repo root. return (0, files_js_1.relativeToAncestorPath)(repositoryRoot, cwd) === undefined ? repositoryRoot : cwd; } // Without a repository marker, keep the existing working-directory boundary. // A file outside cwd cannot be bounded by cwd, so use its own directory. return (0, files_js_1.relativeToAncestorPath)(fileDir, cwd) === undefined ? fileDir : cwd; }