Skip to content

Commit

Permalink
Make resolveParser work like v2 (#13732)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 26, 2022
1 parent 930cf29 commit b722335
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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");
});

0 comments on commit b722335

Please sign in to comment.