Master the Art of the AST

A presentation at Full Stack Fest in September 2017 in Barcelona, Spain by Yonatan Mevorach

Slide 1

Slide 1

Slide 2

Slide 2

Slide 3

Slide 3

//TODO Write linting rules to enforce your team’s code conventions ESLint ESTreebased AST Write your own javascript transpiling code Write powerful “code-mods” to automatically refactor thousands of legacy scripts from ES5 to ES6 + Visitor Pattern jscodeshift

Slide 4

Slide 4

Webpack Babel Uglifyjs Rollup ESLint Istanbul Ternjs jscodeshift

Slide 5

Slide 5

Abstract Syntax Tree cb http://flic.kr/p/gC4EkS

Slide 6

Slide 6

var foo = 'bar'; Program Body Variable Declaration Kind Declarations Variable Declarator Id Init Identifier Literal

Slide 7

Slide 7

var foo = 'bar'; { "type": "Program", "body": [ { "type": "VariableDeclaration", "declarations": [ { "type": "VariableDeclarator", "id": { "type": "Identifier", "name": "foo" }, "init": { "type": "Literal", "value": "bar" } } ], "kind": "var" } ] }

Slide 8

Slide 8

I/ { O{ Parse Traverse Manipulate Generate code cbd http://flic.kr/p/3xNFue

Slide 9

Slide 9

I/ { O{ Parse cbd http://flic.kr/p/3xNFue

Slide 10

Slide 10

astexplorer.net

Slide 11

Slide 11

I/ { Traverse O{ cbd http://flic.kr/p/3xNFue

Slide 12

Slide 12

ESLint

Slide 13

Slide 13

piggy·back (verb)

Slide 14

Slide 14

piggy·back (verb)

Slide 15

Slide 15

piggy·back (verb)

Slide 16

Slide 16

piggy·back (verb) window.Acme Acme window

Slide 17

Slide 17

This code is a simplified version of the eslint-plugin-piggyback plugin: https://github.com/cowchimp/eslint-plugin-piggyback

Slide 18

Slide 18

Slide 19

Slide 19

Slide 20

Slide 20

Slide 21

Slide 21

Slide 22

Slide 22

cb http://flic.kr/p/s8G9qy

Slide 23

Slide 23

I/ { Manipulate O{ cbd http://flic.kr/p/3xNFue

Slide 24

Slide 24

Slide 25

Slide 25

Slide 26

Slide 26

Slide 27

Slide 27

Slide 28

Slide 28

I/ { O { Generate code cbd http://flic.kr/p/3xNFue

Slide 29

Slide 29

jscodeshift

Slide 30

Slide 30

Slide 31

Slide 31

Slide 32

Slide 32

Slide 33

Slide 33

Slide 34

Slide 34

Slide 35

Slide 35

Slide 36

Slide 36

Slide 37

Slide 37

Slide 38

Slide 38

github.com/cowchimp/awesome-ast @cowchimp blog.cowchimp.com