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

Inherits:
Parser::TreeRewriter
  • Object
show all
Extended by:
T::Sig
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.

T.let(i(send lvar dstr begin if case splat const or).freeze, T::Array[Symbol])

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Rewriter.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dependabot/bundler/file_updater/requirement_replacer.rb', line 129

def initialize(
  dependency:,
  file_type:,
  updated_requirement:,
  insert_if_bare:
)
  @dependency = T.let(dependency, Dependabot::Dependency)
  @file_type = T.let(file_type, Symbol)
  @updated_requirement = T.let(updated_requirement, String)
  @insert_if_bare = T.let(insert_if_bare, T::Boolean)

  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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/dependabot/bundler/file_updater/requirement_replacer.rb', line 146

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