jscodeshift throwing error: does not match type string
jscodeshift throwing error: does not match type string
我正在尝试改造这个:
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 编写了以下代码模块:
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
您的代码所做的是将 ExpressionStatement
替换为 UnaryExpression
。用 Expressions
替换 Statements
可能有点挑剔。
它还会查找 BinaryExpression
,但 BinaryExpression 没有 =
作为运算符。
您的过滤器实际上应该是 AssignmentExpression
,因为您实际上想要做的是用 UnaryExpression
替换 ExpressionStatement
内部的 AssignmentExpression
,从而将一个 Expression
替换为另一个
export default function transformer(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.AssignmentExpression, {
operator: "=",
right: {
type: "Literal",
value: null
}
})
.replaceWith(({ node: { left } }) => j.unaryExpression("delete", left))
.toSource();
}
这是一个 ASTExplorer 示例:https://astexplorer.net/#/gist/afb441a9709f82cd6fc3ba2860c98823/6f21cdb12a5a43aa1ca460aaa40d9ef63e1ef4bc
我正在尝试改造这个:
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 编写了以下代码模块:
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
您的代码所做的是将 ExpressionStatement
替换为 UnaryExpression
。用 Expressions
替换 Statements
可能有点挑剔。
它还会查找 BinaryExpression
,但 BinaryExpression 没有 =
作为运算符。
您的过滤器实际上应该是 AssignmentExpression
,因为您实际上想要做的是用 UnaryExpression
替换 ExpressionStatement
内部的 AssignmentExpression
,从而将一个 Expression
替换为另一个
export default function transformer(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.AssignmentExpression, {
operator: "=",
right: {
type: "Literal",
value: null
}
})
.replaceWith(({ node: { left } }) => j.unaryExpression("delete", left))
.toSource();
}
这是一个 ASTExplorer 示例:https://astexplorer.net/#/gist/afb441a9709f82cd6fc3ba2860c98823/6f21cdb12a5a43aa1ca460aaa40d9ef63e1ef4bc