Skip to content
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

Leading comments are treated as trailing comments to the previous function body #250

Closed
aameen951 opened this issue Jan 20, 2019 · 7 comments
Assignees
Labels
AST bug major Major issue - have a real impact on many cases

Comments

@aameen951
Copy link

Hello,

I'm trying to parse the following file:

<?php
/** function 1 summary
 * @param Type $var Description
 **/
function FunctionName1(Type $var = null)
{
}

/** function 2 summary
 * @param Type $var Description
 **/
function FunctionName2(Type $var = null)
{
}

The output of the library is as follows:

...
{
  "kind": "function",
  "leadingComments": [{"kind": "commentblock","value": "/** function 1 summary\r\n * @param Type $var Description\r\n **/"}],
  "name": {
    "kind": "identifier",
    "name": "FunctionName1"
  },
  ...
  "body": {
    "kind": "block",
    "children": [],
    "trailingComments": [{"kind": "commentblock","value": "/** function 2 summary\r\n * @param Type $var Description\r\n **/"}]
  }
}
...

The problem is that the documentation of the FunctionName2 is treated as a trailing comment on the body of FunctionName1 instead of a leading comment on FunctionName2.

@nevadascout
Copy link
Contributor

nevadascout commented Jan 22, 2019

What happens if you move the "summary" text onto a line below the /** - does it still produce this problem?

eg instead of:

/** function 1 summary
 * @param Type $var Description
 **/

have

/**
 * function 1 summary
 * @param Type $var Description
 **/

@ichiriac ichiriac self-assigned this Jan 22, 2019
@ichiriac ichiriac added bug AST major Major issue - have a real impact on many cases labels Jan 22, 2019
@ichiriac
Copy link
Member

That comes from the parser, the way it was implemented. In some cases the documentation block may be attached as trailingComments, only if no other tag comes after, on the same level ...

Here another bug, on the same idea - // bar is attached to foo as a trailingComments

foo();
// bar
bar();

I'll try another approach - the first implementation was way too simplistic 😄

ichiriac pushed a commit that referenced this issue Jan 22, 2019
@ichiriac
Copy link
Member

Hi @aameen951,

The bug should be fixed from my latest commit - it will be released soon.

@ichiriac ichiriac added this to the 3.0.0-prerelease.9 milestone Jan 22, 2019
@aameen951
Copy link
Author

@ichiriac Thank you!

I'll find a way to tell npm to install php-parser with this fix or I might just wait until prerelease.9

@ichiriac
Copy link
Member

I hope to release it this month but meanwhile use the commit id it will be safer as the current version may be introduce breaking changes

@danilopolani
Copy link

danilopolani commented Apr 6, 2019

@ichiriac Any update on this? You can reproduce the bug as well with this (using v3.0.0-prerelease.8): https://astexplorer.net/#/gist/d493a13a7b2ed670ccab73f6aea0c872/latest

@ichiriac
Copy link
Member

@danilopolani, it's normal, actually not yet released - will be done this week-end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AST bug major Major issue - have a real impact on many cases
Projects
None yet
Development

No branches or pull requests

4 participants