Module: Hashira::Smells::Conditions

Defined in:
lib/hashira/smells/conditions.rb

Constant Summary collapse

TESTED =
[Prism::IfNode, Prism::UnlessNode, Prism::CaseNode, Prism::AndNode, Prism::OrNode].freeze

Class Method Summary collapse

Class Method Details

.branches(node) ⇒ Object



23
24
25
26
27
# File 'lib/hashira/smells/conditions.rb', line 23

def branches(node)
  return fork(node) if node.is_a?(Prism::IfNode) || node.is_a?(Prism::UnlessNode)
  return node.conditions + [node.consequent] if node.is_a?(Prism::CaseNode)
  couple?(node) ? [node.right] : [node.body]
end

.condition(node) ⇒ Object



16
17
18
19
20
21
# File 'lib/hashira/smells/conditions.rb', line 16

def condition(node)
  case node
  when Prism::IfNode, Prism::UnlessNode, Prism::CaseNode then node.predicate
  when Prism::AndNode, Prism::OrNode then node.left
  end
end

.couple?(node) ⇒ Boolean

Returns:

  • (Boolean)


14
# File 'lib/hashira/smells/conditions.rb', line 14

def couple?(node) = node.is_a?(Prism::AndNode) || node.is_a?(Prism::OrNode)

.fence?(node) ⇒ Boolean

Returns:

  • (Boolean)


12
# File 'lib/hashira/smells/conditions.rb', line 12

def fence?(node) = Hashira::Smells::Scope::FENCES.include?(node.class)

.fork(node) ⇒ Object



29
30
31
# File 'lib/hashira/smells/conditions.rb', line 29

def fork(node)
  [node.statements, node.is_a?(Prism::IfNode) ? node.subsequent : node.else_clause]
end

.nested(roots) ⇒ Object



33
# File 'lib/hashira/smells/conditions.rb', line 33

def nested(roots) = roots.compact.flat_map { seek(it) }

.plain(node) ⇒ Object



40
41
42
43
# File 'lib/hashira/smells/conditions.rb', line 40

def plain(node)
  return [] if tested?(node) || fence?(node)
  [node] + node.compact_child_nodes.flat_map { plain(it) }
end

.seek(node) ⇒ Object



35
36
37
38
# File 'lib/hashira/smells/conditions.rb', line 35

def seek(node)
  return [node] if tested?(node)
  fence?(node) ? [] : node.compact_child_nodes.flat_map { seek(it) }
end

.tested?(node) ⇒ Boolean

Returns:

  • (Boolean)


10
# File 'lib/hashira/smells/conditions.rb', line 10

def tested?(node) = TESTED.include?(node.class)