123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- "use strict";
- const fs = require("node:fs");
- const fileEntryCache = require("file-entry-cache");
- const stringify = require("json-stable-stringify-without-jsonify");
- const pkg = require("../../package.json");
- const assert = require("../shared/assert");
- const hash = require("./hash");
- const debug = require("debug")("eslint:lint-result-cache");
- const configHashCache = new WeakMap();
- const nodeVersion = process && process.version;
- const validCacheStrategies = ["metadata", "content"];
- const invalidCacheStrategyErrorMessage = `Cache strategy must be one of: ${validCacheStrategies
- .map(strategy => `"${strategy}"`)
- .join(", ")}`;
- function isValidCacheStrategy(cacheStrategy) {
- return (
- validCacheStrategies.includes(cacheStrategy)
- );
- }
- function hashOfConfigFor(config) {
- if (!configHashCache.has(config)) {
- configHashCache.set(config, hash(`${pkg.version}_${nodeVersion}_${stringify(config)}`));
- }
- return configHashCache.get(config);
- }
- class LintResultCache {
-
- constructor(cacheFileLocation, cacheStrategy) {
- assert(cacheFileLocation, "Cache file location is required");
- assert(cacheStrategy, "Cache strategy is required");
- assert(
- isValidCacheStrategy(cacheStrategy),
- invalidCacheStrategyErrorMessage
- );
- debug(`Caching results to ${cacheFileLocation}`);
- const useChecksum = cacheStrategy === "content";
- debug(
- `Using "${cacheStrategy}" strategy to detect changes`
- );
- this.fileEntryCache = fileEntryCache.create(
- cacheFileLocation,
- void 0,
- useChecksum
- );
- this.cacheFileLocation = cacheFileLocation;
- }
-
- getCachedLintResults(filePath, config) {
-
- const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath);
- const hashOfConfig = hashOfConfigFor(config);
- const changed =
- fileDescriptor.changed ||
- fileDescriptor.meta.hashOfConfig !== hashOfConfig;
- if (fileDescriptor.notFound) {
- debug(`File not found on the file system: ${filePath}`);
- return null;
- }
- if (changed) {
- debug(`Cache entry not found or no longer valid: ${filePath}`);
- return null;
- }
- const cachedResults = fileDescriptor.meta.results;
-
- if (!cachedResults) {
- return cachedResults;
- }
-
- const results = { ...cachedResults };
-
- if (results.source === null) {
- debug(`Rereading cached result source from filesystem: ${filePath}`);
- results.source = fs.readFileSync(filePath, "utf-8");
- }
- return results;
- }
-
- setCachedLintResults(filePath, config, result) {
- if (result && Object.hasOwn(result, "output")) {
- return;
- }
- const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath);
- if (fileDescriptor && !fileDescriptor.notFound) {
- debug(`Updating cached result: ${filePath}`);
-
- const resultToSerialize = Object.assign({}, result);
-
- if (Object.hasOwn(resultToSerialize, "source")) {
- resultToSerialize.source = null;
- }
- fileDescriptor.meta.results = resultToSerialize;
- fileDescriptor.meta.hashOfConfig = hashOfConfigFor(config);
- }
- }
-
- reconcile() {
- debug(`Persisting cached results: ${this.cacheFileLocation}`);
- this.fileEntryCache.reconcile();
- }
- }
- module.exports = LintResultCache;
|