Skip to content

Commit a8b783f

Browse files
committed
Let environment self shadow SelfDrop
1 parent 7b368df commit a8b783f

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

lib/liquid/context.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,21 @@ def find_variable(key, raise_on_not_found: true)
206206
# path and find_index() is optimized in MRI to reduce object allocation
207207
index = @scopes.find_index { |s| s.key?(key) }
208208

209-
# `self` resolves to a SelfDrop (enabling `self['var']` lookups),
210-
# but only when it hasn't been explicitly assigned as a local variable.
211-
return @self_drop ||= SelfDrop.new(self) if key == Expression::SELF && !index
209+
fallback_to_self_drop = key == Expression::SELF && index.nil?
212210

213211
variable = if index
214212
lookup_and_evaluate(@scopes[index], key, raise_on_not_found: raise_on_not_found)
215213
else
216-
try_variable_find_in_environments(key, raise_on_not_found: raise_on_not_found)
214+
try_variable_find_in_environments(
215+
key,
216+
raise_on_not_found: raise_on_not_found && !fallback_to_self_drop,
217+
)
217218
end
218219

220+
# `self` resolves to a SelfDrop (enabling `self['var']` lookups),
221+
# but only after the normal environment lookup doesn't find a value.
222+
return @self_drop ||= SelfDrop.new(self) if fallback_to_self_drop && variable.nil?
223+
219224
# update variable's context before invoking #to_liquid
220225
variable.context = self if variable.respond_to?(:context=)
221226

0 commit comments

Comments
 (0)