Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Let’s try to hack AST of JavaScript
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
sota ohara
July 06, 2019
1.9k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Let’s try to hack AST of JavaScript
sota ohara
July 06, 2019
More Decks by sota ohara
See All by sota ohara
Re-Architecture of ReactNative
sottar
0
300
Implement prerendering w/ puppeteer
sottar
1
340
create own CMS from scratch
sottar
2
9.1k
フロントエンドエンジニアが伝えたい最近の事情
sottar
27
18k
複数人で高速に開発するためのnpmモジュール
sottar
5
2.7k
new version of context in React 16.3
sottar
3
1.9k
2ヶ月半でサービスをリリースした話し
sottar
2
2.3k
Redux のディレクトリ構成を考える
sottar
2
15k
モダンなJavaScriptアプリケーションでモダンにスタイリングする方法
sottar
9
5.1k
Featured
See All Featured
Statistics for Hackers
jakevdp
799
230k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
200
Java REST API Framework Comparison - PWX 2021
mraible
34
9.4k
Automating Front-end Workflow
addyosmani
1370
210k
How to build a perfect <img>
jonoalderson
1
5.7k
First, design no harm
axbom
PRO
2
1.2k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
200
Building AI with AI
inesmontani
PRO
1
1.1k
Paper Plane (Part 1)
katiecoart
PRO
0
9k
Discover your Explorer Soul
emna__ayadi
2
1.1k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
Transcript
1 Let’s try to hack AST of JavaScript Sota Ohara
2019.7.4 Battle Conference U30
2 Tech Lead @ Mercari inc. Twitter: @sottar_ GitHub: @sottar
Sota Ohara
3 - Introduce the AST - Create babel plugin Index
4 What’s an AST
5 Abstract Syntax Tree (抽象構文木)
6 What’s an AST An AST is the result of
parsing source code. A tree data structure which extracted necessary information from the source code DOM Tree is one of them Used for analysis and processing the source code It’s using by the compiler and interpreter
7 It seems difficult...
8 JavaScript AST
9 JavaScript AST is not difficult We can use AST
easily in JavaScript What’s a JavaScript AST?
10 Just a JavaScript Object which displays an architecture of
code What’s a JavaScript AST?
11 ESTree de facto standard Parser Acorn Esprima Babylon: used
by Babel, Acorn based Espree: used by ESLint, Acorn based What’s a JavaScript AST?
12 AST Flow Source Code AST New AST New Source
Code parser traverser generator
13 Useful Tool AST Explorer
14 Let’s create a babel plugin
15 Create a babel plugin transform function take care the
parser, traverser and generator. $ npm i -S @babel/core const { transform } = require("babel-core"); index.js
16 Replace var with const
17 Create a babel plugin index.js const { transform }
= require("babel-core"); const src = "var foo = 'foooooo'" const plugin = ({ types }) => ({ visitor: { VariableDeclaration: nodePath => { if (nodePath.node.kind === "var") { nodePath.node.kind = "const"; } } } }); const { code } = transform(src, { plugins: [plugin] }); console.log(code); // -> const foo = 'foooooo'
18 That’s all
19 const { transform } = require("babel-core"); const src =
"var foo = 'foooooo'" const plugin = ({ types }) => ({ visitor: { VariableDeclaration: nodePath => { if (nodePath.node.kind === "var") { nodePath.node.kind = "const"; } } } }); const { code } = transform(src, { plugins: [plugin] }); console.log(code); // -> const foo = 'foooooo' Create babel plugin index.js Original source code Generated source code
20 const { transform } = require("babel-core"); const src =
"var foo = 'foooooo'" const plugin = ({ types }) => ({ visitor: { VariableDeclaration: nodePath => { if (nodePath.node.kind === "var") { nodePath.node.kind = "const"; } } } }); const { code } = transform(src, { plugins: [plugin] }); console.log(code); // -> const foo = 'foooooo' Create babel plugin index.js Call transform function with src and defined plugin
21 const { transform } = require("babel-core"); const src =
"var foo = 'foooooo'" const plugin = ({ types }) => ({ visitor: { VariableDeclaration: nodePath => { if (nodePath.node.kind === "var") { nodePath.node.kind = "const"; } } } }); const { code } = transform(src, { plugins: [plugin] }); console.log(code); // -> const foo = 'foooooo' Create babel plugin index.js Kind of visitor pattern
22 Create babel plugin index.js const { transform } =
require("babel-core"); const src = "var foo = 'foooooo'" const plugin = ({ types }) => ({ visitor: { VariableDeclaration: nodePath => { if (nodePath.node.kind === "var") { nodePath.node.kind = "const"; } } } }); const { code } = transform(src, { plugins: [plugin] }); console.log(code); // -> const foo = 'foooooo' Kind of visitor pattern This function will be fired when this label is found in the AST
23 Understand, but how to write it?
24 Useful Tools AST Explorer
25 Create babel plugin
26 Create babel plugin var foo = 'foooooo'
27 Create babel plugin var foo = 'foooooo'
28 Create babel plugin index.js const { transform } =
require("babel-core"); const src = "var foo = 'foooooo'" const plugin = ({ types }) => ({ visitor: { VariableDeclaration: nodePath => { if (nodePath.node.kind === "var") { nodePath.node.kind = "const"; } } } }); const { code } = transform(src, { plugins: [plugin] }); console.log(code); // -> const foo = 'foooooo'
29 If you have any question or feedback, please get
in touch with twitter (@sottar_) Thank you~
30 AST Explorer https://astexplorer.net/ Acorn https://github.com/acornjs/acorn Esprima https://github.com/jquery/esprima Babylon https://github.com/babel/babel/tree/master/packages/babel-parser
Espree https://github.com/eslint/espree Appendix