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

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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(gemfile:) ⇒ GemfileDeclarationFinder

Returns a new instance of GemfileDeclarationFinder.



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

def initialize(gemfile:)
  @gemfile = gemfile
  @declaration_nodes = T.let({}, T::Hash[T::Hash[String, String], T.nilable(Parser::AST::Node)])
end

Instance Method Details

#enhanced_req_string(dependency) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dependabot/bundler/file_parser/gemfile_declaration_finder.rb', line 29

def enhanced_req_string(dependency)
  return unless gemfile_includes_dependency?(dependency)

  fallback_string = dependency.fetch("requirement")
  req_nodes = declaration_node(dependency)&.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?(dependency) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/dependabot/bundler/file_parser/gemfile_declaration_finder.rb', line 23

def gemfile_includes_dependency?(dependency)
  !declaration_node(dependency).nil?
end