From 4ee1d06d03ad1f354d2ea426cbfdfd0d61e872f9 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 24 Oct 2024 10:38:15 +0200 Subject: [PATCH] Deprecate the range function --- CHANGELOG | 1 + doc/deprecated.rst | 3 +++ doc/functions/range.rst | 5 +++++ src/Extension/CoreExtension.php | 2 +- src/NodeVisitor/SandboxNodeVisitor.php | 1 + tests/Fixtures/functions/range.test | 8 -------- tests/Fixtures/whitespace/trim_block.test | 4 ++-- 7 files changed, 13 insertions(+), 11 deletions(-) delete mode 100644 tests/Fixtures/functions/range.test diff --git a/CHANGELOG b/CHANGELOG index 6eac17dc9a9..02a8f2d5190 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ # 3.15.0 (2024-XX-XX) + * Deprecate the `range` function, use the `..` operator * Deprecate `TempNameExpression` in favor of `LocalVariable` * Deprecate `NameExpression` in favor of `ContextVariable` * Deprecate `AssignNameExpression` in favor of `AssignContextVariable` diff --git a/doc/deprecated.rst b/doc/deprecated.rst index fed5cb38017..0c8483ba228 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -28,6 +28,9 @@ Functions Note that it won't be removed in 4.0 to allow a smoother upgrade path. +* The ``range`` function is deprecated as of Twig 3.15, use the ``..`` operator + instead. + Extensions ---------- diff --git a/doc/functions/range.rst b/doc/functions/range.rst index e537b11a652..08246f7e681 100644 --- a/doc/functions/range.rst +++ b/doc/functions/range.rst @@ -1,6 +1,11 @@ ``range`` ========= +.. warning:: + + The ``attribute`` filter is deprecated as of Twig 3.15. Use the ``..`` + operator instead. + Returns a list containing an arithmetic progression of integers: .. code-block:: twig diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index f8f80ac2f0c..645c797e75d 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -260,7 +260,7 @@ public function getFunctions(): array new TwigFunction('attribute', null, ['parser_callable' => [self::class, 'parseAttributeFunction']]), new TwigFunction('max', 'max'), new TwigFunction('min', 'min'), - new TwigFunction('range', 'range'), + new TwigFunction('range', 'range', ['deprecation_info' => new DeprecatedCallableInfo('twig/twig', '3.15')]), new TwigFunction('constant', [self::class, 'constant']), new TwigFunction('cycle', [self::class, 'cycle']), new TwigFunction('random', [self::class, 'random'], ['needs_charset' => true]), diff --git a/src/NodeVisitor/SandboxNodeVisitor.php b/src/NodeVisitor/SandboxNodeVisitor.php index ab51d33d4a0..c1b80030cdf 100644 --- a/src/NodeVisitor/SandboxNodeVisitor.php +++ b/src/NodeVisitor/SandboxNodeVisitor.php @@ -69,6 +69,7 @@ public function enterNode(Node $node, Environment $env): Node } // the .. operator is equivalent to the range() function + // To be removed in 4.0 if ($node instanceof RangeBinary && !isset($this->functions['range'])) { $this->functions['range'] = $node->getTemplateLine(); } diff --git a/tests/Fixtures/functions/range.test b/tests/Fixtures/functions/range.test deleted file mode 100644 index 2927333b97f..00000000000 --- a/tests/Fixtures/functions/range.test +++ /dev/null @@ -1,8 +0,0 @@ ---TEST-- -"range" function ---TEMPLATE-- -{{ range(low=0+1, high=10+0, step=2)|join(',') }} ---DATA-- -return [] ---EXPECT-- -1,3,5,7,9 diff --git a/tests/Fixtures/whitespace/trim_block.test b/tests/Fixtures/whitespace/trim_block.test index 346a11076c3..2f85a0e5617 100644 --- a/tests/Fixtures/whitespace/trim_block.test +++ b/tests/Fixtures/whitespace/trim_block.test @@ -2,13 +2,13 @@ Whitespace trimming on tags. --TEMPLATE-- Trim on control tag: -{% for i in range(1, 9) -%} +{% for i in 1..9 -%} {{ i }} {%- endfor %} Trim on output tag: -{% for i in range(1, 9) %} +{% for i in 1..9 %} {{- i -}} {% endfor %}