博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用estools修改javascript源代码
阅读量:6179 次
发布时间:2019-06-21

本文共 5654 字,大约阅读时间需要 18 分钟。

  hot3.png

工作中需要将javascript的if语句描述进行一些规范化,将类似

if(test) value0else value1

的语句的全部统一成:

if (test)    result = value0;else    result = value1;

 格式的,便于后续处理。

方法之一使用AST替换来完成,主要用到estools提供的javascript解析和代码生成工具。

主要操作如下

1) 安装node.js

2) mkdir convert

cd convertnpm install esprimanpm install estraversenpm install escodegen

3) 编写AST解析代码,并且查看生成的AST树

var ast = esprima.parse(code);console.log(JSON.stringify(ast,null,4));

4)编写遍历if语句的代码,如果不符合格式,就改变AST的节点。

 

 ast = estraverse.replace(ast, {        leave: function(node, parent)        {            modifiedNode = node;            if (node.type === 'IfStatement') {                if (node.consequent.type === 'ExpressionStatement')                 {                    if (node.consequent.expression.type === 'Literal'){                            modifiedNode['consequent'] = {                                        "type": "ExpressionStatement",                                        "expression": {                                            "type": "AssignmentExpression",                                            "operator": "=",                                            "left": {                                                "type": "Identifier",                                                "name": "result"                                            },                                            "right": node.consequent.expression                                        }                                    }                    };                    if (node.alternate != null && node.alternate.expression.type === 'Literal'){                            modifiedNode['alternate'] = {                                        "type": "ExpressionStatement",                                        "expression": {                                            "type":"AssignmentExpression",                                            "operator": "=",                                            "left": {                                                "type": "Identifier",                                                "name": "result"                                            },                                            "right": node.alternate.expression                                        }                                    }                    };                }            };            return modifiedNode;        }    });

5)修改好后的ast,生成源代码

var modifiedCode = escodegen.generate(ast);

6)运行

获得结果

$ node exc1.js test1.jsReading test1.js=============if (var1 > 2) result = 3;else result = 0;if(var2 < 3) 0else 1if (var3 === 9) 1;=============if (var1 > 2)    result = 3;else    result = 0;if (var2 < 3)    result = 0;else    result = 1;if (var3 === 9)    result = 1;

7) 完整代码

exc1.js-----------/* */var fs = require('fs'),    esprima = require('esprima'),    estraverse = require('estraverse'),    escodegen = require('escodegen');function analyzeCode(code) {    var ast = esprima.parse(code);    //console.log(JSON.stringify(ast,null,4));    ast = estraverse.replace(ast, {        leave: function(node, parent)        {            modifiedNode = node;            if (node.type === 'IfStatement') {                if (node.consequent != null && node.consequent.type === 'ExpressionStatement')                 {                    if (node.consequent.expression.type === 'Literal'){                            modifiedNode['consequent'] = {                                        "type": "ExpressionStatement",                                        "expression": {                                            "type": "AssignmentExpression",                                            "operator": "=",                                            "left": {                                                "type": "Identifier",                                                "name": "result"                                            },                                            "right": node.consequent.expression                                        }                                    }                    };                    if (node.alternate != null && node.alternate.expression.type === 'Literal'){                            modifiedNode['alternate'] = {                                        "type": "ExpressionStatement",                                        "expression": {                                            "type": "AssignmentExpression",                                            "operator": "=",                                            "left": {                                                "type": "Identifier",                                                "name": "result"                                            },                                            "right": node.alternate.expression                                        }                                    }                    };                }            };            return modifiedNode;        }    });    var modifiedCode = escodegen.generate(ast);    console.log('=============');    console.log(modifiedCode);    //console.log(JSON.stringify(ast,null,4));}if (process.argv.length < 3) {    console.log('Usage: zz.js file.js');    process.exit(1);}var filename = process.argv[2];console.log('Reading ' + filename);var code = fs.readFileSync(filename);console.log('=============');console.log(''+code);analyzeCode(code);console.log('Done');--------------
test1.jsif (var1 > 2) result = 3;else result = 0;if(var2 < 3) 0else 1if (var3 === 9) 1

8)

各种语言用来修改ast语法树的一些方法收集见:

http://adamrehn.com/articles/ast-instrumentation-examples-by-language#sec___javascript

转载于:https://my.oschina.net/hxapp/blog/601959

你可能感兴趣的文章
我的友情链接
查看>>
MySQL表结构的导入和导出MySQL表结构的导入和导出
查看>>
JavaSE 学习参考:Map容器遍历
查看>>
salt模块命令
查看>>
基于TBDS的flume异常问题排查过程
查看>>
2017/5 JavaScript基础7--- 数组
查看>>
网络时常断网的解决办法
查看>>
第八次作业及答案
查看>>
linux 日志定时清理脚本
查看>>
java老司机面试题
查看>>
Guice AOP
查看>>
懒汉式单例
查看>>
java递归组装树形结构
查看>>
手把手教你自己写一个模糊搜索的下拉框
查看>>
.Net文档图像处理工具包GdPicture.NET发布v14.0.30,改进PDF/OCR生成速度
查看>>
NetBSD 8.1 RC1 发布
查看>>
12个必备的JavaScript装逼技巧
查看>>
域名备案图文教程
查看>>
iOS ScrollView上的view添加悬停效果
查看>>
Spring与MQ整合简单例子
查看>>