Fixed wrong line numbers in error messages for multi-line statements

This commit is contained in:
Martin Hasoň
2014-10-23 17:29:14 +02:00
parent b9e3e6c51a
commit 08c51964e2
10 changed files with 70 additions and 12 deletions
+9 -1
View File
@@ -164,10 +164,18 @@ abstract class Twig_Node_Expression_Call extends Twig_Node_Expression
}
if (!empty($parameters)) {
$unknownParameter = null;
foreach ($parameters as $parameter) {
if ($parameter instanceof Twig_Node) {
$unknownParameter = $parameter;
break;
}
}
throw new Twig_Error_Syntax(sprintf(
'Unknown argument%s "%s" for %s "%s(%s)".',
count($parameters) > 1 ? 's' : '', implode('", "', array_keys($parameters)), $this->getAttribute('type'), $this->getAttribute('name'), implode(', ', $names)
));
), $unknownParameter ? $unknownParameter->getLine() : -1);
}
return $arguments;
+2
View File
@@ -26,6 +26,8 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
{
$name = $this->getAttribute('name');
$compiler->addDebugInfo($this);
if ($this->getAttribute('is_defined_test')) {
if ($this->isSpecial()) {
$compiler->repr(true);
+5 -3
View File
@@ -38,13 +38,15 @@ abstract class Twig_Test_NodeTestCase extends PHPUnit_Framework_TestCase
return new Twig_Environment();
}
protected function getVariableGetter($name)
protected function getVariableGetter($name, $line = false)
{
$line = $line > 0 ? "// line {$line}\n" : '';
if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name);
return sprintf('%s(isset($context["%s"]) ? $context["%s"] : null)', $line, $name, $name);
}
return sprintf('$this->getContext($context, "%s")', $name);
return sprintf('%s$this->getContext($context, "%s")', $line, $name);
}
protected function getAttributeGetter()
@@ -0,0 +1,13 @@
--TEST--
Exception for multiline array with undefined variable
--TEMPLATE--
{% set foo = {
foo: 'foo',
bar: 'bar',
foobar: foobar,
foo2: 'foo2'
} %}
--DATA--
return array()
--EXCEPTION--
Twig_Error_Runtime: Variable "foobar" does not exist in "index.twig" at line 5
@@ -0,0 +1,12 @@
--TEST--
Exception for multile function with undefined variable
--TEMPLATE--
{{ include('foo',
with_context=with_context
) }}
--TEMPLATE(foo)--
Foo
--DATA--
return array()
--EXCEPTION--
Twig_Error_Runtime: Variable "with_context" does not exist in "index.twig" at line 3
@@ -0,0 +1,9 @@
--TEST--
Exception for multiline function with unknown argument
--TEMPLATE--
{{ include('foo',
with_context=True,
invalid=False
) }}
--EXCEPTION--
Twig_Error_Syntax: Unknown argument "invalid" for function "include(template, variables, with_context, ignore_missing, sandboxed)" in "index.twig" at line 4.
@@ -0,0 +1,12 @@
--TEST--
Exception for multiline tag with undefined variable
--TEMPLATE--
{% include 'foo'
with vars
%}
--TEMPLATE(foo)--
Foo
--DATA--
return array()
--EXCEPTION--
Twig_Error_Runtime: Variable "vars" does not exist in "index.twig" at line 3
@@ -74,7 +74,7 @@ class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase
/**
* @expectedException Twig_Error_Syntax
* @expectedExceptionMessage Unknown argument "foobar" for filter "date(format, timezone)".
* @expectedExceptionMessage Unknown argument "foobar" for filter "date(format, timezone)" at line 1.
*/
public function testCompileWithWrongNamedArgumentName()
{
@@ -34,16 +34,16 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Test_NodeTestCase
$attr = new Twig_Node_Expression_Constant('bar', 1);
$args = new Twig_Node_Expression_Array(array(), 1);
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::ANY_CALL, 1);
$tests[] = array($node, sprintf('%s%s, "bar", array())', $this->getAttributeGetter(), $this->getVariableGetter('foo')));
$tests[] = array($node, sprintf('%s%s, "bar", array())', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1)));
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::ARRAY_CALL, 1);
$tests[] = array($node, sprintf('%s%s, "bar", array(), "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo')));
$tests[] = array($node, sprintf('%s%s, "bar", array(), "array")', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1)));
$args = new Twig_Node_Expression_Array(array(), 1);
$args->addElement(new Twig_Node_Expression_Name('foo', 1));
$args->addElement(new Twig_Node_Expression_Constant('bar', 1));
$node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Template::METHOD_CALL, 1);
$tests[] = array($node, sprintf('%s%s, "bar", array(0 => %s, 1 => "bar"), "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo'), $this->getVariableGetter('foo')));
$tests[] = array($node, sprintf('%s%s, "bar", array(0 => %s, 1 => "bar"), "method")', $this->getAttributeGetter(), $this->getVariableGetter('foo', 1), $this->getVariableGetter('foo')));
return $tests;
}
+4 -4
View File
@@ -28,10 +28,10 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Test_NodeTestCase
$env1 = new Twig_Environment(null, array('strict_variables' => false));
return array(
version_compare(PHP_VERSION, '5.4.0') >= 0 ? array($node, '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))', $env) : array($node, '$this->getContext($context, "foo")', $env),
array($node, $this->getVariableGetter('foo'), $env1),
array($self, '$this'),
array($context, '$context'),
array($node, "// line 1\n".(version_compare(PHP_VERSION, '5.4.0') >= 0 ? '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))' : '$this->getContext($context, "foo")'), $env),
array($node, $this->getVariableGetter('foo', 1), $env1),
array($self, "// line 1\n\$this"),
array($context, "// line 1\n\$context"),
);
}
}