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

[bug] incorrect start location in many nodes #208

Open
alexander-akait opened this issue Oct 25, 2018 · 8 comments
Open

[bug] incorrect start location in many nodes #208

alexander-akait opened this issue Oct 25, 2018 · 8 comments
Assignees
Labels
AST bug investigating minor Minor issue - does not impact the parser too much (edge cases)
Milestone

Comments

@alexander-akait
Copy link
Collaborator

Input:

while (true) // Comment
{
};

Currently while node starts with test node location, but should be start with while keyword. Same for other control structures. It would be great if you show how to fix such things (send a PR with fix) and i will start fix it self and send new PRs. This would speed up problem solving.

@ichiriac
Copy link
Member

Hi,

Any PR is welcome, actually I have a ton of work and I finish late, also a personal project at home, so it's hard to find time to work. A lot of issues can be fixed with simple changes and should not take much time but even 1 hour it's hard to find for me.

Here my email : i.chiriac@yahoo.com, email me and we can continue to chat with skype or whatever.

For your bug it's simple, here the parsing of the node : https://github.com/glayzzle/php-parser/blob/master/src/parser/loops.js#L17

The const result = this.node("while"); node function get track of the position. The parser eats a token and go to the next with the next function, meaning the while token was eaten before creating the AST node and so excluding it from locations.

Take a look at :

return this.next().read_while();
it's here from where the function is called, and you can see that before calling the function, an extra next consumes the token (bug is here).

To fix this bug you have to remove the next call from statement.js, and to consume it into the function read_while juste after the this.node statement. For example here :

if (this.expect("(")) this.next();

Note the next returns this, so you can write something as : this.next().expect(...

If you pay attention into statement.js, you will see that other keywords may have the bug :

      case this.tok.T_FOR:
        return this.next().read_for();

      case this.tok.T_FOREACH:
        return this.next().read_foreach();

      case this.tok.T_WHILE:
        return this.next().read_while();

      case this.tok.T_DO:
        return this.next().read_do();

We can keep this thread for any question around the code

@alexander-akait
Copy link
Collaborator Author

@ichiriac looks i find how fix it, can you look my PR #209 and comment?

@alexander-akait
Copy link
Collaborator Author

Here my email : i.chiriac@yahoo.com, email me and we can continue to chat with skype or whatever.

My English is very bad 😄 It's better for me to type than talk, sorry.

@ichiriac
Copy link
Member

I'll try to explain how you can fix bugs when it's simple. Your help is much appreciated !

@alexander-akait alexander-akait changed the title [bug] incorrect start location for control structures [bug] incorrect start location in many nodes Oct 25, 2018
@ichiriac
Copy link
Member

this bug was fixed & released

@alexander-akait
Copy link
Collaborator Author

@ichiriac let's left it open because not all nodes was fixed

@ichiriac ichiriac reopened this Nov 21, 2018
@ichiriac ichiriac added investigating minor Minor issue - does not impact the parser too much (edge cases) labels Jan 7, 2019
@ichiriac ichiriac added this to the 3.1.0 milestone Jan 7, 2019
@alexander-akait
Copy link
Collaborator Author

/cc @ichiriac find problem with if, input:

if ($bool0
    || $bool1 // if condition 1
    || $bool2 // if condition 2
  ) {
    $ok = true;
}

The test property has invalid loc (https://astexplorer.net/), issue prettier/plugin-php#1092

@ctf0
Copy link

ctf0 commented Dec 15, 2022

any updates ?, 4 years and still no fix 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AST bug investigating minor Minor issue - does not impact the parser too much (edge cases)
Projects
None yet
Development

No branches or pull requests

3 participants