Class: TurboTest::StaticAnalysis::Constants::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_test_static_analysis/constants/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, start_pos:, end_pos:, singleton: false, definition: false) ⇒ Node

Returns a new instance of Node.



10
11
12
13
14
15
16
17
18
19
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 10

def initialize(name:, start_pos:, end_pos:, singleton: false, definition: false)
  @name = name
  @children = []
  @start_pos = start_pos
  @end_pos = end_pos
  @parent = nil
  @singleton = singleton
  @definition = definition
  @top_level = false
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 7

def children
  @children
end

#definitionObject

Returns the value of attribute definition.



8
9
10
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 8

def definition
  @definition
end

#end_posObject

Returns the value of attribute end_pos.



8
9
10
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 8

def end_pos
  @end_pos
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 7

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



7
8
9
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 7

def parent
  @parent
end

#singletonObject (readonly)

Returns the value of attribute singleton.



7
8
9
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 7

def singleton
  @singleton
end

#start_posObject (readonly)

Returns the value of attribute start_pos.



7
8
9
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 7

def start_pos
  @start_pos
end

#top_levelObject

Returns the value of attribute top_level.



8
9
10
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 8

def top_level
  @top_level
end

Instance Method Details

#add_child(child) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 21

def add_child(child)
  return if child.start_pos == start_pos && child.name == name
  return unless contains?(child)

  @children << child
  child.instance_variable_set(:@parent, self)
  child.instance_variable_set(:@definition, definition)
  child
end

#contains?(node) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 31

def contains?(node)
  ((@start_pos <=> node.start_pos) == -1 && (@end_pos <=> node.end_pos) == 1) ||
    ((@start_pos <=> node.start_pos).zero? &&
      ((@end_pos <=> node.end_pos).zero? || (@end_pos <=> node.end_pos) == 1))
end

#full_nameObject



37
38
39
40
41
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 37

def full_name
  return @name unless @parent

  [@parent.full_name, @name].compact.join("::")
end

#named_singleton_with_parent?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 55

def named_singleton_with_parent?
  return false unless parent

  singleton && name != "singleton_class"
end

#parent_is_named_singleton?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 49

def parent_is_named_singleton?
  return false unless parent

  parent.singleton && parent.name != "singleton_class"
end

#rootObject



43
44
45
46
47
# File 'lib/turbo_test_static_analysis/constants/node.rb', line 43

def root
  return self if parent.nil?

  parent.root
end