Skip to content

Commit

Permalink
Introduce rule for named argument operator
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 17, 2024
1 parent f7c337b commit e47f04b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
- `spaceRatio`: how many spaces replace a tab (default 4).
- `useTab`: indentation must be done with tab (default false).

- **NamedArgumentSpacingRule**:

Ensures named arguments use no space around `=` and no space before/one space after `:`.

- **OperatorNameSpacingRule**:

Ensures there is no consecutive spaces inside operator names.
Expand Down Expand Up @@ -146,6 +150,7 @@ new TwigCsFixer\Rules\Whitespace\IndentRule(3);

**Twig**:
- DelimiterSpacingRule
- NamedArgumentSpacingRule
- OperatorNameSpacingRule
- OperatorSpacingRule
- PunctuationSpacingRule
Expand Down
39 changes: 39 additions & 0 deletions src/Rules/Operator/NamedArgumentSpacingRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Rules\Operator;

use TwigCsFixer\Rules\AbstractSpacingRule;
use TwigCsFixer\Token\Token;
use TwigCsFixer\Token\Tokens;
use Webmozart\Assert\Assert;

/**
* 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_OPERATOR_TYPE)) {
return 0;
}

return null;
}

protected function getSpaceAfter(int $tokenIndex, Tokens $tokens): ?int
{
$token = $tokens->get($tokenIndex);
if ($token->isMatching(Token::NAMED_ARGUMENT_OPERATOR_TYPE, '=')) {
return 0;
}
if ($token->isMatching(Token::NAMED_ARGUMENT_OPERATOR_TYPE, ':')) {
return 1;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ foo(bar=true, baz: 42) }}
{% macro foo(bar = true) %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Tests\Rules\Operator\NamedArgumentSpacing;

use TwigCsFixer\Rules\Operator\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.',
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ foo(bar = true, baz :42) }}
{% macro foo(bar = true) %}
2 changes: 2 additions & 0 deletions tests/Standard/TwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use TwigCsFixer\Rules\Delimiter\DelimiterSpacingRule;
use TwigCsFixer\Rules\Operator\NamedArgumentSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorNameSpacingRule;
use TwigCsFixer\Rules\Operator\OperatorSpacingRule;
use TwigCsFixer\Rules\Punctuation\PunctuationSpacingRule;
Expand All @@ -20,6 +21,7 @@ public function testGetRules(): void

static::assertEquals([
new DelimiterSpacingRule(),
new NamedArgumentSpacingRule(),
new OperatorNameSpacingRule(),
new OperatorSpacingRule(),
new PunctuationSpacingRule(),
Expand Down

0 comments on commit e47f04b

Please sign in to comment.