1.2.1 • Published 5 months ago

esrap v1.2.1

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

esrap

Parse in reverse. AST goes in, code comes out.

Usage

import { print } from 'esrap';

const { code, map } = print({
  type: 'Program',
  body: [
    {
      type: 'ExpressionStatement',
      expression: {
        callee: {
          type: 'Identifier',
          name: 'alert'
        },
        arguments: [
          {
            type: 'Literal',
            value: 'hello world!'
          }
        ]
      }
    }
  ]
});

console.log(code); // alert('hello world!');

If the nodes of the input AST have loc properties (e.g. the AST was generated with acorn with the locations option set), sourcemap mappings will be created.

Options

You can pass information that will be added to the resulting sourcemap (note that the AST is assumed to come from a single file):

const { code, map } = print(ast, {
  sourceMapSource: 'input.js',
  sourceMapContent: fs.readFileSync('input.js', 'utf-8')
});

Why not just use Prettier?

Because it's ginormous.

Developing

This repo uses Bun. Once it's installed, do bun install to install dependencies, and bun test to run the tests.

License

MIT

1.2.0

5 months ago

1.1.1

6 months ago

1.1.0

6 months ago

1.2.1

5 months ago

1.0.3

6 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago