Class: Luoma::StaticAnalysis::VariableMap

Inherits:
Object
  • Object
show all
Defined in:
lib/luoma/static_analysis.rb,
sig/luoma/static_analysis.rbs

Overview

Helper to group variables by their root segment during static analysis.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVariableMap

Returns a new instance of VariableMap.



152
153
154
# File 'lib/luoma/static_analysis.rb', line 152

def initialize
  @data = {}
end

Instance Attribute Details

#dataHash[String, Array[StaticVariable]] (readonly)

Returns the value of attribute data.

Returns:



150
151
152
# File 'lib/luoma/static_analysis.rb', line 150

def data
  @data
end

Instance Method Details

#[](var) ⇒ Array[StaticVariable]?

Parameters:

Returns:



156
157
158
159
160
# File 'lib/luoma/static_analysis.rb', line 156

def [](var)
  key = var.root
  @data[key] = [] unless @data.include?(key)
  @data[key]
end

#add(var) ⇒ void

This method returns an undefined value.

Parameters:



162
163
164
# File 'lib/luoma/static_analysis.rb', line 162

def add(var)
  send(:[], var) << var
end

#to_hObject

() -> Hash[String, Array[_Var]]

Returns:

  • (Object)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/luoma/static_analysis.rb', line 167

def to_h
  result = {} #: Hash[String, Array[untyped]]

  @data.each do |k, v|
    a = [] #: Array[untyped]

    v.each do |sv|
      start_line, start_column, end_line, end_column = sv.location.span

      a << {
        segments: sv.segments,
        path: sv.to_s,
        start_index: sv.location.token[1],
        end_index: sv.location.token[2],
        start_line: start_line,
        start_column: start_column,
        end_line: end_line,
        end_column: end_column,
        value: sv.location.value,
        template_name: sv.location.template.name
      }
    end

    result[k] = a
  end

  result
end