Class: Dependabot::Bundler::FileParser::GemfileDeclarationFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/bundler/file_parser/gemfile_declaration_finder.rb

Overview

Checks whether a dependency is declared in a Gemfile

Instance Method Summary collapse

Constructor Details

#initialize(dependency:, gemfile:) ⇒ GemfileDeclarationFinder

Returns a new instance of GemfileDeclarationFinder.


11
12
13
14
# File 'lib/dependabot/bundler/file_parser/gemfile_declaration_finder.rb', line 11

def initialize(dependency:, gemfile:)
  @dependency = dependency
  @gemfile = gemfile
end

Instance Method Details

#enhanced_req_stringObject


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dependabot/bundler/file_parser/gemfile_declaration_finder.rb', line 20

def enhanced_req_string
  return unless gemfile_includes_dependency?

  fallback_string = dependency.fetch("requirement")
  req_nodes = declaration_node.children[3..-1]
  req_nodes = req_nodes.reject { |child| child.type == :hash }

  return fallback_string if req_nodes.none?
  return fallback_string unless req_nodes.all? { |n| n.type == :str }

  original_req_string = req_nodes.map { |n| n.children.last }
  fallback_requirement =
    Gem::Requirement.new(fallback_string.split(", "))
  if fallback_requirement == Gem::Requirement.new(original_req_string)
    original_req_string.join(", ")
  else
    fallback_string
  end
end

#gemfile_includes_dependency?Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/dependabot/bundler/file_parser/gemfile_declaration_finder.rb', line 16

def gemfile_includes_dependency?
  !declaration_node.nil?
end