Syntax error when multiple comments in braceless IF#2311
Syntax error when multiple comments in braceless IF#2311gbrail merged 3 commits intomozilla:masterfrom
Conversation
… 2 consecutive comments in a braceless if statement could lead to EvaluatorException (syntax error) due to bad AST being produced. This is due to the second comment being consumed instead of if body (which is orphaned) and leading to syntax error, as the orphaned node is found where 'else' keyword was expected.
andreabergia
left a comment
There was a problem hiding this comment.
This looks fine to me.
A possible alternative could have been to modify the parsing to "merge" the two comment nodes into just one, with an embedded newline, but I don't mind this fix either.
like that suggestion |
That's clever, I like the idea. Implemented in 8b0f109 |
|
Merging comments makes absolute sense in our parser, because we don't generally need to care about them exceed in terms of their spans. It might be a problem if anybody is using our parse tree for analysis, but it's probably not a big problem. |
Just to clarify, the inline comments that we're merging here are only used to emit string when |
andreabergia
left a comment
There was a problem hiding this comment.
I like this new approach even better.
|
Thanks! According to my AI code review friend: It suggested a test case: and a fix: This all makes sense to me, actually, and I wouldn't have caught it myself. What do you think? |
|
Looks good now, thanks! |
Parser fix for a small bug for when setRecordingComments(true) and if 2 consecutive comments in a braceless if statement could lead to EvaluatorException (syntax error) due to bad AST being produced. For example
This was happening due to the second comment node being consumed instead of the if's body and as the original body node (
doSomething()above) is found where 'else' keyword was expected, leading to a syntax error.The fix is to just iterate on comment nodes until the real body expression is found and saving last comment as InlineComment.
That means that if
.toSource()was called on that AST, we'd lose the first comment in example above. I decided against that as it would require changes in AstNode to allow collecting a list of Inline Comment and use it toSource, which felt like to big of a change for this arguably edge case bug. Happy to explore it further if you feel it's worth it.