2017-09-03 33 views
0

如何将节点转换为javascript?如何将ast-node转换为底层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

推测有你的搜索内容的一个非空的集合:

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

谢谢你,是什么find' VS的'结果之间的区别'找到.at.get'? – user2167582

+0

'find'返回一个'collection',在(索引)处选择该索引处的元素,'get'返回实际节点。 – bluehipy