-
-
Notifications
You must be signed in to change notification settings - Fork 75
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
Sequence expressions not formatted "correctly" #427
Comments
Esprima has no way to represent parenthesized expressions in the AST, so this is kinda impossible currently.
This can be achieved relatively easy though. But there are multiple ways to format a long sequence expr, so it's not something that Esprima should support out of the box IMO. However, the AST to JSON conversion was designed to be customizable (via inheritance), so it shouldn't be too hard to create a custom formatter with the desired formatting for sequence exprs. The logic of the AST to JSON conversion is not something trivial indeed, however you don't need to familiarize yourself with the whole thing to achieve what you want. It's enough to know that the conversion logic has two main components:
So, if you decide to take a stab at it, I suggest you check out the repo and debug through this method, using a simple sequence expression as input: esprima-dotnet/src/Esprima/Utils/AstToJavaScriptConverter.cs Lines 1484 to 1492 in 144ed66
This will help you understand what formatter methods the visitor calls and what context they pass to them. Then you can move on to subclassing These pieces of code can give you further pointers:
|
Just an FYI/FWIW: I released a new JS parser that is capable of preserving parentheses around expressions. It can solve your issue partially as its feature set and public API is pretty similar to Esprima's, so migration is relatively easy. Still no formatting options for long sequence expressions though. |
Version used
v3.0.2
Describe the bug
The pretty formatter handles a sequence expressions in a way that makes it hard to read. I tried looking at how this is handled, but I've gotta admit that the printing logic is a bit of voodoo for me.
To Reproduce
Consider this code:
When parsed and pretty-formatted with the default options, it returns this instead:
Note that it's stripped the parentheses, and it's now all on a single line, which is much harder to read.
Expected behavior
It should most likely retain the parentheses around each expression, and when the expression list is above a certain length, it should put each on its own line.
The text was updated successfully, but these errors were encountered: