diff --git a/changelog_unreleased/api/17167.md b/changelog_unreleased/api/17167.md new file mode 100644 index 000000000..4c0e9f8f6 --- /dev/null +++ b/changelog_unreleased/api/17167.md @@ -0,0 +1,15 @@ +#### Accept `URL` as custom config file in `resolveConfig` (#17167 by @fisker) + +[`prettier.resolveConfig()`](https://prettier.io/docs/api#prettierresolveconfigfileurlorpath--options) now accepts an [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) with `file:` protocol or a url string starts with `file:` as custom config file location. + +```js +// `URL` +await prettier.resolveConfig("path/to/file", { + config: new URL("/path/to/prettier-config-file", import.meta.url), +}); + +// URL string +await prettier.resolveConfig("path/to/file", { + config: "file:///path/to/prettier-config-file", +}); +``` diff --git a/src/config/resolve-config.js b/src/config/resolve-config.js index 801783d21..d817c6f82 100644 --- a/src/config/resolve-config.js +++ b/src/config/resolve-config.js @@ -39,6 +39,7 @@ async function loadPrettierConfig(file, options) { return; } + configFile = toPath(configFile); const config = await loadPrettierConfigFile(configFile, { shouldCache }); return { config, configFile }; diff --git a/src/index.d.ts b/src/index.d.ts index bb52309cc..7a7841ec8 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -607,7 +607,7 @@ export interface ResolveConfigOptions { /** * Pass directly the path of the config file if you don't wish to search for it. */ - config?: string | undefined; + config?: string | URL | undefined; /** * If set to `true` and an `.editorconfig` file is in your project, * Prettier will parse it and convert its properties to the corresponding prettier configuration. diff --git a/tests/integration/__tests__/config-resolution.js b/tests/integration/__tests__/config-resolution.js index 63f13d9d5..b2f733dd7 100644 --- a/tests/integration/__tests__/config-resolution.js +++ b/tests/integration/__tests__/config-resolution.js @@ -459,3 +459,21 @@ test("package.json/package.yaml", async () => { } `); }); + +test("'config' option should accept `URL` and `string`", async () => { + const configFileUrl = new URL( + "../cli/config/custom-config-file-location/my-prettier-config.mjs", + import.meta.url, + ); + const file = new URL("./file.js", configFileUrl); + + for (const configFile of [ + configFileUrl, + configFileUrl.href, + url.fileURLToPath(configFileUrl), + ]) { + await expect( + prettier.resolveConfig(file, { config: configFile }), + ).resolves.toEqual({ tabWidth: 7 }); + } +}); diff --git a/tests/integration/cli/config/custom-config-file-location/my-prettier-config.mjs b/tests/integration/cli/config/custom-config-file-location/my-prettier-config.mjs new file mode 100644 index 000000000..56a0c7c1c --- /dev/null +++ b/tests/integration/cli/config/custom-config-file-location/my-prettier-config.mjs @@ -0,0 +1,3 @@ +export default { + tabWidth: 7 +}