Skip to content

Commit b70e5da

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

File tree

6 files changed

+93
-73
lines changed

6 files changed

+93
-73
lines changed

src/Bridges/FormsLatte/FormMacros.php

+23-21
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,22 @@ public static function install(Latte\Compiler $compiler)
4949
public function macroForm(MacroNode $node, PhpWriter $writer)
5050
{
5151
if ($node->modifiers) {
52-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
52+
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
5353
}
5454
if ($node->prefix) {
5555
throw new CompileException('Did you mean <form n:name=...> ?');
5656
}
5757
$name = $node->tokenizer->fetchWord();
5858
if ($name === FALSE) {
59-
throw new CompileException("Missing form name in {{$node->name}}.");
59+
throw new CompileException('Missing form name in ' . $node->getNotation());
6060
}
6161
$node->replaced = true;
6262
$node->tokenizer->reset();
6363
return $writer->write(
64-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsStack[] = '
64+
"/* line $node->startLine */\n"
65+
. 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $_form = $this->global->formsStack[] = '
6566
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
66-
. '$this->global->uiControl[%node.word], %node.array)'
67+
. '$this->global->uiControl[%node.word], %node.array);'
6768
);
6869
}
6970

@@ -74,17 +75,17 @@ public function macroForm(MacroNode $node, PhpWriter $writer)
7475
public function macroFormContainer(MacroNode $node, PhpWriter $writer)
7576
{
7677
if ($node->modifiers) {
77-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
78+
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
7879
}
7980
$name = $node->tokenizer->fetchWord();
8081
if ($name === FALSE) {
81-
throw new CompileException("Missing name in {{$node->name}}.");
82+
throw new CompileException('Missing name in ' . $node->getNotation());
8283
}
8384
$node->tokenizer->reset();
8485
return $writer->write(
8586
'$this->global->formsStack[] = $formContainer = $_form = '
8687
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
87-
. 'end($this->global->formsStack)[%node.word]'
88+
. 'end($this->global->formsStack)[%node.word];'
8889
);
8990
}
9091

@@ -95,11 +96,11 @@ public function macroFormContainer(MacroNode $node, PhpWriter $writer)
9596
public function macroLabel(MacroNode $node, PhpWriter $writer)
9697
{
9798
if ($node->modifiers) {
98-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
99+
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
99100
}
100101
$words = $node->tokenizer->fetchWords();
101102
if (!$words) {
102-
throw new CompileException("Missing name in {{$node->name}}.");
103+
throw new CompileException('Missing name in ' . $node->getNotation());
103104
}
104105
$node->replaced = true;
105106
$name = array_shift($words);
@@ -131,18 +132,19 @@ public function macroLabelEnd(MacroNode $node, PhpWriter $writer)
131132
public function macroInput(MacroNode $node, PhpWriter $writer)
132133
{
133134
if ($node->modifiers) {
134-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
135+
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
135136
}
136137
$words = $node->tokenizer->fetchWords();
137138
if (!$words) {
138-
throw new CompileException("Missing name in {{$node->name}}.");
139+
throw new CompileException('Missing name in ' . $node->getNotation());
139140
}
140141
$node->replaced = true;
141142
$name = array_shift($words);
142143
return $writer->write(
143144
($name[0] === '$' ? '$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; echo $_input' : 'echo end($this->global->formsStack)[%0.word]')
144145
. '->%1.raw'
145-
. ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : ''),
146+
. ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : '')
147+
. " /* line $node->startLine */",
146148
$name,
147149
$words ? 'getControlPart(' . implode(', ', array_map([$writer, 'formatWord'], $words)) . ')' : 'getControl()'
148150
);
@@ -156,11 +158,11 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
156158
{
157159
$words = $node->tokenizer->fetchWords();
158160
if (!$words) {
159-
throw new CompileException("Missing name in n:{$node->name}.");
161+
throw new CompileException('Missing name in ' . $node->getNotation());
160162
}
161163
$name = array_shift($words);
162164
$tagName = strtolower($node->htmlNode->name);
163-
$node->isEmpty = $tagName === 'input';
165+
$node->empty = $tagName === 'input';
164166

165167
if ($tagName === 'form') {
166168
return $writer->write(
@@ -198,13 +200,13 @@ public function macroNameEnd(MacroNode $node, PhpWriter $writer)
198200
{
199201
$tagName = strtolower($node->htmlNode->name);
200202
if ($tagName === 'form') {
201-
$node->innerContent .= '<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), FALSE) ?>';
203+
$node->innerContent .= '<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), FALSE); ?>';
202204
} elseif ($tagName === 'label') {
203-
if ($node->htmlNode->isEmpty) {
205+
if ($node->htmlNode->empty) {
204206
$node->innerContent = "<?php echo \$_input->getLabelPart()->getHtml() ?>";
205207
}
206208
} elseif ($tagName === 'button') {
207-
if ($node->htmlNode->isEmpty) {
209+
if ($node->htmlNode->empty) {
208210
$node->innerContent = '<?php echo htmlspecialchars($_input->caption) ?>';
209211
}
210212
} else { // select, textarea
@@ -219,16 +221,16 @@ public function macroNameEnd(MacroNode $node, PhpWriter $writer)
219221
public function macroInputError(MacroNode $node, PhpWriter $writer)
220222
{
221223
if ($node->modifiers) {
222-
throw new CompileException("Modifiers are not allowed in {{$node->name}}");
224+
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
223225
}
224226
$name = $node->tokenizer->fetchWord();
225227
$node->replaced = true;
226228
if (!$name) {
227-
return $writer->write('echo %escape($_input->getError())');
229+
return $writer->write('echo %escape($_input->getError());');
228230
} 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);
231+
return $writer->write('$_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; echo %escape($_input->getError());', $name);
230232
} else {
231-
return $writer->write('echo %escape(end($this->global->formsStack)[%0.word]->getError())', $name);
233+
return $writer->write('echo %escape(end($this->global->formsStack)[%0.word]->getError());', $name);
232234
}
233235
}
234236

tests/Forms.Latte/FormMacros.error.phpt

+5-5
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

+3-1
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

+21-18
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)