Skip to content

[Twig] Improve Parser exceptions (minor) #2729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/TwigComponent/src/Twig/ComponentTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\UX\TwigComponent\Twig;

use Symfony\UX\TwigComponent\BlockStack;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\ArrayExpression;
use Twig\Node\Expression\ConstantExpression;
Expand All @@ -33,13 +34,18 @@ final class ComponentTokenParser extends AbstractTokenParser
public function parse(Token $token): Node
{
$stream = $this->parser->getStream();

if (method_exists($this->parser, 'parseExpression')) {
// Since Twig 3.21
$componentName = $this->componentName($this->parser->parseExpression());
} else {
$componentName = $this->componentName($this->parser->getExpressionParser()->parseExpression());
}

if (null === $componentName) {
throw new SyntaxError('Could not parse component name.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
}

[$propsExpression, $only] = $this->parseArguments();

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

private function componentName(AbstractExpression $expression): string
private function componentName(AbstractExpression $expression): ?string
{
if ($expression instanceof ConstantExpression) { // using {% component 'name' %}
return $expression->getAttribute('value');
Expand All @@ -90,7 +96,7 @@ private function componentName(AbstractExpression $expression): string
return $expression->getAttribute('name');
}

throw new \LogicException('Could not parse component name.');
return null;
}

/**
Expand Down
25 changes: 19 additions & 6 deletions src/TwigComponent/tests/Integration/Twig/ComponentParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Twig\Environment;
use Twig\Error\SyntaxError;
use Twig\Loader\ArrayLoader;
use Twig\TemplateWrapper;

Expand All @@ -28,38 +29,50 @@ final class ComponentParserTest extends KernelTestCase
*/
public function testAcceptTwigComponentTagWithValidComponentName(string $name): void
{
$environment = self::createEnvironment();
$environment = $this->createEnvironment();
$source = str_replace('XXX', $name, "{% component 'XXX' %}{% endcomponent %}");

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

self::assertInstanceOf(TemplateWrapper::class, $template);
$this->assertInstanceOf(TemplateWrapper::class, $template);
}

/**
* @dataProvider provideValidComponentNames
*/
public function testAcceptHtmlComponentTagWithValidComponentName(string $name): void
{
$environment = self::createEnvironment();
$environment = $this->createEnvironment();
$source = \sprintf('<twig:%s></twig:%s>', $name, $name);

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

self::assertInstanceOf(TemplateWrapper::class, $template);
$this->assertInstanceOf(TemplateWrapper::class, $template);
}

/**
* @dataProvider provideValidComponentNames
*/
public function testAcceptHtmlSelfClosingComponentTagWithValidComponentName(string $name): void
{
$environment = self::createEnvironment();
$environment = $this->createEnvironment();
$source = \sprintf('<twig:%s />', $name);

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

self::assertInstanceOf(TemplateWrapper::class, $template);
$this->assertInstanceOf(TemplateWrapper::class, $template);
}

public function testItThrowsWhenComponentNameCannotBeParsed(): void
{
$environment = $this->createEnvironment();
$source = '{% component [] %}{% endcomponent %}';

$this->expectException(SyntaxError::class);
$this->expectExceptionMessage('Could not parse component name in "foo.html.twig');
$this->expectExceptionMessage(')" at line 1.');

$environment->createTemplate($source, 'foo.html.twig');
}

public static function provideValidComponentNames(): iterable
Expand Down
Loading