JSCodeshift 声明新变量

Jen*_*che 3 javascript jscodeshift

我想编写一个模板函数来在 JsCodeShift 中创建新变量。

有人有想法吗?或者一些更好的文档?

我根据this尝试了下面的代码。

 const j = api.jscodeshift;
 let test = j.variableDeclaration('let',
    j.variableDeclarator(
      j.identifier('test'),
      null
    )
  );
Run Code Online (Sandbox Code Playgroud)

但我得到了错误

Error: {id: [object Object], init: null, loc: null, type: VariableDeclarator, 
comments: null} does not match field "declarations": [VariableDeclarator | 
Identifier] of type VariableDeclaration
Run Code Online (Sandbox Code Playgroud)

干杯詹斯

Jen*_*che 5

我找到原因了,我忘记把第二个参数放在括号里

所以这有效:

const j = api.jscodeshift;
let test = j.variableDeclaration('let',
   [j.variableDeclarator(
     j.identifier('test'),
     null
   )]
 );
Run Code Online (Sandbox Code Playgroud)