Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Master the Art of the AST (and Take Control of ...

Master the Art of the AST (and Take Control of Your JS!)

Think about the new tools that are taking over the Javascript ecosystem: Babel, Typescript, Rollup, ESLint, and smarter IDEs.
What do they all have in common: they all take Javascript source code as input, and some emit Javascript code as output.
This talk will be a deep dive into the basic building block all these tools share: Transforming your code into a JS Abstract Syntax Tree (AST).
You'll learn to read, traverse, and manipulate the AST so you can extend Babel by writing your own plugins, or by writing a custom ESLint rules to enforce your team's code conventions.
You'll also learn how to create super powerful "code-mods" to automatically convert thousands of legacy ES5 scripts to ES6 in seconds.

cowchimp

March 30, 2016
Tweet

More Decks by cowchimp

Other Decks in Programming

Transcript

  1. var foo = 'bar'; Program Variable Declaration Body Kind Declarations

    Variable Declarator Id Init Identifier Literal
  2. { "type": "Program", "body": [ { "type": "VariableDeclaration", "declarations": [

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

    { "type": "VariableDeclarator", "id": { "type": "Identifier", "name": "foo" }, "init": { "type": "Literal", "value": "bar" } } ], "kind": "var" } ] } var foo = 'bar';
  4. Babel jscodeshift Input ➡ Output Javascript ➡ Javascript Javascript ➡

    Javascript Plugin-based Yes Yes Building Block AST-to-AST Transform function AST-to-AST Transform function Endorsed by Facebook Facebook http://flic.kr/p/91r3Wz cb
  5. Babel jscodeshift Input ➡ Output Javascript ➡ Javascript Javascript ➡

    Javascript Plugin-based Yes Yes Building Block AST-to-AST Transform function AST-to-AST Transform function Endorsed by Facebook Facebook http://flic.kr/p/91r3Wz cb Goal Compiler "Codemod" (refactoring) Example Use ES6 ➡ ES5 ES5 ➡ ES6 Output level Lower level than source Same level as source Output readability Best-effort (boilerplate code) High Usage frequency All the time One-time