php-parser icon indicating copy to clipboard operation
php-parser copied to clipboard

Invalid start location for class constants / properties

Open czosel opened this issue 5 years ago • 7 comments

Input:

<?php
class A
{
    const test = 1;
}

AST (excerpt):

"kind": "classconstant",
          "loc": {
            "source": "test = 1",
            "start": {
              "line": 4,
              "column": 10,
              "offset": 26
            },
            "end": {
              "line": 4,
              "column": 18,
              "offset": 34
            }
          },

I think the start column of the class constant is off, it should be around 4 instead of 10 in the example.

czosel avatar Mar 21 '20 19:03 czosel

The same thing happens with class properties:

<?php
class A
{
    public $test = 1;
}

AST:

"kind": "propertystatement",
          "loc": {
            "source": "$test = 1",
            "start": {
              "line": 4,
              "column": 11,
              "offset": 27
            },
            "end": {
              "line": 4,
              "column": 20,
              "offset": 36
            }
          },

czosel avatar Mar 21 '20 19:03 czosel

Yes, it is old issues :disappointed:

alexander-akait avatar Mar 23 '20 09:03 alexander-akait

Maybe related to https://github.com/glayzzle/php-parser/issues/208 ?

czosel avatar Mar 23 '20 10:03 czosel

Yes, i try to keep all in the one PR, but it's just my preference

alexander-akait avatar Mar 23 '20 10:03 alexander-akait

Doesn't matter as long as we get it fixed :wink:

czosel avatar Mar 23 '20 10:03 czosel

for const the fix is easy, but for properties / functions it's more complicated as the final / abstract / private / protected / public tokens may be eaten before creating the function or property node - one solution would be to use swapLocations on the generated and store on flags the parser position before eating modifiers tokens

ichiriac avatar Mar 26 '20 07:03 ichiriac

Hi @evilebottnawi, it would be great if you have some time that you keep working on branch #fix-issue-511 as the fix is not fully implemented.

Remains to check interfaces, traits, use alias on traits, but the principle remains the same as in the last commit

ichiriac avatar Mar 26 '20 08:03 ichiriac