JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 184
  • Score
    100M100P100Q78380F
  • License MIT

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

Package Exports

  • node-estree

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-estree) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Node.js ESTree (AST) generator

ESTREE Spec compliant AST Generator crafted for Node.js. This project is still experimental and doesn't implement the whole estree spec (doing my best).

Feel free to Pull-request or open issues

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 the JavaScript code with the AST Tree.

"use strict";

const astring = require("astring");
const {
    Expression: { CallExpression, ArrowFunctionExpression, NewExpression },
    Helpers: { CreateMemberExpr },
    Identifier, Literal, Program, VariableDeclaration
} = require("node-estree");

const log = (message) => CallExpression(CreateMemberExpr("console", "log"), [new Literal(message)]);
const createRequire = (name) => CallExpression(CreateMemberExpr("require"), [new Literal(name)]);
const setImmediate = (body) => CallExpression(CreateMemberExpr("setImmediate"), [ArrowFunctionExpression(body)]);

const AST = new Program();
AST.add(VariableDeclaration.createOne("const", "EventEmitter", createRequire("events")));
AST.add(VariableDeclaration.createOne("const", "ee", NewExpression(new Identifier("EventEmitter"))));
AST.add(setImmediate([
    CallExpression(CreateMemberExpr("ee", "emit"), [new Literal("start")])
]));
{
    const body = [log("hello world!")];
    const args = [new Literal("start"), ArrowFunctionExpression(body)];
    AST.add(CallExpression(CreateMemberExpr("ee", "on"), args));
}

console.log(JSON.stringify(AST.body, null, 4));
console.log(astring.generate(AST.toJSON()));

it will produce the following JavaScript code:

const EventEmitter = require("events");
const ee = new EventEmitter();
setImmediate(() => {
  ee.emit("start")
})
ee.on("start", () => {
  console.log("hello world!")
})

API

Expression

FunctionDeclaration

Helpers

Identifier

Literal

Identifier

Modules

Program

Property

Statements

SwitchStatement

Template

VariableDeclaration

VariableDeclarator

Roadmap

  • Implement BigIntLiteral
  • Implement Classes
  • Enhance function (arrow, generator, async etc) declaration and made it simpler.

License

MIT