eslint-plugin-sonarjs
Version:
79 lines (78 loc) • 3.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PRELOADABLE_DEPENDENCY_MANIFESTS = exports.DEPENDENCY_MANIFESTS = exports.PNPM_WORKSPACE_YAML = exports.DENO_JSONC = exports.DENO_JSON = exports.PACKAGE_JSON = void 0;
exports.isPreloadableDependencyManifestPath = isPreloadableDependencyManifestPath;
exports.fillManifestCaches = fillManifestCaches;
exports.clearDependenciesCache = clearDependenciesCache;
exports.getPreloadableDependencyManifestName = getPreloadableDependencyManifestName;
const find_minimatch_js_1 = require("../find-up/find-minimatch.js");
const dependencies_js_1 = require("./dependencies.js");
const closest_js_1 = require("../find-up/closest.js");
const all_in_parent_dirs_js_1 = require("../find-up/all-in-parent-dirs.js");
const posix_1 = require("node:path/posix");
const parsed_dependency_files_js_1 = require("./parsed-dependency-files.js");
exports.PACKAGE_JSON = 'package.json';
exports.DENO_JSON = 'deno.json';
exports.DENO_JSONC = 'deno.jsonc';
exports.PNPM_WORKSPACE_YAML = 'pnpm-workspace.yaml';
exports.DEPENDENCY_MANIFESTS = [exports.DENO_JSON, exports.DENO_JSONC, exports.PACKAGE_JSON];
exports.PRELOADABLE_DEPENDENCY_MANIFESTS = [
...exports.DEPENDENCY_MANIFESTS,
exports.PNPM_WORKSPACE_YAML,
];
function isPreloadableDependencyManifestName(fileName) {
return exports.PRELOADABLE_DEPENDENCY_MANIFESTS.includes(fileName);
}
function isPreloadableDependencyManifestPath(path) {
const normalizedBasename = (0, posix_1.basename)(path).toLowerCase();
return isPreloadableDependencyManifestName(normalizedBasename);
}
/**
* Preloads manifest lookup caches so later rules can resolve manifests
* without re-walking the file system.
*/
function fillManifestCaches(manifestName, manifests, dirnameToParent, topDir) {
const closestCache = closest_js_1.closestPatternCache.get(manifestName).get(topDir);
const manifestsInParentsCache = manifestName === exports.PNPM_WORKSPACE_YAML
? undefined
: all_in_parent_dirs_js_1.patternInParentsCache.get(manifestName).get(topDir);
const sortedDirs = Array.from(dirnameToParent.entries()).sort(([leftDir], [rightDir]) => leftDir.split('/').length - rightDir.split('/').length || leftDir.localeCompare(rightDir));
for (const [dir, parent] of sortedDirs) {
const currentManifest = manifests.get(dir);
const inheritedClosestManifest = parent ? closestCache.get(parent) : undefined;
closestCache.set(dir, currentManifest ?? inheritedClosestManifest);
if (!manifestsInParentsCache) {
continue;
}
const manifestsInParents = parent ? [...manifestsInParentsCache.get(parent)] : [];
if (currentManifest) {
manifestsInParents.unshift(currentManifest);
}
manifestsInParentsCache.set(dir, manifestsInParents);
}
}
/**
* In the case of SonarIDE, when a dependency manifest file changes, the cache can become obsolete.
*/
function clearDependenciesCache() {
dependencies_js_1.dependenciesCache.clear();
dependencies_js_1.moduleTypeCache.clear();
(0, parsed_dependency_files_js_1.clearParsedDependencyFileCache)();
for (const manifestName of exports.PRELOADABLE_DEPENDENCY_MANIFESTS) {
closest_js_1.closestPatternCache.get(manifestName).clear();
if (manifestName !== exports.PNPM_WORKSPACE_YAML) {
all_in_parent_dirs_js_1.patternInParentsCache.get(manifestName).clear();
}
}
find_minimatch_js_1.MinimatchCache.clear();
}
/**
* Returns the preloadable dependency manifest filename if the path points to one.
*/
function getPreloadableDependencyManifestName(path) {
const normalizedBasename = (0, posix_1.basename)(path).toLowerCase();
if (isPreloadableDependencyManifestName(normalizedBasename)) {
return normalizedBasename;
}
return undefined;
}