I 'd like to parse some JavasScript code to list all methods for a given "class" using uglify js 2. In my case the TreeWalker returns a node with name : null
and there is no information that allow conclusions to parent.
Does anyone know a different approach?
I expected something like name : "Test.method_name"
So far I tryied the folowing...
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 node:
{ 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 }