-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbasic4.js
More file actions
23 lines (22 loc) · 529 Bytes
/
basic4.js
File metadata and controls
23 lines (22 loc) · 529 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import traverse from "@babel/traverse";
import { parse } from "@babel/parser";
import generate from "@babel/generator";
import fs from "fs";
const code = fs.readFileSync("codes/code1.js", "utf-8");
let ast = parse(code);
traverse(ast, {
NumericLiteral(path) {
if (path.node.value === 3) {
path.node.value = 5;
}
},
StringLiteral(path) {
if (path.node.value === "hello") {
path.node.value = "hi";
}
},
});
const { code: output } = generate(ast, {
retainLines: true,
});
console.log(output);