Class: Prettyrb::Nodes::IfNode

Inherits:
BaseNode
  • Object
show all
Defined in:
lib/prettyrb/nodes/if_node.rb

Instance Method Summary collapse

Methods inherited from BaseNode

#initialize, #parent, #string?

Constructor Details

This class inherits a constructor from Prettyrb::Nodes::BaseNode

Instance Method Details

#body_nodeObject



18
19
20
21
22
23
24
# File 'lib/prettyrb/nodes/if_node.rb', line 18

def body_node
  if unless_node?
    children[2]
  else
    children[1]
  end
end

#conditionsObject



14
15
16
# File 'lib/prettyrb/nodes/if_node.rb', line 14

def conditions
  children[0]
end

#else_body_nodeObject



26
27
28
29
30
31
32
# File 'lib/prettyrb/nodes/if_node.rb', line 26

def else_body_node
  if unless_node?
    children[1]
  else
    children[2]
  end
end

#else_branchObject



54
55
56
57
58
59
60
# File 'lib/prettyrb/nodes/if_node.rb', line 54

def else_branch
  if has_elsif?
    elsif_branches.last.children[2]
  else
    else_body_node
  end
end

#elsif_branchesObject



46
47
48
49
50
51
52
# File 'lib/prettyrb/nodes/if_node.rb', line 46

def elsif_branches
  if has_elsif?
    [else_body_node] + else_body_node.elsif_branches
  else
    []
  end
end

#has_elsif?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/prettyrb/nodes/if_node.rb', line 34

def has_elsif?
  else_body_node&.type == :if && children[1]&.type != :if
end

#if_typeObject



4
5
6
7
8
9
10
11
12
# File 'lib/prettyrb/nodes/if_node.rb', line 4

def if_type
  if is_elsif?
    "elsif"
  elsif unless_node?
    "unless"
  else
    "if"
  end
end

#is_elsif?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/prettyrb/nodes/if_node.rb', line 38

def is_elsif?
  parent&.type == :if && parent&.children[1]&.type != :if
end

#unless_node?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/prettyrb/nodes/if_node.rb', line 42

def unless_node?
  children[1].nil? && children[2] != :if
end