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

Community
  • 1
  • 1
Andry
  • 16,172
  • 27
  • 138
  • 246

1 Answers1

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

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341