Class: RuboCop::Cop::Require::MissingRequireStatement

Inherits:
RuboCop::Cop
  • Object
show all
Defined in:
lib/rubocop/cop/require/missing_require_statement.rb

Overview

Checks for missing require statements in your code

Examples:

# bad
Faraday.new

# good
require 'faraday'

Faraday.new

Constant Summary collapse

MSG =
'`%<constant>s` not found, you\'re probably missing a require statement or there is a cycle in your dependencies.'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#timelineObject



24
25
26
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 24

def timeline
  @timeline ||= []
end

Instance Method Details

#add_offense(node, location: nil, message:) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 66

def add_offense(node, location: nil, message:)
  # Work around breaking API changes between rubocop 0.49.1 and later (...)
  signature_old = %i[node loc message severity]
  param_info = RuboCop::Cop::Cop.instance_method(:add_offense).parameters
  if param_info.map(&:last) == signature_old
    super(node, location || :expression, message)
  elsif location
    super(node, location: location, message: message)
  else
    super(node, message: message)
  end
end

#find_consts(node) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 87

def find_consts(node)
  inner = node
  outer_const = extract_const(node)
  return unless outer_const
  consts = [outer_const]
  while (inner = extract_inner_const(inner))
    const = extract_const(inner)
    consts << const
  end
  consts.reverse
end

#investigate(processed_source) ⇒ Object

Builds



29
30
31
32
33
34
35
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
63
64
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 29

def investigate(processed_source)
  processing_methods = self.methods.select { |m| m.to_s.start_with? 'process_' }

  stack = [processed_source.ast]
  skip = Set.new
  until stack.empty?
    node = stack.pop
    next unless node

    results = processing_methods.map { |m| self.send(m, node, processed_source) }.compact

    next if node.kind_of? Hash

    to_skip, to_push = %i[skip push].map { |mode| results.flat_map { |r| r[mode] }.compact }

    skip.merge(to_skip)

    children_to_explore = node.children
                              .select { |c| c.kind_of? RuboCop::AST::Node }
                              .reject { |c| skip.include? c }
                              .reverse
    stack.push(*to_push)
    stack.push(*children_to_explore)
  end

  err_events = check_timeline(timeline).group_by { |e| e[:name] }.values
  err_events.each do |events|
    first = events.first
    node = first[:node]
    message = format(
      MSG,
      constant: first[:name]
    )
    add_offense(node, message: message)
  end
end

#process_const(node, _source) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 99

def process_const(node, _source)
  return unless node.kind_of? RuboCop::AST::Node
  consts = find_consts(node)
  return unless consts
  const_name = consts.join('::')

  self.timeline << { event: :const_access, name: const_name, node: node }

  { skip: node.children }
end

#process_const_assign(node, _source) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 114

def process_const_assign(node, _source)
  return unless node.kind_of? RuboCop::AST::Node
  const_assign_name = extract_const_assignment(node)
  return unless const_assign_name

  # When the assigned value is itself a constant reference, the assignment is an
  # alias (e.g. `Foo = Bar::Baz`). Track the target so member access through the
  # alias (`Foo::QUX`) can be resolved to the real constant later.
  value_node = node.children[2]
  alias_target = nil
  if value_node.kind_of?(RuboCop::AST::Node) && value_node.type == :const
    consts = find_consts(value_node)
    alias_target = consts.join('::') if consts
  end

  self.timeline << { event: :const_assign, name: const_assign_name, alias_target: alias_target }

  { skip: node.children }
end

#process_definition(node, _source) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 142

def process_definition(node, _source)
  if node.kind_of? Hash
    self.timeline << node
    return
  end

  return unless is_module_or_class?(node)
  name = find_consts(node.children.first).join('::')
  inherited = find_consts(node.children[1]).join('::') if has_superclass?(node)

  # Inheritance technically has to happen before the actual class definition
  self.timeline << { event: :const_inherit, name: inherited, node: node } if inherited

  self.timeline << { event: :const_def, name: name }

  # First child is the module/class name => skip or it'll be picked up by `process_const`
  skip_list = [node.children.first]
  skip_list << node.children[1] if inherited

  push_list = []
  push_list << { event: :const_undef, name: name }

  { skip: skip_list, push: push_list }
end

#process_require(node, source) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 171

def process_require(node, source)
  return unless node.kind_of? RuboCop::AST::Node
  required = extract_require(node)
  return unless required && required.length == 2
  method, file = required
  self.timeline << { event: method, file: file, path: source.path }

  { skip: node.children }
end