# to-ast

> Converts JavaScript objects to equivalent ASTs

Latest version **1.0.0** (published 2015-04-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install to-ast
pnpm add to-ast
yarn add to-ast
bun add to-ast
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2015-04-02 |
| First published | 2015-04-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 34 |
| Author | Devon Govett |
| Maintainers | devongovett |
| Keywords | ast, object, syntax, escodegen, esprima |

## Links

- npm: https://www.npmjs.com/package/to-ast
- Repository: https://github.com/devongovett/to-ast
- Issues: https://github.com/devongovett/to-ast/issues
- npm.io page: https://npm.io/package/to-ast

## Dependencies (2)

- [esprima](https://npm.io/package/esprima.md) ^2.1.0
- [ast-types](https://npm.io/package/ast-types.md) ^0.7.2

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2015-04-02

## README

# to-ast

This module converts JavaScript objects to an equivalent abstract syntax tree representation, 
compatible with the [Mozilla Parser API](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API).
You can use it to generate JavaScript source code from objects, using 
[escodegen](https://github.com/estools/escodegen).

## Usage

Install with npm:

    npm install to-ast

Here's a simple example:

```javascript

var toAST = require('to-ast');
var escodegen = require('escodegen');

// get an AST from a number...
toAST(2) //=> { type: 'Literal', value: 2 }

// or if you want a source string...
escodegen.generate(toAST(2)) //=> '2'
```

## Supported types

* undefined
* null
* number, string, and boolean literals
* functions
* Node buffers
* arrays
* String, Number, and Boolean object wrappers
* typed arrays and array buffers
* dates
* errors
* regular expressions
* object literals

## Custom types

Most built-in JavaScript types as supported out of the box, but if you want to override the behavior for
your particular object, you can provide a `toAST` method on your object:

```javascript
var toAST = require('to-ast');
var escodegen = require('escodegen');

function Person(name) {
  this.name = name;
}

Person.prototype.toAST = function() {
  return {
    type: 'NewExpression',
    callee: { type: 'Identifier', name: 'Person' },
    arguments: [{ type: 'Literal', value: this.name }]
  };
};

escodegen.generate(toAST(new Person('Devon'))) //=> "new Person('Devon')"
```

## License

MIT

---
_Source: https://npm.io/package/to-ast · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
