Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make resolveParser work like v2 #13732

Merged
merged 1 commit into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/parser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ConfigError } from "../common/errors.js";

function resolveParser({ plugins, parser }) {
for (const { parsers } of plugins) {
/*
Loop from end to allow plugins override builtin plugins,
this is how `resolveParser` works in v2.
This is a temporarily solution, see #13729
*/
for (let index = plugins.length - 1; index >= 0; index--) {
const { parsers } = plugins[index];
if (parsers && Object.hasOwn(parsers, parser)) {
const parserOrParserInitFunction = parsers[parser];

Expand Down
17 changes: 17 additions & 0 deletions tests/integration/__tests__/plugin-override-buitin-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import prettier from "../../config/prettier-entry.js";
import createPlugin from "../../config/utils/create-plugin.cjs";

test("plugins can override builtin plugins", async () => {
const outputWithoutPlugin = await prettier.format("foo()", {
parser: "babel",
});
const outputWithPlugin = await prettier.format("foo()", {
parser: "babel",
plugins: [
createPlugin({ name: "babel", print: () => "fake-babel-output" }),
],
});

expect(outputWithoutPlugin).not.toBe(outputWithPlugin);
expect(outputWithPlugin).toBe("fake-babel-output\n");
});