将 var 转化为 let
2024年11月10日
一、认识
二、实现
const jscodeshift = require("jscodeshift");
function transformVarToLet(sourceCode) {
const ast = jscodeshift(sourceCode);
const { VariableDeclaration, variableDeclaration } = jscodeshift;
return ast
.find(VariableDeclaration, { kind: "var" })
.replaceWith((path) => {
return variableDeclaration("let", path.node.declarations);
})
.toSource();
}
const sourceCode = `var a = 1; var b = 2;`;
const transformedCode = transformVarToLet(sourceCode);
console.log(transformedCode);