2017-09-29 51 views
0

我想編寫一個模板函數來在JsCodeShift中創建新的變量。JSCodeshift聲明新變量

任何人有一個想法如何?還是一些更好的文檔?

根據this,我嘗試了下面的代碼。

const j = api.jscodeshift; 
let test = j.variableDeclaration('let', 
    j.variableDeclarator(
     j.identifier('test'), 
     null 
    ) 
); 

但我得到的錯誤

Error: {id: [object Object], init: null, loc: null, type: VariableDeclarator, 
comments: null} does not match field "declarations": [VariableDeclarator | 
Identifier] of type VariableDeclaration 

乾杯延

回答

0

我找到了原因,我忘了把第二個參數括號

所以此工程:

const j = api.jscodeshift; 
let test = j.variableDeclaration('let', 
    [j.variableDeclarator(
    j.identifier('test'), 
    null 
    )] 
); 
+0

https://www.toptal.com/javascrip t/write-code-to-rewrite-your-code 是最有幫助的文章 –