2017-09-03 6 views
0

ノードをjavascriptに変換するにはどうすればよいですか?astノードを基になる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

おかげで、見つける '' VS find'の結果の違いは何ですが.at.get'? – user2167582

+0

'find'は' collection'を返します。(index)でそのインデックスの要素を選択し、 'get'は実際のノードを返します。 – bluehipy

関連する問題