SlideShare a Scribd company logo
@thejameskyle
git.io/babel-handbook
git.io/i18n
BabelJS - James Kyle at Modern Web UI
Babel Sucks
B S
What is Babel?
A general purpose JavaScript compiler.
code()
0101010
1010100
High Level
Low Level
Static Analysis
Abstract Syntax Tree (AST)
function square(n) {
return n * n;
}
- FunctionDeclaration:
- id:
- Identifier:
- name: square
- params [1]
- Identifier
- name: n
{
type: "FunctionDeclaration",
id: {
type: "Identifier",
name: "square"
},
params: [{
type: "Identifier",
name: "n"
}],
body: {
type: "BlockStatement",
body: [{
type: "ReturnStatement",
argument: {
type: "BinaryExpression",
operator: "*",
left: {
{
type: "FunctionDeclaration",
id: {...},
params: [...],
body: {...}
}
{
type: "Identifier",
name: ...
}
{
type: "BinaryExpression",
operator: ...,
left: {...},
right: {...}
}
interface Node {
type: string;
}
{
type: "FunctionDeclaration",
id: {...},
params: [...],
body: {...}
}
Parsing
Transforming
Generating
Parsing:
1. Lexical Analysis
2. Syntactic Analysis
Lexical Analysis
n * n;
[
{ type: { ... }, value: "n" },
{ type: { ... }, value: "*" },
{ type: { ... }, value: "n" }
]
{
type: {
label: 'name',
keyword: undefined,
beforeExpr: false,
startsExpr: true,
rightAssociative: false,
isLoop: false,
isAssign: false,
prefix: false,
postfix: false,
binop: null,
updateContext: null
},
...
}
Syntactic Analysis
Parsing
Transforming
Generating
Parsing
Transformating
Generating
Traversal
{
type: "FunctionDeclaration",
id: {...},
params: [...],
body: {...}
}
{
type: "FunctionDeclaration",
id: {
type: "Identifier",
name: "square"
},
params: [...],
body: {...}
}
{
type: "FunctionDeclaration",
id: {...},
params: [{
type: "Identifier",
name: "n"
}],
body: {...}
}
{
type: "FunctionDeclaration",
id: {...},
params: [],
body: {
type: "BlockStatement",
body: [...]
}
}
{
type: "BlockStatement",
body: [{
type: "ReturnStatement",
argument: {...}
}]
}
{
type: "ReturnStatement",
argument: {
type: "BinaryExpression",
operator: "*",
left: {...},
right: {...}
}
}
{
type: "BinaryExpression",
operator: "*",
left: {...},
right: {...}
}
{
type: "BinaryExpression",
operator: "*",
left: {
type: "Identifier",
name: "n"
},
right: {...}
}
{
type: "BinaryExpression",
operator: "*",
left: {...},
right: {
type: "Identifier",
name: "n"
}
}
Visitors
const MyVisitor = {
Identifier() {
console.log("Called!");
}
};
function square(n) {
return n * n;
}
function square(n) {
return n * n;
}
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
- FunctionDeclaration
- Identifier (id)
- Identifier (params[0])
- BlockStatement (body)
- ReturnStatement (body)
- BinaryExpression (argument)
- Identifier (left)
- Identifier (right)
const MyVisitor = {
Identifier: {
enter() {
console.log("Entered!");
},
exit() {
console.log("Exited!");
}
}
};
Paths
{
type: "FunctionDeclaration",
id: {
type: "Identifier",
name: "square"
},
...
}
{
parent: {
type: "FunctionDeclaration",
id: {...},
....
},
node: {
type: "Identifier",
name: "square"
}
}
{
parent: {
type: "FunctionDeclaration",
id: {...},
....
},
node: {
type: "Identifier",
name: "square"
}
}
Paths in visitors
const MyVisitor = {
Identifier(path) {
console.log("Visiting: " + path.node.name);
}
};
Scopes
// global scope
function scopeOne() {
// scope 1
function scopeTwo() {
// scope 2
}
}
var global = "I am in the global scope";
function scopeOne() {
var one = "I am in scope one";
function scopeTwo() {
var two = "I am in scope two";
}
}
function scopeOne() {
var one = "I am in scope one";
function scopeTwo() {
one = "I updating a ref in scope one";
}
}
function scopeOne() {
var one = "I am in scope one";
function scopeTwo() {
var one = "I am creating a new `one`";
}
}
Scopes
{
path: path,
block: path.node,
parentBlock: path.parent,
parent: parentScope,
bindings: [...]
}
Bindings
function scopeOnce() {
var ref = "This is a binding";
ref; // This is a reference to a binding
function scopeTwo() {
ref; // This is a reference to a binding
}
}
{
identifier: node,
scope: scope,
path: path,
kind: 'var',
referenced: true,
references: 3,
referencePaths: [path, path, path],
constant: false,
constantViolations: [path]
}
The many modules of Babel
Babel Types
defineType("BinaryExpression", {
builder: ["operator", "left", "right"],
fields: {
operator: {
validate: assertValueType("string")
},
left: {
validate: assertNodeType("Expression")
},
right: {
validate: assertNodeType("Expression")
}
},
visitor: ["left", "right"],
aliases: ["Binary", "Expression"]
});
defineType("BinaryExpression", {
builder: ["operator", "left", "right"],
fields: {
operator: {
validate: assertValueType("string")
},
left: {
validate: assertNodeType("Expression")
},
right: {
validate: assertNodeType("Expression")
}
},
visitor: ["left", "right"],
aliases: ["Binary", "Expression"]
});
t.binaryExpression(
"*",
t.identifier("a"),
t.identifier("b")
);
{
type: "BinaryExpression",
operator: "*",
left: {
type: "Identifier",
name: "a"
},
right: {
type: "Identifier",
name: "b"
}
}
a * b
Babel Types
That's Babel.
Babel
Sucks
Babel doesn't do anything in the least
efficient way possible.
function babel(code) {
return code;
}
function babel(code) {
return code
}
const babel = code => code
Plugins!
Babel is only as good as the
ecosystem built around it
You.
Writing your first Babel Plugin.
export default function(babel) {
// plugin contents
}
export default function(babel) {
var t = babel.types;
// plugin contents
};
export default function(babel) {
var t = babel.types;
return {
visitor: {
// visitor contents
}
};
};
foo === bar;
{
type: "BinaryExpression",
operator: "===",
left: {
type: "Identifier",
name: "foo"
},
right: {
type: "Identifier",
name: "bar"
}
}
export default function(babel) {
var t = babel.types;
return {
visitor: {
// visitor contents
}
};
};
export default function(babel) {
var t = babel.types;
return {
visitor: {
BinaryExpression(path) {
// ...
}
}
};
};
export default function(babel) {
var t = babel.types;
return {
visitor: {
BinaryExpression(path) {
if (path.node.operator !== "===") {
return;
}
// ...
}
}
};
};
export default function(babel) {
var t = babel.types;
return {
visitor: {
BinaryExpression(path) {
if (path.node.operator !== "===") {
return;
}
path.node.left = t.identifier("sebmck");
}
}
};
};
sebmck === bar;
export default function(babel) {
var t = babel.types;
return {
visitor: {
BinaryExpression(path) {
if (path.node.operator !== "===") {
return;
}
path.node.left = t.identifier("sebmck");
path.node.right = t.identifier("dork");
}
}
};
};
sebmck === dork;
export default function(babel) {
var t = babel.types;
return {
visitor: {
BinaryExpression(path) {
if (path.node.operator !== "===") {
return;
}
path.node.left = t.identifier("sebmck");
path.node.right = t.identifier("dork");
}
}
};
};
Fin

More Related Content

PPTX
Introduction to PHP Lecture 1
PPTX
Basics of Java Script (JS)
PDF
Your code sucks, let's fix it! - php|tek13
PDF
From android/java to swift (3)
PDF
Javascript essentials
PDF
Your code is not a string
PDF
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
PPT
Jquery presentation
Introduction to PHP Lecture 1
Basics of Java Script (JS)
Your code sucks, let's fix it! - php|tek13
From android/java to swift (3)
Javascript essentials
Your code is not a string
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Jquery presentation

What's hot (20)

PDF
Entity api
PPTX
Java script
PDF
7 rules of simple and maintainable code
PDF
Introduction to web programming with JavaScript
PDF
Type level programming in Scala
PDF
Introduction to Clean Code
PDF
Your code sucks, let's fix it - DPC UnCon
PPT
Javascript
KEY
JavaScript Neednt Hurt - JavaBin talk
PDF
Функциональный микроскоп: линзы в C++
PPTX
Clean Code: Chapter 3 Function
PPTX
SADI in Perl - Protege Plugin Tutorial (fixed Aug 24, 2011)
PDF
JavaScript Primer
PPTX
JLIFF: Where we are, and where we're going
PPT
Php Sq Lite
PDF
[A 3]Javascript oop for xpages developers - public
PPTX
Object oriented javascript
PPTX
php string-part 2
PDF
Advanced Debugging with Xcode - Extending LLDB
PPTX
Clean code
Entity api
Java script
7 rules of simple and maintainable code
Introduction to web programming with JavaScript
Type level programming in Scala
Introduction to Clean Code
Your code sucks, let's fix it - DPC UnCon
Javascript
JavaScript Neednt Hurt - JavaBin talk
Функциональный микроскоп: линзы в C++
Clean Code: Chapter 3 Function
SADI in Perl - Protege Plugin Tutorial (fixed Aug 24, 2011)
JavaScript Primer
JLIFF: Where we are, and where we're going
Php Sq Lite
[A 3]Javascript oop for xpages developers - public
Object oriented javascript
php string-part 2
Advanced Debugging with Xcode - Extending LLDB
Clean code
Ad

Viewers also liked (15)

PDF
Quo vadis, JavaScript? Devday.pl keynote
PDF
What's New in ES6 for Web Devs
PDF
The ES6 Conundrum - All Things Open 2015
PDF
Introduction into ES6 JavaScript.
PDF
Emacscript 6
PDF
Running BabelJS on Windows (Try ES6 on Windows)
PPTX
Intro to Open Babel
PDF
From Hacker to Programmer (w/ Webpack, Babel and React)
PDF
Инструментируй это
PDF
Современный фронтенд -- как не утонуть в море хайпа?
PDF
Современный фронтенд за 30 минут.
PDF
An Overview of the React Ecosystem
PDF
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
PDF
A call to JS Developers - Let’s stop trying to impress each other and start b...
PDF
Lecture 2: ES6 / ES2015 Slide
Quo vadis, JavaScript? Devday.pl keynote
What's New in ES6 for Web Devs
The ES6 Conundrum - All Things Open 2015
Introduction into ES6 JavaScript.
Emacscript 6
Running BabelJS on Windows (Try ES6 on Windows)
Intro to Open Babel
From Hacker to Programmer (w/ Webpack, Babel and React)
Инструментируй это
Современный фронтенд -- как не утонуть в море хайпа?
Современный фронтенд за 30 минут.
An Overview of the React Ecosystem
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
A call to JS Developers - Let’s stop trying to impress each other and start b...
Lecture 2: ES6 / ES2015 Slide
Ad

Similar to BabelJS - James Kyle at Modern Web UI (20)

PDF
recap-js-and-ts.pdf
PDF
recap-ts.pdf
PDF
JavaScript Parser Infrastructure for Code Quality Analysis
PDF
js+ts fullstack typescript with react and express.pdf
PDF
fullstack typescript with react and express.pdf
PDF
javascript for modern application.pdf
PPTX
Academy PRO: ES2015
PDF
EcmaScript 6 - The future is here
PDF
JavaScript and the AST
PDF
Explaining ES6: JavaScript History and What is to Come
PDF
The SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
PDF
Adding ES6 to Your Developer Toolbox
KEY
ClojureScript Anatomy
PDF
MongoDB.local Berlin: How to add your favorite language to MongoDB Compass
PDF
DEVCON1 - BooJs
PDF
Lint, coverage, doc, autocompletion, transpilation, minification... powered b...
PPTX
Things about Functional JavaScript
PDF
JavaScript ES6
PDF
The Future of JavaScript (Ajax Exp '07)
recap-js-and-ts.pdf
recap-ts.pdf
JavaScript Parser Infrastructure for Code Quality Analysis
js+ts fullstack typescript with react and express.pdf
fullstack typescript with react and express.pdf
javascript for modern application.pdf
Academy PRO: ES2015
EcmaScript 6 - The future is here
JavaScript and the AST
Explaining ES6: JavaScript History and What is to Come
The SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
Adding ES6 to Your Developer Toolbox
ClojureScript Anatomy
MongoDB.local Berlin: How to add your favorite language to MongoDB Compass
DEVCON1 - BooJs
Lint, coverage, doc, autocompletion, transpilation, minification... powered b...
Things about Functional JavaScript
JavaScript ES6
The Future of JavaScript (Ajax Exp '07)

Recently uploaded (20)

PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Cloud computing and distributed systems.
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
NewMind AI Monthly Chronicles - July 2025
PPT
Teaching material agriculture food technology
Chapter 2 Digital Image Fundamentals.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Cloud computing and distributed systems.
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Sensors and Actuators in IoT Systems using pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Per capita expenditure prediction using model stacking based on satellite ima...
madgavkar20181017ppt McKinsey Presentation.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Electronic commerce courselecture one. Pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
20250228 LYD VKU AI Blended-Learning.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
NewMind AI Monthly Chronicles - July 2025
Teaching material agriculture food technology

BabelJS - James Kyle at Modern Web UI