npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

estreval

v2.1.2

Published

Evaluate JavaScript abstract syntax tree in ESTree format

Downloads

693

Readme

Estreval

Evaluate JavaScript abstract syntax tree in ESTree format

Overview

This is a sandboxed runtime to evaluate JavaScript expressions.

Here is an example page to interact with it.

Language

The interpreter supports a reasonable subset of ES5, the 5th Edition of the ECMAScript language specification (PDF). In addition, some modern syntax is supported:

  • Array function expression
  • Block scope, let and const
  • Class
  • JSX - No React or renderer yet
  • Spread and rest expression

Notably missing are Promise and async/await, because there is no event loop.

Evaluate

The main function parses and evaluates an expression.

const estreval = require('estreval')

estreval('1 + 2 * 3') // 7

The expression can be given as string, or an object in ESTree format.

Context

The second argument is a context object (optional). It defines the global context of variables to which the code will have access.

const context = { x: 3 }

estreval('y => x * y', context)(5) // 15

Options

The third argument is an options object (optional).

estreval(code, context, options)

It can have the following properties.

  • timeout - Maximum amount of time (in milliseconds) allowed for the code - Default: 100
  • maxSteps - Maximum number of steps allowed for the code - Default: 1024

Default parser

The default parser uses Acorn.

It can be imported by itself.

const parse = require('estreval/parse')

const tree = parse('y => x * y') // ESTree format

Custom parser

To use a custom parser, first import the evaluate function separately.

The following example uses Esprima.

const evaluate = require('estreval/evaluate')
const { parseScript: parse } = require('esprima')

const tree = parse('y => x * y')
const context = { x: 3 }
const options = { parse }

evaluate(tree, context, options)(5) // 15

The parse function can be passed as the parse option. It will be used when new instances of Function are created inside the runtime. Otherwise, the use of Function will throw an error.

Babel parser

To use the Babel parser, specify its built-in plugin estree to convert the syntax tree to ESTree format.

const { parse } = require('@babel/parser')

const tree = parse(code, {
  plugins: ['estree']
})

This is necessary because Babel uses its own AST format, with some differences from the ESTree specification.

REPL

Start a read-eval-print loop to interact with the runtime in the terminal.

node repl

The following functions are provided for convenience.

  • print( any ) - Show value using inspect and console.log
  • parse( string ) - Parse given string and print abstract syntax tree

Reload

Enter .reload in the REPL to reload the library after editing its files.

Develop the library

Build unminified for development, watch files for changes, and start server for test page

npm run dev

Build minified for production

npm run build

References