Skip to content

Commit

Permalink
Introduce NAMED_ARGUMENT_OPERATOR_TYPE
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 17, 2024
1 parent 10bdf19 commit 3081cdc
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ The `TwigCsFixer\Token\Tokenizer` transform the file into a list of tokens which

The `#}` delimiter.

- **TwigCsFixer\Token\Token::NAMED_ARGUMENT_OPERATOR_TYPE**:

The `=` or `:` operator used when using named argument. Like `{{ foo(bar=true, baz: false) }}`.

Then, the easiest way to write a custom rule is to implement the `TwigCsFixer\Rules\AbstractRule` class
or the `TwigCsFixer\Rules\AbstractFixableRule` if the rule can be automatically fixed.

Expand Down
1 change: 1 addition & 0 deletions src/Token/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ final class Token
public const COMMENT_TAB_TYPE = 'COMMENT_TAB_TYPE';
public const COMMENT_EOL_TYPE = 'COMMENT_EOL_TYPE';
public const COMMENT_END_TYPE = 'COMMENT_END_TYPE';
public const NAMED_ARGUMENT_OPERATOR_TYPE = 'NAMED_ARGUMENT_OPERATOR_TYPE';

public const WHITESPACE_TOKENS = [
self::WHITESPACE_TYPE => self::WHITESPACE_TYPE,
Expand Down
20 changes: 20 additions & 0 deletions src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@ private function lexArrowFunction(): void
$this->pushToken(Token::ARROW_TYPE, '=>');
}

/**
* @throws CannotTokenizeException
*/
private function lexOperator(string $operator): void

Check failure on line 585 in src/Token/Tokenizer.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method TwigCsFixer\Token\Tokenizer::lexOperator() has TwigCsFixer\Exception\CannotTokenizeException in PHPDoc @throws tag but it's not thrown.
{
if ('?' === $operator) {
Expand All @@ -587,6 +590,17 @@ private function lexOperator(string $operator): void
} elseif (':' === $operator && $this->isInTernary()) {
$bracket = array_pop($this->bracketsAndTernary);
$this->pushToken(Token::OPERATOR_TYPE, $operator, $bracket);
} elseif ('=' === $operator) {
$bracket = end($this->bracketsAndTernary);
if (false !== $bracket && '(' === $bracket->getValue()) {
// This is a named argument operator instead
$this->pushToken(Token::NAMED_ARGUMENT_OPERATOR_TYPE, $operator);

return;
}


$this->pushToken(Token::OPERATOR_TYPE, $operator);
} else {
$this->pushToken(Token::OPERATOR_TYPE, $operator);
}
Expand Down Expand Up @@ -681,6 +695,12 @@ private function lexPunctuation(): void

return;
}
if ('(' === $bracket->getValue()) {
// This is a named argument operator instead
$this->pushToken(Token::NAMED_ARGUMENT_OPERATOR_TYPE, $currentCode);

return;
}

$this->pushToken(Token::PUNCTUATION_TYPE, $currentCode);
} else {
Expand Down
2 changes: 2 additions & 0 deletions tests/Rules/String/HashQuote/HashQuoteRuleTest.fixed.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
{% set float = {'12.3': a} %}

{% set needQuote = {'data-foo': a} %}

{% set namedArgument = foo(bar: true, baz: false) %}
2 changes: 2 additions & 0 deletions tests/Rules/String/HashQuote/HashQuoteRuleTest.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
{% set float = {'12.3': a} %}

{% set needQuote = {'data-foo': a} %}

{% set namedArgument = foo(bar: true, baz: false) %}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
{% set float = {'12.3': a} %}

{% set needQuote = {'data-foo': a} %}

{% set namedArgument = foo(bar: true, baz: false) %}
5 changes: 5 additions & 0 deletions tests/Token/Tokenizer/Fixtures/test15.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% set foo=1 %}
{{ foo(foo=1) }}
{{ foo(foo:1) }}
{{ foo({foo:1}) }}
{{ (foo==1) }}
62 changes: 62 additions & 0 deletions tests/Token/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,68 @@ public static function tokenizeDataProvider(): iterable
36 => Token::EOF_TYPE,
],
];

yield [
__DIR__.'/Fixtures/test15.twig',
[
0 => Token::BLOCK_START_TYPE,
1 => Token::WHITESPACE_TYPE,
2 => Token::BLOCK_NAME_TYPE,
3 => Token::WHITESPACE_TYPE,
4 => Token::NAME_TYPE,
5 => Token::OPERATOR_TYPE,
6 => Token::NUMBER_TYPE,
7 => Token::WHITESPACE_TYPE,
8 => Token::BLOCK_END_TYPE,
9 => Token::EOL_TYPE,
10 => Token::VAR_START_TYPE,
11 => Token::WHITESPACE_TYPE,
12 => Token::FUNCTION_NAME_TYPE,
13 => Token::PUNCTUATION_TYPE,
14 => Token::NAME_TYPE,
15 => Token::NAMED_ARGUMENT_OPERATOR_TYPE,
16 => Token::NUMBER_TYPE,
17 => Token::PUNCTUATION_TYPE,
18 => Token::WHITESPACE_TYPE,
19 => Token::VAR_END_TYPE,
20 => Token::EOL_TYPE,
21 => Token::VAR_START_TYPE,
22 => Token::WHITESPACE_TYPE,
23 => Token::FUNCTION_NAME_TYPE,
24 => Token::PUNCTUATION_TYPE,
25 => Token::NAME_TYPE,
26 => Token::NAMED_ARGUMENT_OPERATOR_TYPE,
27 => Token::NUMBER_TYPE,
28 => Token::PUNCTUATION_TYPE,
29 => Token::WHITESPACE_TYPE,
30 => Token::VAR_END_TYPE,
31 => Token::EOL_TYPE,
32 => Token::VAR_START_TYPE,
33 => Token::WHITESPACE_TYPE,
34 => Token::FUNCTION_NAME_TYPE,
35 => Token::PUNCTUATION_TYPE,
36 => Token::PUNCTUATION_TYPE,
37 => Token::NAME_TYPE,
38 => Token::PUNCTUATION_TYPE,
39 => Token::NUMBER_TYPE,
40 => Token::PUNCTUATION_TYPE,
41 => Token::PUNCTUATION_TYPE,
42 => Token::WHITESPACE_TYPE,
43 => Token::VAR_END_TYPE,
44 => Token::EOL_TYPE,
45 => Token::VAR_START_TYPE,
46 => Token::WHITESPACE_TYPE,
47 => Token::PUNCTUATION_TYPE,
48 => Token::NAME_TYPE,
49 => Token::OPERATOR_TYPE,
50 => Token::NUMBER_TYPE,
51 => Token::PUNCTUATION_TYPE,
52 => Token::WHITESPACE_TYPE,
53 => Token::VAR_END_TYPE,
54 => Token::EOL_TYPE,
55 => Token::EOF_TYPE,
],
];
}

/**
Expand Down

0 comments on commit 3081cdc

Please sign in to comment.