Convert Figma logo to code with AI

fkling logoastexplorer

A web tool to explore the ASTs generated by various parsers.

6,224
743
6,224
146

Top Related Projects

10,671

A small, fast, JavaScript-based JavaScript parser

43,312

🐠 Babel is a compiler for writing next generation JavaScript.

49,268

Prettier is an opinionated code formatter.

24,981

Find and fix problems in your JavaScript code.

15,011

An API documentation generator for JavaScript.

Quick Overview

AST Explorer is a web-based tool for exploring and visualizing Abstract Syntax Trees (ASTs) generated by various parsers. It supports multiple programming languages and parser combinations, allowing developers to inspect and understand the structure of their code at a deeper level.

Pros

  • Supports a wide range of programming languages and parsers
  • Provides real-time AST visualization as you type or modify code
  • Offers a user-friendly interface with side-by-side code and AST views
  • Includes features like AST node selection and code highlighting

Cons

  • May have performance issues with very large code samples
  • Limited offline functionality, as it's primarily a web-based tool
  • Some less common languages or parsers may not be supported
  • Learning curve for users unfamiliar with ASTs or parsing concepts

Getting Started

To use AST Explorer:

  1. Visit https://astexplorer.net/ in your web browser
  2. Select your desired language and parser from the top menu
  3. Enter or paste your code in the left panel
  4. The AST will be displayed in the right panel in real-time
  5. Click on AST nodes to highlight corresponding code, or hover over code to highlight AST nodes

For local development or contributing to the project:

git clone https://github.com/fkling/astexplorer.git
cd astexplorer
npm install
npm start

This will start a local development server, typically accessible at http://localhost:8080.

Competitor Comparisons

10,671

A small, fast, JavaScript-based JavaScript parser

Pros of Acorn

  • Lightweight and fast JavaScript parser
  • Extensive documentation and well-maintained codebase
  • Supports ECMAScript 2020 and JSX

Cons of Acorn

  • Limited to parsing JavaScript and JSX only
  • Requires additional plugins for advanced features
  • Less interactive for exploring and visualizing ASTs

Code Comparison

Acorn:

import * as acorn from "acorn";
const ast = acorn.parse("const x = 42;", {ecmaVersion: 2020});
console.log(ast);

AST Explorer:

import React from 'react';
import ASTOutput from './ASTOutput';

const App = () => (
  <ASTOutput code="const x = 42;" parser="acorn" />
);

Summary

Acorn is a focused JavaScript parser library, while AST Explorer is a web-based tool for exploring and visualizing Abstract Syntax Trees (ASTs) for multiple languages and parsers. Acorn excels in performance and is suitable for integration into other projects, whereas AST Explorer provides a more interactive and educational experience for understanding ASTs across various languages and parser implementations.

AST Explorer includes Acorn as one of its supported parsers, allowing users to explore JavaScript ASTs using Acorn within its interface. This combination leverages the strengths of both projects, providing a powerful tool for developers to understand and work with ASTs.

43,312

🐠 Babel is a compiler for writing next generation JavaScript.

Pros of Babel

  • Comprehensive JavaScript compiler with extensive plugin ecosystem
  • Actively maintained with frequent updates and wide community support
  • Provides full-featured tooling for modern JavaScript development

Cons of Babel

  • Steeper learning curve due to its complexity and extensive configuration options
  • Larger project size and potentially slower build times for complex setups

Code Comparison

Babel configuration example:

{
  "presets": ["@babel/preset-env"],
  "plugins": ["@babel/plugin-transform-runtime"]
}

AST Explorer usage example:

import {parse} from '@babel/parser';
const ast = parse('const x = 1;', {sourceType: 'module'});
console.log(JSON.stringify(ast, null, 2));

Summary

Babel is a powerful JavaScript compiler that transforms modern JavaScript code into backwards-compatible versions, while AST Explorer is a web-based tool for exploring and visualizing Abstract Syntax Trees of various languages, including JavaScript.

Babel is more suitable for production-level code transformation and build processes, offering a wide range of plugins and presets. AST Explorer, on the other hand, is primarily designed for educational purposes and quick AST visualization, making it easier to understand code structure and language parsing.

While Babel requires more setup and configuration, it provides comprehensive tooling for JavaScript development. AST Explorer offers a simpler, more immediate way to explore code structure but lacks the advanced features and transformations that Babel provides.

49,268

Prettier is an opinionated code formatter.

Pros of Prettier

  • Widely adopted code formatter with support for multiple languages
  • Integrates seamlessly with popular editors and build tools
  • Highly configurable with extensive documentation

Cons of Prettier

  • Limited to code formatting, lacks AST exploration capabilities
  • May require additional setup for complex projects or custom configurations
  • Less suitable for educational purposes or understanding code structure

Code Comparison

Prettier (formatting JavaScript):

function example(a,b){
return a+b;}

becomes:

function example(a, b) {
  return a + b;
}

AST Explorer (exploring AST):

// Input code
function example(a, b) {
  return a + b;
}

// AST output (simplified)
{
  "type": "FunctionDeclaration",
  "id": { "type": "Identifier", "name": "example" },
  "params": [
    { "type": "Identifier", "name": "a" },
    { "type": "Identifier", "name": "b" }
  ],
  "body": {
    "type": "BlockStatement",
    "body": [
      {
        "type": "ReturnStatement",
        "argument": {
          "type": "BinaryExpression",
          "operator": "+",
          "left": { "type": "Identifier", "name": "a" },
          "right": { "type": "Identifier", "name": "b" }
        }
      }
    ]
  }
}

Summary

Prettier focuses on code formatting and style consistency, while AST Explorer is designed for exploring and understanding abstract syntax trees. Prettier is more practical for day-to-day development, while AST Explorer is better suited for educational purposes and deep code analysis.

24,981

Find and fix problems in your JavaScript code.

Pros of ESLint

  • Comprehensive linting tool for JavaScript and TypeScript
  • Highly configurable with a wide range of built-in rules
  • Large ecosystem of plugins and custom rules

Cons of ESLint

  • Steeper learning curve for configuration and custom rule creation
  • Primarily focused on linting, not AST exploration or visualization

Code Comparison

ESLint rule definition:

module.exports = {
    create: function(context) {
        return {
            Identifier: function(node) {
                if (node.name === 'foo') {
                    context.report(node, "Don't use 'foo' as an identifier.");
                }
            }
        };
    }
};

ASTExplorer usage:

// No direct code comparison available, as ASTExplorer is a web-based tool
// for exploring and visualizing Abstract Syntax Trees (ASTs)

Summary

ESLint is a powerful linting tool for JavaScript and TypeScript, offering extensive configuration options and a large ecosystem of plugins. It excels in enforcing coding standards and identifying potential errors. However, it has a steeper learning curve and focuses primarily on linting rather than AST exploration.

ASTExplorer, on the other hand, is a web-based tool designed for exploring and visualizing Abstract Syntax Trees. It provides an interactive interface for understanding code structure and is particularly useful for developers working on parsers, compilers, or static analysis tools.

While both tools work with ASTs, they serve different purposes: ESLint for code quality enforcement, and ASTExplorer for AST visualization and exploration.

15,011

An API documentation generator for JavaScript.

Pros of JSDoc

  • Comprehensive documentation generation tool for JavaScript
  • Widely adopted and supported by the community
  • Integrates well with various IDEs and code editors

Cons of JSDoc

  • Limited to JavaScript documentation only
  • Requires specific comment syntax, which can be verbose
  • Less interactive compared to AST Explorer

Code Comparison

JSDoc example:

/**
 * Calculates the sum of two numbers.
 * @param {number} a - The first number.
 * @param {number} b - The second number.
 * @returns {number} The sum of a and b.
 */
function sum(a, b) {
  return a + b;
}

AST Explorer doesn't have a specific code syntax but allows interactive exploration of Abstract Syntax Trees for various languages and parsers.

Key Differences

  • Purpose: JSDoc focuses on documentation generation, while AST Explorer is a tool for exploring and understanding code structure
  • Language support: JSDoc is specific to JavaScript, whereas AST Explorer supports multiple languages and parsers
  • Interactivity: AST Explorer provides a more interactive and visual experience for code analysis

Use Cases

  • Use JSDoc for generating comprehensive documentation for JavaScript projects
  • Use AST Explorer for understanding code structure, debugging parsers, or developing tools that work with Abstract Syntax Trees

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

AST explorer

Gitpod ready-to-code Join the chat at https://gitter.im/astexplorer/Lobby Build Status

Paste or drop code into the editor and inspect the generated AST on https://astexplorer.net/

The AST explorer provides following code parsers:

Experimental / custom syntax

Depending on the parser settings, it not only supports ES5/CSS3 but also

Transforms

Since future syntax is supported, the AST explorer is a useful tool for developers who want to create AST transforms. In fact, following transformers are included so you can prototype your own plugins:

More Features

  • Save and fork code snippets. Copy the URL to share them.
  • Copying an AST or dropping a file containing an AST into the window will parse the AST and update the code using escodegen.
  • Otherwise, the content of text editor will be replaced with the content of the file (i.e. you can drag and drop JS files).
  • Choose between multiple parsers and configure them.
  • shift+click on a node expands the full subtree.
  • Hovering over a node highlights the corresponding text in the source code
  • Editing the source or moving the cursor around will automatically highlight the corresponding AST node (or its ancestors of it isn't expanded)
  • You can use $node in the console to refer to the last opened/toggled AST node.

Contributions

I'm happy about any feedback, feature request or PR to make this tool as useful as possible!


How to add a new parser

  1. Go to website/.
  2. Install the new parser as dependency: yarn add theParser
  3. Copy one of the existing examples in src/parsers/{language}.
  4. Adjust the code as necessary:
  • Update metadata.
  • Load the right parser (loadParser).
  • Call the right parsing method with the right/necessary options in parse.
  • Implement the nodeToRange method (this is for highlighting).
  • Implement the getNodeName method (this is for quick look through the tree).
  • Implement opensByDefault method for auto-expansion of specific properties.
  • Define _ignoredProperties set or implement forEachProperty generator method for filtering.
  • Provide a renderSettings method if applicable.

How to add a new transformer

  1. Go to website/.
  2. Install the new transformer as dependency.
  3. Copy one of the existing examples in src/parsers/{language}/transformers.
  4. Adjust the code as necessary:
  • Update metadata and defaultParserID.
  • Load the right transformer (loadTransformer).
  • Call the transformation method in transform.
  • Change sample transformation code in codeExample.txt.

Build your own version for development

IMPORTANT: For various reasons the project still requires Node.js version 16 (see .tools-versions). If you use a tool like asdf switching versions will happen automatically.

  1. Clone the repository.
  2. Go to website/.
  3. Install all dependencies with yarn install

Run yarn run build for the final minimized version. Run yarn run watch for incremental builds.

Run yarn start to start a simple static webserver.