Merge branch '1.x' into 2.x

* 1.x:
  Bump version
  Tweak docs
  Update CHANGELOG
  Support object init from variable
This commit is contained in:
Fabien Potencier
2020-08-23 14:01:18 +02:00
4 changed files with 24 additions and 3 deletions
+2 -2
View File
@@ -321,9 +321,9 @@
* improved the performance of the filesystem loader * improved the performance of the filesystem loader
* removed features that were deprecated in 1.x * removed features that were deprecated in 1.x
# 1.43.2 (2020-XX-XX) # 1.44.0 (2020-XX-XX)
* n/a * Add support for ES2015 style object initialisation shortcut { a } is the same as { 'a': a }
# 1.43.1 (2020-08-05) # 1.43.1 (2020-08-05)
+5
View File
@@ -560,6 +560,11 @@ exist:
{# keys as integer #} {# keys as integer #}
{ 2: 'foo', 4: 'bar' } { 2: 'foo', 4: 'bar' }
{# keys can be omitted if it is the same as the variable name #}
{ foo }
{# is equivalent to the following #}
{ 'foo': foo }
{# keys as expressions (the expression must be enclosed into parentheses) #} {# keys as expressions (the expression must be enclosed into parentheses) #}
{% set foo = 'foo' %} {% set foo = 'foo' %}
{ (foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz' } { (foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz' }
+10 -1
View File
@@ -366,7 +366,16 @@ class ExpressionParser
// * a string -- 'a' // * a string -- 'a'
// * a name, which is equivalent to a string -- a // * a name, which is equivalent to a string -- a
// * an expression, which must be enclosed in parentheses -- (1 + 2) // * an expression, which must be enclosed in parentheses -- (1 + 2)
if (($token = $stream->nextIf(/* Token::STRING_TYPE */ 7)) || ($token = $stream->nextIf(/* Token::NAME_TYPE */ 5)) || $token = $stream->nextIf(/* Token::NUMBER_TYPE */ 6)) { if ($token = $stream->nextIf(/* Token::NAME_TYPE */ 5)) {
$key = new ConstantExpression($token->getValue(), $token->getLine());
// {a} is a shortcut for {a:a}
if ($stream->test(Token::PUNCTUATION_TYPE, [',', '}'])) {
$value = new NameExpression($key->getAttribute('value'), $key->getTemplateLine());
$node->addElement($value, $key);
continue;
}
} elseif (($token = $stream->nextIf(/* Token::STRING_TYPE */ 7)) || $token = $stream->nextIf(/* Token::NUMBER_TYPE */ 6)) {
$key = new ConstantExpression($token->getValue(), $token->getLine()); $key = new ConstantExpression($token->getValue(), $token->getLine());
} elseif ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, '(')) { } elseif ($stream->test(/* Token::PUNCTUATION_TYPE */ 9, '(')) {
$key = $this->parseExpression(); $key = $this->parseExpression();
+7
View File
@@ -84,6 +84,7 @@ class ExpressionParserTest extends TestCase
return [ return [
['{{ [1, "a": "b"] }}'], ['{{ [1, "a": "b"] }}'],
['{{ {"a": "b", 2} }}'], ['{{ {"a": "b", 2} }}'],
['{{ {"a"} }}'],
]; ];
} }
@@ -160,6 +161,12 @@ class ExpressionParserTest extends TestCase
new ConstantExpression('c', 1), new ConstantExpression('c', 1),
], 1), ], 1),
], ],
['{{ {a, b} }}', new ArrayExpression([
new ConstantExpression('a', 1),
new NameExpression('a', 1),
new ConstantExpression('b', 1),
new NameExpression('b', 1),
], 1)],
]; ];
} }