使用解析器从 javascript 中提取私有函数

Extract private function from javascript using parser

我正在尝试使用解析器(通过 uglify-js,但任何其他解析器也可以)以便从输入字符串中提取私有函数。

a.js

的内容
var UglifyJS = require('uglify-js');
var ast = UglifyJS.parse(require('fs').readFileSync('b.js').toString());

var stream = UglifyJS.OutputStream({});

// need to manipulate ast here to extract the `sum` function
ast.print(stream)
console.log(stream+'')

b.js

的内容
var addRandom = (function() {
    function sum(x, y) {
        return x + y
    };

    return function (input) {
        return sum(input, Math.random());
    };
})();

运行 node a.js 产量...

var fn=function(){function sum(x,y){return x+y}function addRandom(input){return sum(input,Math.random())}return{addRandom:addRandom}}();

... 但我需要在输出之前操作 ast,以提取 sum 函数。我要打印出来的是...

function sum(x,y){return x+y}

如何在输出前提取出我想要的AST树部分?

事实证明可以使用 TreeWalker 进行管理...

a.js

的内容
var UglifyJS = require('uglify-js'),
    fs = require('fs');

function showPrivates(file) {

    var privates = {};

    UglifyJS.parse(fs.readFileSync(file).toString('UTF-8')).walk(new UglifyJS.TreeWalker(function(node) {
        if (node instanceof UglifyJS.AST_Defun) {
            var stream = UglifyJS.OutputStream({});
            node.print(stream);
            privates[node.name.name] = stream.toString('UTF-8');
        }
    }));

    return privates;

}

console.log(showPrivates('b.js').sum);

b.js

的内容
var addRandom = (function() {

    function sum(x, y) {
        return x + y;
    };

    function random(x) {
        return Math.floor(Math.random()*x);
    };

    return function (input) {
        return sum(input, random(10));
    };
})();

...检查一下...

console.log(showPrivates('b.js').sum);
// function sum(x,y){return x+y}
var UglifyJS = require('uglify-js');
var code = require('fs').readFileSync('b.js').toString();
var stream = UglifyJS.OutputStream({});
var toplevel = UglifyJS.parse(code);
var walker = new UglifyJS.TreeWalker(function(node){
    if (node instanceof UglifyJS.AST_Defun) {
        node.print(stream);
    }
});

toplevel.walk(walker);
console.log(stream + '');