如何使用Uglify.jsparsing和迭代原型方法?

我想parsing一些JavasScript代码,使用uglify js 2列出给定“类”的所有方法。在我的情况下,TreeWalker返回一个name : null的节点,并且没有任何信息可以让父母得出结论。

有谁知道一个不同的方法? 我期望像name : "Test.method_name"
到目前为止,我尝试了以下…

parsetests.js

 var UglifyJS = require("uglify-js2"); var util = require("util"); var code = require("fs").readFileSync("test.js").toString(); var toplevel = UglifyJS.parse(code); var log = function(obj, depth) { console.log(util.inspect(obj, showHidden=false, depth, colorize=true)); }; var toplevel = UglifyJS.parse(code); var walker = new UglifyJS.TreeWalker(function(node){ if (node instanceof UglifyJS.AST_Function ) { log(node, 2); } }); toplevel.walk(walker); 

test.js

 function Test(argument1) { var m = argument1 + "test"; return this; } Test.prototype.method_name = function(first_argument) { // body... return "a"; }; 

UglifyJS.TreeWalker节点:

 { end: { file: null, comments_before: [], nlb: true, endpos: 156, pos: 155, col: 0, line: 10, value: '}', type: 'punc' }, start: { file: null, comments_before: [], nlb: false, endpos: 111, pos: 103, col: 29, line: 7, value: 'function', type: 'keyword' }, body: [ { end: [Object], start: [Object], value: [Object] } ], cname: undefined, enclosed: undefined, parent_scope: undefined, uses_eval: undefined, uses_with: undefined, functions: undefined, variables: undefined, directives: undefined, uses_arguments: undefined, argnames: [ { end: [Object], start: [Object], thedef: undefined, name: 'first_argument', scope: undefined, init: undefined } ], name: null } 

我写了一个能够parsing和识别这些语法的脚本。 我通过https://github.com/sa/deep-js开源。

目前它涵盖了https://github.com/sa/deep-js/blob/3c1e52b75be197ff19a5530d011e999416e21afd/use-case-main.js中描述的大量用例,并使用https://github.com/sa/deep- js / tree / 3c1e52b75be197ff19a5530d011e999416e21afd / test 。 你可以在https://travis-ci.org/sa/deep-js/builds/58511486看到结果。 目前的代码状态是有限的。 例如,如果在像self这样的另一个variables上使用, this目前还不能解决 深层嵌套的分配和命名空间是另一个问题。 但是到目前为止,如果代码复杂度不是太高,它就是稳定的。

在你的情况下,该函数没有名称,它被分配给一个名称的属性。 你必须命名你的function如下:

 Test.prototype.method_name = function method_name(first_argument) { // body... return "a"; };