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

Printing anonymous classes removes newlines between methods #803

Open
bakerkretzmar opened this issue Aug 27, 2021 · 1 comment
Open

Printing anonymous classes removes newlines between methods #803

bakerkretzmar opened this issue Aug 27, 2021 · 1 comment

Comments

@bakerkretzmar
Copy link

Working on https://github.com/tighten/tlint, Tighten's internal code style and formatting tool, we noticed that the pretty printer removes newlines between methods of anonymous classes.

We're attempting a transformation like this:

<?php

use Illuminate\Database\Migrations\Migration;

- class CreateBuyRequestsTable extends Migration
+ return new class extends Migration
{
    public function up()
    {
        //
    }

    public function down()
    {
        //
    }
- }
+ };

The code we're using for the transformation looks like this:

$traverser = new NodeTraverser;
$traverser->addVisitor(new CloningVisitor);

$originalStatements = $parser->parse($this->code);

$statements = array_map(function (Node $node) {
    if (
        $node instanceof Class_
            && $node->extends->toString() === 'Migration'
            && $node->name
    ) {
        return new Return_(new New_($node));
    }

    return $node;
}, $traverser->traverse($originalStatements));

return (new Standard)->printFormatPreserving($statements, $originalStatements, $lexer->getTokens());

It works, except that the new line between the up() and down() methods is being removed as well.

Is this a bug in the printer or are we doing something wrong? Thanks!

@echoWangyx
Copy link

I have the same problem

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