https://github.com/sveltejs/esrap
Parse in reverse
https://github.com/sveltejs/esrap
Last synced: 10 days ago
JSON representation
Parse in reverse
- Host: GitHub
- URL: https://github.com/sveltejs/esrap
- Owner: sveltejs
- License: mit
- Created: 2023-09-17T00:37:36.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-02T21:47:59.000Z (19 days ago)
- Last Synced: 2025-04-11T05:16:09.636Z (11 days ago)
- Language: JavaScript
- Size: 695 KB
- Stars: 65
- Watchers: 4
- Forks: 8
- Open Issues: 2
-
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 the following options:
```js
const { code, map } = print(ast, {
// Populate the `sources` field of the resulting sourcemap
// (note that the AST is assumed to come from a single file)
sourceMapSource: 'input.js',// Populate the `sourcesContent` field of the resulting sourcemap
sourceMapContent: fs.readFileSync('input.js', 'utf-8'),// Whether to encode the `mappings` field of the resulting sourcemap
// as a VLQ string, rather than an unencoded array. Defaults to `true`
sourceMapEncodeMappings: false,// String to use for indentation — defaults to '\t'
indent: ' ',// Whether to wrap strings in single or double quotes — defaults to 'single'.
// This only applies to string literals with no `raw` value, which generally
// means the AST node was generated programmatically, rather than parsed
// from an original source
quotes: 'single'
});
```## TypeScript
`esrap` can also print TypeScript nodes, assuming they match the ESTree-like [`@typescript-eslint/types`](https://www.npmjs.com/package/@typescript-eslint/types).
## Why not just use Prettier?
Because it's ginormous.
## Developing
This repo uses [pnpm](https://pnpm.io). Once it's installed, do `pnpm install` to install dependencies, and `pnpm test` to run the tests.
## License
[MIT](LICENSE)