Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@putout/printer

Package Overview
Dependencies
Maintainers
1
Versions
600
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@putout/printer

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

  • 1.21.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.8K
decreased by-23.13%
Maintainers
1
Weekly downloads
 
Created
Source

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
`
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
'const {a /* [hello world] */= 5} = b;\n';

Maybe

When you need some condition use maybe. For example, to add newline only when parent node is CallExpression you can use maybe.print.newline(condition):

print(ast, {
    visitors: {
        AssignmentPattern(path, {print, maybe}) {
            maybe.print.newline(path.parentPath.isCallExpression());
            print(' /* [hello world] */= ');
            print('__right');
        },
    },
});

License

MIT

Keywords

FAQs

Package last updated on 04 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc