Improve loadEditorconfig (#15821)
This commit is contained in:
1 parent
21895286a7
commit
bc0be70cf9
12 files changed
+77
-75
No files matched your search
@@ -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",
|
||||
|
||||
File renamed without changes.
@@ -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 };
|
||||
File renamed without changes.
@@ -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<ReturnType<loadConfigWithoutCache>>}
|
||||
* @returns {Promise<ReturnType<loadConfig>>}
|
||||
*/
|
||||
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<string>}
|
||||
*/
|
||||
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 };
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
@@ -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(
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user