# ast-eval

> Statically evaluate AST

Latest version **0.8.0** (published 2015-03-03) · Unlicensed license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.0 |
| Published | 2015-03-03 |
| First published | 2015-02-12 |
| Weekly downloads | 0 |
| License | Unlicensed |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 13 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 11 |
| Author | Deema Ywanov |
| Maintainers | dfcreative |
| Keywords | static-eval, ecmascript, esprima, ast, es-trim, estrim, espurify, ast-eval, static, static-analysis, estools, eval, es-eval, eseval, preeval, pre-eval, fold, constants, compiler, optimizing, dataflow, data-flow, analysis |

## Links

- npm: https://www.npmjs.com/package/ast-eval
- Repository: http://github.com/dfcreative/ast-eval
- Homepage: https://github.com/dfcreative/ast-eval
- Issues: https://github.com/dfcreative/ast-eval/issues
- npm.io page: https://npm.io/package/ast-eval

## Dependencies (13)

- [clone](https://npm.io/package/clone.md) ^1.0.0
- [xtend](https://npm.io/package/xtend.md) ^4.0.0
- [escope](https://npm.io/package/escope.md) ^2.0.4
- [esprima](https://npm.io/package/esprima.md) ^2.0.0
- [esquery](https://npm.io/package/esquery.md) ^0.3.0
- [ast-test](https://npm.io/package/ast-test.md) ^1.1.1
- [tosource](https://npm.io/package/tosource.md) ^0.1.3
- [ast-types](https://npm.io/package/ast-types.md) ^0.6.12
- [escodegen](https://npm.io/package/escodegen.md) ^1.6.1
- [is-object](https://npm.io/package/is-object.md) ~1.0.1
- [ast-replace](https://npm.io/package/ast-replace.md) ^1.1.3
- [is-primitive](https://npm.io/package/is-primitive.md) ^1.0.0
- [ast-redeclare](https://npm.io/package/ast-redeclare.md) ^1.0.4

## 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

- 0.8.0 (latest) — 2015-03-03
- 0.7.1 — 2015-02-14
- 0.7.0 — 2015-02-14
- 0.6.5 — 2015-02-13
- 0.6.4 — 2015-02-13
- 0.6.3 — 2015-02-13
- 0.6.2 — 2015-02-13
- 0.6.1 — 2015-02-13
- 0.6.0 — 2015-02-13
- 0.5.0 — 2015-02-13
- 0.4.2 — 2015-02-13
- 0.4.1 — 2015-02-13
- 0.4.0 — 2015-02-13
- 0.3.0 — 2015-02-13
- 0.2.0 — 2015-02-13
- … 1 more at https://npm.io/package/ast-eval/versions

## README

# ast-eval [![Build Status](https://travis-ci.org/dfcreative/ast-eval.svg?branch=master)](https://travis-ci.org/dfcreative/ast-eval) [![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)

Statically evaluate expressions in AST, also known as [constants folding](http://en.wikipedia.org/wiki/Constant_folding). Useful for precompilation tasks.


## Use

```sh
npm install --save ast-eval
```

```js
var esprima = require('esprima');
var gen = require('escodegen').generate;
var astEval = require('ast-eval');

var ast = esprima.parse('[1, 2 === "2", 3+4*10, [2] === 2]');
ast = astEval(ast);

gen(ast); //'[1, false, 43, false]'
```


## API

### preeval(Node, options) → Node

Evaluate expressions in a Node, return a new Node with optimized shorten expression nodes.

<!--
| Option | Default value | Description |
|---|---|---|
| optimize | `false` | Ignore eval results lengthen than initial source code |
| computeProps | `false` | Try to evaluate `computed` properties |
| externs | `{}` | External constant values or functions |
| exports | `''` | List of variables to provide as exports |
| evalGlobals | `true` | Ast-eval takes supposation that native environment isn’t changed and all built-ins have it’s original or polyfilled methods. If you redefine the built-ins, like with [sugar.js]() or similar library - make sure to provide it as externs. Or you can set `evalGlobals=true` to avoid evaluating globals. |
-->


## Features

* [x] Fold expressions
	* [x] Binary expressions: `1000 * 60 * 60` → `36e6`
	* [x] Logical expressions: `{a:1} && {b:2}` → `true`
	* [x] Math expressions: `Math.sin(Math.Pi / 2 )` → `1`

* [x] Fold arrays
	* [x] Safe methods: `[1,2,3,new Date].concat(4, [5], new Date)` → `[1,2,3,new Date,4,5, new Date]`
	* [x] Unsafe methods: `[1,2,3].map(function(x){ return x*2})` → `[2,4,6]`
	* [ ] Static methods: `Array.from([1, 2, 3], function(x){ return x*2; })` → `[2,4,6]`
	* [ ] Prototype methods: `Array.prototype.slice.call([1,2,3], 1,2)` → `[2]`

* [ ] Fold static globals (what’s that?)

* [x] Decompute object access (optionally)
	* [x] `a['x'] = 1` → `a.x = 1`

* [x] Fold strings
	* [x] `'a b c'.split(' ')` → `['a', 'b', 'c']`

* [ ] [Propagate constants](http://en.wikipedia.org/wiki/Constant_folding#Constant_propagation)
	* [ ] Simple flow analysis: `var x = 1; x + 2;` → `3;`
	* [ ] Scope analysis
	* [ ] Method substitution: `var slice = Array.prototype.slice; var x = [1,2,3]; var y = slice(x)'`

* [ ] Fold loops
	* [ ] `var x = []; for (var i = 0; i < 10; i++) {x[i] = 10*i;}`

* [ ] Fold proxy functions

* [ ] Remove unused props

* [ ] Undead code
	* [ ] Empty isolated functions
	* [ ] Remove unused variables (after enabling constants)
	* [ ] Remove unused functions
	* [ ] Remove unused properties

* [ ] Fold clone-code
	* `a.x`×3 → `var _a = a; _a.x`

* [ ] Data-flow analysis
	* [ ] Precall functions
	* [ ] Substitute variables

* [ ] Provide exports

* [ ] Fold primitives
	* [ ] new Array([1,2,3,...])
	* [ ] [1,2,3,...]

* [ ] Rearrange things
	* [ ] Hoist functions (place after first use)
	* [ ] Fold variable declarations



## References

* [List of compiler optimizations](http://en.wikipedia.org/wiki/Optimizing_compiler) — ideas of folding.
* Substack’s [static-eval](https://github.com/substack/static-eval) — evaluate static expressions.
* [esmangle](https://github.com/estools/esmangle)


[![NPM](https://nodei.co/npm/ast-eval.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/ast-eval/)

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