ternjs / tern

A JavaScript code analyzer for deep, cross-editor language support
https://ternjs.net/
MIT License
4.25k stars 378 forks source link

Gives the capability to customize Acorn.parse #489

Open angelozerr opened 9 years ago

angelozerr commented 9 years ago

@marijnh I'm writing a tern plugin to use ESLint. This tern plugin is not performant because :

In other words, file text is parsed twice.

To improve that I would like to use Acorn AST with ESLint, but it requires that Acorn must be configurated:

var ast = Acorn.parse(text, {
 ranges: true,
 locations: true,
 onToken: function(token) {
  tokens.push(_convertToEsprima(token));
 },
 onComment: comments,
});

See https://bugs.eclipse.org/bugs/show_bug.cgi?id=432940#c17 for more information

It should be cool if tern could provide a mean (perhaps a passes like postParse?) to customize Acorn.parse

marijnh commented 9 years ago

This would be kind of scary, because it would be very easy to pass an option that breaks Tern's own expectations about the AST. When you say your current situation is 'not performant', do you mean it really is too slow, or you just consider the double parse theoretically wasteful? Doing extra work to keep things cleanly separated, when you can afford that work, is often a good idea.

angelozerr commented 9 years ago

When you say your current situation is 'not performant', do you mean it really is too slow, or you just consider the double parse theoretically wasteful?

Yes I consider the double parse theoretically wasteful (I'm afraid with memory too with big projects liek dojotoolkit). I will do tests.

But the second thing is that there is a Orion guy which works on the topic to use Acorn with ESLint to integrates Tern inside Orion (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=432940#c17)

If Orion integrates Tern, Eclipse community will accept more tern, it means more users of tern:)

marijnh commented 9 years ago

Does ESLint keep the AST around? I thought it just creates it and throws it away again after analyzing. In that case, memory should not be a big concern.

angelozerr commented 9 years ago

@mrennie could you please gives us informations about Acorn+ESLint integration. Thanks!

mrennie commented 9 years ago

The mentioned link ( https://bugs.eclipse.org/bugs/show_bug.cgi?id=432940#c17) pretty much sums up where the work is at right now - I have been focusing on our Orion 8.0 release and have not had time to work on it more.

could you please gives us informations about Acorn+ESLint integration

ESLint in Orion is not the stock version, I have customized it to accept an AST in addition to just text. This allows us to get around the parse > verify > throw away cycle Marijn mentioned and use our cached AST - more simply, it does not matter how we parse, just so long as the ASTs are compatible.

The rules used in Orion are also not the ones from the ESLint repo (this is due to an IP provenance issue), so we have written our own, making it easy to customize them as needed to adapt to Acorn.

marijnh commented 9 years ago

My advice would be to use Acorn's regular AST, and rely on the start and end properties for looking up line/column positions. There is a rather efficient, caching implementation of converting between character offsets and line/column objects in the Tern codebase (asLineChar in lib/tern.js), which you could steal. You could even add computed properties to the Node prototype that lazily initialize the properties you need from the start/end numbers.