我现在发现我正在尝试replace表达,我不在乎它是什么.
在这个例子中我发现了this.state.showMenu && this.handleMouseDown部分
<a
onMouseDown={this.state.showMenu && this.handleMouseDown}
>
Run Code Online (Sandbox Code Playgroud)
我需要转换为:
<a
onMouseDown={this.state.showMenu ? this.handleMouseDown : undefined}
>
Run Code Online (Sandbox Code Playgroud)
如何在不明确重建树的情况下这样做?我只是想做点什么
path.replaceText("this.state.showMenu ? this.handleMouseDown : undefined")
Run Code Online (Sandbox Code Playgroud) 好的,我的代码如下所示:
import { wait } from "@testing-library/react";
describe("MyTest", () => {
it("should wait", async () => {
await wait(() => {
console.log("Done");
});
});
});
Run Code Online (Sandbox Code Playgroud)
我想将那个导入成员更改wait为waitFor. 我可以在 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 实现吗?