Convert Figma logo to code with AI

estree logoestree

The ESTree Spec

5,146
359
5,146
44

Top Related Projects

10,671

A small, fast, JavaScript-based JavaScript parser

43,312

🐠 Babel is a compiler for writing next generation JavaScript.

101,477

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

8,757

🗜 JavaScript parser, mangler and compressor toolkit for ES6+

49,809

Prettier is an opinionated code formatter.

Quick Overview

ESTree is a specification for representing JavaScript programs as abstract syntax trees (ASTs). It defines a standard format for ASTs, which is widely used by various JavaScript tools and libraries for parsing, analyzing, and manipulating JavaScript code.

Pros

  • Provides a standardized format for JavaScript ASTs, promoting interoperability between different tools and libraries
  • Widely adopted in the JavaScript ecosystem, with support from popular projects like Acorn, Babel, and ESLint
  • Regularly updated to keep pace with new JavaScript language features
  • Well-documented and maintained by the community

Cons

  • May require frequent updates to accommodate new JavaScript language features
  • Some edge cases or complex language constructs can be challenging to represent accurately
  • Implementations may vary slightly between different parsers, potentially causing inconsistencies
  • Learning curve for developers new to working with ASTs

Code Examples

ESTree is not a code library itself, but rather a specification. However, here are some examples of how ESTree-compatible ASTs might be used with popular parsing libraries:

  1. Parsing JavaScript code with Acorn:
const acorn = require('acorn');

const code = 'const x = 5;';
const ast = acorn.parse(code, { ecmaVersion: 2020 });
console.log(JSON.stringify(ast, null, 2));
  1. Traversing an AST with Estraverse:
const estraverse = require('estraverse');

estraverse.traverse(ast, {
  enter: function (node) {
    console.log('Entering node type:', node.type);
  },
  leave: function (node) {
    console.log('Leaving node type:', node.type);
  }
});
  1. Generating code from an AST with Escodegen:
const escodegen = require('escodegen');

const generatedCode = escodegen.generate(ast);
console.log(generatedCode);

Getting Started

To work with ESTree-compatible ASTs, you typically need to use a parser and other related tools. Here's a quick start guide using Acorn:

  1. Install Acorn:

    npm install acorn
    
  2. Parse JavaScript code:

    const acorn = require('acorn');
    
    const code = 'const x = 5;';
    const ast = acorn.parse(code, { ecmaVersion: 2020 });
    console.log(JSON.stringify(ast, null, 2));
    

This will output the ESTree-compatible AST for the given JavaScript code.

Competitor Comparisons

10,671

A small, fast, JavaScript-based JavaScript parser

Pros of Acorn

  • Acorn is a fast, lightweight JavaScript parser implementation
  • Provides a practical, usable parser with an extensive plugin ecosystem
  • Actively maintained with regular updates and improvements

Cons of Acorn

  • More complex to use compared to ESTree's specification-only approach
  • Requires additional setup and configuration for specific use cases
  • May have a steeper learning curve for beginners

Code Comparison

ESTree (specification):

interface Identifier <: Expression, Pattern {
    type: "Identifier";
    name: string;
}

Acorn (implementation):

function parseIdent(liberal) {
  var node = startNode();
  if (liberal && this.options.allowReserved == "never") liberal = false;
  node.name = this.value;
  this.next();
  return this.finishNode(node, "Identifier");
}

Summary

ESTree provides a standardized AST specification for JavaScript parsers, while Acorn is a concrete implementation of a JavaScript parser. ESTree focuses on defining the structure of the AST, whereas Acorn offers a practical tool for parsing JavaScript code. Acorn follows the ESTree specification but also includes additional features and optimizations for real-world usage.

43,312

🐠 Babel is a compiler for writing next generation JavaScript.

Pros of Babel

  • Actively maintained and widely used in production environments
  • Provides a complete toolchain for JavaScript transpilation and transformation
  • Extensive plugin ecosystem for customizing behavior

Cons of Babel

  • Larger codebase and more complex setup compared to ESTree
  • Can introduce performance overhead due to its comprehensive nature
  • May require additional configuration for specific use cases

Code Comparison

ESTree (AST specification):

{
  "type": "Program",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "Literal",
        "value": 42
      }
    }
  ]
}

Babel (AST transformation):

export default function(babel) {
  return {
    visitor: {
      Identifier(path) {
        path.node.name = path.node.name.split('').reverse().join('');
      }
    }
  };
}

Summary

ESTree focuses on defining the AST specification for JavaScript, while Babel provides a comprehensive toolchain for JavaScript transpilation and transformation. ESTree is more lightweight and specific to AST representation, whereas Babel offers a broader range of features and capabilities for working with JavaScript code. The choice between the two depends on the specific needs of the project and the level of control required over the JavaScript transformation process.

101,477

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

Pros of TypeScript

  • Comprehensive language implementation with compiler, type-checker, and tooling
  • Actively maintained by Microsoft with frequent updates and improvements
  • Large community and ecosystem with extensive documentation and resources

Cons of TypeScript

  • Larger and more complex codebase, potentially harder to contribute to
  • Focuses on TypeScript-specific features, not a general-purpose AST specification
  • May have a steeper learning curve for newcomers

Code Comparison

ESTree (JavaScript AST specification):

{
  "type": "Program",
  "body": [
    {
      "type": "VariableDeclaration",
      "declarations": [
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "x"
          },
          "init": {
            "type": "Literal",
            "value": 5
          }
        }
      ],
      "kind": "const"
    }
  ]
}

TypeScript (AST node interface):

interface VariableDeclaration extends Declaration {
    kind: "var" | "let" | "const";
    declarations: NodeArray<VariableDeclaration>;
}

interface VariableDeclarator extends Node {
    name: BindingName;
    initializer?: Expression;
}

While ESTree provides a JSON representation of the AST, TypeScript defines TypeScript interfaces for AST nodes, offering type safety and integration with the TypeScript ecosystem.

8,757

🗜 JavaScript parser, mangler and compressor toolkit for ES6+

Pros of Terser

  • Active development with frequent updates and bug fixes
  • Provides JavaScript minification and optimization functionality
  • Offers a CLI tool and API for easy integration into build processes

Cons of Terser

  • Larger codebase and more complex than ESTree
  • Focused specifically on minification, not a general-purpose AST specification
  • May have a steeper learning curve for newcomers

Code Comparison

ESTree (AST specification):

{
  "type": "Program",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "Literal",
        "value": 42
      }
    }
  ]
}

Terser (minification example):

// Input
function add(first, second) {
  return first + second;
}
// Output
function add(n,d){return n+d}

Summary

ESTree is a specification for JavaScript Abstract Syntax Trees, while Terser is a JavaScript parser, mangler, and compressor toolkit. ESTree provides a standardized way to represent JavaScript code structure, whereas Terser focuses on optimizing and minifying JavaScript code. While they serve different purposes, both are valuable tools in the JavaScript ecosystem, with ESTree being more foundational and Terser offering practical optimization capabilities.

49,809

Prettier is an opinionated code formatter.

Pros of Prettier

  • Active development with frequent updates and improvements
  • Opinionated code formatter that enforces consistent style across projects
  • Supports multiple languages and integrates with various editors and tools

Cons of Prettier

  • Less flexible than ESTree, as it enforces specific formatting rules
  • May require configuration to match existing project styles
  • Larger project scope and codebase compared to ESTree's focused approach

Code Comparison

ESTree (AST specification):

{
  "type": "Program",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "CallExpression",
        "callee": {
          "type": "Identifier",
          "name": "console"
        }
      }
    }
  ]
}

Prettier (formatted code output):

console.log("Hello, world!");

Summary

ESTree focuses on defining the Abstract Syntax Tree (AST) structure for JavaScript, while Prettier is a code formatter that enforces consistent style. ESTree provides a foundation for tools to parse and manipulate JavaScript code, whereas Prettier actively formats code based on predefined rules. Both projects serve different purposes in the JavaScript ecosystem, with ESTree being more foundational and Prettier offering practical code formatting solutions.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

The ESTree Spec

Once upon a time, an unsuspecting Mozilla engineer created an API in Firefox that exposed the SpiderMonkey engine's JavaScript parser as a JavaScript API. Said engineer documented the format it produced, and this format caught on as a lingua franca for tools that manipulate JavaScript source code.

Meanwhile JavaScript is evolving. This site will serve as a community standard for people involved in building and using these tools to help evolve this format to keep up with the evolution of the JavaScript language.

AST Descriptor Syntax

The spec uses a custom syntax to describe its structures. For example, at the time of writing, 'es2015.md' contained a description of Program as seen below

extend interface Program {
    sourceType: "script" | "module";
    body: [ Statement | ImportOrExportDeclaration ];
}

ESTree Steering Committee

Copyright and License

Copyright Mozilla Contributors and ESTree Contributors.

Licensed under Creative Commons Sharealike.

Philosophy

Suggested additions and modifications must follow these guidelines:

  1. Backwards compatible: Non-additive modifications to existing constructs will not be considered unless immense support is in favor of such changes. (eg. #65)
  2. Contextless: Nodes should not retain any information about their parent. ie. a FunctionExpression should not be aware of if it's a concise method. (eg. #5)
  3. Unique: Information should not be duplicated. ie. a kind property should not be present on Literal if the type can be discerned from the value. (eg. #61)
  4. Extensible: New nodes should be specced to easily allow future spec additions. This means expanding the coverage of node types. ie. MetaProperty over NewTarget to cover future meta properties. (eg. #32)

Acknowledgements

ESTree has benefited from the contributions of many people over the years. We'd like to thank these folks for their significant contributions to this project:

Sebastian McKenzie (Babel), Kyle Simpson (@getify), Mike Sherov (Esprima), Ariya Hidayat (Esprima), Adrian Heine (Acorn), Dave Herman (SpiderMonkey), Michael Ficarra (@michaelficarra).