From 0946b636a66524d1d16f7996fd01bde2fee9c2d6 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 7 May 2022 09:57:40 +0800 Subject: [PATCH] Simplify `loadEditorConfig()` (#12791) --- src/config/resolve-config-editorconfig.js | 29 ++++++++++++++++------- src/config/resolve-config.js | 2 -- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/config/resolve-config-editorconfig.js b/src/config/resolve-config-editorconfig.js index 1f6a8a595..589556619 100644 --- a/src/config/resolve-config-editorconfig.js +++ b/src/config/resolve-config-editorconfig.js @@ -7,26 +7,37 @@ import findProjectRoot from "./find-project-root.js"; const jsonStringifyMem = (fn) => mem(fn, { cacheKey: JSON.stringify }); -const maybeParse = (filePath, parse) => - filePath && - parse(filePath, { +const memoizedLoadEditorConfig = jsonStringifyMem(loadEditorConfig); + +async function loadEditorConfig(filePath) { + if (!filePath) { + return; + } + + const editorConfig = await editorconfig.parse(filePath, { root: findProjectRoot(path.dirname(path.resolve(filePath))), }); -const editorconfigAsyncNoCache = async (filePath) => - editorConfigToPrettier(await maybeParse(filePath, editorconfig.parse)); -const editorconfigAsyncWithCache = jsonStringifyMem(editorconfigAsyncNoCache); + const config = editorConfigToPrettier(editorConfig); + + if (config) { + // We are not using this option + delete config.insertFinalNewline; + } + + return config; +} function getLoadFunction(opts) { if (!opts.editorconfig) { - return () => null; + return () => {}; } - return opts.cache ? editorconfigAsyncWithCache : editorconfigAsyncNoCache; + return opts.cache ? memoizedLoadEditorConfig : loadEditorConfig; } function clearCache() { - memClear(editorconfigAsyncWithCache); + memClear(memoizedLoadEditorConfig); } export { getLoadFunction, clearCache }; diff --git a/src/config/resolve-config.js b/src/config/resolve-config.js index 93c74d5ef..3ea11f435 100644 --- a/src/config/resolve-config.js +++ b/src/config/resolve-config.js @@ -112,8 +112,6 @@ async function resolveConfig(filePath, opts) { return null; } - // We are not using this option - delete merged.insertFinalNewline; return merged; }