Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NoTrailingCommaInSinglelineFixer (#727)
- Loading branch information
1 parent
f5105e4
commit aa1bbd9
Showing
7 changed files
with
294 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer: custom fixers. | ||
* | ||
* (c) 2018 Kuba Werłos | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace PhpCsFixerCustomFixers\Fixer; | ||
|
||
use PhpCsFixer\FixerDefinition\CodeSample; | ||
use PhpCsFixer\FixerDefinition\FixerDefinition; | ||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; | ||
use PhpCsFixer\Tokenizer\CT; | ||
use PhpCsFixer\Tokenizer\Tokens; | ||
|
||
final class NoTrailingCommaInSinglelineFixer extends AbstractFixer | ||
{ | ||
public function getDefinition(): FixerDefinitionInterface | ||
{ | ||
return new FixerDefinition( | ||
'Trailing comma in the list on the same line as the end of the block must be removed.', | ||
[ | ||
new CodeSample("<?php\n\$x = ['foo', 'bar', ];\n"), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Must run after MethodArgumentSpaceFixer, TrailingCommaInMultilineFixer. | ||
*/ | ||
public function getPriority(): int | ||
{ | ||
return -1; | ||
} | ||
|
||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return $tokens->isAnyTokenKindsFound([\T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN, CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE, '(']); | ||
} | ||
|
||
public function isRisky(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function fix(\SplFileInfo $file, Tokens $tokens): void | ||
{ | ||
for ($index = $tokens->count() - 1; $index >= 0; $index--) { | ||
if (!$tokens[$index]->equalsAny([')', [CT::T_ARRAY_SQUARE_BRACE_CLOSE], [CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE]])) { | ||
continue; | ||
} | ||
|
||
$commaIndex = $tokens->getPrevMeaningfulToken($index); | ||
\assert(\is_int($commaIndex)); | ||
|
||
while ($tokens[$commaIndex]->equals(',')) { | ||
if ($tokens->isPartialCodeMultiline($commaIndex, $index)) { | ||
break; | ||
} | ||
|
||
$tokens->removeLeadingWhitespace($commaIndex); | ||
$tokens->removeTrailingWhitespace($commaIndex); | ||
$tokens->clearAt($commaIndex); | ||
|
||
$commaIndex = $tokens->getPrevMeaningfulToken($index); | ||
\assert(\is_int($commaIndex)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer: custom fixers. | ||
* | ||
* (c) 2018 Kuba Werłos | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Tests\Fixer; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \PhpCsFixerCustomFixers\Fixer\NoTrailingCommaInSinglelineFixer | ||
*/ | ||
final class NoTrailingCommaInSinglelineFixerTest extends AbstractFixerTestCase | ||
{ | ||
public function testIsRisky(): void | ||
{ | ||
self::assertFalse($this->fixer->isRisky()); | ||
} | ||
|
||
/** | ||
* @dataProvider provideFixCases | ||
*/ | ||
public function testFix(string $expected, ?string $input = null): void | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
/** | ||
* @return iterable<array{0: string, 1?: string}> | ||
*/ | ||
public static function provideFixCases(): iterable | ||
{ | ||
yield [ | ||
'<?php $x = [ | ||
1, | ||
2, | ||
];', | ||
]; | ||
|
||
yield [ | ||
'<?php $x = [1, 2,// foo | ||
];', | ||
]; | ||
|
||
yield [ | ||
'<?php $array = [1, 2];', | ||
'<?php $array = [1, 2,];', | ||
]; | ||
|
||
yield [ | ||
'<?php $array = [1, 2];', | ||
'<?php $array = [1, 2, ];', | ||
]; | ||
|
||
yield [ | ||
'<?php $array = [1, 2, | ||
3, 4];', | ||
'<?php $array = [1, 2, | ||
3, 4, ];', | ||
]; | ||
|
||
yield [ | ||
'<?php $array = [1, 2] + [3, 4] + [5, 6];', | ||
'<?php $array = [1, 2, ] + [3, 4, ] + [5, 6, ];', | ||
]; | ||
|
||
yield [ | ||
'<?php $array = [1, 2/* foo */];', | ||
'<?php $array = [1, 2, /* foo */];', | ||
]; | ||
|
||
yield [ | ||
'<?php $array = array(1, 2);', | ||
'<?php $array = array(1, 2, );', | ||
]; | ||
|
||
yield [ | ||
'<?php list($x, $y) = $list;', | ||
'<?php list($x, $y,) = $list;', | ||
]; | ||
|
||
yield [ | ||
'<?php [$x, $y] = $list;', | ||
'<?php [$x, $y,] = $list;', | ||
]; | ||
|
||
yield [ | ||
'<?php list($x, $y) = $list;', | ||
'<?php list($x, $y, , ,) = $list;', | ||
]; | ||
|
||
yield [ | ||
'<?php list($x, $y) = $list;', | ||
'<?php list($x, $y , , , ) = $list;', | ||
]; | ||
} | ||
|
||
/** | ||
* @requires PHP ^7.3 | ||
* @dataProvider provideFix73Cases | ||
*/ | ||
public function testFix73(string $expected, ?string $input = null): void | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
/** | ||
* @return iterable<array{0: string, 1?: string}> | ||
*/ | ||
public static function provideFix73Cases(): iterable | ||
{ | ||
yield [ | ||
'<?php foo($x, $y);', | ||
'<?php foo($x, $y, );', | ||
]; | ||
|
||
yield [ | ||
'<?php | ||
$array = [1, 2] + [3, 4] + [5, 6]; | ||
foo( | ||
1, | ||
2, | ||
); | ||
list($x, $y) = $list; | ||
', | ||
'<?php | ||
$array = [1, 2, ] + [3, 4] + [5, 6, ]; | ||
foo( | ||
1, | ||
2, | ||
); | ||
list($x, $y,) = $list; | ||
', | ||
]; | ||
} | ||
|
||
/** | ||
* @requires PHP ^8.0 | ||
* @dataProvider provideFix80Cases | ||
*/ | ||
public function testFix80(string $expected, ?string $input = null): void | ||
{ | ||
$this->doTest($expected, $input); | ||
} | ||
|
||
/** | ||
* @return iterable<array{0: string, 1?: string}> | ||
*/ | ||
public static function provideFix80Cases(): iterable | ||
{ | ||
yield [ | ||
'<?php function foo($x, $y) {}', | ||
'<?php function foo($x, $y, ) {}', | ||
]; | ||
|
||
yield [ | ||
'<?php $f = function ($x, $y) {};', | ||
'<?php $f = function ($x, $y, ) {};', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
tests/priority_fixtures/method_argument_space,Custom_no_trailing_comma_in_singleline.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--CONFIGURATION-- | ||
{ "method_argument_space": {"on_multiline": "ensure_single_line"}, "PhpCsFixerCustomFixers/no_trailing_comma_in_singleline": true } | ||
--EXPECTED-- | ||
<?php | ||
|
||
foo(1, 2, 3); | ||
|
||
function bar($x, $y, $x) {} | ||
|
||
--INPUT-- | ||
<?php | ||
|
||
foo(1, 2, | ||
3, | ||
); | ||
|
||
function bar($x, | ||
$y, | ||
$x, | ||
) {} |
19 changes: 19 additions & 0 deletions
19
...priority_fixtures/trailing_comma_in_multiline,Custom_no_trailing_comma_in_singleline.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--CONFIGURATION-- | ||
{ "trailing_comma_in_multiline": true, "PhpCsFixerCustomFixers/no_trailing_comma_in_singleline": true } | ||
--EXPECTED-- | ||
<?php | ||
$foo = [1, 2, | ||
3, 4]; | ||
$bar = [5, 6, | ||
7, 8]; | ||
$baz = [11, 12, | ||
13, 14]; | ||
|
||
--INPUT-- | ||
<?php | ||
$foo = [1, 2, | ||
3, 4]; | ||
$bar = [5, 6, | ||
7, 8,]; | ||
$baz = [11, 12, | ||
13, 14, ]; |