# escorn

A babel compatibility layer built on [acorn](https://github.com/acornjs/acorn) with strong source mapping capabilities. The main focus is to generate a readable output and making it easy to reuse existing babel plugins.

This library was built for [WMR](https://wmr.dev/), but should be usable outside of that.

## Usage

Installation:

```bash
npm install -D escorn
# or via yarn
yarn add -D escorn
```

Transform example:

```js
import { transform, parse } from "escorn";

function MyBabelPlugin({ types: t }) {
  return {
    name: "my-plugin",
    visitor: {
      NumericLiteral(path) {
        path.replaceWith(t.identifier("foobar"));
      },
    },
  };
}

const result = transform(`const a = 42`, {
  parse,
  plugins: [myBabelPlugin],
});

console.log(result.code);
// Logs: `const a = foobar;`
```

Alternative usage with a custom acorn instance:

```js
import * as acorn from "acorn";
import { transform, acornPlugins } from "escorn";

const parser = acorn.Parser.extend(...acornPlugins);
const parse = (code, opts) =>
  parser.parse({
    ...opts,
    sourceType: "module",
    ecmaVersion: "latest",
  });

const result = transform(`const a = 42`, {
  parse,
  plugins: [],
});
```

## License

MIT, see [the license file](./LICENSE).
