我一直在努力将新对象添加到对象数组中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 浏览器上玩它
我有这个 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) 我想编写一个模板函数来在 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)
干杯詹斯
我一直在编写一些转换来帮助我重构代码库。我所做的基本上是向某些对象表达式添加新属性。为什么要从jscodeshift/recast这些对象中删除尾随逗号?我怎样才能防止这种情况发生?
我正在使用 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) 好的,我的代码如下所示:
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 实现吗?
我想找到此脚本中没有任何父级的调用表达式:
1 + 1
function parent() {
2 + 2
}
3 + 3
Run Code Online (Sandbox Code Playgroud)
这里我想获取1 + 1和3 + 3节点,但不是2 + 2。
我想要实现的目标是:
j(file.source).find(j.ExpressionStatement, {
parent: null
});
Run Code Online (Sandbox Code Playgroud)
那么是否有一个过滤器可以查找表达式是否有父级?
如何将节点转换为 JavaScript?
// 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)