forked from prettier/prettier
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Source: prettier#12800
- Loading branch information
Showing
14 changed files
with
1,267 additions
and
1,140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use strict"; | ||
|
||
const os = require("os"); | ||
const path = require("path"); | ||
const findCacheDir = require("find-cache-dir"); | ||
|
||
/** | ||
* Find default cache file (`./node_modules/.cache/prettier/.prettier-cache`) using https://github.com/avajs/find-cache-dir | ||
* | ||
* @returns {string} | ||
*/ | ||
function findCacheFile() { | ||
const cacheDir = | ||
findCacheDir({ name: "prettier", create: true }) || os.tmpdir(); | ||
const cacheFilePath = path.join(cacheDir, ".prettier-cache"); | ||
return cacheFilePath; | ||
} | ||
|
||
module.exports = findCacheFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"use strict"; | ||
|
||
// Inspired by LintResultsCache from ESLint | ||
// https://github.com/eslint/eslint/blob/c2d0a830754b6099a3325e6d3348c3ba983a677a/lib/cli-engine/lint-result-cache.js | ||
|
||
const fileEntryCache = require("file-entry-cache"); | ||
const stringify = require("fast-json-stable-stringify"); | ||
// eslint-disable-next-line no-restricted-modules | ||
const { version: prettierVersion } = require("../index.js"); | ||
const { createHash } = require("./utils.js"); | ||
|
||
const optionsHashCache = new WeakMap(); | ||
const nodeVersion = process && process.version; | ||
|
||
/** | ||
* @param {*} options | ||
* @returns {string} | ||
*/ | ||
function getHashOfOptions(options) { | ||
if (optionsHashCache.has(options)) { | ||
return optionsHashCache.get(options); | ||
} | ||
const hash = createHash( | ||
`${prettierVersion}_${nodeVersion}_${stringify(options)}` | ||
); | ||
optionsHashCache.set(options, hash); | ||
return hash; | ||
} | ||
|
||
/** | ||
* @typedef {{ hashOfOptions?: string }} OurMeta | ||
* @typedef {import("file-entry-cache").FileDescriptor} FileDescriptor | ||
* | ||
* @param {import("file-entry-cache").FileDescriptor} fileDescriptor | ||
* @returns {FileDescriptor["meta"] & OurMeta} | ||
*/ | ||
function getMetadataFromFileDescriptor(fileDescriptor) { | ||
return fileDescriptor.meta; | ||
} | ||
|
||
class FormatResultsCache { | ||
/** | ||
* @param {string} cacheFileLocation The path of cache file location. (default: `node_modules/.cache/prettier/prettier-cache`) | ||
* @param {string} cacheStrategy | ||
*/ | ||
constructor(cacheFileLocation, cacheStrategy) { | ||
const useChecksum = cacheStrategy === "content"; | ||
|
||
this.cacheFileLocation = cacheFileLocation; | ||
this.fileEntryCache = fileEntryCache.create( | ||
/* cacheId */ cacheFileLocation, | ||
/* directory */ undefined, | ||
useChecksum | ||
); | ||
} | ||
|
||
/** | ||
* @param {string} filePath | ||
* @param {any} options | ||
*/ | ||
existsAvailableFormatResultsCache(filePath, options) { | ||
const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath); | ||
const hashOfOptions = getHashOfOptions(options); | ||
const meta = getMetadataFromFileDescriptor(fileDescriptor); | ||
const changed = | ||
fileDescriptor.changed || meta.hashOfOptions !== hashOfOptions; | ||
|
||
if (fileDescriptor.notFound) { | ||
return false; | ||
} | ||
|
||
if (changed) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param {string} filePath | ||
* @param {any} options | ||
*/ | ||
setFormatResultsCache(filePath, options) { | ||
const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath); | ||
const meta = getMetadataFromFileDescriptor(fileDescriptor); | ||
if (fileDescriptor && !fileDescriptor.notFound) { | ||
meta.hashOfOptions = getHashOfOptions(options); | ||
} | ||
} | ||
|
||
reconcile() { | ||
this.fileEntryCache.reconcile(); | ||
} | ||
} | ||
|
||
module.exports = FormatResultsCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,35 @@ | ||
"use strict"; | ||
|
||
const { promises: fs } = require("fs"); | ||
|
||
// eslint-disable-next-line no-restricted-modules | ||
const { default: sdbm } = require("../../vendors/sdbm.js"); | ||
|
||
// eslint-disable-next-line no-console | ||
const printToScreen = console.log.bind(console); | ||
|
||
module.exports = { printToScreen }; | ||
/** | ||
* @param {string} source | ||
* @returns {string} | ||
*/ | ||
function createHash(source) { | ||
return String(sdbm(source)); | ||
} | ||
|
||
/** | ||
* Get stats of a given path. | ||
* @param {string} filePath The path to target file. | ||
* @returns {Promise<import('fs').Stats | undefined>} The stats. | ||
*/ | ||
async function statSafe(filePath) { | ||
try { | ||
return await fs.stat(filePath); | ||
} catch (error) { | ||
/* istanbul ignore next */ | ||
if (error.code !== "ENOENT") { | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
module.exports = { printToScreen, createHash, statSafe }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.