class Dependabot::Bundler::FileUpdater::GemspecDependencyNameFinder

Attributes

gemspec_content[R]

Public Class Methods

new(gemspec_content:) click to toggle source
# File lib/dependabot/bundler/file_updater/gemspec_dependency_name_finder.rb, line 12
def initialize(gemspec_content:)
  @gemspec_content = gemspec_content
end

Public Instance Methods

dependency_name() click to toggle source

rubocop:disable Security/Eval

# File lib/dependabot/bundler/file_updater/gemspec_dependency_name_finder.rb, line 17
def dependency_name
  ast = Parser::CurrentRuby.parse(gemspec_content)
  dependency_name_node = find_dependency_name_node(ast)
  return unless dependency_name_node

  begin
    eval(dependency_name_node.children[2].loc.expression.source)
  rescue StandardError
    nil # If we can't evaluate the expression just return nil
  end
end

Private Instance Methods

declares_dependency_name?(node) click to toggle source
# File lib/dependabot/bundler/file_updater/gemspec_dependency_name_finder.rb, line 42
def declares_dependency_name?(node)
  return false unless node.is_a?(Parser::AST::Node)

  node.children[1] == :name=
end
find_dependency_name_node(node) click to toggle source

rubocop:enable Security/Eval

# File lib/dependabot/bundler/file_updater/gemspec_dependency_name_finder.rb, line 32
def find_dependency_name_node(node)
  return unless node.is_a?(Parser::AST::Node)
  return node if declares_dependency_name?(node)

  node.children.find do |cn|
    dependency_name_node = find_dependency_name_node(cn)
    break dependency_name_node if dependency_name_node
  end
end