Accept URL in resolveConfigFile (#15332)

Co-authored-by: SUZUKI Sosuke <aosukeke@gmail.com>
This commit is contained in:
fisker CheungandSUZUKI Sosuke authored and GitHub committed 2023-09-04 19:59:42 +08:00
1 parent 37139afeaa
commit 56752e00e5
7 files changed
+44 -11

No files matched your search

+11
View File
@@ -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));
```
+2 -2
View File
@@ -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);
+1
View File
@@ -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"
+6 -3
View File
@@ -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) {
+3 -1
View File
@@ -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<string | null>;
export function resolveConfigFile(
fileUrlOrPath?: string | URL,
): Promise<string | null>;
/**
* As you repeatedly call `resolveConfig`, the file system structure will be cached for performance. This function will clear the cache.
@@ -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);
});
+1
View File
@@ -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