【问题标题】:ERROR {...} does not match type string when adding expression with jscodeshift使用 jscodeshift 添加表达式时,错误 {...} 与类型字符串不匹配
【发布时间】:2025-12-30 06:40:04
【问题描述】:

我正在尝试 jscodeshift,但每当我尝试插入新表达式时,我都会收到以下错误

{operator: ==, left: [object Object], right: [object Object], loc: null, type: BinaryExpression, comments: null} does not match type string

这是我的小测试中的内容:

  var testBinary = j.binaryExpression("==", j.literal(2), j.literal(3))

  return j(file.source)
    .find(j.IfStatement)
    .insertBefore(testBinary)
    .toSource();

你可以在这里试一试https://astexplorer.net/#/P6euf9XIlR/1

如果我使用 j(file.source).find().replaceWith() 它没有任何问题。

我在这里做错了什么?

【问题讨论】:

标签: javascript runtime-error jscodeshift


【解决方案1】:

用途:

var testBinary = j.binaryExpression("==", j.literal(2), j.literal(3)),
  root = j(j(file.source);


    j(root.find(j.IfStatement).at(0).get())
    .insertBefore(testBinary);

    return root.toSource();

【讨论】: