Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Babel to output a file's AST?

Is there a way I can get Babel to output the AST of a file, as a JSON or similar, rather than condense it back into JS?

The reason is that I want to be able to do some simple static analysis / code gen, and while I aim to eventually do it within a plugin for Babel (or similar), I feel it would simplify things significantly if I can start with a static model.

like image 507
Sophistifunk Avatar asked Nov 30 '25 05:11

Sophistifunk


1 Answers

There's babylon, babel's own parser:

npm install -g babylon

babylon your_file.js > ast.json


Node API example and source: https://github.com/babel/babel/tree/master/packages/babylon

Also the babel plugin handbook might come in handy for AST reference, and to get started with plugin development.

like image 90
Rūdolfs Vikmanis Avatar answered Dec 01 '25 17:12

Rūdolfs Vikmanis