Class: RuboCop::Cop::Require::MissingRequireStatement
- Inherits:
-
RuboCop::Cop
- Object
- RuboCop::Cop
- RuboCop::Cop::Require::MissingRequireStatement
- Defined in:
- lib/rubocop/cop/require/missing_require_statement.rb
Overview
Checks for missing require statements in your code
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
- #find_consts(node) ⇒ Object
-
#investigate(processed_source) ⇒ Object
Builds.
- #process_const(node, _source) ⇒ Object
- #process_const_assign(node, _source) ⇒ Object
- #process_definition(node, _source) ⇒ Object
- #process_require(node, source) ⇒ Object
Instance Attribute Details
#timeline ⇒ Object
24 25 26 |
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 24 def timeline @timeline ||= [] end |
Instance Method Details
#find_consts(node) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 74 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] = format( MSG, constant: first[:name] ) add_offense(node, message: ) end end |
#process_const(node, _source) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 86 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
101 102 103 104 105 106 107 108 |
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 101 def process_const_assign(node, _source) const_assign_name = extract_const_assignment(node) return unless const_assign_name self.timeline << { event: :const_assign, name: const_assign_name } { skip: node.children } end |
#process_definition(node, _source) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 118 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
147 148 149 150 151 152 153 154 155 |
# File 'lib/rubocop/cop/require/missing_require_statement.rb', line 147 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 |