Building JavaScript Tools

Greg Franko


2/12/2014

Use arrow keys to navigate

About The Speaker



Greg Franko

JavaScript Engineer at AddThis

Open Source Developer

Author

Speaker

What Types of Tools Are We Talking About?

Static Code Analysis

What Do These Tools Typically Do?

Analyze Code

Performance, Maintainability, Validation

Generate Code For

Transpilers, Minifiers, Visualizers

Could You Provide Some Examples?

UglifyJS

Compressor/Minifier That Reduces The Size Of Your JS Code

JSHint

Helps Detect Problems In Your JS Code Before It Is Too Late

CSS Lint

Helps Detect Problems In Your CSS Code Before It Is Too Late

CoffeeScript

Language That Compiles To JavaScript

Plato

Code Complexity Visualizations

RequireJS Optimizer

Concatenates AMD Projects Into A Single File

AMDClean

Converts AMD code To Standard JavaScript

Browserify

Concatenates CommonJS Projects Into A Single File For The Browser

Have You Used A Static Code Analysis Tool?

You Probably Have

Have You Written A Static Code Analysis Tool?

You Probably Haven't

Why Not?

You Don't Know Where To Start

Let's Change That

Static Code Analysis Basics

Let's Learn

It Starts With A String Of Code


var code = 'function jqcon() {}';

That Is Converted To An Object

{
  "type": "FunctionDeclaration",
  "id": {
    "type": "Identifier",
    "name": "jqcon"
  },
  "params": [],
  "body": {
    "type": "BlockStatement",
    "body": []
  },
  "expression": false
}

This Object Is Called an Abstract Syntax Tree (AST)

Abstract Syntax Tree

Common AST Questions

Let's Generate an AST!

Esprima

Generating An AST With Esprima

// Node.js Environment
var code = 'function jqcon() {}',
  esprima = require('esprima'),
  ast = esprima.parse(code);
  

That's All There Is To Creating An AST

Awesome, It Works!


ASTs Are So Cool

Common Esprima Questions

What Now?


Estraverse

Traversing An AST With Estraverse

// Node.js Environment
var code = 'function jqcon() {}',
  esprima = require('esprima'),
  ast = esprima.parse(code),
  estraverse = require('estraverse');
  
estraverse.traverse(ast, {
  enter: function (node, parent) {},
  leave: function(node, parent) {}
});

Traversing And Updating An AST With Estraverse

estraverse.traverse(ast, {
  enter: function (node, parent) {
    if(node.type === 'Identifier' && node.name === 'jqcon') {
      // Changes the 'jqcon' function name to 'jqcon_is_awesome'
      node.name = node.name + '_is_awesome';  
    }
  },
});

You Could Also Use the Estraverse.replace() Method...

Traversing And Updating An AST With Estraverse

estraverse.replace(ast, {
  enter: function (node, parent) {
    if(node.type === 'Identifier' && node.name === 'jqcon') {
      // Changes the 'jqcon' function name to 'jqcon_is_awesome'
      return { 'type': 'Identifier', 'name': 'jqcon_is_awesome' };
    }
  }
});

Awesome, It Works!


Traversing and Updating ASTs Is Fun

Common Estraverse Questions

Let's Review

What Now?


Escodegen

Generating Code From An AST With Escodegen

// Node.js Environment
var code = 'function jqcon() {}',
  esprima = require('esprima'),
  ast = esprima.parse(code),
  escodegen = require('escodegen'),
  // Returns a string of code represented by the AST
  regenerated_code = escodegen.parse(ast);
  
View On JSBin

Amazing!


Generating Code From An AST Is Hot

Common Escodegen Questions

Let's Review

Wrap Up Questions

Thanks!


Twitter: GregFranko

Github: gfranko