/
min_branches_count.rb
40 lines (32 loc) · 1.02 KB
/
min_branches_count.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true
module RuboCop
module Cop
# Common functionality for checking minimum branches count.
module MinBranchesCount
private
def min_branches_count?(node)
branches =
if node.case_type?
node.when_branches
elsif node.if_type?
if_conditional_branches(node)
else
raise ArgumentError, "Unsupported #{node.type.inspect} node type"
end
branches.size >= min_branches_count
end
def min_branches_count
length = cop_config['MinBranchesCount'] || 3
return length if length.is_a?(Integer) && length.positive?
raise 'MinBranchesCount needs to be a positive integer!'
end
def if_conditional_branches(node, branches = [])
return [] if node.nil? || !node.if_type?
branches << node.if_branch
else_branch = node.else_branch
if_conditional_branches(else_branch, branches) if else_branch&.if_type?
branches
end
end
end
end