-
Notifications
You must be signed in to change notification settings - Fork 397
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
fix for comment following headers #3173
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,14 +83,17 @@ public HttpSyntaxTree Parse() | |
return _syntaxTree; | ||
} | ||
|
||
private static void PrependCommentsIfAny(List<HttpCommentNode> commentsToPrepend, HttpSyntaxNode toNode) | ||
private static void AddCommentsIfAny( | ||
List<HttpCommentNode> comments, | ||
HttpSyntaxNode toNode, | ||
bool addBefore = true) | ||
{ | ||
foreach (var comment in commentsToPrepend) | ||
foreach (var comment in comments) | ||
{ | ||
toNode.Add(comment, addBefore: true); | ||
toNode.Add(comment, addBefore: addBefore); | ||
} | ||
|
||
commentsToPrepend.Clear(); | ||
comments.Clear(); | ||
} | ||
|
||
private IEnumerable<HttpVariableDeclarationAndAssignmentNode>? ParseVariableDeclarations() | ||
|
@@ -336,18 +339,27 @@ private T ParseTrailingWhitespace<T>(T node, bool stopAfterNewLine = false, bool | |
ParseTrailingWhitespace(requestNode, stopAfterNewLine: true); | ||
} | ||
|
||
var commentsToPrepend = new List<HttpCommentNode>(); | ||
var comments = new List<HttpCommentNode>(); | ||
if (ParseComment() is { } comment) | ||
{ | ||
commentsToPrepend.Add(comment); | ||
comments.Add(comment); | ||
} | ||
|
||
var headersNode = ParseHeaders(); | ||
if (headersNode is not null) | ||
{ | ||
PrependCommentsIfAny(commentsToPrepend, headersNode); | ||
AddCommentsIfAny(comments, headersNode); | ||
requestNode.Add(headersNode); | ||
} | ||
else | ||
{ | ||
AddCommentsIfAny(comments, requestNode, addBefore: false); | ||
} | ||
|
||
if (ParseComment() is { } commentAfterHeaders) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work if there are multiple comments OR multiple comments separated by new lines? Also looks like this won't handle any leading whitespace before I still feel this is better handled via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One other thought is that perhaps (Also wondering how we currently parse a document that only contains comments but no requests i.e., under what construct do such comments end up being added today? It may be a good idea to add (skipped) tests for the above cases assuming we don't have them already.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have a consistent pattern here but the same problems applies as we encountered with comments generally. Parsing leading whitespace in that way e.g. within Leading whitespace before comments is not supported in the old parser for either kind of comment prefix ( We don't support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current design where we can only parse a single comment in these spots seems problematic. It is also odd that we don't support leading whitespace before comments just at these spots (we do support leading whitespace for any comments parsed via I can take a closer look and take a stab at improving this later. Will sync up offline once I have a PR / proposal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This might work correctly now. I believe there were lookaheads in places that didn't take There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is just a bug. I haven't added combinatorial tests for all known syntax combinations in order to avoid trying to fix too many issues in a single massive PR. That and |
||
{ | ||
requestNode.Add(commentAfterHeaders); | ||
} | ||
|
||
ParseTrailingWhitespace(requestNode); | ||
|
||
|
@@ -358,7 +370,7 @@ private T ParseTrailingWhitespace<T>(T node, bool stopAfterNewLine = false, bool | |
} | ||
else | ||
{ | ||
PrependCommentsIfAny(commentsToPrepend, requestNode); | ||
AddCommentsIfAny(comments, requestNode, addBefore: false); | ||
} | ||
|
||
ParseTrailingWhitespace(requestNode); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed? So far, the explicit Add methods were only required for nodes that appear as properties...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're needed for all node types that can be added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We had discussed this and decided to include the
Add
method for comments on the baseHttpSyntaxNode
class (because otherwise implementingParseLeadingCommentsAndWhitespace
becomes cumbersome),HttpSyntaxNode
already has anAdd
method that handles comments. Looks like it has an additionaladdBefore
parameter - perhaps we can make that parameter optional with a default value and remove the explicit overload inHttpRequestNode
above?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since comments aren't valid in all syntax comments, adding this
Add
method onHttpSyntaxNode
would be misleading.