-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
chore: added blog article: 'ASTs and TypeScript-ESLint' #6039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ff613e0
chore: added blog article: 'ASTs and TypeScript-ESLint'
JoshuaKGoldberg ca23cd5
Added netlify.toml redirect
JoshuaKGoldberg baf62c3
Merge branch 'main' into blog-asts-and-typescript-eslint
JoshuaKGoldberg 8060289
Update packages/website/blog/2022-12-01-asts-and-typescript-eslint.md
JoshuaKGoldberg 4a6a236
Merge branch 'main' into blog-asts-and-typescript-eslint
JoshuaKGoldberg 0320328
Pushed post release date to 12-05
JoshuaKGoldberg edd436e
Added more further resources
JoshuaKGoldberg 5004140
cspell
JoshuaKGoldberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/website/blog/2022-12-05-asts-and-typescript-eslint.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
--- | ||
authors: | ||
- image_url: https://www.joshuakgoldberg.com/img/josh.jpg | ||
name: Josh Goldberg | ||
title: TypeScript ESLint Maintainer | ||
url: https://github.com/JoshuaKGoldberg | ||
description: Describing what an AST (Abstract Syntax Tree) is and why it's useful for ESLint and TypeScript tooling. | ||
slug: asts-and-typescript-eslint | ||
tags: [ast, abstract syntax tree, parser, parsing, prettier] | ||
title: ASTs and TypeScript-ESLint | ||
--- | ||
|
||
Programmers who work with tools like [ESLint](https://eslint.org) and [Prettier](https://prettier.io) 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. | ||
|
||
> In other words, an AST is a description of your code's syntax. | ||
|
||
### An Example AST | ||
|
||
Take this single line of code: | ||
|
||
```js | ||
1 + 2; | ||
``` | ||
|
||
ESLint's AST format, **[ESTree]**, would describe that line of code as an object like: | ||
|
||
```json | ||
{ | ||
"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: | ||
|
||
- _ExpressionStatement_: `1 + 2;` | ||
- _BinaryExpression_: `1 + 2` | ||
- _Literal_: `1` | ||
- _Literal_: `2` | ||
|
||
That ESTree object representation of the code is what static analysis tools such as [ESLint](https://eslint.org) and [Prettier](https://prettier.io) work with. | ||
|
||
## AST Formats | ||
|
||
ESTree 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. | ||
|
||
- TS's AST is optimized for its use case of parsing incomplete code and typechecking. | ||
- ESTree is unoptimized and intended for "general purpose" use-cases of traversing the AST. | ||
|
||
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 TSESTree | ||
|
||
To resolve the incompatibilities between ESTrees and the TypeScript AST typescript-eslint provides its own [`@typescript-eslint/parser` package](https://typescript-eslint.io/architecture/Parser.mdx) which: | ||
|
||
1. First parses TypeScript syntax into a TypeScript AST | ||
1. Creates an ESTree AST based on that TypeScript AST | ||
1. Keeps track of equivalent nodes across each AST | ||
|
||
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](https://typescript-eslint.io/getting-started) 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 Playground | ||
|
||
The [TypeScript ESLint playground](https://typescript-eslint.io/play#showAST=es) 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 Resources | ||
|
||
You can play more with various other ASTs on [astexplorer.net], including those for other languages such as CSS and HTML. | ||
|
||
The [AST Wikipedia article](https://en.wikipedia.org/wiki/Abstract_syntax_tree) has a great deal more context and history on ASTs. | ||
|
||
### Glossary | ||
|
||
Putting together all the terms introduces in this article: | ||
|
||
- **AST (Abstract Syntax Tree)**: An object representation of your code's syntax. | ||
- **Espree**: ESLint's built-in parser that outputs an ESTree-shaped AST. | ||
- **ESTree**: The AST specification used by ESLint and other common JavaScript tools. | ||
- **Node Type**: What kind of code syntax an AST node refers to, such as _BinaryExpression_ or _Literal_. | ||
- **Node**: A single range of code syntax in an AST. | ||
- **Parser**: A tool that reads in a string and outputs an AST. | ||
- **TSESTree**: Our extension to the ESTree AST format that also includes TypeScript-specific syntax. | ||
|
||
### TypeScript Lint Rules and ASTs | ||
|
||
Interested in how these ASTs work with ESLint rules? | ||
We collaborated with our friends at Sourcegraph on a [Tour de Source on TypeScript ESLint](https://sourcegraph.com/notebooks/Tm90ZWJvb2s6MTA2OA==). | ||
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. | ||
|
||
[astexplorer.net]: https://astexplorer.net | ||
[espree]: https://github.com/eslint/espree | ||
[estree]: https://github.com/ESTree/ESTree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's been bugging me that the article kinda fizzled at the end... this adds in an actual conclusion.