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

Get different line number for attrGroups and its node #1043

Open
samsonasik opened this issue Nov 23, 2024 · 4 comments
Open

Get different line number for attrGroups and its node #1043

samsonasik opened this issue Nov 23, 2024 · 4 comments

Comments

@samsonasik
Copy link
Contributor

samsonasik commented Nov 23, 2024

currently, I can't found a way to verify the same line and different line number of code:

#[AllowDynamicProperties]
class User
{
}

vs

#[AllowDynamicProperties]class User
{
}

the getStartline() is always equal, between attrGroups vs node, so when downgrading, it can't be verified if it needs to be reprint or not, as in php 7, it will require get printed attrGroups and marked as comment attribute.

any way to do it?

@samsonasik samsonasik changed the title Make different line for attrGroups and its node Get different line number for attrGroups and its node Nov 23, 2024
@nikic
Copy link
Owner

nikic commented Nov 24, 2024

A good heuristic would be to check the line of the name. If you want to be pedantic, get the token end position of the last attrgroup and then check whether it's followed by a newline.

@samsonasik
Copy link
Contributor Author

It seems I can do with:

if (isset($oldTokens[$attrGroup->getEndTokenPos() + 1]) && ! str_contains((string) $oldTokens[$attrGroup->getEndTokenPos() + 1], "\n")) {
    $print = $this->betterStandardPrinter->print($attrGroup);
    $attributesAsComments[] = new Comment($print);
}

then remove attribute, and setAttribute('comment', $attributesAsComments), just wondering if there is a better way.

@nikic
Copy link
Owner

nikic commented Nov 24, 2024

Looks reasonable to me

@samsonasik
Copy link
Contributor Author

I found that add new line to next token seems works:

if (isset($oldTokens[$attrGroup->getEndTokenPos() + 1]) && ! str_contains((string) $oldTokens[$attrGroup->getEndTokenPos() + 1], "\n")) {
    // add new line
   $oldTokens[$attrGroup->getEndTokenPos() + 1]->text = "\n" . $oldTokens[$attrGroup->getEndTokenPos() + 1]->text;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants