Javascript jscodeshift更改对象文字值

Javascript jscodeshift更改对象文字值,javascript,jscodeshift,Javascript,Jscodeshift,使用jscodeshift,如何转换 //一些代码。。。 常量someObj={ x:{ 傅:3 } }; //更多的代码。。。 到 //一些代码。。。 常量someObj={ x:{ 傅:4,, 酒吧:“5” } }; //更多的代码。。。 ? 我试过了 module.exports=函数(文件、api、选项){ const j=api.jscodeshift; const root=j(file.source); 返回根 .find(j.Identifier) .filter(路径=>(

使用jscodeshift,如何转换

//一些代码。。。
常量someObj={
x:{
傅:3
}
};
//更多的代码。。。

//一些代码。。。
常量someObj={
x:{
傅:4,,
酒吧:“5”
}
};
//更多的代码。。。
?

我试过了

module.exports=函数(文件、api、选项){
const j=api.jscodeshift;
const root=j(file.source);
返回根
.find(j.Identifier)
.filter(路径=>(
path.node.name==='someObj'
))
.replaceWith(JSON.stringify({foo:4,bar:'5'}))
.toSource();
}
但我最后还是

//一些代码。。。
常量someObj={
{“foo”:4,“bar”:“5”}:{
傅:3
}
};
//更多的代码。。。

这表明
replaceWith
只需更改键而不是值。

您必须搜索
对象表达式而不是
标识符:

module.exports = function(file, api, options) {
  const j = api.jscodeshift;
  const root = j(file.source);

  j(root.find(j.ObjectExpression).at(0).get())
    .replaceWith(JSON.stringify({
      foo: 4,
      bar: '5'
    }));

  return root.toSource();
}

是否可以在我们要替换的键和值上获得循环,而不是整个对象?