Slide 1

Slide 1 text

Hiking Through the JavaScript Forest

Slide 2

Slide 2 text

An AST makes it easier to inspect and manipulate your code with confidence.

Slide 3

Slide 3 text

An abstract syntax tree is basically a DOM for your code.

Slide 4

Slide 4 text

Syntax Highlighting Code Completion Static Analysis Code Coverage • Minification • JIT Compilation in the Browser • Source Maps • Transpilers (CoffeeScript, Babel) Interesting Things

Slide 5

Slide 5 text

Getting Started with ASTs Parser 
 
 Tree Code

Slide 6

Slide 6 text

var seattle = rain + trees;

Slide 7

Slide 7 text

var acorn = require(“acorn”) acorn.parse(“var seattle = rain + trees”) Could also be esprima, espree, babylon, etc.

Slide 8

Slide 8 text

Program Variable Declaration Variable Declarator Binary Expression

Slide 9

Slide 9 text

Program Variable Declaration Variable Declarator Binary Expression var seattle rain + trees

Slide 10

Slide 10 text

{ "type": "Program", "body": [{ "type": “VariableDeclaration", "kind": "var" "declarations": [{ "type": "VariableDeclarator", "id": { "type": “Identifier", "name": "seattle" }, "init": { "type": "BinaryExpression", "left": { "type": “Identifier", "name": “rain" }, "operator": "+", "right": { "type": “Identifier", "name": “trees" } } }] }] }

Slide 11

Slide 11 text

{ "type": "Program", "body": [{ "type": “VariableDeclaration", "kind": "var" "declarations": [{ "type": "VariableDeclarator", "id": { "type": “Identifier", "name": "seattle" }, "init": { "type": "BinaryExpression", "left": { "type": “Identifier", "name": “rain" }, "operator": "+", "right": { "type": “Identifier", "name": “trees" } } }] }] }

Slide 12

Slide 12 text

{ "type": "Program", "body": [{ "type": “VariableDeclaration", "kind": "var" "declarations": [{ "type": "VariableDeclarator", "id": { "type": “Identifier", "name": "seattle" }, "init": { "type": "BinaryExpression", "left": { "type": “Identifier", "name": “rain" }, "operator": "+", "right": { "type": “Identifier", "name": “trees" } } }] }] }

Slide 13

Slide 13 text

Fun With Trees Static Analysis Codemods Transpilers

Slide 14

Slide 14 text

Tree Wrangling 1. Use ESLint to perform static analysis 2. Use jscodeshift for one-time code migration (codemods) 3. Use Babel for build-time transformation

Slide 15

Slide 15 text

Types of AST Tools Most use something called the visitor pattern Some tools prefer a “jQuery-like” API

Slide 16

Slide 16 text

Visitor Pattern You’re climbing the tree, but you stop and visit certain parts that seem really nice.

Slide 17

Slide 17 text

{ "type": "Program", "body": [{ "type": “VariableDeclaration", "kind": "var" "declarations": [{ "type": "VariableDeclarator", "id": { "type": “Identifier", "name": "seattle" }, "init": { "type": "BinaryExpression", "left": { "type": “Identifier", "name": “rain" }, "operator": "+", "right": { "type": “Identifier", "name": “trees" } } }] }] }

Slide 18

Slide 18 text

Visitor Pattern {
 “VariableDeclaration”: function(node) {
 console.log(node);
 }
 }

Slide 19

Slide 19 text

{ "type": “VariableDeclaration", "kind": "var" “declarations": [ … ] } One Branch of the Tree

Slide 20

Slide 20 text

Static Analysis

Slide 21

Slide 21 text

It’s not just about formatting.

Slide 22

Slide 22 text

Fix a bug. Add a unit test. Fix a similar bug…

Slide 23

Slide 23 text

Fix a bug. Write an ESLint Rule. Prevent that entire class of bugs from entering your codebase ever again.

Slide 24

Slide 24 text

var seattle = rain + trees;

Slide 25

Slide 25 text

const seattle = rain + trees;

Slide 26

Slide 26 text

Visitor Pattern (Revisited) {
 “VariableDeclaration”: function(node) {
 console.log(node);
 }
 }

Slide 27

Slide 27 text

{ "type": “VariableDeclaration", "kind": "var" “declarations": [ … ] } One Branch of the Tree

Slide 28

Slide 28 text

ESLint Rules! module.exports = function(context) {
 // …
 return vistor;
 }

Slide 29

Slide 29 text

ESLint Rules! {
 “VariableDeclaration”: function(node) {
 if (node.kind === “var”)
 context.report(node, “You’re var-y bad!”);
 }
 }

Slide 30

Slide 30 text

ESLint Rules! module.exports = function(context) { return {
 “VariableDeclaration”: function(node) {
 if (node.kind === “var”) {
 context.report(node, “You’re var-y bad!”);
 }
 }
 }}

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Transforms

Slide 33

Slide 33 text

Basic AST Flow Chart Parser 
 
 Tree Code

Slide 34

Slide 34 text

Reverse AST Flow Chart Generator 
 
 Tree Code

Slide 35

Slide 35 text

Reverse AST Flow Chart Generator 
 
 Tree Code

Slide 36

Slide 36 text

Program Variable Declaration Variable Declarator Binary Expression

Slide 37

Slide 37 text

Program Variable Declaration Variable Declarator Binary Expression

Slide 38

Slide 38 text

Tools like jscodeshift and babel give you an API to manipulate an AST and then convert that back into source code.

Slide 39

Slide 39 text

Build a Simple Babel Plugin

Slide 40

Slide 40 text

const seattle = rain + trees;

Slide 41

Slide 41 text

Program Variable Declaration Variable Declarator Binary Expression

Slide 42

Slide 42 text

Babel Plugins module.exports = function(babel) {
 // var visitor = …
 return { visitor: visitor };
 }

Slide 43

Slide 43 text

Babel Plugins {
 “VariableDeclaration”: function(path) {
 if (path.node.kind === “const”) {
 path.node.kind = “var”;
 }
 }
 }

Slide 44

Slide 44 text

Babel Plugins module.exports = function(babel) {
 return {
 visitor: {
 “VariableDeclaration”: function(path){
 if (path.node.kind === “const”) {
 path.node.kind = “var”;
 }
 }
 }
 }}

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

AST Explorer

Slide 47

Slide 47 text

export default function(file, api) { const j = api.jscodeshift; return j(file.source) .find(j.VariableDeclaration) .replaceWith( p => j.variableDeclaration(“const”, p.value.declarations) ) .toSource(); }; JSCodeshift Transform