self_example/Spider/Chapter11_JavaScript逆向/apply-ast/basic1.js

28 lines
727 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//@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)