Files
test-repo/changelog_unreleased/api/13268.md
T

1.6 KiB

[BREAKING] The second argument parsers passed to parsers.parse has been removed (#13268 by @fisker)

The plugin's print function signature changed from

function parse(text: string, parsers: object, options: object): AST;

to

function parse(text: string, options: object): Promise<AST> | AST;

The second argument parsers has been removed, if you still need other parser during parse process, you can:

  1. Import the parser yourself

    import parserBabel from "prettier/parser-babel.js";
    
    const myCustomPlugin = {
      parsers: {
        "my-custom-parser": {
          async parse(text) {
            const ast = await parserBabel.parsers.babel.parse(text);
            ast.program.body[0].expression.callee.name = "_";
            return ast;
          },
          astFormat: "estree",
        },
      },
    };
    
  2. Get the parser from the options argument

    function getParserFromOptions(options, parserName) {
      const parserOrParserInitFunction = options.plugins.find(({ parsers }) =>
        Object.prototype.hasOwnProperty.call(parsers, parserName)
      )?.parsers[parserName];
      return typeof parserOrParserInitFunction === "function"
        ? parserOrParserInitFunction()
        : parserOrParserInitFunction;
    }
    
    const myCustomPlugin = {
      parsers: {
        "my-custom-parser": {
          async parse(text, options) {
            const babelParser = await getParserFromOptions(options, "babel");
            const ast = await babelParser.parse(text);
            ast.program.body[0].expression.callee.name = "_";
            return ast;
          },
          astFormat: "estree",
        },
      },
    };