How to convert an ast-node to a string of the underlying javascript its representing

1.1k views Asked by At

How can I convert the node to javascript?

https://astexplorer.net/#/gist/cf11a829035dd865a3fbf6744aa4b146/50e921c2b4bea27c5d1b214acae3c5ef11a2f1af

// target file
function execute() { 
  var a = 'a'
}

// jscodeshift
export default(file, api) => {
  const j = api.jscodeshift
  const root = j(file.source)

  var node = root.find(j.VariableDeclaration)
  // i want to see what node looks like in javascript.

  return root.toSource()
}

1

There are 1 answers

2
bluehipy On BEST ANSWER

Presumable there is a non empty collection of what you search for:

var node = root.find(j.VariableDeclaration).at(0).get();
// in js node looks like
return j(node).toSource();