我正在尝试改变它:
function twist() {
this.settings = null;
delete this.settings;
this.whatever = null;
this.something['hello'] = null;
this.hello = "test";
}
进入这个:
function twist() {
delete this.settings;
delete this.settings;
delete this.whatever;
delete this.something['hello'];
this.hello = "test";
}
所以我为jscodeshift编写了以下codemod:
export default function transformer(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.ExpressionStatement, {expression: j.BinaryExpression})
.filter(p => p.value.expression.operator === "=")
.filter(p => p.value.expression.right.type === "Literal" && p.value.expression.right.raw === "null")
.filter(p => p.value.expression.left.type === "MemberExpression")
.replaceWith(path => j.unaryExpression("delete", path.value.expression.left))
//.filter(p => { console.log(p.value); return true;})
.toSource();
}
但我得到了错误:
{operator: delete, argument: [object Object], prefix: true, loc: null, type: UnaryExpression, comments: null} does not match type string