UNPKG

eslint-plugin-tsdoc

Version:

An ESLint plugin that validates TypeScript doc comments

121 lines 5.71 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigCache = void 0; const path = __importStar(require("node:path")); const tsdoc_config_1 = require("@microsoft/tsdoc-config"); const Debug_1 = require("./Debug"); // How often to check for modified input files. If a file's modification timestamp has changed, then we will // evict the cache entry immediately. const CACHE_CHECK_INTERVAL_MS = 3 * 1000; // Evict old entries from the cache after this much time, regardless of whether the file was detected as being // modified or not. const CACHE_EXPIRE_MS = 20 * 1000; // If this many objects accumulate in the cache, then it is cleared to avoid a memory leak. const CACHE_MAX_SIZE = 100; class ConfigCache { /** * Node.js equivalent of performance.now(). */ static _getTimeInMs() { const [seconds, nanoseconds] = process.hrtime(); return seconds * 1000 + nanoseconds / 1000000; } static getForSourceFile(sourceFilePath, tsConfigRootDir) { const sourceFileFolder = path.dirname(path.resolve(sourceFilePath)); // First, determine the file to be loaded. If not found, the configFilePath will be an empty string. // If the eslint config has specified where the tsconfig file is, use that path directly without probing the filesystem. const configFilePath = tsConfigRootDir ? path.join(tsConfigRootDir, tsdoc_config_1.TSDocConfigFile.FILENAME) : tsdoc_config_1.TSDocConfigFile.findConfigPathForFolder(sourceFileFolder); // If configFilePath is an empty string, then we'll use the folder of sourceFilePath as our cache key // (instead of an empty string) const cacheKey = configFilePath || sourceFileFolder + '/'; Debug_1.Debug.log(`Cache key: "${cacheKey}"`); const nowMs = ConfigCache._getTimeInMs(); let cachedConfig = undefined; // Do we have a cached object? cachedConfig = ConfigCache._cachedConfigs.get(cacheKey); if (cachedConfig) { Debug_1.Debug.log('Cache hit'); // Is the cached object still valid? const loadAgeMs = nowMs - cachedConfig.loadTimeMs; const lastCheckAgeMs = nowMs - cachedConfig.lastCheckTimeMs; if (loadAgeMs > CACHE_EXPIRE_MS || loadAgeMs < 0) { Debug_1.Debug.log('Evicting because item is expired'); cachedConfig = undefined; ConfigCache._cachedConfigs.delete(cacheKey); } else if (lastCheckAgeMs > CACHE_CHECK_INTERVAL_MS || lastCheckAgeMs < 0) { Debug_1.Debug.log('Checking for modifications'); cachedConfig.lastCheckTimeMs = nowMs; if (cachedConfig.configFile.checkForModifiedFiles()) { // Invalidate the cache because it failed to load completely Debug_1.Debug.log('Evicting because item was modified'); cachedConfig = undefined; ConfigCache._cachedConfigs.delete(cacheKey); } } } // Load the object if (!cachedConfig) { if (ConfigCache._cachedConfigs.size > CACHE_MAX_SIZE) { Debug_1.Debug.log('Clearing cache'); ConfigCache._cachedConfigs.clear(); // avoid a memory leak } const configFile = tsdoc_config_1.TSDocConfigFile.loadFile(configFilePath); if (configFile.fileNotFound) { Debug_1.Debug.log(`File not found: "${configFilePath}"`); } else { Debug_1.Debug.log(`Loaded: "${configFilePath}"`); } cachedConfig = { configFile, lastCheckTimeMs: nowMs, loadTimeMs: nowMs }; ConfigCache._cachedConfigs.set(cacheKey, cachedConfig); } return cachedConfig.configFile; } } exports.ConfigCache = ConfigCache; // findConfigPathForFolder() result --> loaded tsdoc.json configuration ConfigCache._cachedConfigs = new Map(); ConfigCache._cachedPaths = new Map(); //# sourceMappingURL=ConfigCache.js.map