From c270a6f37828f42411b3c9bae73067f9813b44d0 Mon Sep 17 00:00:00 2001 From: "Charles-P. Clermont" Date: Mon, 22 Mar 2021 14:19:10 -0400 Subject: [PATCH] Add ParseTreeVisitor to Echo tag This fixes theme-check#218, wherein variables used in echo tags are not considered used by the linter. It is because our visitor doesn't see the :variable_lookup's in the echo tag since the children array is empty. But this array is empty because it is swallowed by the @variable. --- lib/liquid/tags/echo.rb | 8 ++++++++ test/unit/parse_tree_visitor_test.rb | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/lib/liquid/tags/echo.rb b/lib/liquid/tags/echo.rb index 1f78937..19026a0 100644 --- a/lib/liquid/tags/echo.rb +++ b/lib/liquid/tags/echo.rb @@ -12,6 +12,8 @@ module Liquid # {% echo user | link %} # class Echo < Tag + attr_reader :variable + def initialize(tag_name, markup, parse_context) super @variable = Variable.new(markup, parse_context) @@ -20,6 +22,12 @@ module Liquid def render(context) @variable.render_to_output_buffer(context, +'') end + + class ParseTreeVisitor < Liquid::ParseTreeVisitor + def children + [@node.variable] + end + end end Template.register_tag('echo', Echo) diff --git a/test/unit/parse_tree_visitor_test.rb b/test/unit/parse_tree_visitor_test.rb index 32dbcf6..2c085b3 100644 --- a/test/unit/parse_tree_visitor_test.rb +++ b/test/unit/parse_tree_visitor_test.rb @@ -26,6 +26,13 @@ class ParseTreeVisitorTest < Minitest::Test ) end + def test_echo + assert_equal( + ["test"], + visit(%({% echo test %})) + ) + end + def test_if_condition assert_equal( ["test"],