Skip to content

Commit

Permalink
Support type block
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Nov 5, 2024
1 parent d73fa57 commit 5899cd5
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,18 @@ private function lexOperator(string $operator): void
}
}

$this->pushToken(Token::OPERATOR_TYPE, $operator);
} elseif ('?:' === $operator) {
if (
self::STATE_BLOCK === $this->getState()
&& 'types' === $this->getStateParam('blockName')
) {
// This is an optional variable type declaration instead
$this->pushToken(Token::PUNCTUATION_TYPE, $operator);

return;
}

$this->pushToken(Token::OPERATOR_TYPE, $operator);
} else {
$this->pushToken(Token::OPERATOR_TYPE, $operator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ Untouch +-/*%==:
{{ a is not array }}
{{ a is
not array or b is foo }}

{% types {foo: 'int', bar?: 'string'} %}
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ Untouch +-/*%==:
{{ a is not array }}
{{ a is
not array or b is foo }}

{% types {foo: 'int', bar?: 'string'} %}
1 change: 1 addition & 0 deletions tests/Rules/String/HashQuote/HashQuoteRuleTest.fixed.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
{% set needQuote = {'data-foo': a} %}

{% set namedArgument = foo(bar: true, baz: false) %}
{% types {foo: 'int', bar?: 'string'} %}
1 change: 1 addition & 0 deletions tests/Rules/String/HashQuote/HashQuoteRuleTest.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
{% set needQuote = {'data-foo': a} %}

{% set namedArgument = foo(bar: true, baz: false) %}
{% types {foo: 'int', bar?: 'string'} %}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
{% set needQuote = {'data-foo': a} %}

{% set namedArgument = foo(bar: true, baz: false) %}
{% types {foo: 'int', bar?: 'string'} %}
1 change: 1 addition & 0 deletions tests/Token/Tokenizer/Fixtures/test16.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% types {foo: 'int', bar?: 'string'} %}
26 changes: 26 additions & 0 deletions tests/Token/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,32 @@ public static function tokenizeDataProvider(): iterable
78 => Token::EOF_TYPE,
],
];

yield [
__DIR__.'/Fixtures/test16.twig',
[
0 => Token::BLOCK_START_TYPE,
1 => Token::WHITESPACE_TYPE,
2 => Token::BLOCK_NAME_TYPE,
3 => Token::WHITESPACE_TYPE,
4 => Token::PUNCTUATION_TYPE,
5 => Token::NAME_TYPE,
6 => Token::PUNCTUATION_TYPE,
7 => Token::WHITESPACE_TYPE,
8 => Token::STRING_TYPE,
9 => Token::PUNCTUATION_TYPE,
10 => Token::WHITESPACE_TYPE,
11 => Token::NAME_TYPE,
12 => Token::PUNCTUATION_TYPE,
13 => Token::WHITESPACE_TYPE,
14 => Token::STRING_TYPE,
15 => Token::PUNCTUATION_TYPE,
16 => Token::WHITESPACE_TYPE,
17 => Token::BLOCK_END_TYPE,
18 => Token::EOL_TYPE,
19 => Token::EOF_TYPE,
],
];
}

/**
Expand Down

0 comments on commit 5899cd5

Please sign in to comment.