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

Updating a node comments removes empty lines between the comment block and the node #7617

Closed
leandr opened this issue Nov 22, 2022 · 1 comment
Labels

Comments

@leandr
Copy link

leandr commented Nov 22, 2022

Bug Report

Subject Details
Rector version v0.14.8
PHP version 8.1.12

When you do something with a node's comments, the procedure removes empty lines between the comment block and the node, so sometimes it breaks the structure of the commented code and makes it harder to read.

Minimal PHP Code Causing Issue

Custom rule:

<?php

namespace Bumble\RefactoringTools\Rector\CustomRules;

use PhpParser\Node;
use PhpParser\Comment;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

class TestRector extends AbstractRector
{
    public function getRuleDefinition(): RuleDefinition
    {
        return new RuleDefinition('Hello!', [
            new CodeSample(
                <<<'PHP'
return;
PHP,
                <<<'PHP'
return;
PHP,
            ),
        ]);
    }

    public function getNodeTypes(): array
    {
        return [
            Node::class,
        ];
    }

    public function refactor(Node $node): ?Node
    {
        /** @var Comment[] $comments */
        $comments = $node->getAttribute(AttributeKey::COMMENTS);
        if (!$comments) {
            return null;
        }

        $newComments = [];
        foreach ($comments as $comment) {
            // do something with comments
            $newComments[] = new Comment($comment->getText());
        }

        $node->setAttribute(AttributeKey::COMMENTS, $newComments);
        return $node;
    }
}

PHP code to format:

<?php
/**
 * Annotation of the file
 * Needs an empty line after it
 */

namespace TestMe;

/**
 * Class description
 */
class MyClass
{
    /**
     * Go for it
     *
     * @param array $api
     * @return void
     */
    public function myMethod(array $api): void
    {
        // do something
        $myVariable = array_reverse($api);

        // important comment, well highlighted

        // just export the var
        echo var_export($myVariable, true);
    }
}

Actual Result

No empty line before the 'namespace' line, and no empty line after the 'important comment':

<?php
/**
 * Annotation of the file
 * Needs an empty line after it
 */
namespace TestMe;

/**
 * Class description
 */
class MyClass
{
    /**
     * Go for it
     *
     * @param array $api
     * @return void
     */
    public function myMethod(array $api): void
    {
        // do something
        $myVariable = array_reverse($api);

        // important comment, well highlighted
        // just export the var
        echo var_export($myVariable, true);
    }
}

Expected Result

Lines between comments and nodes remain.

<?php
/**
 * Annotation of the file
 * Needs an empty line after it
 */

namespace TestMe;

/**
 * Class description
 */
class MyClass
{
    /**
     * Go for it
     *
     * @param array $api
     * @return void
     */
    public function myMethod(array $api): void
    {
        // do something
        $myVariable = array_reverse($api);

        // important comment, well highlighted

        // just export the var
        echo var_export($myVariable, true);
    }
}
@leandr leandr added the bug label Nov 22, 2022
@TomasVotruba
Copy link
Member

This is inherited feature from php-parser. See nikic/PHP-Parser#344

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

No branches or pull requests

2 participants