admin管理员组

文章数量:1254580

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 plexity in the AST that these libraries use. I would also like to avoid printing js code into a few files and then parsing and piling them, so my question is:

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

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 plexity in the AST that these libraries use. I would also like to avoid printing js code into a few files and then parsing and piling them, so my question is:

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

Share Improve this question edited Apr 3, 2018 at 13:20 oskar132 asked Apr 3, 2018 at 12:04 oskar132oskar132 8393 gold badges13 silver badges35 bronze badges 5
  • 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 Commented Apr 3, 2018 at 12:28
  • Thanks for the feedback @Bergi . I have edited the question, hope it's clear now. – oskar132 Commented Apr 3, 2018 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 plex" babel AST and use the babel serializer or b) just serialize into JS code directly? – Bergi Commented Apr 3, 2018 at 15:43
  • 1 Yes, that is exactly what I would like to find out! @Bergi What I mean by unnecessarily plex 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 plex for good reasons. – oskar132 Commented Apr 3, 2018 at 15:52
  • I always found the format quite straightforward. Sure, if the output es 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 mon babel transformations on the JS AST (like convert to ES5), then I'd still remend to go this route, otherwise just try whether you can do it simpler yourself. – Bergi Commented Apr 3, 2018 at 18:10
Add a ment  | 

2 Answers 2

Reset to default 7

Something like this? https://github./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'.

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
},


本文标签: javascriptHow can I transform a custom AST into JS codeStack Overflow