diff --git a/src/main/parser.js b/src/main/parser.js index d46ff8beb..412b5cf1f 100644 --- a/src/main/parser.js +++ b/src/main/parser.js @@ -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]; diff --git a/tests/integration/__tests__/plugin-override-buitin-plugins.js b/tests/integration/__tests__/plugin-override-buitin-plugins.js new file mode 100644 index 000000000..9bc08ff03 --- /dev/null +++ b/tests/integration/__tests__/plugin-override-buitin-plugins.js @@ -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"); +});