Issue while Parsing JS files to retrieve JS function names.

Skip to first unread message

sunil kaja

Feb 25, 2013, 1:58:44 AM2/25/13
I am using Rhino 1.7R4 to parse JS files to retrieve function names.

Using the following snippet to parse JS file. I have got this code from some of the posts in this group.
Reader reader = null;
try {
String file = "foo.js";
reader = new FileReader(file);
AstNode node = new Parser().parse(reader, file, 1);
node.visit(new Printer());
} finally {
class Printer implements NodeVisitor {
public boolean visit(AstNode node) {
if (node instanceof FunctionNode) {
FunctionNode function = (FunctionNode) node;
//Need this functionality to do some custom processing
System.out.println("Has children" + function.getParent().hasChildren()); // Always returning false.
return true;

My issue is why function.getParent().hasChildren() always returning false. It should return me true every time in my case. I am not able to access siblings of fucntion node as well.

Am I doing any mistake while parsing, new Parser().parse(reader, file, 1) ?? or is it a bug in Rhino??

Apr 10, 2013, 12:41:53 AM4/10/13
I use the following code, and it works

if (node instanceof FunctionNode) {
FunctionNode functionNode = (FunctionNode)node;
if (functionNode.getFunctionName() != null) {
functionName = functionNode.getFunctionName().getIdentifier();

Version: rhino1_7R4
Reply all
Reply to author
0 new messages