estools / estraverse

ECMAScript JS AST traversal functions
BSD 2-Clause "Simplified" License
943 stars 131 forks source link

Large JS files not fully traversed #37

Open keheliya opened 9 years ago

keheliya commented 9 years ago

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 commented 9 years ago

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.