Module: RuboCop::Cop::RequireLibrary

Extended by:
Macros
Included in:
Style::SpecialGlobalVars
Defined in:
lib/rubocop/cop/mixin/require_library.rb

Overview

Ensure a require statement is present for a standard library determined by variable library_name

Constant Summary collapse

RESTRICT_ON_SEND =
[:require].freeze

Instance Method Summary collapse

Instance Method Details

#ensure_required(corrector, node, library_name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/cop/mixin/require_library.rb', line 12

def ensure_required(corrector, node, library_name)
  node = node.parent while node.parent&.parent?

  if node.parent&.begin_type?
    return if @required_libs.include?(library_name)

    remove_subsequent_requires(corrector, node, library_name)
  end

  RequireLibraryCorrector.correct(corrector, node, library_name)
end

#on_send(node) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/rubocop/cop/mixin/require_library.rb', line 33

def on_send(node)
  return if node.parent&.parent?

  name = require_any_library?(node)
  return if name.nil?

  @required_libs.add(name)
end

#remove_subsequent_requires(corrector, node, library_name) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rubocop/cop/mixin/require_library.rb', line 24

def remove_subsequent_requires(corrector, node, library_name)
  node.right_siblings.each do |sibling|
    next unless require_library_name?(sibling, library_name)

    range = range_by_whole_lines(sibling.source_range, include_final_newline: true)
    corrector.remove(range)
  end
end