53 lines
1.1 KiB
JavaScript
53 lines
1.1 KiB
JavaScript
import {parse} from "@babel/parser"
|
|
import CodeGenerator from "@babel/generator"
|
|
import fs from "fs"
|
|
import {traverse} from "@babel/core";
|
|
|
|
|
|
const code = fs.readFileSync("codes/code1.js", "utf-8");
|
|
let ast = parse(code)
|
|
// traverse(ast, {
|
|
// enter(path) {
|
|
// let node = path.node
|
|
// if (node.type === "NumericLiteral" && node.value === 3) {
|
|
// node.value = 5;
|
|
// }
|
|
// if (node.type === "StringLiteral" && node.value === "hello") {
|
|
// node.value = "hi";
|
|
// }
|
|
// },
|
|
// });
|
|
|
|
// traverse(ast, {
|
|
// NumericLiteral(path) {
|
|
//
|
|
// if (path.node.value === 3) {
|
|
// path.node.value = 5;
|
|
// }
|
|
//
|
|
// },
|
|
// StringLiteral(path) {
|
|
// if (path.node.value === "hello") {
|
|
// path.node.value = "hi";
|
|
// }
|
|
// },
|
|
// });
|
|
|
|
traverse(ast, {
|
|
CallExpression(path) {
|
|
let node = path.node;
|
|
if (node.callee.object.name === "console" &&node.callee.property.name === "log") {
|
|
path.remove();
|
|
}
|
|
},
|
|
});
|
|
|
|
const {code: output} = CodeGenerator.default(ast, {
|
|
retainLinesL: true,
|
|
});
|
|
console.log(output)
|
|
|
|
|
|
|
|
|