-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce NAMED_ARGUMENT_SEPARATOR_TYPE and related rules. (#302)
- Loading branch information
1 parent
9402a6a
commit 1158bcd
Showing
25 changed files
with
334 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
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Rules\Function; | ||
|
||
use TwigCsFixer\Environment\StubbedEnvironment; | ||
use TwigCsFixer\Rules\AbstractFixableRule; | ||
use TwigCsFixer\Token\Token; | ||
use TwigCsFixer\Token\Tokens; | ||
|
||
/** | ||
* Ensures named arguments use `:` syntax instead of `=` (For `twig/twig >= 3.12.0`). | ||
*/ | ||
final class NamedArgumentSeparatorRule extends AbstractFixableRule | ||
{ | ||
public function __construct() | ||
{ | ||
if (!StubbedEnvironment::satisfiesTwigVersion(3, 12)) { | ||
throw new \InvalidArgumentException('Named argument with semi colons requires twig/twig >= 3.12.0'); | ||
} | ||
} | ||
|
||
protected function process(int $tokenIndex, Tokens $tokens): void | ||
{ | ||
$token = $tokens->get($tokenIndex); | ||
if (!$token->isMatching(Token::NAMED_ARGUMENT_SEPARATOR_TYPE)) { | ||
return; | ||
} | ||
|
||
if (':' === $token->getValue()) { | ||
return; | ||
} | ||
|
||
$fixer = $this->addFixableError( | ||
\sprintf('Named arguments should be declared with the separator "%s".', ':'), | ||
$token | ||
); | ||
|
||
if (null === $fixer) { | ||
return; | ||
} | ||
|
||
$fixer->replaceToken($tokenIndex, ':'); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Rules\Function; | ||
|
||
use TwigCsFixer\Rules\AbstractSpacingRule; | ||
use TwigCsFixer\Token\Token; | ||
use TwigCsFixer\Token\Tokens; | ||
|
||
/** | ||
* Ensures named arguments use no space around `=` and no space before/one space after `:`. | ||
*/ | ||
final class NamedArgumentSpacingRule extends AbstractSpacingRule | ||
{ | ||
protected function getSpaceBefore(int $tokenIndex, Tokens $tokens): ?int | ||
{ | ||
$token = $tokens->get($tokenIndex); | ||
if ($token->isMatching(Token::NAMED_ARGUMENT_SEPARATOR_TYPE)) { | ||
return 0; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected function getSpaceAfter(int $tokenIndex, Tokens $tokens): ?int | ||
{ | ||
$token = $tokens->get($tokenIndex); | ||
if ($token->isMatching(Token::NAMED_ARGUMENT_SEPARATOR_TYPE, '=')) { | ||
return 0; | ||
} | ||
if ($token->isMatching(Token::NAMED_ARGUMENT_SEPARATOR_TYPE, ':')) { | ||
return 1; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
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
3 changes: 3 additions & 0 deletions
3
tests/Rules/Function/NamedArgumentSeparatorRuleTest.fixed.twig
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,3 @@ | ||
{{ foo(bar:true, baz:42) }} | ||
{% macro foo(bar=true) %} | ||
{% set foo = true %} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Tests\Rules\Function; | ||
|
||
use Composer\InstalledVersions; | ||
use Composer\Semver\VersionParser; | ||
use TwigCsFixer\Rules\Function\NamedArgumentSeparatorRule; | ||
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase; | ||
|
||
final class NamedArgumentSeparatorRuleTest extends AbstractRuleTestCase | ||
{ | ||
public function testRule(): void | ||
{ | ||
if (!InstalledVersions::satisfies(new VersionParser(), 'twig/twig', '>=3.12.0')) { | ||
$this->expectException(\InvalidArgumentException::class); | ||
} | ||
|
||
$this->checkRule(new NamedArgumentSeparatorRule(), [ | ||
'NamedArgumentSeparator.Error:1:11' => 'Named arguments should be declared with the separator ":".', | ||
]); | ||
} | ||
} |
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,3 @@ | ||
{{ foo(bar=true, baz:42) }} | ||
{% macro foo(bar=true) %} | ||
{% set foo = true %} |
2 changes: 2 additions & 0 deletions
2
tests/Rules/Function/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.fixed.twig
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,2 @@ | ||
{{ foo(bar=true, baz: 42) }} | ||
{% macro foo(bar = true) %} |
21 changes: 21 additions & 0 deletions
21
tests/Rules/Function/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.php
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Tests\Rules\Function\NamedArgumentSpacing; | ||
|
||
use TwigCsFixer\Rules\Function\NamedArgumentSpacingRule; | ||
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase; | ||
|
||
final class NamedArgumentSpacingRuleTest extends AbstractRuleTestCase | ||
{ | ||
public function testRule(): void | ||
{ | ||
$this->checkRule(new NamedArgumentSpacingRule(), [ | ||
'NamedArgumentSpacing.After:1:12' => 'Expecting 0 whitespace after "="; found 1.', | ||
'NamedArgumentSpacing.Before:1:12' => 'Expecting 0 whitespace before "="; found 1.', | ||
'NamedArgumentSpacing.After:1:24' => 'Expecting 1 whitespace after ":"; found 0.', | ||
'NamedArgumentSpacing.Before:1:24' => 'Expecting 0 whitespace before ":"; found 1.', | ||
]); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
tests/Rules/Function/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.twig
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,2 @@ | ||
{{ foo(bar = true, baz :42) }} | ||
{% macro foo(bar = true) %} |
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
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
Oops, something went wrong.