Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Rich-Harris/esrap
Parse in reverse
https://github.com/Rich-Harris/esrap
Last synced: 3 months ago
JSON representation
Parse in reverse
- Host: GitHub
- URL: https://github.com/Rich-Harris/esrap
- Owner: Rich-Harris
- License: mit
- Created: 2023-09-17T00:37:36.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-04-30T13:48:19.000Z (7 months ago)
- Last Synced: 2024-05-02T07:57:24.461Z (7 months ago)
- Language: JavaScript
- Size: 444 KB
- Stars: 39
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# esrap
Parse in reverse. AST goes in, code comes out.
## Usage
```js
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`](https://github.com/acornjs/acorn/tree/master/acorn/#interface) 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):
```js
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](https://bun.sh/). Once it's installed, do `bun install` to install dependencies, and `bun test` to run the tests.
## License
[MIT](LICENSE)