From e47f04bc4963622af7209a0a56cec54d12ac5fb8 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 17 Aug 2024 16:39:15 +0200 Subject: [PATCH] Introduce rule for named argument operator --- docs/rules.md | 5 +++ .../Operator/NamedArgumentSpacingRule.php | 39 +++++++++++++++++++ .../NamedArgumentSpacingRuleTest.fixed.twig | 2 + .../NamedArgumentSpacingRuleTest.php | 21 ++++++++++ .../NamedArgumentSpacingRuleTest.twig | 2 + tests/Standard/TwigTest.php | 2 + 6 files changed, 71 insertions(+) create mode 100644 src/Rules/Operator/NamedArgumentSpacingRule.php create mode 100644 tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.fixed.twig create mode 100644 tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.php create mode 100644 tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.twig diff --git a/docs/rules.md b/docs/rules.md index 3d5ec245..d476954f 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -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. @@ -146,6 +150,7 @@ new TwigCsFixer\Rules\Whitespace\IndentRule(3); **Twig**: - DelimiterSpacingRule +- NamedArgumentSpacingRule - OperatorNameSpacingRule - OperatorSpacingRule - PunctuationSpacingRule diff --git a/src/Rules/Operator/NamedArgumentSpacingRule.php b/src/Rules/Operator/NamedArgumentSpacingRule.php new file mode 100644 index 00000000..931cb60e --- /dev/null +++ b/src/Rules/Operator/NamedArgumentSpacingRule.php @@ -0,0 +1,39 @@ +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; + } +} diff --git a/tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.fixed.twig b/tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.fixed.twig new file mode 100644 index 00000000..bf48e841 --- /dev/null +++ b/tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.fixed.twig @@ -0,0 +1,2 @@ +{{ foo(bar=true, baz: 42) }} +{% macro foo(bar = true) %} diff --git a/tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.php b/tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.php new file mode 100644 index 00000000..840daada --- /dev/null +++ b/tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.php @@ -0,0 +1,21 @@ +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.', + ]); + } +} diff --git a/tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.twig b/tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.twig new file mode 100644 index 00000000..15aa370c --- /dev/null +++ b/tests/Rules/Operator/NamedArgumentSpacing/NamedArgumentSpacingRuleTest.twig @@ -0,0 +1,2 @@ +{{ foo(bar = true, baz :42) }} +{% macro foo(bar = true) %} diff --git a/tests/Standard/TwigTest.php b/tests/Standard/TwigTest.php index 27d8a82c..78316406 100644 --- a/tests/Standard/TwigTest.php +++ b/tests/Standard/TwigTest.php @@ -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; @@ -20,6 +21,7 @@ public function testGetRules(): void static::assertEquals([ new DelimiterSpacingRule(), + new NamedArgumentSpacingRule(), new OperatorNameSpacingRule(), new OperatorSpacingRule(), new PunctuationSpacingRule(),