AST for JavaScript
developers
I am Bohdan Liashenko
and I work at awesome company @zero-plus-x
You can find me at:
@bliashenko
Abstract Syntax Tree
Wh-why?
AST
Plan for today
js2flowchart
Visualization library to
convert any JavaScript
code into beautiful
SVG flowchart
What is AST?
Lexical analysis;
Syntax analysis;
Babylon;
...
Use cases
Babel ;
JSCodeshift;
Prettier;
...
Abstract Syntax Tree
Wh-what?
...simplified example
How to get AST from
plain code?
Scratch the surface of compiler
HLL
Code
Lexical
analyzer
Syntax
analyzer
Semantic
analyzer
Intermediate
code generator
Machine independent
code optimiser
Code
generato
r
Machine dependent
code optimiser
0101
0111
AST
Lexical analyzer (scanner)
Takes a string of code and splits it into a list of
tokens
Longest Match Rule
c
c
o n s t _ a
c
o o
n
c
o
n
s
c
o
n
s
t
c
o
n
s
t
...
const keyword
... ...
Language Dictionary
Syntax analyzer (parser)
Takes a list of tokens and turns it into an AST
representation
Lexical
analyzer
Syntax
analyzer
Want to learn more about
compilers?
the-super-tiny-
compiler
Compile some lisp-like
function calls into some
C-like function calls.
https://github.com/jamiebuilds/the-super-
tiny-compiler
LangSandbox
Project to illustrate how
to build a programming
language
https://github.com/ftomassetti/LangSandbox
Can I just use a library?
AST Explorer
Choose one
https://astexplorer.net/
Babylon
Babylon is a JavaScript
parser used in Babel.
Support for JSX, Flow,
Typescript.
Use case:
Code transpiling. Babel.
“Babel is not a ‘tool for having ES6
support’. Well, it is, but it is far not
only what it is about.
JavaScript compiler, specifically
a source-to-source compiler,
often called a "transpiler".
Babel
parse transform generate
Babel plugin
… in fact, you only need to transform AST
babel-handbook
A guided handbook on
how to use Babel and how
to create plugins for
Babel
https://github.com/jamiebuilds/babel-handbook
Use case:
Code refactoring. JSCodeshift.
Clean my code in one click
jscodeshift & codemods
jscodeshift is a toolkit for running
“codemods”.
A “codemod” is a code which describes
what transformation should be made
to AST
… boring, show me some code
Some code
React updates/migrations
Replace PropTypes to prop-types
across entire app
Try yourself
https://github.com/facebook/jscod
eshift
https://github.com/reactjs/react-
codemod
Use case:
Code formatting. Prettier.
Make my code great again
IR - Intermediate representation
(also known as a Doc), describes
how AST nodes can be formatted.
Prettier
Code AST IR Code
A prettier printer
Book which explains a
magic behind code
formatting
http://homepages.inf.ed.ac.uk/wadler/pape
rs/prettier/prettier.pdf
js2flowchart
What?
Why?
Automation!
And it was fun.
https://github.com/Bogdan-Lyashenko/js-code-to-
svg-flowchart
Use case
▷ explain/document your code by flowcharts
▷ learn others code by visual understanding
▷ create flowcharts for any process simply
described by valid JS syntax
Paste code -
download SVG.
Live editor
https://bogdan-lyashenko.github.io/js-code-to-svg-flowchart/docs/live-editor/index.html
▷ From code:
▷ CLI: js2flowchart yourFile.js
▷ VS Code extension (built by @LucasBadico)
Or, you can use it:
Features
▷ Abstractions levels
▷ Presentation generator
▷ Flow tree modifiers
▷ Colour themes support
▷ etc...
Abstractions levels
Tree modifiers
FT - Flow tree
ST - ShapesTree
How it works?
Code AST FT ST SVG
Try it out
Docs &
Examples
provided
Thanks!
Any questions?
https://github.com/jamiebuilds/the-super-tiny-compiler
https://github.com/ftomassetti/LangSandbox
https://astexplorer.net/
https://github.com/jamiebuilds/babel-handbook
https://github.com/facebook/jscodeshift
https://github.com/reactjs/react-codemod
http://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf
https://github.com/Bogdan-Lyashenko/js-code-to-svg-flowchart
https://bogdan-lyashenko.github.io/js-code-to-svg-flowchart/docs/live-editor/index.html
https://marketplace.visualstudio.com/items?itemName=lucasbadico.code-flowchart
Links

AST for JavaScript developers