Skip to content

Commit fd381fb

Browse files
committed
FormMacros: added ; after some statement, {form} and {input} print line numbers
1 parent d306e45 commit fd381fb

File tree

6 files changed

+94
-73
lines changed

6 files changed

+94
-73
lines changed

src/Bridges/FormsLatte/FormMacros.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Nette;
1111
use Latte;
12+
use Latte\Helpers;
1213
use Latte\MacroNode;
1314
use Latte\PhpWriter;
1415
use Latte\CompileException;
@@ -49,21 +50,22 @@ public static function install(Latte\Compiler $compiler)
4950
public function macroForm(MacroNode $node, PhpWriter $writer)
5051
{
5152
if ($node->modifiers) {
52-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
53+
throw new CompileException('Modifiers are not allowed in ' . Helpers::formatNotation($node));
5354
}
5455
if ($node->prefix) {
5556
throw new CompileException('Did you mean <form n:name=...> ?');
5657
}
5758
$name = $node->tokenizer->fetchWord();
5859
if ($name === FALSE) {
59-
throw new CompileException("Missing form name in {{$node->name}}.");
60+
throw new CompileException('Missing form name in ' . Helpers::formatNotation($node));
6061
}
6162
$node->replaced = true;
6263
$node->tokenizer->reset();
6364
return $writer->write(
64-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsStack[] = '
65+
"/* line $node->startLine */\n"
66+
. 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsStack[] = '
6567
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
66-
. '$this->global->uiControl[%node.word], %node.array)'
68+
. '$this->global->uiControl[%node.word], %node.array);'
6769
);
6870
}
6971

@@ -74,17 +76,17 @@ public function macroForm(MacroNode $node, PhpWriter $writer)
7476
public function macroFormContainer(MacroNode $node, PhpWriter $writer)
7577
{
7678
if ($node->modifiers) {
77-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
79+
throw new CompileException('Modifiers are not allowed in ' . Helpers::formatNotation($node));
7880
}
7981
$name = $node->tokenizer->fetchWord();
8082
if ($name === FALSE) {
81-
throw new CompileException("Missing name in {{$node->name}}.");
83+
throw new CompileException('Missing name in ' . Helpers::formatNotation($node));
8284
}
8385
$node->tokenizer->reset();
8486
return $writer->write(
8587
'$this->global->formsStack[] = $formContainer = $_form = '
8688
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
87-
. 'end($this->global->formsStack)[%node.word]'
89+
. 'end($this->global->formsStack)[%node.word];'
8890
);
8991
}
9092

@@ -95,11 +97,11 @@ public function macroFormContainer(MacroNode $node, PhpWriter $writer)
9597
public function macroLabel(MacroNode $node, PhpWriter $writer)
9698
{
9799
if ($node->modifiers) {
98-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
100+
throw new CompileException('Modifiers are not allowed in ' . Helpers::formatNotation($node));
99101
}
100102
$words = $node->tokenizer->fetchWords();
101103
if (!$words) {
102-
throw new CompileException("Missing name in {{$node->name}}.");
104+
throw new CompileException('Missing name in ' . Helpers::formatNotation($node));
103105
}
104106
$node->replaced = true;
105107
$name = array_shift($words);
@@ -131,18 +133,19 @@ public function macroLabelEnd(MacroNode $node, PhpWriter $writer)
131133
public function macroInput(MacroNode $node, PhpWriter $writer)
132134
{
133135
if ($node->modifiers) {
134-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
136+
throw new CompileException('Modifiers are not allowed in ' . Helpers::formatNotation($node));
135137
}
136138
$words = $node->tokenizer->fetchWords();
137139
if (!$words) {
138-
throw new CompileException("Missing name in {{$node->name}}.");
140+
throw new CompileException('Missing name in ' . Helpers::formatNotation($node));
139141
}
140142
$node->replaced = true;
141143
$name = array_shift($words);
142144
return $writer->write(
143145
($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; echo $_input' : 'echo end($this->global->formsStack)[%0.word]')
144146
. '->%1.raw'
145-
. ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : ''),
147+
. ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : '')
148+
. " /* line $node->startLine */",
146149
$name,
147150
$words ? 'getControlPart(' . implode(', ', array_map([$writer, 'formatWord'], $words)) . ')' : 'getControl()'
148151
);
@@ -156,11 +159,11 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
156159
{
157160
$words = $node->tokenizer->fetchWords();
158161
if (!$words) {
159-
throw new CompileException("Missing name in n:{$node->name}.");
162+
throw new CompileException('Missing name in ' . Helpers::formatNotation($node));
160163
}
161164
$name = array_shift($words);
162165
$tagName = strtolower($node->htmlNode->name);
163-
$node->isEmpty = $tagName === 'input';
166+
$node->empty = $tagName === 'input';
164167

165168
if ($tagName === 'form') {
166169
return $writer->write(
@@ -198,13 +201,13 @@ public function macroNameEnd(MacroNode $node, PhpWriter $writer)
198201
{
199202
$tagName = strtolower($node->htmlNode->name);
200203
if ($tagName === 'form') {
201-
$node->innerContent .= '<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), FALSE) ?>';
204+
$node->innerContent .= '<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), FALSE); ?>';
202205
} elseif ($tagName === 'label') {
203-
if ($node->htmlNode->isEmpty) {
206+
if ($node->htmlNode->empty) {
204207
$node->innerContent = "<?php echo \$_input->getLabelPart()->getHtml() ?>";
205208
}
206209
} elseif ($tagName === 'button') {
207-
if ($node->htmlNode->isEmpty) {
210+
if ($node->htmlNode->empty) {
208211
$node->innerContent = '<?php echo htmlspecialchars($_input->caption) ?>';
209212
}
210213
} else { // select, textarea
@@ -219,16 +222,16 @@ public function macroNameEnd(MacroNode $node, PhpWriter $writer)
219222
public function macroInputError(MacroNode $node, PhpWriter $writer)
220223
{
221224
if ($node->modifiers) {
222-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
225+
throw new CompileException('Modifiers are not allowed in ' . Helpers::formatNotation($node));
223226
}
224227
$name = $node->tokenizer->fetchWord();
225228
$node->replaced = true;
226229
if (!$name) {
227-
return $writer->write('echo %escape($_input->getError())');
230+
return $writer->write('echo %escape($_input->getError());');
228231
} elseif ($name[0] === '$') {
229-
return $writer->write('$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; echo %escape($_input->getError())', $name);
232+
return $writer->write('$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; echo %escape($_input->getError());', $name);
230233
} else {
231-
return $writer->write('echo %escape(end($this->global->formsStack)[%0.word]->getError())', $name);
234+
return $writer->write('echo %escape(end($this->global->formsStack)[%0.word]->getError());', $name);
232235
}
233236
}
234237

tests/Forms.Latte/FormMacros.error.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Assert::exception(function () use ($latte) {
2222

2323
Assert::exception(function () use ($latte) {
2424
$latte->compile('<form n:name></form>');
25-
}, Latte\CompileException::class, 'Missing name in n:name.');
25+
}, Latte\CompileException::class, 'Missing name in n:name');
2626

2727
Assert::exception(function () use ($latte) {
2828
$latte->compile('<form n:inner-name></form>');
@@ -31,20 +31,20 @@ Assert::exception(function () use ($latte) {
3131

3232
Assert::exception(function () use ($latte) {
3333
$latte->compile('<html>{form /}');
34-
}, Latte\CompileException::class, 'Missing form name in {form}.');
34+
}, Latte\CompileException::class, 'Missing form name in {form}');
3535

3636
Assert::exception(function () use ($latte) {
3737
$latte->compile('<html>{formContainer /}');
38-
}, Latte\CompileException::class, 'Missing name in {formContainer}.');
38+
}, Latte\CompileException::class, 'Missing name in {formContainer}');
3939

4040

4141
Assert::exception(function () use ($latte) {
4242
$latte->compile('<html>{label /}');
43-
}, Latte\CompileException::class, 'Missing name in {label}.');
43+
}, Latte\CompileException::class, 'Missing name in {label}');
4444

4545
Assert::exception(function () use ($latte) {
4646
$latte->compile('<html>{input /}');
47-
}, Latte\CompileException::class, 'Missing name in {input}.');
47+
}, Latte\CompileException::class, 'Missing name in {input}');
4848

4949
Assert::exception(function () use ($latte) {
5050
$latte->compile('<html>{name /}');

tests/Forms.Latte/expected/FormMacros.button.phtml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class Template%a% extends Latte\Template
2222
<button<?php
2323
$_input = end($this->global->formsStack)["send"];
2424
echo $_input->getControlPart()->attributes() ?>><?php echo htmlspecialchars($_input->caption) ?></button>
25-
<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), FALSE) ?></form>
25+
<?php
26+
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), FALSE);
27+
?></form>
2628
<?php
2729
}
2830

tests/Forms.Latte/expected/FormMacros.formContainer.phtml

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,85 @@ class Template%a% extends Latte\Template
77
function render()
88
{
99
%A%
10-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) ?>
10+
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []);
11+
?>
1112

1213
<table>
1314
<tr>
1415
<th><?php if ($_label = end($this->global->formsStack)["input1"]->getLabel()) echo $_label ?></th>
15-
<td><?php echo end($this->global->formsStack)["input1"]->getControl() ?></td>
16+
<td><?php echo end($this->global->formsStack)["input1"]->getControl() /* line 5 */ ?></td>
1617
</tr>
17-
<?php $this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["cont1"] ?>
18+
<?php
19+
$this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["cont1"];
20+
?>
1821
<tr>
1922
<th><?php if ($_label = end($this->global->formsStack)["input2"]->getLabel()) echo $_label ?></th>
20-
<td><?php echo end($this->global->formsStack)["input2"]->getControl() ?></td>
23+
<td><?php echo end($this->global->formsStack)["input2"]->getControl() /* line 10 */ ?></td>
2124
</tr>
2225
<tr>
2326
<th><?php if ($_label = end($this->global->formsStack)["input3"]->getLabel()) echo $_label ?></th>
24-
<td><?php echo end($this->global->formsStack)["input3"]->getControl() ?></td>
27+
<td><?php echo end($this->global->formsStack)["input3"]->getControl() /* line 14 */ ?></td>
2528
</tr>
2629
<tr>
2730
<th>Checkboxes</th>
2831
<td>
29-
<?php $this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["cont2"] ?> <ol>
32+
<?php
33+
$this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["cont2"];
34+
?> <ol>
3035
<?php
3136
$iterations = 0;
32-
if (isset($this->params['name'])) trigger_error('Variable $name overwritten in foreach.');
33-
if (isset($this->params['field'])) trigger_error('Variable $field overwritten in foreach.');
3437
foreach ($formContainer->controls AS $name => $field) {
3538
?> <li><?php
3639
$_input = is_object($field) ? $field : end($this->global->formsStack)[$field];
37-
echo $_input->getControl() ?></li>
40+
echo $_input->getControl() /* line 20 */ ?></li>
3841
<?php
3942
$iterations++;
4043
}
41-
?> </ol>
44+
?> </ol>
4245
<?php
4346
array_pop($this->global->formsStack);
4447
$formContainer = $_form = end($this->global->formsStack) ?>
4548
</td>
4649
</tr>
4750
<tr>
4851
<th><?php if ($_label = end($this->global->formsStack)["input7"]->getLabel()) echo $_label ?></th>
49-
<td><?php echo end($this->global->formsStack)["input7"]->getControl() ?></td>
52+
<td><?php echo end($this->global->formsStack)["input7"]->getControl() /* line 26 */ ?></td>
5053
</tr>
5154
<?php
5255
array_pop($this->global->formsStack);
5356
$formContainer = $_form = end($this->global->formsStack);
54-
$this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["items"] ?>
57+
$this->global->formsStack[] = $formContainer = $_form = end($this->global->formsStack)["items"];
58+
?>
5559
<tr>
5660
<th>Items</th>
5761
<td>
5862
<?php
5963
$items = array(1, 2, 3);
6064
$iterations = 0;
61-
if (isset($this->params['item'])) trigger_error('Variable $item overwritten in foreach.');
6265
foreach ($items as $item) {
6366
if (!isset($formContainer[$item])) continue;
64-
$this->global->formsStack[] = $formContainer = $_form = is_object($item) ? $item : end($this->global->formsStack)[$item] ?> <?php
65-
echo end($this->global->formsStack)["input"]->getControl() ?>
67+
$this->global->formsStack[] = $formContainer = $_form = is_object($item) ? $item : end($this->global->formsStack)[$item];
68+
?> <?php echo end($this->global->formsStack)["input"]->getControl() /* line 37 */ ?>
6669

6770
<?php
6871
array_pop($this->global->formsStack);
6972
$formContainer = $_form = end($this->global->formsStack);
7073
$iterations++;
7174
}
72-
?>
75+
?>
7376
</td>
7477
</tr>
7578
<?php
7679
array_pop($this->global->formsStack);
7780
$formContainer = $_form = end($this->global->formsStack) ?>
7881
<tr>
7982
<th><?php if ($_label = end($this->global->formsStack)["input8"]->getLabel()) echo $_label ?></th>
80-
<td><?php echo end($this->global->formsStack)["input8"]->getControl() ?></td>
83+
<td><?php echo end($this->global->formsStack)["input8"]->getControl() /* line 45 */ ?></td>
8184
</tr>
8285
</table>
8386
<?php
8487
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
85-
?>
88+
?>
8689

8790
<?php
8891
}

0 commit comments

Comments
 (0)