Skip to content

Commit

Permalink
[TwigComponent] Minimal support of comment lines
Browse files Browse the repository at this point in the history
Twig introduced the inline comments in twigphp/Twig#4349

This PR add minimal support for it the PreLexer / HTML syntax

```twig
<twig:Button
    # comment
    bar="bar"
/>
```

I'd like some IRL feedbacks on this one :)
  • Loading branch information
smnandre committed Dec 22, 2024
1 parent c3ee75b commit 63f4160
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/TwigComponent/src/Twig/TwigPreLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ private function consumeAttributes(string $componentName): string
$isAttributeDynamic = false;

// :someProp="dynamicVar"
$this->consumeWhitespace();
if ($this->check(':')) {
$this->consume(':');
$isAttributeDynamic = true;
Expand All @@ -244,6 +245,7 @@ private function consumeAttributes(string $componentName): string

// <twig:component someProp> -> someProp: true
if (!$this->check('=')) {
$this->consumeWhitespace();
// don't allow "<twig:component :someProp>"
if ($isAttributeDynamic) {
throw new SyntaxError(\sprintf('Expected "=" after ":%s" when parsing the "<twig:%s" syntax.', $key, $componentName), $this->line);
Expand Down Expand Up @@ -332,6 +334,12 @@ private function consumeWhitespace(): void
$whitespace = substr($this->input, $this->position, strspn($this->input, " \t\n\r\0\x0B", $this->position));
$this->line += substr_count($whitespace, "\n");
$this->position += \strlen($whitespace);

if ($this->check('#')) {
$this->consume('#');
$this->consumeUntil("\n");
$this->consumeWhitespace();
}
}

/**
Expand Down
60 changes: 60 additions & 0 deletions src/TwigComponent/tests/Unit/TwigPreLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,5 +376,65 @@ public static function getLexTests(): iterable
'<twig:foobar bar="baz" {{ ...attr }}>content</twig:foobar>',
'{% component \'foobar\' with { bar: \'baz\', ...attr } %}{% block content %}content{% endblock %}{% endcomponent %}',
];
yield 'component_with_comment_line' => [
"<twig:foo \n # bar \n />",
'{{ component(\'foo\') }}',
];
yield 'component_with_comment_line_between_args' => [
<<<TWIG
<twig:foo
# bar
bar="baz"
/>
TWIG,
'{{ component(\'foo\', { bar: \'baz\' }) }}',
];
yield 'component_with_comment_lines_between_args' => [
<<<TWIG
<twig:foo
# comment
foo="foo"
# comment
bar="bar"
/>
TWIG,
'{{ component(\'foo\', { foo: \'foo\', bar: \'bar\' }) }}',
];
yield 'component_with_comment_line_containing_ending_tag' => [
<<<TWIG
<twig:foo
# comment /></twig:foo>
bar="bar"
/>
TWIG,
'{{ component(\'foo\', { bar: \'bar\' }) }}',
];
yield 'component_with_comment_line_in_argument_value' => [
<<<TWIG
<twig:foo
bar="# bar"
/>
TWIG,
'{{ component(\'foo\', { bar: \'# bar\' }) }}',
];
yield 'component_with_comment_line_in_argument_array_value_is_kept' => [
<<<TWIG
<twig:foo
bar="{{ {
a: 'b',
# comment
c: 'd'
} }}"
/>
TWIG,
// Twig will remove the comment, we don't need to remove it
<<<TWIG
{{ component('foo', { bar: ({
a: 'b',
# comment
c: 'd'
}) }) }}
TWIG,
];
}
}

0 comments on commit 63f4160

Please sign in to comment.