babel-prettier-parser

raw JSON →
0.10.8 verified Sat Apr 25 auth: no javascript

A Prettier parser wrapper that enables formatting of JavaScript/TypeScript code containing experimental or non-standard syntaxes that Babel can transform, such as pipeline operators, decorators, and do expressions. Version 0.10.8 is the latest stable release with no fixed release cadence. It ships TypeScript types and requires Prettier ^2.2.1 as a peer dependency. Unlike Prettier's default Babel parser, this package allows parsing of code with Babel plugins that are not included by default in Prettier, making it suitable for projects using advanced ECMAScript proposals. However, it is a niche tool with limited community adoption and may have compatibility issues with future Prettier versions.

error Cannot find module 'babel-prettier-parser'
cause Package not installed or missed in dependencies.
fix
Run npm install babel-prettier-parser --save-dev
error Must use import to load ES Module: require() of ES modules is not supported
cause CJS require not supported for ESM-only package.
fix
Use import syntax or dynamic import().
error TypeError: parse is not a function
cause Default import is an object, not a function.
fix
Use { parse } instead of default import.
breaking Version 0.8.0: parse now returns an object with a `type` property. Old code expects an AST node.
fix Access the root node via result.ast or similar; check the updated API.
deprecated The `text` parameter in parse has been deprecated in favor of `code`.
fix Use { code: source } instead of { text: source }.
gotcha The parser does not support all Prettier options; custom Babel plugins must be passed explicitly.
fix Always pass the required Babel plugins in the options object to parse.
npm install babel-prettier-parser
yarn add babel-prettier-parser
pnpm add babel-prettier-parser

Demonstrates configuring Prettier to use babel-prettier-parser for formatting code with do expressions.

import prettier from 'prettier';
import { parse } from 'babel-prettier-parser';

const code = `const x = do { if (true) 1; else 2; };`;

// Register the parser with Prettier
prettier.format(code, {
  parser: 'babel',
  plugins: [{
    parsers: {
      babel: {
        parse: (text, parsers) => {
          const ast = parse(text, {
            plugins: ['doExpressions'],
            sourceType: 'module',
          });
          // Transform AST to Prettier-compatible AST if needed
          return ast;
        },
      },
    },
  }],
}).then(result => console.log(result));