Skip to content

Commit

Permalink
PhpWriter: fixed invalid code for ternary [Closes #208]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 14, 2020
1 parent 574bba7 commit bdf2d8d
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Latte/Compiler/PhpWriter.php
Expand Up @@ -302,7 +302,7 @@ public function optionalChainingPass(MacroTokens $tokens): MacroTokens

do {
if ($tokens->nextToken('?')) {
if ($tokens->isNext() && (!$tokens->isNext($tokens::T_CHAR) || $tokens->isNext('(', '[', '{', ':', '!', '@'))) { // is it ternary operator?
if ($tokens->isNext() && (!$tokens->isNext($tokens::T_CHAR) || $tokens->isNext('(', '[', '{', ':', '!', '@', '\\'))) { // is it ternary operator?
$expr->append($addBraces . ' ?');
break;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Latte/PhpWriter.formatArgs().phpt
Expand Up @@ -62,6 +62,7 @@ test(function () { // short ternary operators
Assert::same("fce() ?? 'a'", formatArgs('fce() ?? a')); // null coalesce is ignored
Assert::same("'a'?", formatArgs('a?')); // value must exists
Assert::same('$a ?(1) : null', formatArgs('$a?(1)')); // with braces
Assert::same('$a ? \Foo::BAR : null', formatArgs('$a ? \Foo::BAR'));
});


Expand Down
2 changes: 2 additions & 0 deletions tests/Latte/PhpWriter.optionalChainingPass().phpt
Expand Up @@ -75,4 +75,6 @@ test(function () { // ternary
Assert::same('(($_tmp = $a ?? null) === null ? null : $_tmp->foo) ? [1, 2, ([3 ? 2 : 1])] : $b', optionalChaining('$a?->foo ? [1, 2, ([3 ? 2 : 1])] : $b'));
Assert::same('(($_tmp = $a ?? null) === null ? null : ($_tmp->foo ?? null)) ? [1, 2, ([3 ? 2 : 1])] : $b', optionalChaining('$a?->foo? ? [1, 2, ([3 ? 2 : 1])] : $b'));
Assert::same('($a->foo ?? null) ? [1, 2, ([3 ? 2 : 1])] : $b', optionalChaining('$a->foo? ? [1, 2, ([3 ? 2 : 1])] : $b'));

Assert::same('$a ? \Foo::BAR : \Foo::BAR', optionalChaining('$a ? \Foo::BAR : \Foo::BAR'));
});

0 comments on commit bdf2d8d

Please sign in to comment.