Module: RuboCop::Cop::Betterment::Utils::Parser

Defined in:
lib/rubocop/cop/betterment/utils/parser.rb

Class Method Summary collapse

Class Method Details

.explicit_returns(node) ⇒ Object



64
65
66
67
68
# File 'lib/rubocop/cop/betterment/utils/parser.rb', line 64

def self.explicit_returns(node)
  node.descendants.select(&:return_type?).filter_map do |x|
    x&.children&.first
  end
end

.get_extracted_parameters(node, param_aliases: []) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rubocop/cop/betterment/utils/parser.rb', line 87

def self.get_extracted_parameters(node, param_aliases: []) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  return [] unless node.send_type?

  parameter_names = []
  param_aliases << :params

  if node.method?(:[]) && param_aliases.include?(get_root_token(node))
    return node.arguments.select { |x|
      x.sym_type? || x.str_type?
    }.map(&:value)
  end

  children = node.descendants.select do |child|
    child.send_type? && param_aliases.include?(child.method_name)
  end

  children.each do |child|
    ancestors = child.ancestors.select do |ancestor|
      ancestor.send_type? && ancestor.method?(:permit)
    end

    ancestors.each do |ancestor|
      parameter_names += params_from_arguments(ancestor.arguments)
    end
  end

  parameter_names.map(&:to_sym)
end

.get_return_values(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/betterment/utils/parser.rb', line 36

def self.get_return_values(node)
  return [] unless node
  return explicit_returns(node) + get_return_values(node.body) if node.def_type?
  return [node] if node.literal? || node.variable?

  case node.type
  when :begin
    get_return_values(node.children.last)
  when :block
    get_return_values(node.body)
  when :if
    if_rets = get_return_values(node.if_branch)
    else_rets = get_return_values(node.else_branch)
    if_rets + else_rets
  when :case
    cases = []
    node.each_when do |block|
      cases += get_return_values(block.body)
    end

    cases + get_return_values(node.else_branch)
  when :send
    [node]
  else
    []
  end
end

.get_root_token(node) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



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
# File 'lib/rubocop/cop/betterment/utils/parser.rb', line 8

def self.get_root_token(node) # rubocop:disable Metrics/PerceivedComplexity
  return nil unless node

  return get_root_token(node.receiver) if node.receiver

  # rubocop:disable InternalAffairs/NodeDestructuring
  if node.send_type?
    name = node.method_name
  elsif node.variable?
    name, = *node
  elsif node.literal?
    _, name = *node
  elsif node.const_type?
    name = node.const_name.to_sym
  elsif node.sym_type?
    name = node.value
  elsif node.self_type?
    name = :self
  elsif node.block_pass_type?
    name, = *node.children
  else
    name = nil
  end
  # rubocop:enable InternalAffairs/NodeDestructuring

  name
end

.params_from_arguments(arguments) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubocop/cop/betterment/utils/parser.rb', line 70

def self.params_from_arguments(arguments) # rubocop:disable Metrics/PerceivedComplexity
  parameter_names = []

  arguments.each do |arg|
    if arg.hash_type?
      arg.children.each do |pair|
        value = pair.value
        parameter_names << value.value if value.sym_type? || value.str_type?
      end
    elsif arg.sym_type? || arg.str_type?
      parameter_names << arg.value
    end
  end

  parameter_names
end