Simplify loadEditorConfig() (#12791)
This commit is contained in:
1 parent
ff98159873
commit
0946b636a6
2 files changed
+20
-11
No files matched your search
@@ -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 };
|
||||
@@ -112,8 +112,6 @@ async function resolveConfig(filePath, opts) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// We are not using this option
|
||||
delete merged.insertFinalNewline;
|
||||
return merged;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user