explaingit

acornjs/acorn

11,381JavaScriptAudience · developerComplexity · 2/5LicenseSetup · easy

TLDR

A small, fast JavaScript parser that converts source code into a structured syntax tree, a building block used inside linters, bundlers, formatters, and other developer tools.

Mindmap

mindmap
  root((repo))
    What it does
      JS code parsing
      AST generation
      Error tolerant mode
    Packages
      acorn core
      acorn-loose
      acorn-walk
    Use cases
      Build linters
      Build bundlers
      Build formatters
    Extensibility
      Plugin system
      JSX support
      Custom dialects
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Parse JavaScript source code into a syntax tree to power a custom linter or static analysis tool.

USE CASE 2

Extend the parser with plugins to handle JSX or other JavaScript syntax extensions in a toolchain.

USE CASE 3

Use acorn-loose to parse JavaScript with syntax errors and still extract useful structure in an editor integration.

USE CASE 4

Traverse a syntax tree with acorn-walk to find and transform specific code patterns across a codebase.

Tech stack

JavaScriptNode.jsnpm

Getting it running

Difficulty · easy Time to first run · 5min
Use freely for any purpose including commercial projects, as long as you keep the copyright notice.

In plain English

Acorn is a small, fast JavaScript parser written entirely in JavaScript. A parser reads source code and converts it into a structured data format (called an Abstract Syntax Tree, or AST) that describes the code's structure as a tree of nodes. This is not a tool you use directly as an end user. It is a building block used inside other developer tools. Many JavaScript tools, such as linters, code formatters, bundlers, and compilers, need to understand the structure of JavaScript code before they can do their work. Acorn provides that parsing step. It takes a string of JavaScript source code as input and returns an AST that the calling tool can then analyze or transform. The repository contains three packages. The main acorn package is the strict parser for valid JavaScript. The acorn-loose package is an error-tolerant parser that can still produce a usable output even when the input code has syntax errors. The acorn-walk package provides utilities for traversing the AST that the parser produces. Acorn supports a plugin system that allows developers to extend the parser to understand JavaScript dialects or syntax extensions. For example, community-built plugins exist for JSX (the XML-like syntax used in React) and BigInt literals. Plugins are composed by extending the base Parser class, and multiple plugins can be combined together in a single parser instance. The library is MIT-licensed and is widely used as a dependency inside other open source JavaScript tooling. It has no runtime dependencies of its own and is installed via npm.

Copy-paste prompts

Prompt 1
Using acorn, write a Node.js script that parses a JavaScript file and prints every function declaration with its line number.
Prompt 2
Show me how to combine the acorn JSX plugin with the base parser to parse a React component file into an AST.
Prompt 3
Using acorn-walk, write code to traverse an AST and collect all variable names declared with const or let.
Prompt 4
I have broken JavaScript with syntax errors. Show me how to use acorn-loose to parse it and still extract the function names.
Open on GitHub → Explain another repo

← acornjs on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.