-
-
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.
- Loading branch information
1 parent
8b843a1
commit 4571068
Showing
7 changed files
with
207 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Rules\String; | ||
|
||
use TwigCsFixer\Rules\AbstractFixableRule; | ||
use TwigCsFixer\Rules\ConfigurableRuleInterface; | ||
use TwigCsFixer\Runner\FixerInterface; | ||
use TwigCsFixer\Token\Token; | ||
use TwigCsFixer\Token\Tokenizer; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* Ensures that hashes key use quotes (or not). | ||
*/ | ||
final class HashQuoteRule extends AbstractFixableRule implements ConfigurableRuleInterface | ||
{ | ||
public function __construct(private bool $useQuote = true) | ||
{ | ||
} | ||
|
||
public function getConfiguration(): array | ||
{ | ||
return [ | ||
'useQuote' => $this->useQuote, | ||
]; | ||
} | ||
|
||
protected function process(int $tokenPosition, array $tokens): void | ||
{ | ||
$token = $tokens[$tokenPosition]; | ||
if (!$this->isTokenMatching($token, Token::PUNCTUATION_TYPE, ':')) { | ||
return; | ||
} | ||
|
||
$previous = $this->findPrevious(Token::EMPTY_TOKENS, $tokens, $tokenPosition - 1, true); | ||
Assert::notFalse($previous, 'A punctuation cannot be the first token.'); | ||
|
||
if ($this->useQuote) { | ||
$this->nameShouldBeString($previous, $tokens); | ||
} else { | ||
$this->stringShouldBeName($previous, $tokens); | ||
} | ||
} | ||
|
||
/** | ||
* @param array<int, Token> $tokens | ||
*/ | ||
private function nameShouldBeString(int $tokenPosition, array $tokens): void | ||
{ | ||
$token = $tokens[$tokenPosition]; | ||
|
||
$value = $token->getValue(); | ||
$error = sprintf('The hash key "%s" should be quoted.', $value); | ||
|
||
if ($this->isTokenMatching($token, Token::NUMBER_TYPE)) { | ||
// A value like `012` or `12.3` is cast to `12` by twig, | ||
// so we let the developer chose the right value. | ||
$fixable = $this->isInteger($value); | ||
} elseif ($this->isTokenMatching($token, Token::NAME_TYPE)) { | ||
$fixable = true; | ||
} else { | ||
return; | ||
} | ||
|
||
$fixer = $fixable | ||
? $this->addFixableError($error, $token) | ||
: $this->addError($error, $token); | ||
|
||
if ($fixer instanceof FixerInterface) { | ||
$success = $fixer->replaceToken($tokenPosition, '\''.$value.'\''); | ||
} | ||
} | ||
|
||
/** | ||
* @param array<int, Token> $tokens | ||
*/ | ||
private function stringShouldBeName(int $tokenPosition, array $tokens): void | ||
{ | ||
$token = $tokens[$tokenPosition]; | ||
if (!$this->isTokenMatching($token, Token::STRING_TYPE)) { | ||
return; | ||
} | ||
|
||
$expectedValue = substr($token->getValue(), 1, -1); | ||
if ( | ||
!$this->isInteger($expectedValue) | ||
&& 1 !== preg_match('/^'.Tokenizer::NAME_PATTERN.'$/', $expectedValue) | ||
) { | ||
return; | ||
} | ||
|
||
$fixer = $this->addFixableError( | ||
sprintf('The hash key "%s" does not require to be quoted.', $expectedValue), | ||
$token | ||
); | ||
if (null !== $fixer) { | ||
$fixer->replaceToken($tokenPosition, $expectedValue); | ||
} | ||
} | ||
|
||
private function isInteger(string $value): bool | ||
{ | ||
return $value === (string) (int) $value; | ||
} | ||
} |
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,17 @@ | ||
{% set foo = 42 %} | ||
{% set expression = {(foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz'} %} | ||
|
||
{% set conditional1 = true ? 'foo' : 'bar' %} | ||
{% set conditional2 = 'foo' ?: 'bar' %} | ||
|
||
{% set text = {'a': a} %} | ||
{% set text = {'a': a} %} | ||
|
||
{% set number = {'123': a} %} | ||
{% set number = {'123': a} %} | ||
{% set numberWithZero = {0123: a} %} {# It's the same than `{123: a}` #} | ||
{% set numberWithZero = {'0123': a} %} | ||
{% set float = {12.3: a} %} {# It's the same than `{123: a}` #} | ||
{% set float = {'12.3': a} %} | ||
|
||
{% set needQuote = {'data-foo': a} %} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Tests\Rules\String\HashQuote; | ||
|
||
use TwigCsFixer\Rules\String\HashQuoteRule; | ||
use TwigCsFixer\Tests\Rules\AbstractRuleTestCase; | ||
|
||
class HashQuoteRuleTest extends AbstractRuleTestCase | ||
{ | ||
public function testConfiguration(): void | ||
{ | ||
static::assertSame( | ||
['useQuote' => true], | ||
(new HashQuoteRule())->getConfiguration() | ||
); | ||
static::assertSame( | ||
['useQuote' => false], | ||
(new HashQuoteRule(false))->getConfiguration() | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->checkRule(new HashQuoteRule(), [ | ||
'HashQuote.Error:8:16' => 'The hash key "a" should be quoted.', | ||
'HashQuote.Error:10:18' => 'The hash key "123" should be quoted.', | ||
'HashQuote.Error:12:26' => 'The hash key "0123" should be quoted.', | ||
'HashQuote.Error:14:17' => 'The hash key "12.3" should be quoted.', | ||
]); | ||
} | ||
|
||
public function testRuleWithoutSingleQuote(): void | ||
{ | ||
$this->checkRule(new HashQuoteRule(false), [ | ||
'HashQuote.Error:7:16' => 'The hash key "a" does not require to be quoted.', | ||
'HashQuote.Error:11:18' => 'The hash key "123" does not require to be quoted.', | ||
], fixedFilePath: __DIR__.'/HashQuoteRuleTest.without.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,17 @@ | ||
{% set foo = 42 %} | ||
{% set expression = {(foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz'} %} | ||
|
||
{% set conditional1 = true ? 'foo' : 'bar' %} | ||
{% set conditional2 = 'foo' ?: 'bar' %} | ||
|
||
{% set text = {'a': a} %} | ||
{% set text = {a: a} %} | ||
|
||
{% set number = {123: a} %} | ||
{% set number = {'123': a} %} | ||
{% set numberWithZero = {0123: a} %} {# It's the same than `{123: a}` #} | ||
{% set numberWithZero = {'0123': a} %} | ||
{% set float = {12.3: a} %} {# It's the same than `{123: a}` #} | ||
{% set float = {'12.3': a} %} | ||
|
||
{% set needQuote = {'data-foo': a} %} |
17 changes: 17 additions & 0 deletions
17
tests/Rules/String/HashQuote/HashQuoteRuleTest.without.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,17 @@ | ||
{% set foo = 42 %} | ||
{% set expression = {(foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz'} %} | ||
|
||
{% set conditional1 = true ? 'foo' : 'bar' %} | ||
{% set conditional2 = 'foo' ?: 'bar' %} | ||
|
||
{% set text = {a: a} %} | ||
{% set text = {a: a} %} | ||
|
||
{% set number = {123: a} %} | ||
{% set number = {123: a} %} | ||
{% set numberWithZero = {0123: a} %} {# It's the same than `{123: a}` #} | ||
{% set numberWithZero = {'0123': a} %} | ||
{% set float = {12.3: a} %} {# It's the same than `{123: a}` #} | ||
{% set float = {'12.3': a} %} | ||
|
||
{% set needQuote = {'data-foo': a} %} |