Package Exports
- @putout/printer
Readme
Printer 
Prints Babel AST to readable JavaScript.
- βοΈ Similar to Recast, but twice faster, also 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 works directly with Babel AST.
- βοΈ Easily extandable with help of Overrides.
Supports:
- β ES2023;
- β JSX;
- β TypeScript;
Install
npm i @putout/printerπ Support of Printer
Printer has first class support from πPutout with help of @putout/plugin-printer. So install:
npm i @putout/plugin-printer -aDAnd update .putout.json:
{
"printer": "putout",
"plugins": [
"printer"
]
}To benefit from it.
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 Pathprint, 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 {/* [hello world] */= 5} = b;\n';print
Used in previous example print can be used for a couple purposes:
- to print
string; - to print
nodewhenobjectpassed; - to print
nodewhenstringstarted with__;
print(ast, {
visitors: {
AssignmentPattern(path, {print, maybe}) {
maybe.print.newline(path.parentPath.isCallExpression());
print('/* [hello world] */= ');
print('__right');
},
},
});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');
},
},
});write
When are you going to output string you can use low-level function write:
print(ast, {
visitors: {
BlockStatement(path, {write}) {
write('hello');
},
},
});indent
When you need to add indentation use indent, for example when you output body,
you need to increment indentation, and then decrement it back:
print(ast, {
visitors: {
BlockStatement(path, {write, indent}) {
write('{');
indent.inc();
indent();
write('some;');
indent.dec();
write('{');
},
},
});traverse
When are you needing to traverse node, you can use traverse:
print(ast, {
visitors: {
AssignmentExpression(path, {traverse}) {
traverse(path.get('left'));
},
},
});This is the same as print('__left') but more low-level, and supports only objects.
Speed Comparison
About speed, for file speed.js:
const putout = require('putout');
const {readFileSync} = require('fs');
const parser = require('@babel/parser');
const code = readFileSync('./lib/tokenize/tokenize.js', 'utf8');
const ast = parser.parse(code);
speed('recast');
speed('putout');
function speed(printer) {
console.time(printer);
for (let i = 0; i < 1000; i++) {
putout(code, {
printer,
plugins: [
'remove-unused-variables',
],
});
}
console.timeEnd(printer);
}With contents of tokenize.js, we have:

License
MIT