Class: Dependabot::Bundler::FileUpdater::RequirementReplacer::Rewriter

Inherits:
Parser::TreeRewriter
  • Object
show all
Defined in:
lib/dependabot/bundler/file_updater/requirement_replacer.rb

Constant Summary collapse

SKIPPED_TYPES =

TODO: Ideally we wouldn’t have to ignore all of these, but implementing each one will be tricky.

%i(send lvar dstr begin if splat const or).freeze

Instance Method Summary collapse

Constructor Details

#initialize(dependency:, file_type:, updated_requirement:, insert_if_bare:) ⇒ Rewriter

Returns a new instance of Rewriter.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dependabot/bundler/file_updater/requirement_replacer.rb', line 78

def initialize(dependency:, file_type:, updated_requirement:,
               insert_if_bare:)
  @dependency          = dependency
  @file_type           = file_type
  @updated_requirement = updated_requirement
  @insert_if_bare      = insert_if_bare

  return if %i(gemfile gemspec).include?(file_type)

  raise "File type must be :gemfile or :gemspec. Got #{file_type}."
end

Instance Method Details

#on_send(node) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dependabot/bundler/file_updater/requirement_replacer.rb', line 90

def on_send(node)
  return unless declares_targeted_gem?(node)

  req_nodes = node.children[3..-1]
  req_nodes = req_nodes.reject { |child| child.type == :hash }

  return if req_nodes.none? && !insert_if_bare?
  return if req_nodes.any? { |n| SKIPPED_TYPES.include?(n.type) }

  quote_characters = extract_quote_characters_from(req_nodes)
  space_after_specifier = space_after_specifier?(req_nodes)
  use_equality_operator = use_equality_operator?(req_nodes)

  new_req = new_requirement_string(
    quote_characters: quote_characters,
    space_after_specifier: space_after_specifier,
    use_equality_operator: use_equality_operator
  )
  if req_nodes.any?
    replace(range_for(req_nodes), new_req)
  else
    insert_after(range_for(node.children[2..2]), ", #{new_req}")
  end
end