如何将ast节点转换为其代表的底层javascript的字符串

时间:2017-09-03 19:41:44

标签: javascript jscodeshift

如何将节点转换为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 个答案:

答案 0 :(得分:3)

可以推测搜索的内容是非空的集合:

class Offers(models.Model):
    business = models.ForeignKey(Business, related_name="business_offer",
                                 on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    subtext = models.CharField(max_length=255)