0. Preface
JavaScript parser — Acorn and AST are useful things. They help us automatically and efficiently edit source code. This post shows you how to build and edit the AST of JavaScript code.
1. Content
- Install the JavaScript Parser — Acorn
- Use Acorn to Create an Abstract Syntax Tree
- Understand the Structure of an Abstract Syntax Tree
- Use Node.js to Traverse an Abstract Syntax Tree
- Summary
2. Install the JavaScript Parser — Acorn
To quote from the github repository, Acorn is a tiny, fast JavaScript parser, written completely in JavaScript, released under a MIT license. Acorn can generate abstract syntax trees for JavaScript codes. It has 3 modules: the main JavaScript parser named “acorn”, the error-tolerant parser named “acorn-loose”, the syntax tree walker named “acorn-walk”. This post focuses on the main parser. In this section, we introduce its installation.