UNPKG

eslint-plugin-sonarjs

Version:
91 lines (90 loc) • 3.39 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.clearParsedDependencyFileCache = clearParsedDependencyFileCache; exports.parsePackageJson = parsePackageJson; exports.parsePackageJsonContent = parsePackageJsonContent; exports.parsePnpmWorkspace = parsePnpmWorkspace; exports.parseDenoManifest = parseDenoManifest; /* * SonarQube JavaScript Plugin * Copyright (C) SonarSource Sàrl * mailto:info AT sonarsource DOT com * * You can redistribute and/or modify this program under the terms of * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the Sonar Source-Available License for more details. * * You should have received a copy of the Sonar Source-Available License * along with this program; if not, see https://sonarsource.com/license/ssal/ */ const filesystem_js_1 = require("../filesystem.js"); const typescript_1 = __importDefault(require("typescript")); const yaml_1 = __importDefault(require("yaml")); const files_js_1 = require("../files.js"); const parsedPackageJsonCache = new filesystem_js_1.FileSystemMap(); const parsedPnpmWorkspaceCache = new filesystem_js_1.FileSystemMap(); const parsedDenoManifestCache = new filesystem_js_1.FileSystemMap(); function clearParsedDependencyFileCache() { parsedPackageJsonCache.clear(); parsedPnpmWorkspaceCache.clear(); parsedDenoManifestCache.clear(); } function getOrSetParsedDependencyFile(cache, file, parse) { const cacheKey = (0, files_js_1.normalizeToAbsolutePath)(file.filePath); if (cache.has(cacheKey)) { return cache.get(cacheKey); } const parsed = parse(file); cache.set(cacheKey, parsed); return parsed; } function parsePackageJson(file) { return getOrSetParsedDependencyFile(parsedPackageJsonCache, file, file => parsePackageJsonContent(file.fileContent)); } function parsePackageJsonContent(content) { try { return JSON.parse((0, files_js_1.stripBOM)(content)); } catch { return undefined; } } function parsePnpmWorkspace(file) { return getOrSetParsedDependencyFile(parsedPnpmWorkspaceCache, file, parsePnpmWorkspaceContent); } function parsePnpmWorkspaceContent(file) { try { const parsedPnpm = yaml_1.default.parse(file.fileContent); if (parsedPnpm && ('catalog' in parsedPnpm || 'catalogs' in parsedPnpm || 'packages' in parsedPnpm)) { return parsedPnpm; } return undefined; } catch { return undefined; } } function parseDenoManifest(file) { return getOrSetParsedDependencyFile(parsedDenoManifestCache, file, parseDenoManifestContent); } function parseDenoManifestContent(file) { try { // ts.parseConfigFileTextToJson handles JSON with comments and trailing commas const parsed = typescript_1.default.parseConfigFileTextToJson(file.filePath, (0, files_js_1.stripBOM)(file.fileContent)); if (parsed.error) { return; } return parsed.config; } catch { return; } }