Skip to content

Commit

Permalink
Strict type Dependabot::Bundler::GemspecFinder (#10004)
Browse files Browse the repository at this point in the history
* Strict type Dependabot::Bundler::GemspecFinder.

* Strict type Dependabot::Bundler::GemspecFinder.
  • Loading branch information
raj-meka committed Jun 17, 2024
1 parent 73c786a commit 663bc8e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions bundler/lib/dependabot/bundler/file_fetcher/gemspec_finder.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "pathname"
require "parser/current"
require "dependabot/bundler/file_fetcher"
require "dependabot/errors"
require "sorbet-runtime"

module Dependabot
module Bundler
class FileFetcher
# Finds the directories of any gemspecs declared using `gemspec` in the
# passed Gemfile.
class GemspecFinder
extend T::Sig

sig { params(gemfile: Dependabot::DependencyFile).void }
def initialize(gemfile:)
@gemfile = gemfile
end

sig { returns(T::Array[String]) }
def gemspec_directories
ast = Parser::CurrentRuby.parse(gemfile.content)
ast = Parser::CurrentRuby.parse(T.must(gemfile).content)
find_gemspec_paths(ast)
rescue Parser::SyntaxError
raise Dependabot::DependencyFileNotParseable, gemfile.path
raise Dependabot::DependencyFileNotParseable, T.must(gemfile).path
end

private

sig { returns(T.nilable(Dependabot::DependencyFile)) }
attr_reader :gemfile

sig { params(node: T.untyped).returns(T::Array[T.untyped]) }
def find_gemspec_paths(node)
return [] unless node.is_a?(Parser::AST::Node)

Expand All @@ -35,7 +42,7 @@ def find_gemspec_paths(node)
return [clean_path(".")] unless path_node

unless path_node.type == :str
path = gemfile.path
path = T.must(gemfile).path
msg = "Dependabot only supports uninterpolated string arguments " \
"to gemspec. Got " \
"`#{path_node.loc.expression.source}`"
Expand All @@ -51,18 +58,21 @@ def find_gemspec_paths(node)
end
end

sig { returns(T.nilable(String)) }
def current_dir
@current_dir ||= gemfile.name.rpartition("/").first
@current_dir ||= T.let(gemfile&.name&.rpartition("/")&.first, T.nilable(String))
@current_dir = nil if @current_dir == ""
@current_dir
end

sig { params(node: Parser::AST::Node).returns(T::Boolean) }
def declares_gemspec_dependency?(node)
return false unless node.is_a?(Parser::AST::Node)

node.children[1] == :gemspec
end

sig { params(path: String).returns(Pathname) }
def clean_path(path)
if Pathname.new(path).absolute?
base_path = Pathname.new(File.expand_path(Dir.pwd))
Expand All @@ -72,6 +82,7 @@ def clean_path(path)
Pathname.new(path).cleanpath
end

sig { params(node: Parser::AST::Node).returns(T.nilable(Parser::AST::Node)) }
def path_node_for_gem_declaration(node)
return unless node.children.last.is_a?(Parser::AST::Node)
return unless node.children.last.type == :hash
Expand All @@ -87,6 +98,7 @@ def path_node_for_gem_declaration(node)
path_hash_pair.children.last
end

sig { params(node: Parser::AST::Node).returns(Symbol) }
def key_from_hash_pair(node)
node.children.first.children.first.to_sym
end
Expand Down

0 comments on commit 663bc8e

Please sign in to comment.