Fix plugin loading in prettier.getFileInfo() (#17548)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
eaa40aa266
commit
d49444352a
10 files changed
+113
-13
No files matched your search
@@ -0,0 +1,29 @@
|
||||
#### Fix plugin loading in `prettier.getFileInfo()` (#17548 by @fisker)
|
||||
|
||||
In previous version, `prettier.getFileInfo()` only read `parser` config from `.prettierrc`, but doesn't load plugin to infer parser from plugin `languages`, Prettier main fixed it.
|
||||
|
||||
```js
|
||||
// prettier-plugin-foo
|
||||
export const languages = [
|
||||
{
|
||||
parsers: ["foo"],
|
||||
extensions: [".foo"],
|
||||
},
|
||||
];
|
||||
```
|
||||
|
||||
```yaml
|
||||
# .prettierrc
|
||||
plugins:
|
||||
- prettier-plugin-foo
|
||||
```
|
||||
|
||||
```console
|
||||
prettier --file-info file.foo
|
||||
|
||||
# Prettier stable
|
||||
{ "ignored": false, "inferredParser": null }
|
||||
|
||||
# Prettier main
|
||||
{ "ignored": false, "inferredParser": "foo" }
|
||||
```
|
||||
@@ -3,6 +3,7 @@ import stringify from "fast-json-stable-stringify";
|
||||
import { format, getFileInfo } from "../index.js";
|
||||
import { printToScreen } from "./utils.js";
|
||||
|
||||
// Note: This does not work with `--config-precedence`
|
||||
async function logFileInfoOrDie(context) {
|
||||
const {
|
||||
fileInfo: file,
|
||||
@@ -15,7 +16,7 @@ async function logFileInfoOrDie(context) {
|
||||
const fileInfo = await getFileInfo(path.resolve(file), {
|
||||
ignorePath,
|
||||
withNodeModules,
|
||||
plugins,
|
||||
plugins: plugins.length > 0 ? plugins : undefined,
|
||||
resolveConfig: config !== false,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { resolveConfig } from "../config/resolve-config.js";
|
||||
import { loadBuiltinPlugins, loadPlugins } from "../main/plugins/index.js";
|
||||
import { isIgnored } from "../utils/ignore.js";
|
||||
import inferParser from "../utils/infer-parser.js";
|
||||
|
||||
@@ -13,10 +14,9 @@ import inferParser from "../utils/infer-parser.js";
|
||||
* @returns {Promise<FileInfoResult>}
|
||||
*
|
||||
* Please note that prettier.getFileInfo() expects options.plugins to be an array of paths,
|
||||
* not an object. A transformation from this array to an object is automatically done
|
||||
* internally by the method wrapper. See withPlugins() in index.js.
|
||||
* not an object.
|
||||
*/
|
||||
async function getFileInfo(file, options) {
|
||||
async function getFileInfo(file, options = {}) {
|
||||
if (typeof file !== "string" && !(file instanceof URL)) {
|
||||
throw new TypeError(
|
||||
`expect \`file\` to be a string or URL, got \`${typeof file}\``,
|
||||
@@ -30,10 +30,9 @@ async function getFileInfo(file, options) {
|
||||
}
|
||||
|
||||
const ignored = await isIgnored(file, { ignorePath, withNodeModules });
|
||||
|
||||
let inferredParser;
|
||||
if (!ignored) {
|
||||
inferredParser = await getParser(file, options);
|
||||
inferredParser = options.parser ?? (await getParser(file, options));
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -51,7 +50,16 @@ async function getParser(file, options) {
|
||||
});
|
||||
}
|
||||
|
||||
return config?.parser ?? inferParser(options, { physicalFile: file });
|
||||
if (config?.parser) {
|
||||
return config.parser;
|
||||
}
|
||||
|
||||
let plugins = options.plugins ?? config?.plugins ?? [];
|
||||
plugins = (
|
||||
await Promise.all([loadBuiltinPlugins(), loadPlugins(plugins)])
|
||||
).flat();
|
||||
|
||||
return inferParser({ plugins }, { physicalFile: file });
|
||||
}
|
||||
|
||||
export default getFileInfo;
|
||||
+1
-5
@@ -11,7 +11,6 @@ import leven from "leven";
|
||||
import picocolors from "picocolors";
|
||||
import * as vnopts from "vnopts";
|
||||
import * as errors from "./common/errors.js";
|
||||
import getFileInfoWithoutPlugins from "./common/get-file-info.js";
|
||||
import { mockable } from "./common/mockable.js";
|
||||
import {
|
||||
clearCache as clearConfigCache,
|
||||
@@ -84,9 +83,6 @@ async function clearCache() {
|
||||
clearPluginCache();
|
||||
}
|
||||
|
||||
/** @type {typeof getFileInfoWithoutPlugins} */
|
||||
const getFileInfo = withPlugins(getFileInfoWithoutPlugins);
|
||||
|
||||
/** @type {typeof getSupportInfoWithoutPlugins} */
|
||||
const getSupportInfo = withPlugins(getSupportInfoWithoutPlugins, 0);
|
||||
|
||||
@@ -136,11 +132,11 @@ export {
|
||||
clearCache as clearConfigCache,
|
||||
format,
|
||||
formatWithCursor,
|
||||
getFileInfo,
|
||||
getSupportInfo,
|
||||
resolveConfig,
|
||||
resolveConfigFile,
|
||||
};
|
||||
export { default as getFileInfo } from "./common/get-file-info.js";
|
||||
export * as doc from "./document/public.js";
|
||||
export { default as version } from "./main/version.evaluate.js";
|
||||
export * as util from "./utils/public.js";
|
||||
@@ -1,4 +1,4 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
|
||||
|
||||
exports[`extracts file-info for a file in not_node_modules 1`] = `
|
||||
{
|
||||
|
||||
@@ -397,6 +397,58 @@ test("API getFileInfo accepts path or URL", async () => {
|
||||
expect(resultByRelativePath).toEqual(expectedResult);
|
||||
});
|
||||
|
||||
test("getFileInfo should support `parser` and `plugins` in options", async () => {
|
||||
{
|
||||
const { inferredParser: parser } = await prettier.getFileInfo(
|
||||
new URL("../cli/file-info/empty-config/foo.js", import.meta.url),
|
||||
{
|
||||
parser: "parser-from-options",
|
||||
},
|
||||
);
|
||||
expect(parser).toBe("parser-from-options");
|
||||
}
|
||||
|
||||
{
|
||||
const { inferredParser: parser } = await prettier.getFileInfo(
|
||||
new URL("../cli/file-info/empty-config/foo.js", import.meta.url),
|
||||
);
|
||||
expect(parser).toBe("babel");
|
||||
}
|
||||
|
||||
{
|
||||
const { inferredParser: parser } = await prettier.getFileInfo(
|
||||
new URL("../cli/file-info/config-with-parser/foo.js", import.meta.url),
|
||||
);
|
||||
expect(parser).toBe("parser-from-prettierrc");
|
||||
}
|
||||
|
||||
{
|
||||
const { inferredParser: parser } = await prettier.getFileInfo(
|
||||
new URL("../cli/file-info/config-with-plugin/file.foo", import.meta.url),
|
||||
);
|
||||
expect(parser).toBe("parser-for-foo-file-from-plugin");
|
||||
}
|
||||
|
||||
{
|
||||
const { stdout } = await runCli("cli/file-info/config-with-plugin", [
|
||||
"--file-info",
|
||||
"file.foo",
|
||||
]);
|
||||
const { inferredParser: parser } = JSON.parse(stdout);
|
||||
expect(parser).toBe("parser-for-foo-file-from-plugin");
|
||||
}
|
||||
|
||||
{
|
||||
const { inferredParser: parser } = await prettier.getFileInfo(
|
||||
new URL("../cli/file-info/empty-config/file.bar", import.meta.url),
|
||||
{
|
||||
plugins: [new URL("../cli/file-info/plugin.js", import.meta.url)],
|
||||
},
|
||||
);
|
||||
expect(parser).toBe("parser-for-bar-file-from-plugin");
|
||||
}
|
||||
});
|
||||
|
||||
test("API getFileInfo accepts path or URL as ignorePath", async () => {
|
||||
const file = new URL(
|
||||
"../cli/ignore-path/file-info-test/ignored-by-customignore.js",
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
parser: parser-from-prettierrc
|
||||
@@ -0,0 +1,2 @@
|
||||
plugins:
|
||||
- ../plugin.js
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
@@ -0,0 +1,10 @@
|
||||
export const languages = [
|
||||
{
|
||||
parsers: ['parser-for-foo-file-from-plugin'],
|
||||
extensions: ['.foo']
|
||||
},
|
||||
{
|
||||
parsers: ['parser-for-bar-file-from-plugin'],
|
||||
extensions: ['.bar']
|
||||
}
|
||||
]
|
||||
Reference in new issue
Block a user