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

node-estree

v4.0.0

Published

Node.js EStree (AST) Generator for Node.js

Downloads

402

Readme

Node.js ESTree

version Maintenance MIT size dep

Complete and compliant ESTree spec implementation in TypeScript (for Node.js and the Browser). This project includes types definitions, variant functions and some helpers aside to help generating code.

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm install node-estree
# or
$ yarn add node-estree

Usage example

Following example require the astring package to generate a JavaScript code with the EStree compliant AST.

"use strict";
const { ESTree, Helpers, VarDeclaration, Switch } = require("node-estree");
const astring = require("astring");

const logNative = (body) => ESTree.CallExpression(Helpers.AutoChain("console", "log"), body);
const log = (message) => logNative([ESTree.Literal(message)]);

// Note: i use a generator function because I find it easier to read
function* program() {
    yield ESTree.ExpressionStatement(log("hello world!"));
    {
        const objExpr = Helpers.PlainObject({
            yoo: "foo", test: Helpers.Symbol("bar"), lol: true
        });

        yield ESTree.BlockStatement([
            VarDeclaration.let("myObject", objExpr),
            ESTree.ExpressionStatement(logNative([ESTree.Identifier("myObject")]))
        ]);
    }

    yield ESTree.ExpressionStatement(Helpers.AutoChainStr("a.b.?hello().c.d()", {
        hello: [ESTree.Literal(true)]
    }));

    yield VarDeclaration.const("myArray", Helpers.LiteralArray(1, 2, true, "boo"));

    yield new VarDeclaration({ kind: "var" })
        .declare("a", ESTree.Literal(1))
        .declareEx("b", 2)
        .toJSON();

    yield new Switch(ESTree.Identifier("a"), { autoBreak: true })
        .case(1, [log("foo")])
        .case(2, [log("bar")])
        .toJSON();
}

const prog = ESTree.Program("module", [...program()]);
console.log("\n");
console.log(astring.generate(prog));

The astring lib will generate the following JavaScript code:

console.log("hello world!");
{
  let myObject = {
    yoo: "foo",
    test: Symbol("bar"),
    lol: true
  };
  console.log(myObject);
}
a.b.hello(true).c.d();
const myArray = [1, 2, true, "boo"];
var a = 1, b = 2;
switch (a) {
  case 1:
    console.log("foo")
    break;
  case 2:
    console.log("bar")
    break;
}

When you want to generate a given code I recommend you to use astexplorer. You can observe how the tree is built and reproduce the same thing with my lib.

API

ESTree

The ESTree object exports all members for all versions of ECMAScript (the current implementation doesn't work by version.. everything to ES2020 has been implemented). Variant names are exactly the same as the Specification. Everything has been done to be as close to the specification as possible.

Technical notes:

  • MethodDefinition use the property isStatic instead of static (because static is a reserved keyword).
  • Some Variant with to much properties (like FunctionDeclarations etc) use an options payload.
FunctionDeclaration(Identifier("functionName"), { body:[], params: [], async: true, generator: false, });

Available Variant

type Variant =
    "Program" |
    "Identifier" |
    "Literal" |
    "RegExpLiteral" |
    "ExpressionStatement" |
    "BlockStatement" |
    "EmptyStatement" |
    "DebuggerStatement" |
    "WithStatement" |
    "ReturnStatement" |
    "LabeledStatement" |
    "BreakStatement" |
    "ContinueStatement" |
    "IfStatement" |
    "SwitchStatement" |
    "SwitchCase" |
    "ThrowStatement" |
    "TryStatement" |
    "CatchClause" |
    "WhileStatement" |
    "DoWhileStatement" |
    "ForStatement" |
    "ForInStatement" |
    "FunctionDeclaration" |
    "VariableDeclaration" |
    "VariableDeclarator" |
    "ThisExpression" |
    "ArrayExpression" |
    "ObjectExpression" |
    "Property" |
    "FunctionExpression" |
    "UnaryExpression" |
    "UpdateExpression" |
    "BinaryExpression" |
    "AssignmentExpression" |
    "LogicalExpression" |
    "MemberExpression" |
    "ConditionalExpression" |
    "CallExpression" |
    "NewExpression" |
    "SequenceExpression" |
    "ForOfStatement" |
    "Super" |
    "SpreadElement" |
    "ArrowFunctionExpression" |
    "YieldExpression" |
    "TemplateLiteral" |
    "TaggedTemplateExpression" |
    "TemplateElement" |
    "ObjectPattern" |
    "ArrayPattern" |
    "RestElement" |
    "AssignmentPattern" |
    "ClassBody" |
    "MethodDefinition" |
    "ClassDeclaration" |
    "ClassExpression" |
    "MetaProperty" |
    "ImportDeclaration" |
    "ImportExpression" |
    "ImportSpecifier" |
    "ImportDefaultSpecifier" |
    "ImportNamespaceSpecifier" |
    "ExportNamedDeclaration" |
    "ExportSpecifier" |
    "ExportDefaultDeclaration" |
    "ExportAllDeclaration" |
    "AwaitExpression" |
    "ChainExpression" |
    "BigIntLiteral";

Others

Those implementation are experimental and may change in future (they are not related to the ESTree spec in any way). Please also feel free to feedback or PR new things!

License

MIT