mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Add bare-bracket rejection to Parser#expression in strict2 mode, so that
`['var']` is disallowed and `self['var']` is the required syntax.
- Add `Expression::SELF` constant ('self')
- Add `Parser#reject_bare_brackets` option, checked in `expression`
- Add `ParseContext#reject_bare_brackets?` and `force_reject_bare_brackets`
- Add `VariableLookupDrop` for `self['var']` scope-chain lookups
- Add `Variable#==` for rewriter state comparison
- Update `Context#find_variable` to return `VariableLookupDrop` for `self`
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
39 lines
1.0 KiB
Ruby
39 lines
1.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Liquid
|
|
# @liquid_public_docs
|
|
# @liquid_type object
|
|
# @liquid_name self
|
|
# @liquid_summary
|
|
# Provides access to variables through the current scope chain.
|
|
# @liquid_description
|
|
# The `self` object resolves variables through the normal lookup hierarchy
|
|
# (local > file > global) without exposing filters, interrupts, errors,
|
|
# or other context internals. It's used when bare bracket notation
|
|
# (`['variable']`) needs to be replaced with an explicit variable lookup.
|
|
#
|
|
# If `self` is explicitly assigned as a local variable (e.g. `{% assign self = 'value' %}`),
|
|
# then the local value takes precedence over the `self` object.
|
|
# @liquid_access global
|
|
class SelfDrop < Drop
|
|
def initialize(context)
|
|
super()
|
|
@context = context
|
|
end
|
|
|
|
def [](key)
|
|
@context.find_variable(key)
|
|
rescue UndefinedVariable
|
|
nil
|
|
end
|
|
|
|
def key?(key)
|
|
@context.variable_defined?(key)
|
|
end
|
|
|
|
def to_liquid
|
|
self
|
|
end
|
|
end
|
|
end
|