Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict type Dependabot::Bundler::RequireRelativeFinder #10028

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# 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 paths of any files included using `require_relative` in the
# passed file.
class RequireRelativeFinder
extend T::Sig

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

sig { returns(T::Array[String]) }
def require_relative_paths
ast = Parser::CurrentRuby.parse(file.content)
find_require_relative_paths(ast)
Expand All @@ -25,8 +30,10 @@ def require_relative_paths

private

sig { returns(Dependabot::DependencyFile) }
attr_reader :file

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

Expand All @@ -44,12 +51,14 @@ def find_require_relative_paths(node)
end
end

sig { returns(T.nilable(String)) }
def current_dir
@current_dir ||= file.name.rpartition("/").first
@current_dir ||= T.let(file.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_require_relative?(node)
return false unless node.is_a?(Parser::AST::Node)

Expand Down
Loading