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
Complete and compliant ESTree spec implementation in TypeScript. This project includes type definition, functions and some helpers aside.
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-estreeUsage 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)]);
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 code I recommend the use of astexplorer.
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