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 🙏

© 2026 – Pkg Stats / Ryan Hefner

just-expression

v1.0.0

Published

Traverse and transform JavaScript AST to make sure it's JUST an expression

Readme

just-expression

Traverse and transform JavaScript AST to make sure it's JUST an expression.

Motivation

Sometimes we need to receive some user-defined JavaScript code, and execute it safely.

This package receives a parsed JavaScript AST, traverses and transforms it, make sure it's JUST an expression.

It only accepts a small subset of JavaScript AST (specifically expression node types and some related ones), then traverses it to guarantee it's a pure expression.

Besides, all variables will be captured, and only user-specified variables are allowed.

Finally, it compiles the AST into a function that users can call safely.

Installation

npm i just-expression

Usage

import type { Expression } from 'estree'
import { generate } from 'astring'
import { compile } from 'just-expression'
import { parse } from 'meriyah'

function to_ast(code: string): Expression {
    const prog = parse(code)
    assert(prog.body.length === 1, 'too complex program')
    assert(prog.body[0].type === 'ExpressionStatement', 'not pure expression')
    return prog.body[0].expression
}

// Compile the AST to a function
const calc = compile(generate, to_ast('a + b'), ['a', 'b'])
calc(3, 4) // => 7

// Can't compile an expression with unscoped variables
compile(generate, to_ast('Math.max(1, 2)')) // ReferenceError: variable 'Math' is not defined

// Must provide usable variable manually
// So that variable 'Math' is visible
compile(generate, to_ast('Math.max(1, 2)'), ['Math'])(Math) // => 2

// Using global to capture all unscoped variables
// Specify name '_' in param list to be global variable
// So unscoped variable 'a' becomes _.a
compile(generate, to_ast('Math.max(a, 2)'), ['Math', '_'], '_')(Math, { a: 5 }) // => 5

// Pass the global object as the global variable to remove all limitations
compile(generate, to_ast('eval("123")'), ['_'], '_')(globalThis) // => 123

// Can return arrow function with expression body
compile(generate, to_ast('(a, b) => a + b'))()(1, 2) // => 3

API

.transform(ast, params, global, enables)

Core API that traverses and transforms JavaScript AST.

JavaScript AST should be ESTree-compatible; it can come from parsers like acorn or meriyah.

ast

Type: Expression

The JavaScript expression AST.

params

Type: string[]

Default: []

List of allowed variables.

If global is not set, variables in ast that are not in this list will throw an error.

global

Type: string | 'this' | null

Default: null

Specify a variable in params to be global variable; any variables in ast that are not listed in params will become property accesses on this global variable.

enables

Type: object

Switches to enable or disable some syntax.

enables.this

Type: boolean

Default: false

Enable this expression.

enables.call

Type: boolean

Default: true

Enable function call, including normal function call (f()), new operator (new f()), and tagged template (tag`text` ).

enables.arrow

Type: boolean

Default: true

Enable arrow function expression (()=>1), function body must be expression.

enables.update

Type: boolean

Default: false

Enable update operations, including the delete operator, update operators (++, --), and assignment operators (=, += ,-=, etc.).

These operations may modify original data.

enables.inspect

Type: boolean

Default: false

Enable the typeof, in and instanceof operators.

These operations inspect the data structure, which seems unnecessary.

Return

Return the transformed AST. The returned AST reuses unmodified parts of the original AST.

.compile(codegen, ast, params, global, enables)

Traverses and transforms the AST, then compiles it into a function.

The compiled function receives the same parameters as listed in params.

codegen

Type: (ast: Expression) => string

A function that generates JavaScript code from an ESTree-compliant AST.

ast, params, global, enables

Same as .transform().

Return

Returns a function equivalent to ast, with parameters matching params.

Limitation

just-expression only supports the following expressions (ESTree Nodes):

  • Identifier
  • Literal
  • ArrayExpression
  • ObjectExpression
  • MemberExpression
  • ChainExpression
  • LogicalExpression
  • SequenceExpression
  • ConditionalExpression
  • TemplateLiteral

The following expressions are affected by options:

  • ThisExpression
  • UnaryExpression
  • BinaryExpression
  • UpdateExpression
  • AssignmentExpression
  • NewExpression
  • CallExpression
  • TaggedTemplateExpression
  • ArrowFunctionExpression (function body must be expression)

The following expressions are not supported:

  • AwaitExpression
  • FunctionExpression
  • ClassExpression
  • MetaProperty
  • YieldExpression
  • ImportExpression

License

MIT