有一个关于 esprima 和插入 ast 节点的问题。
我尝试生成一个 ast 节点以用新节点 (node = newNode) 替换其他节点,但它不起作用。
estraverse.traverse(tree, {
enter(node, parent) {
try {
if (node.type === "ExpressionStatement") {
if(node.expression.right.type == "FunctionExpression"){
// the id attribution can be replaced
node.expression.right.id = node.expression.left;
// but node can not be replaced
node = node.expression.right;
node.type = "FunctionDeclaration";
}
}
} catch (error) {
}
},});