javascriptjscodeshift

JSCodeshift declare new variable


I want to write a template function to create new Variables in JsCodeShift.

Anyone has an Idea how? Or some better documentation?

I tried the code below down, according to this.

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

But I get the error

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

Cheers Jens


Solution

  • I found out why, I forget to put the second parameter in brackets

    so this works:

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