a tree-structured representation of the source code of a computer program that captures its essential structural and semantic content while abstracting away unnecessary syntactic detail
https://www.twilio.com/en-us/blog/developers/tutorials/building-blocks/abstract-syntax-trees
- fundamental for compilers
- Visitor pattern (ast)
use cases
- Linting:
- write custom rules based on AST nodes
- ex
- pylint
- prettier uses ASTs to reformat code
- Modifications
- Babel modifies ASTs to down-transpile newer features, or turn JSX into function calls ← what happens when compiling React
- Bundling code - understanding of structure of files makes it easier to merge and adjust imports
- ex. webpack
- test coverage
- inject code increasing diff counters for lines, functions, statements - inspect counters and provide detail on what has been executed
- optimization, updating large parts of codebase
- React team uses react-codemod to perform common operations related to updating React version
- could write script to replace all calls to alert with console.error automatically, in a more robust way than string replacement