diff --git a/package.json b/package.json index 3604b7050..bf16a04de 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,6 @@ "leven": "4.0.0", "lines-and-columns": "2.0.4", "linguist-languages": "7.27.0", - "mem": "9.0.2", "meriyah": "4.3.9", "micromatch": "4.0.5", "minimist": "1.2.8", diff --git a/src/config/editorconfig-to-prettier.js b/src/config/editorconfig/editorconfig-to-prettier.js similarity index 100% rename from src/config/editorconfig-to-prettier.js rename to src/config/editorconfig/editorconfig-to-prettier.js diff --git a/src/config/editorconfig/index.js b/src/config/editorconfig/index.js new file mode 100644 index 000000000..4c3d25e5d --- /dev/null +++ b/src/config/editorconfig/index.js @@ -0,0 +1,51 @@ +import path from "node:path"; + +import editorconfig from "editorconfig"; + +import findProjectRootWithoutCache from "../find-project-root.js"; +import editorConfigToPrettier from "./editorconfig-to-prettier.js"; + +const projectRootCache = new Map(); +const editorconfigCache = new Map(); + +function clearEditorconfigCache() { + projectRootCache.clear(); + editorconfigCache.clear(); +} + +// TODO: Improve this cache +function findProjectRoot(directory, { shouldCache }) { + if (!shouldCache || !projectRootCache.has(directory)) { + // Even if `shouldCache` is false, we still cache the result, so we can use it when `shouldCache` is true + projectRootCache.set(directory, findProjectRootWithoutCache(directory)); + } + return projectRootCache.get(directory); +} + +async function loadEditorconfigInternal(file, { shouldCache }) { + const directory = path.dirname(file); + const root = await findProjectRoot(directory, { shouldCache }); + const editorConfig = await editorconfig.parse(file, { root }); + const config = editorConfigToPrettier(editorConfig); + return config; +} + +/** + * @param {string} file + * @param {{shouldCache?: boolean}} options + */ +function loadEditorconfig(file, { shouldCache }) { + file = path.resolve(file); + + if (!shouldCache || !editorconfigCache.has(file)) { + // Even if `shouldCache` is false, we still cache the result, so we can use it when `shouldCache` is true + editorconfigCache.set( + file, + loadEditorconfigInternal(file, { shouldCache }), + ); + } + + return editorconfigCache.get(file); +} + +export { clearEditorconfigCache, loadEditorconfig }; diff --git a/src/config/prettier-config-explorer/config-searcher.js b/src/config/prettier-config/config-searcher.js similarity index 100% rename from src/config/prettier-config-explorer/config-searcher.js rename to src/config/prettier-config/config-searcher.js diff --git a/src/config/prettier-config-explorer/index.js b/src/config/prettier-config/index.js similarity index 74% rename from src/config/prettier-config-explorer/index.js rename to src/config/prettier-config/index.js index 2e60b9057..41c0f984e 100644 --- a/src/config/prettier-config-explorer/index.js +++ b/src/config/prettier-config/index.js @@ -2,11 +2,11 @@ import path from "node:path"; import mockable from "../../common/mockable.js"; import ConfigSearcher from "./config-searcher.js"; -import loadConfigWithoutCache from "./load-config.js"; +import loadConfig from "./load-config.js"; const loadCache = new Map(); const searchCache = new Map(); -function clearCache() { +function clearPrettierConfigCache() { loadCache.clear(); searchCache.clear(); } @@ -14,15 +14,14 @@ function clearCache() { /** * @param {string} configFile * @param {{shouldCache?: boolean}} param1 - * @returns {Promise>} + * @returns {Promise>} */ -function loadConfig(configFile, { shouldCache }) { +function loadPrettierConfig(configFile, { shouldCache }) { configFile = path.resolve(configFile); + if (!shouldCache || !loadCache.has(configFile)) { - const promise = loadConfigWithoutCache(configFile); - // Even if `shouldCache` is false, we still cache it, so we can use it later - loadCache.set(configFile, promise); - return promise; + // Even if `shouldCache` is false, we still cache the result, so we can use it when `shouldCache` is true + loadCache.set(configFile, loadConfig(configFile)); } return loadCache.get(configFile); @@ -50,7 +49,7 @@ function getSearchFunction({ shouldCache, stopDirectory }) { * @param {{shouldCache?: boolean, stopDirectory?: string}} options * @returns {Promise} */ -function searchConfig(startDirectory, options = {}) { +function searchPrettierConfig(startDirectory, options = {}) { startDirectory = startDirectory ? path.resolve(startDirectory) : process.cwd(); @@ -62,4 +61,4 @@ function searchConfig(startDirectory, options = {}) { return search(startDirectory); } -export { clearCache, loadConfig, searchConfig }; +export { clearPrettierConfigCache, loadPrettierConfig, searchPrettierConfig }; diff --git a/src/config/prettier-config-explorer/load-config.js b/src/config/prettier-config/load-config.js similarity index 100% rename from src/config/prettier-config-explorer/load-config.js rename to src/config/prettier-config/load-config.js diff --git a/src/config/prettier-config-explorer/load-external-config.js b/src/config/prettier-config/load-external-config.js similarity index 100% rename from src/config/prettier-config-explorer/load-external-config.js rename to src/config/prettier-config/load-external-config.js diff --git a/src/config/prettier-config-explorer/loaders.js b/src/config/prettier-config/loaders.js similarity index 100% rename from src/config/prettier-config-explorer/loaders.js rename to src/config/prettier-config/loaders.js diff --git a/src/config/resolve-config.js b/src/config/resolve-config.js index 2bb2bbc46..410fc4c7d 100644 --- a/src/config/resolve-config.js +++ b/src/config/resolve-config.js @@ -1,41 +1,39 @@ import path from "node:path"; -import mem, { memClear } from "mem"; import micromatch from "micromatch"; import { toPath } from "url-or-path"; import partition from "../utils/partition.js"; import { - clearCache as clearPrettierConfigCache, - loadConfig as loadPrettierConfigFile, - searchConfig as searchPrettierConfig, -} from "./prettier-config-explorer/index.js"; -import loadEditorConfigWithoutCache from "./resolve-editorconfig.js"; + clearEditorconfigCache, + loadEditorconfig as loadEditorconfigForFile, +} from "./editorconfig/index.js"; +import { + clearPrettierConfigCache, + loadPrettierConfig as loadPrettierConfigFile, + searchPrettierConfig, +} from "./prettier-config/index.js"; -const memoizedLoadEditorConfig = mem(loadEditorConfigWithoutCache); function clearCache() { clearPrettierConfigCache(); - memClear(memoizedLoadEditorConfig); + clearEditorconfigCache(); } -function loadEditorConfig(filePath, options) { - if (!filePath || !options.editorconfig) { +function loadEditorconfig(file, options) { + if (!file || !options.editorconfig) { return; } - return ( - options.useCache ? memoizedLoadEditorConfig : loadEditorConfigWithoutCache - )(filePath); + const shouldCache = options.useCache; + return loadEditorconfigForFile(file, { shouldCache }); } -async function loadPrettierConfig(filePath, options) { +async function loadPrettierConfig(file, options) { const shouldCache = options.useCache; let configFile = options.config; if (!configFile) { - const directory = filePath - ? path.dirname(path.resolve(filePath)) - : undefined; + const directory = file ? path.dirname(path.resolve(file)) : undefined; configFile = await searchPrettierConfig(directory, { shouldCache }); } @@ -54,7 +52,7 @@ async function resolveConfig(fileUrlOrPath, options) { const [result, editorConfigured] = await Promise.all([ loadPrettierConfig(filePath, options), - loadEditorConfig(filePath, options), + loadEditorconfig(filePath, options), ]); if (!result && !editorConfigured) { diff --git a/src/config/resolve-editorconfig.js b/src/config/resolve-editorconfig.js deleted file mode 100644 index 82c81d928..000000000 --- a/src/config/resolve-editorconfig.js +++ /dev/null @@ -1,18 +0,0 @@ -import path from "node:path"; - -import editorconfig from "editorconfig"; - -import editorConfigToPrettier from "./editorconfig-to-prettier.js"; -import findProjectRoot from "./find-project-root.js"; - -async function loadEditorConfig(filePath) { - const editorConfig = await editorconfig.parse(filePath, { - root: await findProjectRoot(path.dirname(path.resolve(filePath))), - }); - - const config = editorConfigToPrettier(editorConfig); - - return config; -} - -export default loadEditorConfig; diff --git a/tests/unit/editorconfig-to-prettier.js b/tests/unit/editorconfig-to-prettier.js index 872535ab0..c12c88019 100644 --- a/tests/unit/editorconfig-to-prettier.js +++ b/tests/unit/editorconfig-to-prettier.js @@ -1,4 +1,4 @@ -import editorconfigToPrettier from "../../src/config/editorconfig-to-prettier.js"; +import editorconfigToPrettier from "../../src/config/editorconfig/editorconfig-to-prettier.js"; test("editorconfigToPrettier", () => { expect( diff --git a/yarn.lock b/yarn.lock index 5977c5502..bf7b43284 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6376,15 +6376,6 @@ __metadata: languageName: node linkType: hard -"map-age-cleaner@npm:^0.1.3": - version: 0.1.3 - resolution: "map-age-cleaner@npm:0.1.3" - dependencies: - p-defer: "npm:^1.0.0" - checksum: cb2804a5bcb3cbdfe4b59066ea6d19f5e7c8c196cd55795ea4c28f792b192e4c442426ae52524e5e1acbccf393d3bddacefc3d41f803e66453f6c4eda3650bc1 - languageName: node - linkType: hard - "markdown-escapes@npm:^1.0.0": version: 1.0.4 resolution: "markdown-escapes@npm:1.0.4" @@ -6392,16 +6383,6 @@ __metadata: languageName: node linkType: hard -"mem@npm:9.0.2": - version: 9.0.2 - resolution: "mem@npm:9.0.2" - dependencies: - map-age-cleaner: "npm:^0.1.3" - mimic-fn: "npm:^4.0.0" - checksum: 82f899e73212509c8bccb26399f03c61193e15b796c9e8e10332db6a3eb1cad65edbe4ab144554e88a0db3dc53af3b9e960a2576cb3521fa968ba1ca5a0b6719 - languageName: node - linkType: hard - "memorystream@npm:^0.3.1": version: 0.3.1 resolution: "memorystream@npm:0.3.1" @@ -6912,13 +6893,6 @@ __metadata: languageName: node linkType: hard -"p-defer@npm:^1.0.0": - version: 1.0.0 - resolution: "p-defer@npm:1.0.0" - checksum: 1d8fb7138a0ccebb65479160fd93f245303c06c977c976105d75838f7f504a9a6ef11b7e058f98b4c957a6a8df268c616da1ee339285d565f9e5ba00304e027b - languageName: node - linkType: hard - "p-limit@npm:^2.2.0": version: 2.3.0 resolution: "p-limit@npm:2.3.0" @@ -7362,7 +7336,6 @@ __metadata: lines-and-columns: "npm:2.0.4" linguist-languages: "npm:7.27.0" magic-string: "npm:0.30.5" - mem: "npm:9.0.2" meriyah: "npm:4.3.9" micromatch: "npm:4.0.5" minimist: "npm:1.2.8"