Skip to content

Commit

Permalink
Support type block (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Nov 5, 2024
1 parent d73fa57 commit ce4b0f1
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Rules/Punctuation/PunctuationSpacingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class PunctuationSpacingRule extends AbstractSpacingRule implements Config
'.' => 0,
',' => 0,
'|' => 0,
'?:' => 0,
];
private const DEFAULT_SPACE_AFTER = [
'(' => 0,
Expand All @@ -32,6 +33,7 @@ final class PunctuationSpacingRule extends AbstractSpacingRule implements Config
'|' => 0,
':' => 1,
',' => 1,
'?:' => 1,
];

/** @var array<string, int|null> */
Expand Down
6 changes: 6 additions & 0 deletions src/Rules/String/HashQuoteRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ private function nameShouldBeString(int $tokenIndex, Tokens $tokens): void
// so we let the developer chose the right value.
$fixable = $this->isInteger($value);
} elseif ($token->isMatching(Token::NAME_TYPE)) {
$blockName = $tokens->findPrevious(Token::BLOCK_TOKENS, $tokenIndex - 1);
if (false !== $blockName && $tokens->get($blockName)->isMatching(Token::BLOCK_NAME_TYPE, 'types')) {
// {% types {'foo': 'int'} %} is not a valid syntax
return;
}

$fixable = true;
} else {
return;
Expand Down
6 changes: 6 additions & 0 deletions src/Token/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ final class Token
self::COMMENT_END_TYPE => self::COMMENT_END_TYPE,
];

public const BLOCK_TOKENS = [
self::BLOCK_START_TYPE => self::BLOCK_START_TYPE,
self::BLOCK_NAME_TYPE => self::BLOCK_NAME_TYPE,
self::BLOCK_END_TYPE => self::BLOCK_END_TYPE,
];

public function __construct(
private int|string $type,
private int $line,
Expand Down
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'} %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
{{ a|trans }}
{{ [{a: 1,},] }}
{{ 'test'[1:2] }}
{% types {foo: 'int', bar?: 'string'} %}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testConfiguration(): void
'.' => 0,
',' => 0,
'|' => 0,
'?:' => 0,
],
'after' => [
'(' => 0,
Expand All @@ -30,6 +31,7 @@ public function testConfiguration(): void
'|' => 0,
':' => 1,
',' => 1,
'?:' => 1,
],
],
(new PunctuationSpacingRule())->getConfiguration()
Expand All @@ -45,6 +47,7 @@ public function testConfiguration(): void
'.' => 0,
',' => 0,
'|' => 0,
'?:' => 0,
],
'after' => [
'{' => null,
Expand All @@ -54,6 +57,7 @@ public function testConfiguration(): void
'|' => 0,
':' => 1,
',' => 1,
'?:' => 1,
],
],
(new PunctuationSpacingRule(['}' => null], ['{' => null]))->getConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
{{ a | trans }}
{{ [{a: 1, }, ] }}
{{ 'test'[1:2] }}
{% 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 ce4b0f1

Please sign in to comment.