Compare commits

..
Author SHA1 Message Date
Sam Doiron cc6cd64065 Special case Hash#last in variable lookup
Ruby's defines Hash#first via Enumerable but not Hash#last.
This is a confusing behaviour, and it bubbles up to Liquid.

Solution: add a special case for command-lookups of Hash#last to return
hash.values.last.
2022-11-01 14:40:36 -03:00
6 changed files with 32 additions and 18 deletions
+13
View File
@@ -1,6 +1,19 @@
# frozen_string_literal: true
module Liquid
# @liquid_public_docs
# @liquid_type tag
# @liquid_category syntax
# @liquid_name inline_comment
# @liquid_summary
# Prevents an expression from being rendered or output.
# @liquid_description
# Any text inside an `inline_comment` tag won't be rendered or output.
#
# You can create multi-line inline comments. However, each line must begin with a `#`.
# @liquid_syntax
# {% # content %}
# @liquid_syntax_keyword content The content of the comment.
class InlineComment < Tag
def initialize(tag_name, markup, options)
super
+2 -8
View File
@@ -8,15 +8,11 @@ module Liquid
@source = source.to_s.to_str
@line_number = line_number || (line_numbers ? 1 : nil)
@for_liquid_tag = for_liquid_tag
@offset = 0
@tokens = tokenize
end
def shift
token = @tokens[@offset]
return nil unless token
@offset += 1
(token = @tokens.shift) || return
if @line_number
@line_number += @for_liquid_tag ? 1 : token.count("\n")
@@ -35,9 +31,7 @@ module Liquid
tokens = @source.split(TemplateParser)
# removes the rogue empty element at the beginning of the array
if tokens[0]&.empty?
@offset += 1
end
tokens.shift if tokens[0]&.empty?
tokens
end
+5 -1
View File
@@ -61,11 +61,15 @@ module Liquid
# as commands and call them on the current object
elsif lookup_command?(i) && object.respond_to?(key)
object = object.send(key).to_liquid
elsif lookup_command?(i) && object.is_a?(Hash) && key == 'last'
# Ruby defines Hash#first via Enumerable but not Hash#last.
# This is unexpected, so we explicitly handle that case here.
object = object.values.last.to_liquid
else
# No key was present with the desired value and it wasn't one of the directly supported
# keywords either. The only thing we got left is to return nil or
# raise an exception if `strict_variables` option is set to true
else
return nil unless context.strict_variables
raise Liquid::UndefinedVariable, "undefined variable #{key}"
end
+7 -8
View File
@@ -6,21 +6,20 @@ class IncrementTagTest < Minitest::Test
include Liquid
def test_inc
assert_template_result('0 1', '{%increment port %} {{ port }}')
assert_template_result(' 0 1 2', '{{port}} {%increment port %} {%increment port%} {{port}}')
assert_template_result('0', '{%increment port %}', {})
assert_template_result('0 1', '{%increment port %} {%increment port%}', {})
assert_template_result('0 0 1 2 1',
'{%increment port %} {%increment starboard%} ' \
'{%increment port %} {%increment port%} ' \
'{%increment starboard %}')
'{%increment starboard %}', {})
end
def test_dec
assert_template_result('-1 -1', '{%decrement port %} {{ port }}', { 'port' => 10 })
assert_template_result(' -1 -2 -2', '{{port}} {%decrement port %} {%decrement port%} {{port}}')
assert_template_result('0 1 2 0 3 1 1 3',
'{%increment starboard %} {%increment starboard%} {%increment starboard%} ' \
assert_template_result('9', '{%decrement port %}', { 'port' => 10 })
assert_template_result('-1 -2', '{%decrement port %} {%decrement port%}', {})
assert_template_result('1 5 2 2 5',
'{%increment port %} {%increment starboard%} ' \
'{%increment port %} {%decrement port%} ' \
'{%decrement starboard %}')
'{%decrement starboard %}', { 'port' => 1, 'starboard' => 5 })
end
end
+4
View File
@@ -130,4 +130,8 @@ class VariableTest < Minitest::Test
def test_raw_value_variable
assert_template_result('bar', '{{ [key] }}', { 'key' => 'foo', 'foo' => 'bar' })
end
def test_hash_last
assert_template_result('last', '{{ hash.last }}', { 'hash' => { 'first' => 'first', 'last' => 'last' } })
end
end
+1 -1
View File
@@ -44,7 +44,7 @@ module Minitest
template = Liquid::Template.parse(template, line_numbers: true, error_mode: error_mode&.to_sym)
file_system = StubFileSystem.new(partials || {})
registers = Liquid::Registers.new(file_system: file_system)
context = Liquid::Context.build(static_environments: assigns, rethrow_errors: !render_errors, registers: registers)
context = Liquid::Context.build(environments: assigns, rethrow_errors: !render_errors, registers: registers)
output = template.render(context)
assert_equal(expected, output, message)
end