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

Comments in attributes (e.g. for an array item) are weirdly moved to somewhere completely else #2282

Open
AndreasA opened this issue Nov 24, 2023 · 2 comments

Comments

@AndreasA
Copy link
Contributor

AndreasA commented Nov 24, 2023

Various comments inside attributes are moved to really weird places instead of staying where they were.
The example below is not the only one where something like this happens. There are a few other scenarios like e..g for a parameter etc.

Prettier 2.88.0 (but also tried the latest)

PHP Plugin 0.19.6 (but also tried the latest)

# Options (if any):
--no-options

Input:

<?php declare(strict_types=1);

namespace App\Controller\General;

use App\Dto\BarDto;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class EnrollmentController extends AbstractController
{
    #[Route(path: '/foo', name: 'app.foo', methods: [
        Request::METHOD_GET, // GET is required for third party app.
        Request::METHOD_POST,
    ])]
    public function save(): Response
    {
        return $this->json(['foo']);
    }
}

Output:

<?php declare(strict_types=1);

namespace App\Controller\General;

use App\Dto\BarDto;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class EnrollmentController extends AbstractController // GET is required for third party app.
{
    #[Route(path: '/foo', name: 'app.foo', methods: [Request::METHOD_GET, Request::METHOD_POST])]
    public function save(): Response
    {
        return $this->json(['foo']);
    }
}

Expected behavior:

Output should keep the comment where it was. IT definitely should not be moved to the class:

<?php declare(strict_types=1);

namespace App\Controller\General;

use App\Dto\BarDto;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class EnrollmentController extends AbstractController
{
    #[Route(path: '/foo', name: 'app.foo', methods: [
          Request::METHOD_GET, // GET is required for third party app.
          Request::METHOD_POST
    ])]
    public function save(): Response
    {
        return $this->json(['foo']);
    }
}

This seems to be the case for quite some time as even the old version on the playground shows the issue: Link to playground

@movva-gpu
Copy link

Got the same thing happenning, but instead of only moving, it also duplicates.

I got that simple thing with a PHPDoc so my IDE doesn't tell me my var is undefined (I know I could work around it but I just find this easier for now)

Input:

<footer>
    &copy; •••••• -- All rights reserved.
</footer>
<?php /** @var Script[] $scripts */
if (isset($scripts)) {
    foreach ($scripts as $script) {
        echo $script;
    }
}

Output:

<footer>
    &copy; Grapes -- All rights reserved.
</footer>
 /** @var Script[] $scripts */<?php
/** @var Script[] $scripts */
if (isset($scripts)) {
    foreach ($scripts as $script) {
        echo $script;
    }
}

What's even weirder is, when I format using Prettier again:

<footer>
    &copy; Grapes -- All rights reserved.
</footer>
 /** @var Script[] $scripts */<?php /** @var Script[] $scripts */
 if (isset($scripts)) {
     foreach ($scripts as $script) {
         echo $script;
     }
 }

And again:

<footer>
    &copy; Grapes -- All rights reserved.
</footer>
 /** @var Script[] $scripts */ /** @var Script[] $scripts */<?php
/** @var Script[] $scripts */
if (isset($scripts)) {
     foreach ($scripts as $script) {
         echo $script;
     }
 }

And then it just keeps repeating the comment every two times I format:

<footer>
    &copy; Grapes -- All rights reserved.
</footer>
 /** @var Script[] $scripts */ /** @var Script[] $scripts */ /** @var Script[] $scripts */ /** @var Script[] $scripts */ /** @var Script[] $scripts */ /** @var Script[] $scripts */<?php /** @var Script[] $scripts */
 if (isset($scripts)) {
     foreach ($scripts as $script) {
         echo $script;
     }
 }

@movva-gpu
Copy link

movva-gpu commented Jun 5, 2024

After testing on Playground, it only occurs when there's a line about the <?php for example:

This:

<?php /** @var Script[] $scripts */
if (isset($scripts)) {
    foreach ($scripts as $script) {
        echo $script;
    }
}

doesn't change.

But this :

// empty line
<?php /** @var Script[] $scripts */
 if (isset($scripts)) {
     foreach ($scripts as $script) {
         echo $script;
     }
 }

turns into this :

// empty line
 /** @var Script[] $scripts */<?php
/** @var Script[] $scripts */
if (isset($scripts)) {
    foreach ($scripts as $script) {
        echo $script;
    }
}

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