JSPM

@putout/printer

1.13.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 21732
  • Score
    100M100P100Q145983F
  • License MIT

Easiest possible opinionated Babel AST printer made with ❤️ to use in 🐊Putout

Package Exports

  • @putout/printer

Readme

Printer NPM version

Prints Babel AST to readable JavaScript.

  • ☝️ Similar to Recast, but simpler and easier in maintenance, since it supports only Babel.
  • ☝️ As opinionated as Prettier, but has more user-friendly output and works directly with AST.
  • ☝️ Like ESLint but without any configuration and plugins 🤷‍, also works directly with Babel AST only.

Install

npm i @putout/printer

API

const {print} = require('@putout/printer');
const {parse} = require('@babel/parser');
const ast = parse('const a = (b, c) => {const d = 5; return a;}');

print(ast);

// returns
// returns
`
const a = (b, c) => {
    const d = 5;
    return a;
};
`;

Overrides

When you need to extend syntax of @putout/printer just pass a function which receives:

  • path, Babel Path
  • print, a function to output result of printing into token array;

When path contains to dashes __ and name, it is the same as: print(path.get('right')), and this is actually traverse(path.get('right')) shortened to simplify read and process.

Here is how you can override AssignmentPattern:

const ast = parse('const {a = 5} = b');

print(ast, {
    format: {
        indent: '    ',
    },
    visitors: {
        AssignmentPattern(path, {print}) {
            print(' /* [hello world] */= ');
            print('__right');
        },
    },
});

// returns
// returns
'const {a /* [hello world] */= 5} = b;\n';

License

MIT