Simplify loadEditorConfig() (#12791)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-05-07 09:57:40 +08:00
1 parent ff98159873
commit 0946b636a6
2 files changed
+20 -11

No files matched your search

+20 -9
View File
@@ -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 };
-2
View File
@@ -112,8 +112,6 @@ async function resolveConfig(filePath, opts) {
return null;
}
// We are not using this option
delete merged.insertFinalNewline;
return merged;
}