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
JavaScript AST
Search
MURAKAMI Masahiko
December 08, 2017
Technology
1.8k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
JavaScript AST
MURAKAMI Masahiko
December 08, 2017
More Decks by MURAKAMI Masahiko
See All by MURAKAMI Masahiko
AmplifyHostingConstructからSSRフレームワークのためのホスティング設計を考察する/amplify-hosting-construct
fossamagna
1
520
AWS Blocks を触ってみた/first-tach-aws-blocks
fossamagna
2
230
AWS Amplifyをもっと便利に使うための取り組み/amplify-tools-and-contributions
fossamagna
1
180
Amazon Verified Permissions実践入門 〜Cedar活用とAppSync導入事例/Practical Introduction to Amazon Verified Permissions
fossamagna
2
440
Amplify Gen2から知るAWS CDK Toolkit Libraryの使い方/How to use the AWS CDK Toolkit Library as known from Amplify Gen2
fossamagna
1
980
AWS Amplify Gen2向けのVSCode拡張を作って公開してみた話/lts-techday-2024
fossamagna
0
72
CDKアプリとしてのAmplify Gen2 - @aws-amplify/backendのアーキテクチャにみるCDKベストプラクティス -
fossamagna
3
3.7k
Amplify Gen2の 新機能と実践的な使用例 AWS Amplify Gen 2 Festival in Japan/New features and practical use cases in Amplify Gen2
fossamagna
0
880
Amplify Gen2を 拡張してみよう JAWS-UG北陸新幹線 ( 福井開催 ) 2024-04-06/Let's extend Amplify Gen2
fossamagna
0
1.1k
Other Decks in Technology
See All in Technology
深夜のクラウド懺悔室 1:29:300 or 1:0:0
kazzpapa3
0
160
ブラウザアプリの継続的パフォーマンスモニタリング (序) / Continuous Browser Application Performance Monitoring; Act 1
moznion
0
110
コスト最適化の「めんどくさい」を AWS FinOps Agent でチョット楽にする
classmethod_kaz
0
160
PfEingのアプローチで働こう
rindrics
0
170
Azure App Service / Container Apps の組み込み認証
kuniteru
0
190
【試作】IoT x AIエージェント
happysamurai294
0
120
Bet AI Day 2026丨バクラク Autopilot、業務システムの再設計
layerx
PRO
2
1.2k
Nav2、Nav3 ... はたまた自作?〜 作って理解する Nav3 の設計意図 〜 / Nav2, Nav3 ... or Build Your Own? — Understanding Nav3's design intent by building it from scratch
yanzm
0
220
Bet AI Day 2026丨AIによって本質に戻るシステムリスク管理
layerx
PRO
0
880
Driving AI Adoption Using In-House GPUs to Serve Qwen
po3rin
2
450
いかに伝えるか 〜新卒エンジニアの教育のための、ライトノベル活用の一例
ikedon
1
190
DGX Sparkを2台使って いろいろ動かす話
sonoda_mj
1
130
Featured
See All Featured
Scaling GitHub
holman
464
140k
RailsConf 2023
tenderlove
30
1.5k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
How to build a perfect <img>
jonoalderson
1
6k
Making the Leap to Tech Lead
cromwellryan
135
10k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
680
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
400
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
440
Raft: Consensus for Rubyists
vanstee
141
7.7k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Transcript
JavaScript AST A-LT 2017-12-08 村上 雅彦 @fossamagna
村上 雅彦 a.k.a @fossamagna IT サー ビス事業部 所属 2
今日の話 JavaScript のAST の話 AST って何? AST で何ができるの? JavaScript とAST
3
AST って何? 4
Abstract Syntax Tree プログラムをパー スして意味のある情 報を木構造で表現したデー タ 5
AST をもう少し詳しく Abstract プログラムの行番号、 空白、 括弧などの情報は 取り除かれている( 存在する場合もある) Syntax プログラムの構文
Tree 木構造。 字句など木構造の枝葉として表現され る 6
var ast = "Abstract Syntax Tree"; のAST {"type": "Program", "body":
[{ "type": "VariableDeclaration", "declarations": [{ "type": "VariableDeclarator", "id": { "type": "Identifier", "name": "ast" }, "init": { "type": "Literal", "value": "Abstract Syntax Tree", "raw": "\"Abstract Syntax Tree\"" } }], "kind": "var"}] , "sourceType": "script"} 7
AST で何ができるの? コー ドを処理するツー ルが作れる Lint ツー ルやドキュメント生成、 コー ド生成も。
新しい機能やライブラリに対応するようにコー ド変換するなど。 パー サー によってはJSX, Flow なども対応可能 CoffeeScript, TypeScript などのAltJS なんかのト ランスパイラ 8
JavaScript のパー サ AST はパー サでソー スコー ドをパー スして作る。 JavaScript
の主要なパー サには以下のものがある。 babylon:Babel で使っているECMAScript パー サ esprima:estree が標準化しているJavaScript の AST フォー マットに準拠したパー サ espree:ESLint が使っているacorn ベー スのパー サ acorn:parse_js の作者がesprima に対抗して作成し たパー サ 9
JS をパー スしてAST を作ってみる(esprima) esprima でJavaScript コー ドをパー スするJavaScript var
esprima = require('esprima'); var program = 'var ast = "Abstract Syntax Tree"'; var ast = esprima.parseScript(program); console.log(ast); パー スした結果のAST オブジェクト { type: 'Program', body: [ { type: 'VariableDeclaration', declarations: [Object], kind: 'var' } ], sourceType: 'script' } 10
JavaScript のAST JavaScript のAST はJavaScript オブジェクト JavaScript オブジェクトなので自由に変更できる。 var program
= 'var ast = "Abstract Syntax Tree"'; var ast = esprima.parseScript(program); // var を const に変える! ast.body[0].kind = 'const'; { type: 'Program', body: [ { type: 'VariableDeclaration', declarations: [Object], kind: 'const' } ], sourceType: 'script' } 11
AST をJavaScript コー ドに変換 ジェネレー タを使うとAST をコー ドに変換できる JS Code
-> (Parser) -> AST -> (Generator) -> JS Code escodegen esprima などのAST からコー ドを生成する babel-generator babel のAST からコー ドを生成する 12
AST をJavaScript コー ドに変換 var をconst に変換するコー ド: var esprima
= require('esprima'); var escodegen = require('escodegen'); var program = 'var ast = "Abstract Syntax Tree"'; var ast = esprima.parseScript(program); // var を const に変える! ast.body[0].kind = 'const'; var code = escodegen.generate(ast); console.log(code); 変換したJavaScript: const ast = "Abstract Syntax Tree"'; 13
AST を利用したツー ルの紹介 Babel ESLint browserify gasify by @fossamagna 14
参考資料 JavaScript AST を始める最初の一歩 Acorn: yet another JavaScript parser 15