Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/liquid/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def [](expression)
end

def key?(key)
self[key] != nil
find_variable(key, raise_on_not_found: false) != nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an opinionated change. Instead of evaluating Expression.parse to look up the tree if we don't find a match we should assume that somebody is passing a simple lookup to key? and check for it directly.

end

def evaluate(object)
Expand Down
15 changes: 15 additions & 0 deletions test/integration/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,21 @@ def test_has_key_will_not_add_an_error_for_missing_keys
end
end

def test_key_lookup_will_raise_for_missing_keys_when_strict_variables_is_enabled
context = Context.new
context.strict_variables = true
assert_raises(Liquid::UndefinedVariable) do
context['unknown']
end
end

def test_has_key_will_not_raise_for_missing_keys_when_strict_variables_is_enabled
context = Context.new
context.strict_variables = true
refute(context.key?('unknown'))
assert_empty(context.errors)
end

def test_context_always_uses_static_registers
registers = {
my_register: :my_value,
Expand Down