mmoreram / php-formatter

PHP Formatter is a PHP developer friendly set of tools
MIT License
166 stars 17 forks source link

Grouping use-statements sometimes leaves gaps #24

Closed matt-usurp closed 7 years ago

matt-usurp commented 7 years ago

I have been noticing this issue often and have been meaning to attempt to fix it. I haven't had time and I keep forgetting so I am creating this issue as a kind of reminder. Also hoping that someone will know exactly why this is happening and be able to submit a fix faster than me.

It looks like when sorting use-statements in fairly complex groups and two groups consecutively are not being used there are some additional spaces added.

Example pulled from my test classes.

use Project\Core\AClass;
use Project\Core\BClass;
use Project\Core\CClass;

use PHPUnit_Framework_MockObject_MockObject as MockObject;

With the following configuration:

use-sort:
  group:
    - Project\Core
    - Project\Integration
    - [Symfony, Sensio, Doctrine]
    - _main
  group-type: each
  group-skip-empty: true
  sort-type: alph
  sort-direction: asc

Please note that group-skip-empty is enabled also.

Expecting output:

use Project\Core\AClass;
use Project\Core\BClass;
use Project\Core\CClass;

use PHPUnit_Framework_MockObject_MockObject as MockObject;
mmoreram commented 7 years ago

@matt-usurp Great!

Could you please add some tests failing in a branch¿? After that we can add the fixes :)

Thanks!

matt-usurp commented 7 years ago

@mmoreram Yeah sure, I don't know how you want to do this but I have a branch on my fork.

https://github.com/matt-usurp/php-formatter/tree/issue-24-grouping-whitespace

I added two failing tests that look like they will cover it. One for when removal of empty groups is enabled and one without, so looks like maybe skip empty groups isn't the issue.

matt-usurp commented 7 years ago

Not to worry, I have applied the fix and updated the PR.

@mmoreram Can you take a look please, it would be great to get this fixed.