28 lines
727 B
JavaScript
28 lines
727 B
JavaScript
//@Author : dingjiawen
|
||
//@Date : 2024/03/25 15:26
|
||
//@Usage :
|
||
//@Desc :使用AST技术,还原code1.js
|
||
|
||
|
||
import {traverse,types} from "@babel/core";
|
||
import {parse} from "@babel/parser";
|
||
import CodeGenerator from "@babel/generator";
|
||
// import * as types from "@babel/types";
|
||
import fs from "fs";
|
||
|
||
|
||
|
||
const code =fs.readFileSync("code/code1.js","utf-8");
|
||
let ast =parse(code)
|
||
|
||
traverse(ast,{
|
||
"UnaryExpression|BinaryExpression|ConditionalExpression|CallExpression":(path) =>{
|
||
const {confident,value} = path.evaluate();
|
||
if(value==Infinity ||value==-Infinity) return;
|
||
confident&&path.replaceWith(types.valueToNode(value));
|
||
},
|
||
});
|
||
|
||
const {code:output} = CodeGenerator.default(ast);
|
||
console.log(output)
|