-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Disappearing comments #950
Comments
The core problem here is that there is no place to put these comments. We do retain comments in cases like
by inserting a I would love to retain these comments somehow, but I'm not sure how this could be represented. It is possible to recover these comments from the tokens, e.g. #382 (comment) has some sample code to get all "interior" comments of a node (i.e. directly in the node, not part of a subnode). I think it should basically still work though can be a bit simplified nowadays. That just gives you the comments though, not where exactly they occurred, so it might not be useful for your use-case. |
Thanks for the pointers @nikic -- I will try out the tokens and report back. |
Have not tried the tokens yet, but wanted to point this out in the mean time: the final comment on the final array element disappears; the parser does not appear to retain it. This may be a different problem from the above; if so, I can open a separate issue. $map = [
34 => 'quot', // quotation mark
38 => 'amp', // ampersand
60 => 'lt', // less-than sign
62 => 'gt', // greater-than sign -- this comment disappears
]; The parser nodes and output:
|
To add another case to this: function foo(/* retained */ $param) /* not retained */
{
} Cause is what @nikic wrote above: there is nothing to attach the comment to. Was investing this as at some point at my company we decided to (also) write return types (and parameter types) like this as a comment at a time we hadn't updated to a PHP version which supported type hints yet. This with the idea to later just drop the |
BLUF: The 4.x parser appears to lose comments in some cases; while their removal has no effect on the logical operation of code rebuilt from the AST, their disappearance is surprising when using something like PHP-Styler.
Given code like the following ...
... the comments noted do not appear at all in the array of parsed nodes:
Is it possible to retain them somehow, so that PHP-Styler can reproduce them faithfully when pretty-printing the AST?
The text was updated successfully, but these errors were encountered: