1

I am using SpiderMonkey for a project and I need it for 2 tasks:

  1. Getting AST node information given a JavaScript string.
  2. Writing JavaScript from an AST node.

The first task is accomplished by means of js Reflect.parse('var a = 13;'); which returns me the AST, fine!

From AST to JavaScript

The second task is what I need. I want to instruct js (or probably the Reflect object, I suppose) to take a node:

{
  type:"Program", 
  body:[
    {
      type:"VariableDeclaration", 
      kind:"var", 
      declarations:[
        {
          type:"VariableDeclarator", 
          id:{type:"Identifier", name:"a"}, 
          init:{type:"Literal", value:13}
        }
      ]
    }
  ]
}

And write down: var a = 13;.

How can I achieve this which is sort of a reverse process to Reflect.parse? Thanks

0

1 Answer 1

1

If Spidermonkey hasn't got a built-in module to do this, you'll have to build one yourself.

All the details you need to do that are here: Compiling an AST back to source code

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.