我一直在努力将新对象添加到具有jscodeshift
. 我的问题是我无法弄清楚在获得VariableDeclarator
. 我需要在数组中获取最后一个元素,然后才能插入一个新节点。这是代码:
export default function transformer(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
const changeList = root.find(j.VariableDeclarator, {
id: {name: 'list'},
}).closest(j.ArrayExpression, {
elements: [""]
}).replaceWith(p => {
console.log(p);
}).toSource();
};