AST操作之获得当前节点的源代码

This commit is contained in:
aiyingfeng 2023-07-18 17:03:43 +08:00
parent 300f8b5d84
commit 351c00433f
4 changed files with 58 additions and 4 deletions

View File

@ -0,0 +1,33 @@
# AST操作之获得当前节点的源代码
path: path.toString()
node: generator(node).code;
```javascript
const fs = require('fs');
const {parse} = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const generator = require("@babel/generator").default;
let encode_file = "./encode.js";
let js_code = fs.readFileSync(encode_file, {encoding: "utf-8"});
let ast = parse(js_code, {
sourceType: 'module',
});
const visitor = {
VariableDeclarator(path) {
console.log(path.toString())
let {code} = generator(path.node);
console.log(code)
},
}
traverse(ast, visitor);
```
输出:
a = 123
a = 123

View File

@ -0,0 +1,21 @@
const fs = require('fs');
const {parse} = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const generator = require("@babel/generator").default;
let encode_file = "./encode.js";
let js_code = fs.readFileSync(encode_file, {encoding: "utf-8"});
let ast = parse(js_code, {
sourceType: 'module',
});
const visitor = {
VariableDeclarator(path) {
console.log(path.toString())
let {code} = generator(path.node);
console.log(code)
},
}
traverse(ast, visitor);

View File

@ -0,0 +1 @@
var a = 123;

View File

@ -194,7 +194,7 @@ CREATE TABLE `clean_buyin_authorStatData_authorProfile` (
`act_info` varchar(255) DEFAULT '', `act_info` varchar(255) DEFAULT '',
`deduplication` varchar(100) DEFAULT '' COMMENT '去重字段', `deduplication` varchar(100) DEFAULT '' COMMENT '去重字段',
`spider_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '爬虫抓取时间', `spider_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '爬虫抓取时间',
UNIQUE KEY `task_id` (`deduplication`) USING BTREE, UNIQUE KEY `account_douyin` (`account_douyin`) USING BTREE,
KEY `uid` (`uid`) USING BTREE KEY `uid` (`uid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
@ -230,8 +230,7 @@ CREATE TABLE `clean_buyin_authorStatData_seekAuthor` (
`author_tag_is_star` smallint(1) DEFAULT '0' COMMENT '是否明星', `author_tag_is_star` smallint(1) DEFAULT '0' COMMENT '是否明星',
`deduplication` varchar(100) DEFAULT '' COMMENT '去重字段', `deduplication` varchar(100) DEFAULT '' COMMENT '去重字段',
`spider_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '爬虫抓取时间', `spider_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '爬虫抓取时间',
UNIQUE KEY `task_id` (`deduplication`) USING BTREE, UNIQUE KEY `deduplication` (`deduplication`) USING BTREE
KEY `author_base_uid` (`author_base_uid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET character_set_client = @saved_cs_client */;
@ -283,4 +282,4 @@ CREATE TABLE `project_buyin_authorStatData` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2023-07-13 12:09:55 -- Dump completed on 2023-07-18 16:58:29