Class: Liquidbook::TemplateAnalyzer::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/liquidbook/template_analyzer.rb

Overview

Tracks local variables with proper scoping for for-loops. Uses a parent chain so child scopes see parent locals, while scoped variables (loop vars) don’t leak upward.

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Scope

Returns a new instance of Scope.



37
38
39
40
# File 'lib/liquidbook/template_analyzer.rb', line 37

def initialize(parent = nil)
  @parent = parent
  @locals = []
end

Instance Method Details

#add_local(name) ⇒ Object



42
43
44
# File 'lib/liquidbook/template_analyzer.rb', line 42

def add_local(name)
  @locals << name
end

#child_with(extra_locals) ⇒ Object



50
51
52
53
54
# File 'lib/liquidbook/template_analyzer.rb', line 50

def child_with(extra_locals)
  child = Scope.new(self)
  extra_locals.each { |l| child.add_local(l) }
  child
end

#local?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/liquidbook/template_analyzer.rb', line 46

def local?(name)
  @locals.include?(name) || @parent&.local?(name) || false
end