Skip to content

Commit d7befba

Browse files
committed
FormNode: deprecate missing comma before {form} arguments
The {form name attr=val} syntax (no comma between the name and the attributes) was tolerated. Trigger E_USER_DEPRECATED when a token follows the name without a comma, pointing to the offending column. The form still compiles — this only nudges users toward the canonical {form name, attr=val} so future syntax extensions can use bare keywords next to the name unambiguously.
1 parent 3638828 commit d7befba

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

src/Bridges/FormsLatte/Nodes/FormNode.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ public static function create(Tag $tag): \Generator
4141
$tag->expectArguments();
4242
$node = $tag->node = new static;
4343
$node->name = $tag->parser->parseUnquotedStringOrExpression();
44-
$tag->parser->stream->tryConsume(',');
44+
if (!$tag->parser->stream->tryConsume(',') && !$tag->parser->isEnd()) {
45+
$position = $tag->parser->stream->peek()->position;
46+
trigger_error("Missing comma before arguments in {{$tag->name}} tag $position.", E_USER_DEPRECATED);
47+
}
4548
$node->attributes = $tag->parser->parseArguments();
4649
$node->print = $tag->name === 'form';
4750

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types=1);
2+
3+
use Nette\Bridges\FormsLatte\FormsExtension;
4+
use Nette\Forms\Form;
5+
use Tester\Assert;
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
10+
$form = new Form;
11+
$form->addText('name');
12+
13+
$latte = new Latte\Engine;
14+
$latte->setLoader(new Latte\Loaders\StringLoader);
15+
$latte->addExtension(new FormsExtension);
16+
$latte->addProvider('uiControl', ['myForm' => $form]);
17+
18+
19+
test('no warning when only the form name is given', function () use ($latte) {
20+
Assert::noError(fn() => $latte->compile("{form myForm}{/form}\n"));
21+
});
22+
23+
24+
test('no warning when comma separates name and arguments', function () use ($latte) {
25+
Assert::noError(fn() => $latte->compile("{form myForm, class => 'a'}{/form}\n"));
26+
});
27+
28+
29+
test('deprecation warning when comma is missing before arguments', function () use ($latte) {
30+
Assert::error(
31+
fn() => $latte->compile("{form myForm class => 'a'}{/form}\n"),
32+
E_USER_DEPRECATED,
33+
'Missing comma before arguments in {form} tag on line 1 at column 14.',
34+
);
35+
});
36+
37+
38+
test('deprecation warning also fires for {formContext}', function () use ($latte) {
39+
Assert::error(
40+
fn() => $latte->compile("{formContext myForm extra}{/formContext}\n"),
41+
E_USER_DEPRECATED,
42+
'Missing comma before arguments in {formContext} tag on line 1 at column 21.',
43+
);
44+
});

tests/Forms.Latte/templates/forms.latte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{form myForm id => myForm, class=>"ajax"}
1+
{form myForm, id => myForm, class=>"ajax"}
22
{foreach [id, username, select, area, send] as $name}
33
{label $name /}
44
{input $name title => Hello, size => 10}

0 commit comments

Comments
 (0)