Programmers who work with tools like ESLint and Prettier often refer to ASTs. But what is an AST, why is it useful for these kinds of tools, and how does that interact with ESLint and TypeScript tooling? Let's dig in!
What's an AST?Static analysis tools are those that look at code without running it. They typically parse code, or transform it from a string into a standard format they can reason about known as an Abstract Syntax Tree (AST). ASTs are called such because although they might contain information on the location of constructs within source code, they are an abstract representation that cares more about the semantic structure.
An Example ASTIn other words, an AST is a description of your code's syntax.
Take this single line of code:
ESLint's AST format, ESTree, would describe that line of code as an object like:
{
"type": "ExpressionStatement",
"expression": {
"type": "BinaryExpression",
"left": {
"type": "Literal",
"value": 1,
"raw": "1"
},
"operator": "+",
"right": {
"type": "Literal",
"value": 2,
"raw": "2"
}
}
}
Each piece of code described within an AST description is referred to as a node, or AST node. Each node is given a node type indicating the type of code syntax it represents That code snippet includes four nodes of the following types:
1 + 2;
1 + 2
1
2
That ESTree object representation of the code is what static analysis tools such as ESLint and Prettier work with.
AST FormatsESTree is more broadly used than just for ESLint -- it is a popular community standard. ESLint's built-in parser that outputs an ESTree-shaped AST is also a separate package, called Espree.
TypeScript has its own separate AST format, often referred to as the TypeScript AST. Because TypeScript is developed separately and with different goals from ESLint, ESTree, and Espree, its AST also represents nodes differently in many cases.
ESLint rules are by default only given nodes in the ESTree AST format - which has no knowledge of TypeScript-specific syntax such as interfaces. On the other hand, TypeScript's type checking APIs require nodes in the TypeScript AST format.
Enter TSESTreeTo resolve the incompatibilities between ESTrees and the TypeScript AST typescript-eslint provides its own @typescript-eslint/parser
package which:
By creating both an ESTree AST and a TypeScript AST, the typescript-eslint parser allows ESLint rules to work with TypeScript code. That's why the Getting Started guide for typescript-eslint has you specify parser: '@typescript-eslint/parser'
in your ESLint config!
We commonly refer to the ESTree format that also includes TypeScript-specific syntax as TSESTree.
AST PlaygroundThe typescript-eslint playground contains an AST explorer that generates an interactive AST for any code entered into the playground. You can activate it under Options > AST Explorer on its left sidebar by selecting the value of AST Viewer.
Further ResourcesYou can play more with various other ASTs on astexplorer.net, including those for other languages such as CSS and HTML.
The AST Wikipedia article has a great deal more context and history on ASTs.
GlossaryPutting together all the terms introduces in this article:
Interested in how these ASTs work with ESLint rules? We collaborated with our friends at Sourcegraph on a Tour de Source on typescript-eslint. Read on to learn how ESLint rules use ASTs to analyze code files and, thanks to @typescript-eslint/parser
, call TypeScript's type checking APIs to analyze code.
If you enjoyed this blog post and/or use typescript-eslint, please consider supporting us on Open Collective. We're a small volunteer team and could use your support to make the ESLint experience on TypeScript great. Thanks! 💖
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4