Skip to content

Commit c1a0dc3

Browse files
committed
[Twig] Improves Parser exception
(following Twig #4601 : twigphp/Twig#4601)
1 parent eb9d397 commit c1a0dc3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/TwigComponent/src/Twig/ComponentTokenParser.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\TwigComponent\Twig;
1313

1414
use Symfony\UX\TwigComponent\BlockStack;
15+
use Twig\Error\SyntaxError;
1516
use Twig\Node\Expression\AbstractExpression;
1617
use Twig\Node\Expression\ArrayExpression;
1718
use Twig\Node\Expression\ConstantExpression;
@@ -33,13 +34,18 @@ final class ComponentTokenParser extends AbstractTokenParser
3334
public function parse(Token $token): Node
3435
{
3536
$stream = $this->parser->getStream();
37+
3638
if (method_exists($this->parser, 'parseExpression')) {
3739
// Since Twig 3.21
3840
$componentName = $this->componentName($this->parser->parseExpression());
3941
} else {
4042
$componentName = $this->componentName($this->parser->getExpressionParser()->parseExpression());
4143
}
4244

45+
if (null === $componentName) {
46+
throw new SyntaxError('Could not parse component name.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
47+
}
48+
4349
[$propsExpression, $only] = $this->parseArguments();
4450

4551
// Write a fake: "extends __parent__" into the "embedded" template.
@@ -80,7 +86,7 @@ public function getTag(): string
8086
return 'component';
8187
}
8288

83-
private function componentName(AbstractExpression $expression): string
89+
private function componentName(AbstractExpression $expression): ?string
8490
{
8591
if ($expression instanceof ConstantExpression) { // using {% component 'name' %}
8692
return $expression->getAttribute('value');
@@ -90,7 +96,7 @@ private function componentName(AbstractExpression $expression): string
9096
return $expression->getAttribute('name');
9197
}
9298

93-
throw new \LogicException('Could not parse component name.');
99+
return null;
94100
}
95101

96102
/**

src/TwigComponent/tests/Integration/Twig/ComponentParserTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,19 @@ public function testAcceptHtmlSelfClosingComponentTagWithValidComponentName(stri
5959

6060
$template = $environment->createTemplate($source);
6161

62-
self::assertInstanceOf(TemplateWrapper::class, $template);
62+
$this->assertInstanceOf(TemplateWrapper::class, $template);
63+
}
64+
65+
public function testItThrowsWhenComponentNameCannotBeParsed(): void
66+
{
67+
$environment = $this->createEnvironment();
68+
$source = "{% component [] %}{% endcomponent %}";
69+
70+
$this->expectException(SyntaxError::class);
71+
$this->expectExceptionMessage('Could not parse component name in "foo.html.twig');
72+
$this->expectExceptionMessage(')" at line 1.');
73+
74+
$environment->createTemplate($source, 'foo.html.twig');
6375
}
6476

6577
public static function provideValidComponentNames(): iterable

0 commit comments

Comments
 (0)