From 6d39050e1e4ee7ce3c93492cd58df52caaaa256b Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Mon, 19 Oct 2020 11:32:18 -0400 Subject: [PATCH 1/3] Use a case statement in Liquid::Parser#expression --- lib/liquid/parser.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/liquid/parser.rb b/lib/liquid/parser.rb index e038037..5b38eac 100644 --- a/lib/liquid/parser.rb +++ b/lib/liquid/parser.rb @@ -46,16 +46,14 @@ module Liquid tok[0] == type end - SINGLE_TOKEN_EXPRESSION_TYPES = [:string, :number].freeze - private_constant :SINGLE_TOKEN_EXPRESSION_TYPES - def expression token = @tokens[@p] - if token[0] == :id + case token[0] + when :id variable_signature - elsif SINGLE_TOKEN_EXPRESSION_TYPES.include?(token[0]) + when :string, :number consume - elsif token.first == :open_round + when :open_round consume first = expression consume(:dotdot) From 420a1c79e1010f9184ecad0692a72f3bee023e08 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Mon, 19 Oct 2020 12:10:32 -0400 Subject: [PATCH 2/3] Refactor variable lookup strict parsing to reduce coupling on dot lookup --- lib/liquid/parser.rb | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/liquid/parser.rb b/lib/liquid/parser.rb index 5b38eac..b43576e 100644 --- a/lib/liquid/parser.rb +++ b/lib/liquid/parser.rb @@ -50,7 +50,8 @@ module Liquid token = @tokens[@p] case token[0] when :id - variable_signature + str = consume + str << variable_lookups when :string, :number consume when :open_round @@ -76,16 +77,19 @@ module Liquid str end - def variable_signature - str = consume(:id) - while look(:open_square) - str << consume - str << expression - str << consume(:close_square) - end - if look(:dot) - str << consume - str << variable_signature + def variable_lookups + str = +"" + loop do + if look(:open_square) + str << consume + str << expression + str << consume(:close_square) + elsif look(:dot) + str << consume + str << consume(:id) + else + break + end end str end From 46fd63da5f149f5c6485dfc26982a9e2b5651a48 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Mon, 19 Oct 2020 12:07:39 -0400 Subject: [PATCH 3/3] Fix strict parsing of find variable with a name expression --- lib/liquid/parser.rb | 5 +++++ test/integration/variable_test.rb | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/lib/liquid/parser.rb b/lib/liquid/parser.rb index b43576e..609601a 100644 --- a/lib/liquid/parser.rb +++ b/lib/liquid/parser.rb @@ -52,6 +52,11 @@ module Liquid when :id str = consume str << variable_lookups + when :open_square + str = consume + str << expression + str << consume(:close_square) + str << variable_lookups when :string, :number consume when :open_round diff --git a/test/integration/variable_test.rb b/test/integration/variable_test.rb index 45268bd..168f43a 100644 --- a/test/integration/variable_test.rb +++ b/test/integration/variable_test.rb @@ -95,4 +95,8 @@ class VariableTest < Minitest::Test def test_render_symbol assert_template_result('bar', '{{ foo }}', 'foo' => :bar) end + + def test_dynamic_find_var + assert_template_result('bar', '{{ [key] }}', 'key' => 'foo', 'foo' => 'bar') + end end