12

I am currently generating a custom AST from a new language specification I have designed. This custom AST contains different nodes that I have designed with all the information I need in order to now generate JavaScript code. For example:

Say I have a customExpressionNode which I wish to translate into a JavaScript function which contains a couple of if conditions.

I am currently looking into libraries like Babylon and Esprima for generating a new Javascript AST from my Custom AST, but from what I've seen there is quite a lot of complexity in the AST that these libraries use. I would also like to avoid printing js code into a few files and then parsing and compiling them, so my question is:

Is there a better way of generating programmatically a JavaScript compliant AST that I can use to generate JavaScript code?

oskar132
  • 794
  • 2
  • 12
  • 32
  • 1
    I don't understand your question. Your title asks for generating JS code from the AST you have, which should be simple with the respective Babel method. The body of your post asks about generating the AST, but your first sentence states that you already can do that with your own parser from files in your own language? – Bergi Apr 03 '18 at 12:28
  • Thanks for the feedback @Bergi . I have edited the question, hope it's clear now. – oskar132 Apr 03 '18 at 13:28
  • 1
    So you have an AST from your custom language that is not quite in the format that babel (etc) use for JS ASTs, and you wonder whether the best way is to a) translate it to a "unnecessarily complex" babel AST and use the babel serializer or b) just serialize into JS code directly? – Bergi Apr 03 '18 at 15:43
  • 1
    Yes, that is exactly what I would like to find out! @Bergi What I mean by unnecessarily complex is that it seems to contain a lot of extra information that I may not need for this specific case, however I am aware that those ASTs are complex for good reasons. – oskar132 Apr 03 '18 at 15:52
  • I always found the format quite straightforward. Sure, if the output comes from parsing a JS file there's lots of debugging stuff in it like line numbers that can be used to generate source maps, but you shouldn't need all of that when you are generating it yourself. I'd guess many of the properties are optional, just try to leave them out. If you want to use some common babel transformations on the JS AST (like convert to ES5), then I'd still recommend to go this route, otherwise just try whether you can do it simpler yourself. – Bergi Apr 03 '18 at 18:10

2 Answers2

6

Something like this? https://github.com/estools/escodegen

A simple example: the program

escodegen.generate({
    type: 'BinaryExpression',
    operator: '+',
    left: { type: 'Literal', value: 40 },
    right: { type: 'Literal', value: 2 }
})

produces the string '40 + 2'.

Dara Java
  • 2,410
  • 3
  • 27
  • 52
  • 1
    But how do you get the AST in the first place from Javascript? Put another way, I see libraries for parsing Javascript to AST, each with different syntaxes, but how do I turn them back into javascript after I have done whatever manipulations are necessary? – Michael Apr 25 '22 at 23:18
  • 1
    @Michael Use escodegen for AST -> Code and acorn for Code -> AST; do your manipulations while it's an AST – Jesus is Lord Jan 30 '23 at 01:58
  • https://github.com/acornjs/acorn – Jesus is Lord Jan 30 '23 at 01:58
4

install @babel/generator

npm install --save-dev @babel/generator
const { default: generate } = require("@babel/generator");

Identifier(path) {
  console.log(generate(path.node).code); // code string
},


januw a
  • 2,056
  • 5
  • 18
  • 39