diff --git a/changelog_unreleased/api/15332.md b/changelog_unreleased/api/15332.md new file mode 100644 index 000000000..f366d4d17 --- /dev/null +++ b/changelog_unreleased/api/15332.md @@ -0,0 +1,11 @@ +#### Accept `URL` in `prettier.resolveConfigFile()` (#15332 by @fisker) + +[`prettier.resolveConfigFile()`](https://prettier.io/docs/en/api#prettierresolveconfigfilefileurlorpath) now accepts an [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL) with `file:` protocol or a url string starts with `file://`. + +```js +await prettier.resolveConfigFile(import.meta.url); +``` + +```js +await prettier.resolveConfigFile(new URL("./path/to/file", import.meta.url)); +``` diff --git a/docs/api.md b/docs/api.md index bbcebfac1..7cf2a0f2a 100644 --- a/docs/api.md +++ b/docs/api.md @@ -57,7 +57,7 @@ If `options.editorconfig` is `true` and an [`.editorconfig` file](https://editor - `indent_size`/`tab_width` - `max_line_length` -## `prettier.resolveConfigFile([filePath])` +## `prettier.resolveConfigFile([fileUrlOrPath])` `resolveConfigFile` can be used to find the path of the Prettier configuration file that will be used when resolving the config (i.e. when calling `resolveConfig`). A promise is returned which will resolve to: @@ -66,7 +66,7 @@ If `options.editorconfig` is `true` and an [`.editorconfig` file](https://editor The promise will be rejected if there was an error parsing the configuration file. -The search starts at `process.cwd()`, or at `filePath` if provided. Please see the [lilconfig docs](https://github.com/antonk52/lilconfig) for details on how the resolving works. +The search starts at `process.cwd()`, or at `fileUrlOrPath` if provided. Please see the [lilconfig docs](https://github.com/antonk52/lilconfig) for details on how the resolving works. ```js const configFile = await prettier.resolveConfigFile(filePath); diff --git a/package.json b/package.json index d2fd223a0..d72497be4 100644 --- a/package.json +++ b/package.json @@ -103,6 +103,7 @@ "typescript": "5.2.2", "unicode-regex": "4.0.0", "unified": "9.2.2", + "url-or-path": "2.1.0", "vnopts": "2.0.0", "wcwidth": "1.0.1", "yaml-unist-parser": "2.0.1" diff --git a/src/config/resolve-config.js b/src/config/resolve-config.js index a867080bf..e21fcbf3e 100644 --- a/src/config/resolve-config.js +++ b/src/config/resolve-config.js @@ -1,6 +1,7 @@ import path from "node:path"; import micromatch from "micromatch"; import mem, { memClear } from "mem"; +import { toPath } from "url-or-path"; import partition from "../utils/partition.js"; import loadEditorConfigWithoutCache from "./resolve-editorconfig.js"; import getPrettierConfigExplorerWithoutCache from "./get-prettier-config-explorer.js"; @@ -62,10 +63,12 @@ async function resolveConfig(filePath, options) { return merged; } -async function resolveConfigFile(filePath) { +async function resolveConfigFile(fileUrlOrPath) { const { search } = getPrettierConfigExplorer({ cache: false }); - const result = await search(filePath ? path.resolve(filePath) : undefined); - return result ? result.filepath : null; + const result = await search( + fileUrlOrPath ? path.resolve(toPath(fileUrlOrPath)) : undefined, + ); + return result?.filepath ?? null; } function mergeOverrides(configResult, filePath) { diff --git a/src/index.d.ts b/src/index.d.ts index 7b31e2e43..cdc0fd599 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -636,7 +636,9 @@ export function resolveConfig( * * The promise will be rejected if there was an error parsing the configuration file. */ -export function resolveConfigFile(filePath?: string): Promise; +export function resolveConfigFile( + fileUrlOrPath?: string | URL, +): Promise; /** * As you repeatedly call `resolveConfig`, the file system structure will be cached for performance. This function will clear the cache. diff --git a/tests/integration/__tests__/resolve-config-file.js b/tests/integration/__tests__/resolve-config-file.js index 075f70ee8..bf1537315 100644 --- a/tests/integration/__tests__/resolve-config-file.js +++ b/tests/integration/__tests__/resolve-config-file.js @@ -1,10 +1,25 @@ -import path from "node:path"; -import createEsmUtils from "esm-utils"; +import * as urlOrPath from "url-or-path"; import prettier from "../../config/prettier-entry.js"; -const { __dirname } = createEsmUtils(import.meta); - test("API resolveConfigFile", async () => { const result = await prettier.resolveConfigFile(); - expect(result).toEqual(path.join(__dirname, "../../../.prettierrc")); + expect(result).toEqual( + urlOrPath.toPath(new URL("../../../.prettierrc", import.meta.url)), + ); +}); + +test("API resolveConfigFile accepts path or URL", async () => { + const fileUrl = new URL("../cli/config/filepath/foo.js", import.meta.url); + const expectedConfigFilePath = urlOrPath.toPath( + new URL("./.prettierrc", fileUrl), + ); + + const resultByUrl = await prettier.resolveConfigFile(fileUrl); + const resultByUrlHref = await prettier.resolveConfigFile(fileUrl.href); + const resultByPath = await prettier.resolveConfigFile( + urlOrPath.toPath(fileUrl), + ); + expect(resultByUrl).toEqual(expectedConfigFilePath); + expect(resultByUrlHref).toEqual(expectedConfigFilePath); + expect(resultByPath).toEqual(expectedConfigFilePath); }); diff --git a/yarn.lock b/yarn.lock index 566e3c96b..0f7395277 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7258,6 +7258,7 @@ __metadata: typescript: 5.2.2 unicode-regex: 4.0.0 unified: 9.2.2 + url-or-path: 2.1.0 vnopts: 2.0.0 wcwidth: 1.0.1 yaml-unist-parser: 2.0.1