-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce MacroArgumentNameRule (#337)
- Loading branch information
1 parent
d742797
commit de965b7
Showing
11 changed files
with
136 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Rules\Function; | ||
|
||
use TwigCsFixer\Rules\AbstractRule; | ||
use TwigCsFixer\Rules\ConfigurableRuleInterface; | ||
use TwigCsFixer\Token\Token; | ||
use TwigCsFixer\Token\Tokens; | ||
use TwigCsFixer\Util\StringUtil; | ||
|
||
/** | ||
* Ensures that named argument are in snake_case (Configurable). | ||
*/ | ||
final class MacroArgumentNameRule extends AbstractRule implements ConfigurableRuleInterface | ||
{ | ||
// Kebab case is not a valid case for argument. | ||
public const SNAKE_CASE = 'snake_case'; | ||
public const CAMEL_CASE = 'camelCase'; | ||
public const PASCAL_CASE = 'PascalCase'; | ||
|
||
/** | ||
* @param self::* $case | ||
*/ | ||
public function __construct(private string $case = self::SNAKE_CASE) | ||
{ | ||
} | ||
|
||
public function getConfiguration(): array | ||
{ | ||
return [ | ||
'case' => $this->case, | ||
]; | ||
} | ||
|
||
protected function process(int $tokenIndex, Tokens $tokens): void | ||
{ | ||
$token = $tokens->get($tokenIndex); | ||
if (!$token->isMatching(Token::MACRO_VAR_NAME_TYPE)) { | ||
return; | ||
} | ||
|
||
$name = $token->getValue(); | ||
$expected = match ($this->case) { | ||
self::SNAKE_CASE => StringUtil::toSnakeCase($name), | ||
self::CAMEL_CASE => StringUtil::toCamelCase($name), | ||
self::PASCAL_CASE => StringUtil::toPascalCase($name), | ||
}; | ||
|
||
if ($expected !== $name) { | ||
$this->addError( | ||
\sprintf('The macro argument must use %s; expected %s.', $this->case, $expected), | ||
$token, | ||
); | ||
} | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
tests/Rules/Function/MacroArgumentName/MacroArgumentNameRuleTest.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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Tests\Rules\Function\MacroArgumentName; | ||
|
||
use TwigCsFixer\Rules\Function\MacroArgumentNameRule; | ||
use TwigCsFixer\Test\AbstractRuleTestCase; | ||
|
||
final class MacroArgumentNameRuleTest extends AbstractRuleTestCase | ||
{ | ||
public function testConfiguration(): void | ||
{ | ||
static::assertSame( | ||
[ | ||
'case' => MacroArgumentNameRule::SNAKE_CASE, | ||
], | ||
(new MacroArgumentNameRule())->getConfiguration() | ||
); | ||
static::assertSame( | ||
[ | ||
'case' => MacroArgumentNameRule::CAMEL_CASE, | ||
], | ||
(new MacroArgumentNameRule(MacroArgumentNameRule::CAMEL_CASE))->getConfiguration() | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->checkRule(new MacroArgumentNameRule(), [ | ||
'MacroArgumentName.Error:1:14' => 'The macro argument must use snake_case; expected foo_bar1.', | ||
'MacroArgumentName.Error:1:33' => 'The macro argument must use snake_case; expected foo_bar3.', | ||
]); | ||
} | ||
|
||
public function testRulePascalCase(): void | ||
{ | ||
$this->checkRule(new MacroArgumentNameRule(MacroArgumentNameRule::PASCAL_CASE), [ | ||
'MacroArgumentName.Error:1:14' => 'The macro argument must use PascalCase; expected FooBar1.', | ||
'MacroArgumentName.Error:1:23' => 'The macro argument must use PascalCase; expected FooBar2.', | ||
]); | ||
} | ||
|
||
public function testRuleCamelCase(): void | ||
{ | ||
$this->checkRule(new MacroArgumentNameRule(MacroArgumentNameRule::CAMEL_CASE), [ | ||
'MacroArgumentName.Error:1:23' => 'The macro argument must use camelCase; expected fooBar2.', | ||
'MacroArgumentName.Error:1:33' => 'The macro argument must use camelCase; expected fooBar3.', | ||
]); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
tests/Rules/Function/MacroArgumentName/MacroArgumentNameRuleTest.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 @@ | ||
{% macro foo(fooBar1, foo_bar2, FooBar3 = true) %} | ||
{% endmacro %} |
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