Skip to content

Commit 203cafa

Browse files
committed
TemplateGenerator: don't emit /** source */ comment for multi-line names
(after 046594f)
1 parent 576e8b3 commit 203cafa

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎src/Latte/Compiler/TemplateGenerator.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public function generate(
5454
$this->addConstant('ContentType', $node->contentType);
5555
}
5656

57-
if ($templateName !== null && !preg_match('#\n|\?#', $templateName)) {
57+
$templateName = $templateName === null || preg_match('#\n|\?#', $templateName) ? null : $templateName;
58+
if ($templateName !== null) {
5859
$this->addConstant('Source', $templateName);
5960
}
6061

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
use Tester\Assert;
4+
5+
require __DIR__ . '/../bootstrap.php';
6+
7+
8+
test('simple name is emitted as Source constant and /** source */ comment', function () {
9+
$latte = createLatte();
10+
$code = $latte->compile('hello.latte');
11+
Assert::contains("public const Source = 'hello.latte';", $code);
12+
Assert::contains('/** source: hello.latte */', $code);
13+
});
14+
15+
16+
test('multi-line name (e.g. string loader content) is not emitted', function () {
17+
$latte = createLatte();
18+
$code = $latte->compile("{switch 0}\n{case ''}string\n{/switch}");
19+
Assert::notContains('public const Source', $code);
20+
Assert::notContains('/** source:', $code);
21+
});
22+
23+
24+
test('name with question mark is not emitted', function () {
25+
$latte = createLatte();
26+
$code = $latte->compile('template?query');
27+
Assert::notContains('public const Source', $code);
28+
Assert::notContains('/** source:', $code);
29+
});

0 commit comments

Comments
 (0)