-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add CommentNode to the AST #4009
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||
<?php | ||||||
|
||||||
/* | ||||||
* This file is part of Twig. | ||||||
* | ||||||
* (c) Fabien Potencier | ||||||
* (c) Armin Ronacher | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be removed |
||||||
* | ||||||
* For the full copyright and license information, please view the LICENSE | ||||||
* file that was distributed with this source code. | ||||||
*/ | ||||||
|
||||||
namespace Twig\Node; | ||||||
|
||||||
use Twig\Attribute\YieldReady; | ||||||
use Twig\Compiler; | ||||||
|
||||||
/** | ||||||
* Represents a comment node. | ||||||
* | ||||||
* @author Jeroen Versteeg <[email protected]> | ||||||
*/ | ||||||
#[YieldReady] | ||||||
class CommentNode extends Node | ||||||
{ | ||||||
public function __construct(string $data, int $lineno) | ||||||
{ | ||||||
parent::__construct([], ['text' => $data], $lineno); | ||||||
} | ||||||
|
||||||
public function compile(Compiler $compiler): void | ||||||
{ | ||||||
// skip comments in compilation | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -353,6 +353,26 @@ public function testUnterminatedBlock() | |||||||||||||
$lexer->tokenize(new Source($template, 'index')); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public function testCommentValues() | ||||||||||||||
{ | ||||||||||||||
$template = '{# comment #}some text{#another one#}'; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
$lexer = new Lexer(new Environment($this->createMock(LoaderInterface::class))); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
As this is how it's done in 4.x. |
||||||||||||||
$stream = $lexer->tokenize(new Source($template, 'index')); | ||||||||||||||
|
||||||||||||||
self::assertEquals( | ||||||||||||||
'comment', // assert that whitespace is stripped | ||||||||||||||
$stream->expect(Token::COMMENT_TYPE)->getValue() // implicit assertion is that expect() doesn't throw | ||||||||||||||
); | ||||||||||||||
self::assertEquals( | ||||||||||||||
'some text', | ||||||||||||||
$stream->expect(Token::TEXT_TYPE)->getValue() | ||||||||||||||
); | ||||||||||||||
self::assertEquals( | ||||||||||||||
'another one', // assert that comment is parsed | ||||||||||||||
$stream->expect(Token::COMMENT_TYPE)->getValue() | ||||||||||||||
); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public function testOverridingSyntax() | ||||||||||||||
{ | ||||||||||||||
$template = '[# comment #]{# variable #}/# if true #/true/# endif #/'; | ||||||||||||||
|
@@ -362,6 +382,7 @@ public function testOverridingSyntax() | |||||||||||||
'tag_variable' => ['{#', '#}'], | ||||||||||||||
]); | ||||||||||||||
$stream = $lexer->tokenize(new Source($template, 'index')); | ||||||||||||||
$stream->expect(Token::COMMENT_TYPE); | ||||||||||||||
$stream->expect(Token::VAR_START_TYPE); | ||||||||||||||
$stream->expect(Token::NAME_TYPE, 'variable'); | ||||||||||||||
$stream->expect(Token::VAR_END_TYPE); | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Twig\Tests\Node; | ||
|
||
/* | ||
* This file is part of Twig. | ||
* | ||
* (c) Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
use Twig\Node\CommentNode; | ||
use Twig\Test\NodeTestCase; | ||
|
||
class CommentTest extends NodeTestCase | ||
{ | ||
public function testConstructor() | ||
{ | ||
$node = new CommentNode('foo', 1); | ||
|
||
$this->assertEquals('foo', $node->getAttribute('text')); | ||
} | ||
|
||
public function getTests() | ||
{ | ||
return [ | ||
[new CommentNode('foo', 1), ''], | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.