Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Large JS files not fully traversed #37

Open
keheliya opened this issue Dec 15, 2014 · 1 comment
Open

Large JS files not fully traversed #37

keheliya opened this issue Dec 15, 2014 · 1 comment

Comments

@keheliya
Copy link

I'm trying to traverse the AST of a JS file with 32482 LOC. I can see that lines after line 27860 has not been traversed. I can see the FunctionExpression node at that line has been entered but not left. Is this a known limitation in estraverse? Is there a way around this? Here is the file I'm trying to analyze.

@Constellation
Copy link
Member

Thank you for your report.

Is this a known limitation in estraverse? Is there a way around this? Here is the file I'm trying to analyze.

No. There's no limitation in estraverse.

To reproduce your report, I've created the file,

var estraverse = require('../');  // Now I'm in estraverse/tmp directory
var esprima = require('esprima');
var fs = require('fs');

var contents = fs.readFileSync('gistfile1.js', 'utf-8');
var ast = esprima.parse(contents, { loc: true });
console.log(ast);

estraverse.traverse(ast, {
    enter: function (node) {
        console.log('enter line:(' + node.loc.start.line + ')');
    },
    leave: function (node) {
        console.log('leave line:(' + node.loc.start.line + ')');
    }
});

And got the result, https://gist.github.com/Constellation/3994f10c4ab597ec9e66
Seeing the result,

...
leave line:(27860)
enter line:(27860)
enter line:(27861)
enter line:(27861)
enter line:(27861)
leave line:(27861)
enter line:(27861)
enter line:(27861)
enter line:(27861)
leave line:(27861)
enter line:(27861)
leave line:(27861)
leave line:(27861)
enter line:(27861)
...

estraverse correctly traverses the file. So I cannot reproduce that.
Could you show me the actual traversing code?

I guess that you may return some node object from enter function. When using estraverse.replace, the target node will be replaced with the returned node.
So when using estraverse in some AltJS langs, you need to care about enter and leave function's return values. For example, while ECMAScript requires explicit return statement, CoffeeScript returns the last evaluated expression implicitly. Sometimes it causes trouble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants