But it’s not all roses 🌹🌹🌹…

These tools are all great to use, but writing them can be quite a painful experience. There are lots of barriers to getting started with creating your own tools. To me, the biggest obstacle is getting your head around the idea of an Abstract Syntax Tree (AST), and how you can interrogate and manipulate one.

An AST is a data structure that represents the structure of code in a programming language, without any actual syntax. It describes the ideas that make up a piece of code, without talking about the specific keywords, or specific syntactical tokens.

Y ou can read more about ASTs in this excellent article by Gabriele Petronella

A tree (but not abstract, nor related to syntax).

An example of an AST could look something like this:

An example of an AST for of some TypeScript code.

The “abstract” nature of the data structure is important, as it means that an AST doesn’t necessarily correlate to any particular programming language. It also means that you no longer need to use regular expressions or string manipulation to decipher or modify your source code! Instead, you can refer to parts of the code using the underlying concepts, whether it be the idea of a class, or the fact that a class has a name, or a constructor.

The above AST is a description of the following piece of TypeScript code:

A bit of TypeScript, showing a class called “MyClass”, and its constructor

Let’s say we want to know something about the constructor of MyClass : Does it take any arguments? Does it actually do anything? We could look at the code directly and find out the answers, but we could also find out by looking at the AST.