PHP-Parser icon indicating copy to clipboard operation
PHP-Parser copied to clipboard

Inline comment before semicolon not in AST

Open pableu opened this issue 4 years ago • 0 comments

Steps to reproduce:

Create a simple PHP file like this:

<?php
echo 'foo' /* bar */ ;

The comment between the end of the function and the ; is crucial. It's also reproducible with code like $x = str_contains('abcde', 'a') /* test */; or $x = 7 /* seven */;.

Parse it:

$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$ast = $parser->parse(file_get_contents('sample.php');
print_r($ast);

Actual Result

array(1) {
  [0]=>
  object(PhpParser\Node\Stmt\Echo_)#9202 (2) {
    ["exprs"]=>
    array(1) {
      [0]=>
      object(PhpParser\Node\Scalar\String_)#9206 (2) {
        ["value"]=>
        string(3) "foo"
        ["attributes":protected]=>
        array(3) {
          ["startLine"]=>
          int(2)
          ["endLine"]=>
          int(2)
          ["kind"]=>
          int(1)
        }
      }
    }
    ["attributes":protected]=>
    array(2) {
      ["startLine"]=>
      int(2)
      ["endLine"]=>
      int(2)
    }
  }
}

Expected Result

I'd like to see to "bar" comment to show up somewhere in the AST. As an attribute of the Stmt\Echo_ most likely.

Use Case

We use this in our internal translation system to annotate calls to our translation engine.

pableu avatar Aug 10 '21 10:08 pableu