-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
Incorrect output due to parens messed up after expression was parsed #41
Comments
You must use presenters for this complex syntax. |
I'm sorry but that is not an acceptable answer - because, how does one know what syntax is "too complex"? Either Edge can parse JS syntax or it cannot. If it can, it should work in 100% of the cases (because it would be easy to get bugs this way that are not immediately detected). If it cannot, it shouldn't pretend that it can, and it should have the restrictions written somewhere and throw an error if an expression is not parsable. My point is simply, silently changing logic in a way that is possibly hard to detect and debug is not an acceptable failure mode. It is simply buggy behavior and can ruin our day, and that's something that should be fixed (or rejected with an error). |
Mind sharing a PR for same? |
I see what you are saying, if I'm critizing, I'm welcome to fix it ;) Well yes but I don't have that understanding you have, so it's unlikely I'll have a PR anytime soon. What I'm saying is simply that "use a presenter" is not a solution - if you mean it's a hard problem for you as well and you can't fix it anytime soon, then I would have hoped that you would maybe add a warning to the docs or something like this, pointing out which things are not supported. For me the problem is that it now feels like "for anything other than So, my point is that it sounded like you were trying to say "it's as designed, not a bug", when it's clearly unexpected behavior and cannot be predicted, and that's what I was critizing. It's clear to me that you can't fix every issue right away, don't get me wrong! |
Yup seems like the one liner gave the wrong message. The AST generation layer of Edge needs some work to work with any Javascript expression, so more involved expressions produces these weird behaviour. I’ll add it to the docs and will see, when I can resume my work on Edge to address these issues. Currently got too much to handle. If you are interested, then my plans is to remove Esprima and use Babel AST generator, since the root of these bugs is esprima not giving enough information |
A quick fix seems to be wrapping about everything in parens. However this creates ugly code (and I'm not sure whether it creates new problems on the way):
I have to say I don't understand the logic of this parser because the decision whether to wrap things in parens doesn't really check operator precedence, it seems like it checks only for very specific cases. |
My two cents: In general it seems to me that it's not such a good idea to do the "convert back to code" part yourself. I would separate the part where you replace member access with access functions from the code generations. What I mean: I would convert the expression to an AST using (By the way: Your list of arithmetic operators is missing |
Acorn, doesn't return the proper information about the
Feel free to create a PR for same.
Not sure how flexible |
Power operator: #44 Your accessFn handles the null/undefined thing, right? My general idea was along the lines of:
|
Proof of concept here: https://repl.it/@CherryDT/EdgeAstModPoC
gives:
...which is exactly what it should be. And all the heavy lifting is moved to acorn/astring, your code will become a lot simpler that way, I guess. (I'm not sure what you meant regarding acorn not returning parens... It works fine for me. The parens are not needed as separate piece of information, the tree already encapsulates that info. In fact, that means that unnecessary parens (and only those) will be gone at the end, which is actually a good thing.) However, I can't put this into a PR because I don't understand enough about the big picture of your parser, because it seems like it is also used for contents of tags and things like that. But I hope this proof-of-concept brought the idea across. EDIT: Added support for |
They may not be needed in some cases, but completely required when doing mathematical calculations. For example 2 * (3 + 2 * 4)
// and this
2 * 3 + 2 * 4 Above both statements will return different output, based upon the parens in place Also I am using Esprima and not Acorn, so things maybe different. I liked the idea of using If you think, that you can replace Esprima with |
Also when using https://astexplorer.net, you can see that AcornBabylon |
Also I just tried In short, if we can start by replacing Esprima with Acorn and retain Later I want to optimize Edge AST parser too, which detects |
I think there is still some kind of misunderstanding here regarding the parens. In short: You don't need to preserve parens as such. Because the position of things in the tree has all the information needed. Everything else (such as where unnecessary parens were) are cosmetic information which is entirely superfluous in your use case. Sure, you won't know whether an expression was in parens or not when you look only at the node in the AST alone, but you don't have to - the code generator will know because it sees the whole tree and not just the node alone! In a way, it's like saying "but we need to preserve the information whether single quotes or double quotes were used, because the escaping will be different, e.g.
You are right that the parens are by default gone in For the other example, where the parens actually make a difference, you can see that they are kept. That's because the tree is all you need:
This is a perfectly good representation of both As for the other example:
And this represents Note that
As you can see, the operations happen in a different order, i.e. the literals are in different places in the tree. This is enough information to accurately represent an expression with all its logic (and only that). Btw I tried a proof of concept of this inside Edge, by monkey-patching |
If I take the below statement to AST explorer, I don't see where the information regarding parens is persisted.
|
Okay, so the different tree thing does makes sense. |
I am happy accepting a PR that does replace Esprima and custom |
I encountered a weird error, that was initially puzzling: I got an output which should have been impossible when looking at the expression I wrote in a mustache block. Only when I looked at it in the debugger, I noticed that edge.js was transforming my expression to something it was never intended to be, messing up its logic.
Code in template:
Compiled code:
Values:
Expected result:
aaa (unselected)
Actual result:
(undefined)
<< that is a space and then "(undefined)"I used edge.js 1.1.3 from
@adonisjs/framework
4.0.28The text was updated successfully, but these errors were encountered: