10.2.0 β€’ Published 2 months ago

@putout/printer v10.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Printer License NPM version Build Status Coverage Status

Prints Babel AST to readable JavaScript. Use 🐊Putout to parse your code.

You may also use Babel 8 with estree-to-babel for ESTree and Babel AST to put .extra.raw to .raw (which is simpler for transforms, no need to use Optional Chaining and add extra values every time).

  • ☝️ 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 extendable with help of Overrides.

Supports:

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 -aD

And update .putout.json:

{
    "printer": "putout",
    "plugins": ["printer"]
}

To benefit from it.

Example

const {print} = require('@putout/printer');
const {parse} = require('putout');
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: write(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: '    ',
        newline: '\n',
        space: ' ',
        splitter: '\n',
        quote: `'`,
        endOfFile: '\n',
    },
    semantics: {
        comments: true,
        maxSpecifiersInOneLine: 2,
        maxElementsInOneLine: 3,
        maxVariablesInOneLine: 4,
        maxPropertiesInOneLine: 2,
        maxPropertiesLengthInOneLine: 15,
        trailingComma: true,
        encodeSingleQuote: true,
        encodeDoubleQuote: false,
        roundBraces: {
            arrow: true,
            sequence: true,
            assign: false,
        },
    },
    visitors: {
        AssignmentPattern(path, {print}) {
            print('/* [hello world] */= ');
            print('__right');
        },
    },
});

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

format

Options related to visuals and not related to logic of output can be changed with help of format, you can override next options:

const overrides = {
    format: {
        indent: '    ',
        newline: '\n',
        space: ' ',
        splitter: '\n',
        endOfFile: '\n',
    },
};
  • indent - use two spaces, tabs, or anything you want;
  • newline - symbol used for line separation;
  • space - default symbol used for space character;
  • splitter - mandatory symbol that used inside of statements like this:

Default options produce:

if (a > 3)
    console.log('ok');
else
    console.log('not ok');

But you can override them with:

const overrides = {
    format: {
        indent: '',
        newline: '',
        space: '',
        splitter: ' ',
    },
};

And have minified code:

if(a>3)console.log('ok');else console.log('not ok');

Semantics

Options used to configure logic of output, similar to ESLint rules:

  • βœ… maxElementsInOneLine - count of ArrayExpression and ArrayPattern elements placed in one line.
  • βœ… maxVariablesInOneLine - count of VariableDeclarators in one line.
  • βœ… maxPropertiesInOneLine - count of ObjectProperties in one line.
  • βœ… maxPropertiesLengthInOneLine - maximum length of Object Property, when violated splits event if maxPropertiesInOneLine satisfies;
  • βœ… roundBraces to output braces or not
    • arrow: In a single argument arrow function expressions enabled: (a) => {}, disabled: a => {};
    • sequence: In sequence expressions: enabled: for(let e of l) (a(), b()), disabled: for(let e of l) a(), b();
    • assign: In assignment expressions: enabled: (e.o=w(e.o), disabled: e.o=w(e.o);

Visitors API

When you want to improve support of existing visitor or extend Printer with a new ones, you need next base operations:

override

When you need to override behavior of existing visitor use:

import {
    print,
    visitors as v,
} from '@putout/printer';

print(ast, {
    visitors: {
        CallExpression(path, printer, semantics) {
            const {print} = printer;
            
            if (!path.node.goldstein)
                return v.CallExpression(path, printer, semantics);
            
            print('__goldstein');
        },
    },
});

print

Used in previous example print can be used for a couple purposes:

  • to write string;
  • to write node when object passed;
  • to write node when string started with __;
print(ast, {
    visitors: {
        AssignmentPattern(path, {print, maybe}) {
            maybe.write.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.write.newline(condition):

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

write

When 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 you need to traverse node path, 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 {readFileSync} = require('node:fs');

const putout = require('putout');
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:

image

License

MIT

10.0.0

2 months ago

10.0.1

2 months ago

10.2.0

2 months ago

10.1.0

2 months ago

10.1.1

2 months ago

9.19.0

2 months ago

9.18.0

3 months ago

9.17.0

3 months ago

9.16.0

3 months ago

9.15.1

3 months ago

9.15.2

3 months ago

9.15.0

4 months ago

9.14.0

4 months ago

9.13.0

4 months ago

9.8.1

5 months ago

9.8.0

5 months ago

9.12.0

4 months ago

9.9.0

5 months ago

9.11.0

5 months ago

9.10.0

5 months ago

8.40.0

7 months ago

9.4.0

6 months ago

8.41.0

7 months ago

9.5.0

6 months ago

8.42.0

7 months ago

9.6.0

5 months ago

8.43.0

7 months ago

9.7.0

5 months ago

8.44.0

7 months ago

9.0.3

7 months ago

8.45.0

7 months ago

8.46.0

7 months ago

9.0.2

7 months ago

9.0.1

7 months ago

9.0.0

7 months ago

8.47.0

7 months ago

9.1.0

7 months ago

9.3.0

6 months ago

8.48.1

7 months ago

8.48.0

7 months ago

9.2.0

7 months ago

8.38.0

7 months ago

8.39.0

7 months ago

8.39.1

7 months ago

8.37.0

7 months ago

8.36.0

7 months ago

8.35.2

7 months ago

8.35.0

7 months ago

8.35.1

7 months ago

8.34.0

8 months ago

8.30.0

8 months ago

8.31.0

8 months ago

8.32.0

8 months ago

8.33.0

8 months ago

8.26.0

8 months ago

8.27.0

8 months ago

8.28.0

8 months ago

8.29.0

8 months ago

8.25.0

8 months ago

8.23.0

8 months ago

8.24.0

8 months ago

8.21.0

8 months ago

8.22.0

8 months ago

8.20.0

8 months ago

8.20.1

8 months ago

8.19.0

8 months ago

8.18.0

8 months ago

8.15.0

8 months ago

8.16.0

8 months ago

8.17.0

8 months ago

8.14.0

8 months ago

8.12.0

9 months ago

8.12.1

8 months ago

8.13.0

8 months ago

8.11.0

9 months ago

8.10.1

9 months ago

8.10.0

9 months ago

8.9.0

9 months ago

8.8.1

10 months ago

8.8.0

10 months ago

8.7.1

10 months ago

8.7.0

10 months ago

8.6.1

10 months ago

8.6.0

10 months ago

8.4.0

10 months ago

8.3.0

10 months ago

8.5.0

10 months ago

8.2.0

10 months ago

8.1.0

11 months ago

8.0.1

11 months ago

8.0.0

12 months ago

7.3.0

1 year ago

7.4.0

1 year ago

7.2.0

1 year ago

7.1.2

1 year ago

7.1.1

1 year ago

7.1.0

1 year ago

7.0.0

1 year ago

6.17.0

1 year ago

2.69.0

1 year ago

2.46.0

1 year ago

5.30.0

1 year ago

3.2.0

1 year ago

2.57.0

1 year ago

2.45.0

1 year ago

4.0.0

1 year ago

5.42.0

1 year ago

5.0.0

1 year ago

2.68.0

1 year ago

2.56.0

1 year ago

5.31.0

1 year ago

3.1.0

1 year ago

6.16.0

1 year ago

2.79.0

1 year ago

2.67.0

1 year ago

2.44.0

1 year ago

5.43.0

1 year ago

5.20.0

1 year ago

5.1.0

1 year ago

2.55.0

1 year ago

5.32.0

1 year ago

2.78.0

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

6.15.0

1 year ago

3.0.0

1 year ago

2.43.0

1 year ago

2.66.0

1 year ago

2.89.0

1 year ago

5.44.0

1 year ago

5.21.0

1 year ago

5.2.0

1 year ago

6.0.1

1 year ago

6.0.0

1 year ago

6.0.2

1 year ago

2.54.0

1 year ago

5.33.0

1 year ago

5.10.0

1 year ago

2.77.0

1 year ago

6.14.0

1 year ago

2.42.1

1 year ago

2.65.0

1 year ago

5.22.1

1 year ago

5.22.0

1 year ago

2.88.0

1 year ago

2.53.0

1 year ago

2.76.0

1 year ago

3.6.0

1 year ago

2.64.0

1 year ago

2.87.0

1 year ago

2.75.0

1 year ago

2.52.0

1 year ago

3.5.0

1 year ago

2.86.0

1 year ago

2.63.0

1 year ago

2.74.0

1 year ago

2.51.0

1 year ago

3.4.0

1 year ago

2.85.0

1 year ago

2.62.0

1 year ago

4.2.0

1 year ago

5.40.0

1 year ago

2.73.1

1 year ago

2.96.0

1 year ago

2.73.0

1 year ago

2.50.0

1 year ago

3.3.0

1 year ago

2.84.0

1 year ago

4.1.0

1 year ago

2.61.0

1 year ago

5.41.0

1 year ago

6.5.0

1 year ago

5.27.0

1 year ago

5.15.0

1 year ago

2.95.0

1 year ago

2.72.0

1 year ago

5.39.0

1 year ago

5.16.0

1 year ago

6.6.0

1 year ago

2.83.0

1 year ago

2.60.0

1 year ago

5.8.0

1 year ago

5.28.0

1 year ago

2.94.0

1 year ago

2.71.0

1 year ago

5.17.0

1 year ago

6.7.0

1 year ago

2.82.0

1 year ago

5.9.0

1 year ago

5.29.0

1 year ago

2.93.0

1 year ago

2.70.0

1 year ago

3.8.0

1 year ago

5.18.0

1 year ago

6.8.1

1 year ago

6.8.0

1 year ago

6.8.2

1 year ago

2.81.0

1 year ago

2.92.0

1 year ago

3.7.1

1 year ago

3.7.0

1 year ago

5.19.0

1 year ago

6.9.0

1 year ago

2.80.0

1 year ago

5.3.0

1 year ago

6.1.0

1 year ago

6.1.1

1 year ago

5.34.0

1 year ago

5.11.0

1 year ago

2.91.0

1 year ago

6.13.0

1 year ago

5.23.0

1 year ago

5.4.0

1 year ago

6.2.0

1 year ago

2.49.0

1 year ago

5.35.1

1 year ago

5.35.2

1 year ago

5.35.0

1 year ago

5.12.0

1 year ago

2.90.0

1 year ago

6.12.0

1 year ago

5.24.0

1 year ago

5.5.1

1 year ago

5.5.0

1 year ago

6.3.0

1 year ago

2.48.0

1 year ago

5.36.0

1 year ago

5.13.0

1 year ago

6.11.6

1 year ago

6.11.1

1 year ago

6.11.0

1 year ago

6.11.5

1 year ago

6.11.4

1 year ago

6.11.3

1 year ago

6.11.2

1 year ago

2.59.0

1 year ago

5.25.0

1 year ago

5.6.0

1 year ago

2.47.0

1 year ago

6.4.1

1 year ago

6.4.0

1 year ago

5.37.0

1 year ago

5.14.0

1 year ago

6.10.0

1 year ago

2.58.0

1 year ago

5.38.0

1 year ago

5.26.0

1 year ago

5.7.2

1 year ago

5.7.1

1 year ago

5.7.0

1 year ago

1.79.0

2 years ago

1.79.1

2 years ago

1.94.0

2 years ago

2.11.0

2 years ago

2.34.0

2 years ago

2.4.0

2 years ago

1.141.0

2 years ago

1.82.0

2 years ago

1.82.1

2 years ago

1.82.2

2 years ago

1.149.0

2 years ago

1.126.0

2 years ago

1.103.0

2 years ago

2.19.0

2 years ago

1.130.2

2 years ago

1.130.1

2 years ago

2.22.0

2 years ago

1.130.0

2 years ago

1.115.2

2 years ago

1.115.0

2 years ago

1.115.1

2 years ago

1.138.0

2 years ago

1.93.0

2 years ago

2.3.0

2 years ago

2.33.0

2 years ago

2.10.0

2 years ago

1.140.0

2 years ago

1.83.0

2 years ago

1.83.1

2 years ago

1.125.0

2 years ago

1.148.0

2 years ago

1.102.0

2 years ago

1.148.1

2 years ago

2.18.0

2 years ago

2.21.0

2 years ago

1.137.1

2 years ago

1.137.0

2 years ago

1.114.0

2 years ago

1.96.0

2 years ago

2.29.0

2 years ago

2.32.0

2 years ago

2.2.0

2 years ago

1.143.0

2 years ago

1.120.0

2 years ago

1.128.1

2 years ago

1.128.0

2 years ago

1.84.0

2 years ago

1.84.1

2 years ago

1.105.0

2 years ago

2.17.0

2 years ago

1.132.0

2 years ago

2.20.0

2 years ago

1.117.0

2 years ago

1.95.0

2 years ago

2.28.0

2 years ago

1.142.0

2 years ago

2.31.0

2 years ago

2.9.0

2 years ago

1.104.0

2 years ago

1.85.0

2 years ago

2.1.0

2 years ago

1.127.0

2 years ago

2.39.0

2 years ago

2.16.0

2 years ago

2.42.0

2 years ago

1.131.0

2 years ago

1.139.0

2 years ago

1.116.0

2 years ago

1.98.1

2 years ago

1.98.0

2 years ago

1.90.1

2 years ago

1.90.0

2 years ago

2.27.0

2 years ago

2.0.2

2 years ago

1.145.0

2 years ago

1.122.0

2 years ago

2.30.0

2 years ago

2.8.0

2 years ago

1.107.0

2 years ago

1.86.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

2.15.0

2 years ago

2.38.0

2 years ago

1.111.0

2 years ago

1.134.0

2 years ago

2.41.0

2 years ago

1.119.0

2 years ago

1.134.1

2 years ago

1.97.2

2 years ago

1.97.1

2 years ago

1.97.0

2 years ago

2.26.1

2 years ago

2.26.0

2 years ago

1.121.0

2 years ago

1.144.0

2 years ago

2.7.0

2 years ago

1.129.0

2 years ago

1.106.2

2 years ago

1.106.0

2 years ago

1.106.1

2 years ago

1.87.0

2 years ago

2.37.0

2 years ago

2.14.0

2 years ago

1.133.1

2 years ago

1.133.0

2 years ago

1.110.0

2 years ago

2.40.0

2 years ago

1.118.0

2 years ago

1.98.3

2 years ago

1.98.2

2 years ago

1.92.4

2 years ago

1.92.3

2 years ago

1.92.2

2 years ago

1.92.1

2 years ago

1.92.0

2 years ago

2.25.0

2 years ago

1.101.0

2 years ago

2.6.0

2 years ago

1.80.0

2 years ago

1.80.1

2 years ago

1.109.0

2 years ago

1.80.2

2 years ago

1.80.3

2 years ago

1.80.4

2 years ago

1.101.3

2 years ago

1.147.0

2 years ago

1.124.0

2 years ago

1.101.4

2 years ago

1.101.1

2 years ago

1.101.2

2 years ago

1.89.0

2 years ago

1.88.0

2 years ago

2.36.0

2 years ago

2.13.0

2 years ago

2.13.1

2 years ago

1.136.0

2 years ago

1.113.0

2 years ago

1.113.1

2 years ago

1.76.1

2 years ago

1.99.0

2 years ago

1.78.0

2 years ago

1.91.0

2 years ago

2.24.0

2 years ago

1.100.0

2 years ago

2.12.0

2 years ago

2.5.0

2 years ago

1.123.0

2 years ago

1.108.0

2 years ago

1.81.0

2 years ago

1.81.1

2 years ago

1.81.2

2 years ago

1.81.3

2 years ago

1.81.4

2 years ago

1.81.5

2 years ago

1.109.1

2 years ago

2.35.0

2 years ago

1.112.0

2 years ago

2.23.0

2 years ago

1.150.0

2 years ago

1.150.1

2 years ago

1.135.1

2 years ago

1.135.0

2 years ago

1.77.0

2 years ago

1.76.0

2 years ago

1.75.0

2 years ago

1.74.0

2 years ago

1.73.2

2 years ago

1.73.1

2 years ago

1.73.0

2 years ago

1.72.7

2 years ago

1.72.6

2 years ago

1.72.5

2 years ago

1.72.4

2 years ago

1.72.3

2 years ago

1.72.2

2 years ago

1.72.1

2 years ago

1.72.0

2 years ago

1.71.2

2 years ago

1.71.1

2 years ago

1.71.0

2 years ago

1.70.4

2 years ago

1.70.3

2 years ago

1.70.2

2 years ago

1.70.1

2 years ago

1.70.0

2 years ago

1.69.1

2 years ago

1.69.0

2 years ago

1.68.0

2 years ago

1.67.0

2 years ago

1.66.0

2 years ago

1.65.0

2 years ago

1.64.0

2 years ago

1.63.1

2 years ago

1.63.0

2 years ago

1.62.0

2 years ago

1.61.1

2 years ago

1.61.0

2 years ago

1.60.0

2 years ago

1.59.0

2 years ago

1.58.0

2 years ago

1.57.0

2 years ago

1.56.0

2 years ago

1.55.0

2 years ago

1.54.5

2 years ago

1.54.4

2 years ago

1.54.3

2 years ago

1.54.2

2 years ago

1.54.1

2 years ago

1.54.0

2 years ago

1.53.0

2 years ago

1.52.4

2 years ago

1.52.3

2 years ago

1.52.2

2 years ago

1.52.1

2 years ago

1.52.0

2 years ago

1.51.0

2 years ago

1.50.0

2 years ago

1.49.1

2 years ago

1.49.0

2 years ago

1.48.0

2 years ago

1.47.0

2 years ago

1.46.0

2 years ago

1.45.0

2 years ago

1.44.0

2 years ago

1.43.0

2 years ago

1.42.0

2 years ago

1.41.0

2 years ago

1.40.2

2 years ago

1.40.1

2 years ago

1.40.0

2 years ago

1.39.0

2 years ago

1.38.0

2 years ago

1.37.0

2 years ago

1.36.0

2 years ago

1.35.0

2 years ago

1.34.0

2 years ago

1.33.0

2 years ago

1.32.0

2 years ago

1.31.0

2 years ago

1.30.0

2 years ago

1.29.0

2 years ago

1.28.0

2 years ago

1.27.0

2 years ago

1.26.0

2 years ago

1.25.1

2 years ago

1.25.0

2 years ago

1.24.0

2 years ago

1.23.0

2 years ago

1.22.0

2 years ago

1.21.0

2 years ago

1.20.0

2 years ago

1.19.0

2 years ago

1.18.2

2 years ago

1.18.1

2 years ago

1.18.0

2 years ago

1.17.1

2 years ago

1.17.0

2 years ago

1.16.2

2 years ago

1.16.1

2 years ago

1.16.0

2 years ago

1.15.4

2 years ago

1.15.3

2 years ago

1.15.2

2 years ago

1.15.1

2 years ago

1.15.0

2 years ago

1.14.3

2 years ago

1.14.2

2 years ago

1.14.1

2 years ago

1.14.0

2 years ago

1.13.1

2 years ago

1.13.0

2 years ago

1.12.0

2 years ago

1.11.0

2 years ago

1.10.0

2 years ago

1.9.1

2 years ago

1.9.0

2 years ago

1.8.11

2 years ago

1.8.10

2 years ago

1.8.9

2 years ago

1.8.8

2 years ago

1.8.7

2 years ago

1.8.6

2 years ago

1.8.5

2 years ago

1.8.4

2 years ago

1.8.3

2 years ago

1.8.2

2 years ago

1.8.1

2 years ago

1.8.0

2 years ago

1.7.4

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.13

2 years ago

1.6.12

2 years ago

1.6.11

2 years ago

1.6.10

2 years ago

1.6.9

2 years ago

1.6.8

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.6.5

2 years ago

1.6.4

2 years ago

1.6.3

2 years ago

1.6.2

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago