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

AST: Awesome Super Tool

AST: Awesome Super Tool

It’s always good to understand how things work under the hood. Not only because it allows you to understand how to properly use things or how things may fail, but also to add that knowledge to your toolkit when needing to solve a problem.

How does Babel work? And ESLint? What about Prettier? Oh, and codemods? They all share a common concept which will unlock a world of possibilities if you understand it: the AST (stands for Abstract Syntax Tree).

Let’s learn a bit about yet another acronym, in order to learn how we can use it to bend the code to our will, and also have fun experimenting.

Demo link: https://astexplorer.net/#/gist/799185cf7b15f16d5d3b151a9bc67ac0/f6592234348f404263ad5c03c10ba3098a84d706

Leonardo Garcia Crespo

April 02, 2019
Tweet

More Decks by Leonardo Garcia Crespo

Other Decks in Technology

Transcript

  1. function getFriends() { return props.user == null ? props.user :

    props.user.friends == null ? props.user.friends : props.user.friends[0] == null ? props.user.friends[0] : props.user.friends[0].friends }
  2. const name = 'Bob Hope' const person = preval` const

    [first, last] = require('./name-splitter')(${name}) module.exports = {first, last} `
  3. AST Code PARSER const hello = 'world'; { type: 'Program',

    sourceType: 'module', body: [ { type: 'VariableDeclaration', kind: 'const', declarations: [ { type: 'VariableDeclarator', id: { type: 'Identifier', name: 'hello', }, init: { type: 'StringLiteral', value: 'world', }, }, ],
  4. AST

  5. AST

  6. Use Babylon (Babel’s parser) to parse your app files and

    extract i18n messages into another file to send to a translation service.
  7. https://kentcdodds.com/talks/#writing-custom-babel-and-eslint- plugins-with-asts https://blog.kentcdodds.com/how-writing-custom-babel-and-eslint- plugins-can-increase-your-productivity-and-improve-user- fd6dd8076e26 https://kentcdodds.com/workshops/#code-transformation-and-linting Kent C. Dodds James

    Kyle https://github.com/thejameskyle/babel-handbook https://github.com/thejameskyle/the-super-tiny-compiler Henry Zhu http://henryzoo.com/babel-plugin-slides/assets/player/ KeynoteDHTMLPlayer.html#0