Class: PlatformosCheck::UndefinedObject::TemplateInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/platformos_check/checks/undefined_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_file: nil) ⇒ TemplateInfo

Returns a new instance of TemplateInfo.



13
14
15
16
17
18
19
20
# File 'lib/platformos_check/checks/undefined_object.rb', line 13

def initialize(app_file: nil)
  @all_variable_lookups = {}
  @all_assigns = {}
  @all_captures = {}
  @all_forloops = {}
  @all_renders = {}
  @app_file = app_file
end

Instance Attribute Details

#all_assignsObject (readonly)

Returns the value of attribute all_assigns.



22
23
24
# File 'lib/platformos_check/checks/undefined_object.rb', line 22

def all_assigns
  @all_assigns
end

#all_capturesObject (readonly)

Returns the value of attribute all_captures.



22
23
24
# File 'lib/platformos_check/checks/undefined_object.rb', line 22

def all_captures
  @all_captures
end

#all_forloopsObject (readonly)

Returns the value of attribute all_forloops.



22
23
24
# File 'lib/platformos_check/checks/undefined_object.rb', line 22

def all_forloops
  @all_forloops
end

#all_rendersObject (readonly)

Returns the value of attribute all_renders.



22
23
24
# File 'lib/platformos_check/checks/undefined_object.rb', line 22

def all_renders
  @all_renders
end

#app_fileObject (readonly)

Returns the value of attribute app_file.



22
23
24
# File 'lib/platformos_check/checks/undefined_object.rb', line 22

def app_file
  @app_file
end

Instance Method Details

#add_render(name:, node:) ⇒ Object



24
25
26
27
# File 'lib/platformos_check/checks/undefined_object.rb', line 24

def add_render(name:, node:)
  @all_renders[name] ||= []
  @all_renders[name] << node
end

#add_variable_lookup(name:, node:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/platformos_check/checks/undefined_object.rb', line 29

def add_variable_lookup(name:, node:)
  parent = node
  line_number = nil
  loop do
    line_number = parent.line_number
    parent = parent.parent
    break unless line_number.nil? && parent
  end
  key = [name, line_number]
  @all_variable_lookups[key] = node
end

#all_variablesObject



41
42
43
# File 'lib/platformos_check/checks/undefined_object.rb', line 41

def all_variables
  all_assigns.keys + all_captures.keys + all_forloops.keys
end

#each_partialObject



45
46
47
48
49
50
51
# File 'lib/platformos_check/checks/undefined_object.rb', line 45

def each_partial
  @all_renders.each do |(name, nodes)|
    nodes.each do |node|
      yield [name, node]
    end
  end
end

#each_variable_lookup(unique_keys = false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/platformos_check/checks/undefined_object.rb', line 53

def each_variable_lookup(unique_keys = false)
  seen = Set.new
  @all_variable_lookups.each do |(key, info)|
    name, _line_number = key

    next if unique_keys && seen.include?(name)

    seen << name

    yield [key, info]
  end
end

#first_declaration(name) ⇒ Object



66
67
68
# File 'lib/platformos_check/checks/undefined_object.rb', line 66

def first_declaration(name)
  [all_assigns[name], all_captures[name]].compact.min_by(&:line_number)
end