标签: jscodeshift

创建一个 codemod 以向数组添加新元素

我一直在努力将新对象添加到对象数组中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();

};
Run Code Online (Sandbox Code Playgroud)

我正在AST 浏览器上玩它

javascript abstract-syntax-tree jscodeshift

4
推荐指数
1
解决办法
1291
查看次数

如何在 jscodeshift 中包装除导入之外的所有顶级语句和声明?

我有这个 AST 浏览器片段,它几乎可以让我做我想做的事情,那就是:

function bar() {return 42;}
var baz = {}
import 'get-outta-here';
Run Code Online (Sandbox Code Playgroud)

进入

import 'get-outta-here';

function wrap() {
  function bar() {return 42;}
  var baz = {}
}();
Run Code Online (Sandbox Code Playgroud)

即包装除那些之外的所有顶级语句和声明import。但是,我的 jscodeshift 转换存在我无法理解的错误。

  • 我可以包装并删除导入,但无法将它们移动到顶部。
  • 我可以将它们移至顶部但不能包裹

我怀疑我的包装逻辑已经关闭:root.get().value.program.body听起来很老套。

export default function transformer(file, api) {
  const j = api.jscodeshift;
  const root = j(file.source);

  const wrapper = j(j.expressionStatement(
        j.callExpression(
          j.functionExpression(
            j.identifier('foo'), [],
            j.blockStatement(root.get().value.program.body)
          ), []), []));
  
  const nodes = root.find(j.ImportDeclaration).nodes();
  root.find(j.ImportDeclaration).remove();

  // wraps, but doesn't re-add the import at top-level …
Run Code Online (Sandbox Code Playgroud)

javascript jscodeshift

4
推荐指数
1
解决办法
546
查看次数

JSCodeshift 声明新变量

我想编写一个模板函数来在 JsCodeShift 中创建新变量。

有人有想法吗?或者一些更好的文档?

我根据this尝试了下面的代码。

 const j = api.jscodeshift;
 let test = j.variableDeclaration('let',
    j.variableDeclarator(
      j.identifier('test'),
      null
    )
  );
Run Code Online (Sandbox Code Playgroud)

但我得到了错误

Error: {id: [object Object], init: null, loc: null, type: VariableDeclarator, 
comments: null} does not match field "declarations": [VariableDeclarator | 
Identifier] of type VariableDeclaration
Run Code Online (Sandbox Code Playgroud)

干杯詹斯

javascript jscodeshift

3
推荐指数
1
解决办法
1421
查看次数

如何强制 jscodeshift/recast 在对象表达式中保留尾随逗号?

我一直在编写一些转换来帮助我重构代码库。我所做的基本上是向某些对象表达式添加新属性。为什么要从jscodeshift/recast这些对象中删除尾随逗号?我怎样才能防止这种情况发生?

javascript jscodeshift

2
推荐指数
1
解决办法
912
查看次数

如何在 Jscodeshift 中正确导出 const

我正在使用 Jscodeshift 编写我的第一个 codemod。我当前的目标是导出分配了特定标识符的常量。

因此,如果我定位每个名为 的变量stuff,它将在脚本运行后被命名导出。

在:

const stuff = 4;
Run Code Online (Sandbox Code Playgroud)

出去:

export const stuff = 4;
Run Code Online (Sandbox Code Playgroud)

这是我所拥有的的精简版本。它确实有效,但看起来很脆弱,并且有很多缺点。

const constName = "stuff";

module.exports = (fileInfo, api) => {
  const j = api.jscodeshift;
  const root = j(fileInfo.source);

  const declaration = root.find(j.VariableDeclaration, {
    declarations: [
      {
        id: {
          type: "Identifier",
          name: constName
        }
      }
    ]
  });

  declaration.forEach(n => {
    n.insertBefore("export");
  });

  return root.toSource();
};

Run Code Online (Sandbox Code Playgroud)

谷草转氨酶

这将导致(注意不需要的新行)

export
const stuff = 4;
Run Code Online (Sandbox Code Playgroud)

如果将此源提供给脚本,这也会严重失败。

在:

// hey
const stuff = …
Run Code Online (Sandbox Code Playgroud)

javascript jscodeshift

2
推荐指数
1
解决办法
1002
查看次数

通过 jscodeshift 替换特定导入的所有实例

好的,我的代码如下所示:

import { wait } from "@testing-library/react";

describe("MyTest", () => {
  it("should wait", async () => {
    await wait(() => {
      console.log("Done");
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

我想将那个导入成员更改waitwaitFor. 我可以在 AST 中更改它,如下所示:

    source
      .find(j.ImportDeclaration)
      .filter((path) => path.node.source.value === "@testing-library/react")
      .find(j.ImportSpecifier)
      .filter((path) => path.node.imported.name === "wait")
      .replaceWith(j.importSpecifier(j.identifier("waitFor")))
      .toSource()
Run Code Online (Sandbox Code Playgroud)

但是,输出的代码将如下所示:

import { waitFor } from "@testing-library/react";

describe("MyTest", () => {
  it("should wait", async () => {
    await wait(() => {
      console.log("Done");
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种方法来更改该导入的所有后续用法以匹配新名称

这可以用 jscodeshift 实现吗?

javascript jscodeshift codemod

2
推荐指数
1
解决办法
2025
查看次数

如何使用jscodeshift找到没有父节点的节点?

我想找到此脚本中没有任何父级的调用表达式:

1 + 1

function parent() {
    2 + 2
}

3 + 3
Run Code Online (Sandbox Code Playgroud)

这里我想获取1 + 13 + 3节点,但不是2 + 2

我想要实现的目标是:

j(file.source).find(j.ExpressionStatement, {
    parent: null 
});
Run Code Online (Sandbox Code Playgroud)

那么是否有一个过滤器可以查找表达式是否有父级?

这是一个真实的用例。

javascript abstract-syntax-tree jscodeshift

1
推荐指数
1
解决办法
1138
查看次数

如何将 ast-node 转换为其代表的底层 javascript 字符串

如何将节点转换为 JavaScript?

https://astexplorer.net/#/gist/cf11a829035dd865a3fbf6744aa4b146/50e921c2b4bea27c5d1b214acae3c5ef11a2f1af

// target file
function execute() { 
  var a = 'a'
}

// jscodeshift
export default(file, api) => {
  const j = api.jscodeshift
  const root = j(file.source)

  var node = root.find(j.VariableDeclaration)
  // i want to see what node looks like in javascript.

  return root.toSource()
}
Run Code Online (Sandbox Code Playgroud)

javascript jscodeshift

0
推荐指数
1
解决办法
1148
查看次数