From 133edb369653a0c592e0bf133bdcc923adaa05b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20TAMARELLE?= Date: Fri, 21 Feb 2020 14:56:39 +0100 Subject: [PATCH 1/4] Support object init from variable {a} is a shortcut for {a:a} From ECMAScript 2015 syntax https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer --- doc/templates.rst | 4 ++++ src/ExpressionParser.php | 11 ++++++++++- tests/ExpressionParserTest.php | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/doc/templates.rst b/doc/templates.rst index adf2b5395..bfafa1959 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -568,6 +568,10 @@ exist: {# keys as integer #} { 2: 'foo', 4: 'bar' } + {# keys get the variable name when omitted #} + {% set foo = 'foo' %} + { foo } + {# keys as expressions (the expression must be enclosed into parentheses) -- as of Twig 1.5 #} {% set foo = 'foo' %} { (foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz' } diff --git a/src/ExpressionParser.php b/src/ExpressionParser.php index 0e0a12a82..88de4cd35 100644 --- a/src/ExpressionParser.php +++ b/src/ExpressionParser.php @@ -376,7 +376,16 @@ class ExpressionParser // * a string -- 'a' // * a name, which is equivalent to a string -- a // * an expression, which must be enclosed in parentheses -- (1 + 2) - if (($token = $stream->nextIf(Token::STRING_TYPE)) || ($token = $stream->nextIf(Token::NAME_TYPE)) || $token = $stream->nextIf(Token::NUMBER_TYPE)) { + if ($token = $stream->nextIf(Token::NAME_TYPE)) { + $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)) || $token = $stream->nextIf(Token::NUMBER_TYPE)) { $key = new ConstantExpression($token->getValue(), $token->getLine()); } elseif ($stream->test(Token::PUNCTUATION_TYPE, '(')) { $key = $this->parseExpression(); diff --git a/tests/ExpressionParserTest.php b/tests/ExpressionParserTest.php index ef31fd715..77db4a340 100644 --- a/tests/ExpressionParserTest.php +++ b/tests/ExpressionParserTest.php @@ -83,6 +83,7 @@ class ExpressionParserTest extends \PHPUnit\Framework\TestCase return [ ['{{ [1, "a": "b"] }}'], ['{{ {"a": "b", 2} }}'], + ['{{ {"a"} }}'], ]; } @@ -159,6 +160,12 @@ class ExpressionParserTest extends \PHPUnit\Framework\TestCase new ConstantExpression('c', 1), ], 1), ], + ['{{ {a, b} }}', new ArrayExpression([ + new ConstantExpression('a', 1), + new NameExpression('a', 1), + new ConstantExpression('b', 1), + new NameExpression('b', 1), + ], 1)] ]; } From b0ce4f64bae78459997170cf0888923c1188f7ce Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 23 Aug 2020 13:56:18 +0200 Subject: [PATCH 2/4] Update CHANGELOG --- CHANGELOG | 2 +- tests/ExpressionParserTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d962657a3..fef7a11a0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ # 1.43.2 (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) diff --git a/tests/ExpressionParserTest.php b/tests/ExpressionParserTest.php index 77db4a340..2c70ef4d5 100644 --- a/tests/ExpressionParserTest.php +++ b/tests/ExpressionParserTest.php @@ -165,7 +165,7 @@ class ExpressionParserTest extends \PHPUnit\Framework\TestCase new NameExpression('a', 1), new ConstantExpression('b', 1), new NameExpression('b', 1), - ], 1)] + ], 1)], ]; } From 244ef52df69ffe97f1a302118d073923f010ce65 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 23 Aug 2020 13:58:27 +0200 Subject: [PATCH 3/4] Tweak docs --- doc/templates.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/templates.rst b/doc/templates.rst index bfafa1959..872e0b567 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -568,9 +568,10 @@ exist: {# keys as integer #} { 2: 'foo', 4: 'bar' } - {# keys get the variable name when omitted #} - {% set foo = 'foo' %} + {# 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) -- as of Twig 1.5 #} {% set foo = 'foo' %} From c6927de9e758ca31a7fa50b11ff7c9892b82944d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 23 Aug 2020 13:59:06 +0200 Subject: [PATCH 4/4] Bump version --- CHANGELOG | 2 +- composer.json | 2 +- src/Environment.php | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index fef7a11a0..25ce100e4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,4 @@ -# 1.43.2 (2020-XX-XX) +# 1.44.0 (2020-XX-XX) * Add support for ES2015 style object initialisation shortcut { a } is the same as { 'a': a } diff --git a/composer.json b/composer.json index d4e096dc5..b60b32a50 100644 --- a/composer.json +++ b/composer.json @@ -46,7 +46,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.43-dev" + "dev-master": "1.44-dev" } } } diff --git a/src/Environment.php b/src/Environment.php index 6dffa9462..330a9f3f8 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -41,11 +41,11 @@ use Twig\TokenParser\TokenParserInterface; */ class Environment { - const VERSION = '1.43.2-DEV'; - const VERSION_ID = 14302; + const VERSION = '1.44.0-DEV'; + const VERSION_ID = 14400; const MAJOR_VERSION = 1; - const MINOR_VERSION = 43; - const RELEASE_VERSION = 2; + const MINOR_VERSION = 44; + const RELEASE_VERSION = 0; const EXTRA_VERSION = 'DEV'; protected $charset;