const fs = require('fs');
const {parse} = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const t = require("@babel/types");
const generator = require("@babel/generator").default;
let jscode = `var s = 92;
b = Z(1324801, 92);`;
let ast = parse(jscode);
const visitor = {
VariableDeclarator(path)
{//还原var、let、const 定义的变量
const {id,init} = path.node;
if (!t.isLiteral(init)) return;//只处理字面量
const binding = path.scope.getBinding(id.name);
if (!binding || binding.constantViolations.length > 0)
{//如果该变量的值被修改则不能处理
return;
}
for (const refer_path of binding.referencePaths)
{
refer_path.replaceWith(init);
}
path.remove();
},
}
traverse(ast, visitor);
let {code} = generator(ast);
console.log(code);
//--------------------
b = Z(1324801, 92);