|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +// Inspired by LintResultsCache from ESLint |
| 4 | +// https://github.com/eslint/eslint/blob/c2d0a830754b6099a3325e6d3348c3ba983a677a/lib/cli-engine/lint-result-cache.js |
| 5 | + |
| 6 | +const fileEntryCache = require("file-entry-cache"); |
| 7 | +const stringify = require("fast-json-stable-stringify"); |
| 8 | +// eslint-disable-next-line no-restricted-modules |
| 9 | +const { version: prettierVersion } = require("../index.js"); |
| 10 | +const { createHash } = require("./utils.js"); |
| 11 | + |
| 12 | +const optionsHashCache = new WeakMap(); |
| 13 | +const nodeVersion = process && process.version; |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {*} options |
| 17 | + * @returns {string} |
| 18 | + */ |
| 19 | +function getHashOfOptions(options) { |
| 20 | + if (optionsHashCache.has(options)) { |
| 21 | + return optionsHashCache.get(options); |
| 22 | + } |
| 23 | + const hash = createHash( |
| 24 | + `${prettierVersion}_${nodeVersion}_${stringify(options)}` |
| 25 | + ); |
| 26 | + optionsHashCache.set(options, hash); |
| 27 | + return hash; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * @typedef {{ hashOfOptions?: string }} OurMeta |
| 32 | + * @typedef {import("file-entry-cache").FileDescriptor} FileDescriptor |
| 33 | + * |
| 34 | + * @param {import("file-entry-cache").FileDescriptor} fileDescriptor |
| 35 | + * @returns {FileDescriptor["meta"] & OurMeta} |
| 36 | + */ |
| 37 | +function getMetadataFromFileDescriptor(fileDescriptor) { |
| 38 | + return fileDescriptor.meta; |
| 39 | +} |
| 40 | + |
| 41 | +class FormatResultsCache { |
| 42 | + /** |
| 43 | + * @param {string} cacheFileLocation The path of cache file location. (default: `node_modules/.cache/prettier/prettier-cache`) |
| 44 | + * @param {string} cacheStrategy |
| 45 | + */ |
| 46 | + constructor(cacheFileLocation, cacheStrategy) { |
| 47 | + const useChecksum = cacheStrategy === "content"; |
| 48 | + |
| 49 | + this.cacheFileLocation = cacheFileLocation; |
| 50 | + this.fileEntryCache = fileEntryCache.create( |
| 51 | + /* cacheId */ cacheFileLocation, |
| 52 | + /* directory */ undefined, |
| 53 | + useChecksum |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param {string} filePath |
| 59 | + * @param {any} options |
| 60 | + */ |
| 61 | + existsAvailableFormatResultsCache(filePath, options) { |
| 62 | + const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath); |
| 63 | + const hashOfOptions = getHashOfOptions(options); |
| 64 | + const meta = getMetadataFromFileDescriptor(fileDescriptor); |
| 65 | + const changed = |
| 66 | + fileDescriptor.changed || meta.hashOfOptions !== hashOfOptions; |
| 67 | + |
| 68 | + if (fileDescriptor.notFound) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + if (changed) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param {string} filePath |
| 81 | + * @param {any} options |
| 82 | + */ |
| 83 | + setFormatResultsCache(filePath, options) { |
| 84 | + const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath); |
| 85 | + const meta = getMetadataFromFileDescriptor(fileDescriptor); |
| 86 | + if (fileDescriptor && !fileDescriptor.notFound) { |
| 87 | + meta.hashOfOptions = getHashOfOptions(options); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + reconcile() { |
| 92 | + this.fileEntryCache.reconcile(); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +module.exports = FormatResultsCache; |
0 commit comments