Add type signatures for PATH.

This commit is contained in:
Markus Reiter 2020-10-19 22:10:21 +02:00
parent 7a3c43e3cd
commit a582b6e371

View File

@ -1,52 +1,71 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
# Represention of a `*PATH` environment variable. # Represention of a `*PATH` environment variable.
# #
# @api private # @api private
class PATH class PATH
extend T::Sig
include Enumerable include Enumerable
extend Forwardable extend Forwardable
def_delegator :@paths, :each def_delegator :@paths, :each
# FIXME: Enable cop again when https://github.com/sorbet/sorbet/issues/3532 is fixed.
# rubocop:disable Style/MutableConstant
Element = T.type_alias { T.nilable(T.any(Pathname, String, PATH)) }
private_constant :Element
Elements = T.type_alias { T.any(Element, T::Array[Element]) }
private_constant :Elements
# rubocop:enable Style/MutableConstant
sig { params(paths: Elements).void }
def initialize(*paths) def initialize(*paths)
@paths = parse(*paths) @paths = parse(paths)
end end
sig { params(paths: Elements).returns(T.self_type) }
def prepend(*paths) def prepend(*paths)
@paths = parse(*paths, *@paths) @paths = parse(paths + @paths)
self self
end end
sig { params(paths: Elements).returns(T.self_type) }
def append(*paths) def append(*paths)
@paths = parse(*@paths, *paths) @paths = parse(@paths + paths)
self self
end end
sig { params(index: Integer, paths: Elements).returns(T.self_type) }
def insert(index, *paths) def insert(index, *paths)
@paths = parse(*@paths.insert(index, *paths)) @paths = parse(@paths.insert(index, *paths))
self self
end end
sig { params(block: T.proc.params(arg0: String).returns(T::Boolean)).returns(T.self_type) }
def select(&block) def select(&block)
self.class.new(@paths.select(&block)) self.class.new(@paths.select(&block))
end end
sig { params(block: T.proc.params(arg0: String).returns(T::Boolean)).returns(T.self_type) }
def reject(&block) def reject(&block)
self.class.new(@paths.reject(&block)) self.class.new(@paths.reject(&block))
end end
sig { returns(T::Array[String]) }
def to_ary def to_ary
@paths.dup.to_ary @paths.dup.to_ary
end end
alias to_a to_ary alias to_a to_ary
sig { returns(String) }
def to_str def to_str
@paths.join(File::PATH_SEPARATOR) @paths.join(File::PATH_SEPARATOR)
end end
alias to_s to_str alias to_s to_str
sig { params(other: T.untyped).returns(T::Boolean) }
def ==(other) def ==(other)
if other.respond_to?(:to_ary) && to_ary == other.to_ary if other.respond_to?(:to_ary) && to_ary == other.to_ary
true true
@ -57,10 +76,12 @@ class PATH
end end
end end
sig { returns(T::Boolean) }
def empty? def empty?
@paths.empty? @paths.empty?
end end
sig { returns(T.nilable(T.self_type)) }
def existing def existing
existing_path = select(&File.method(:directory?)) existing_path = select(&File.method(:directory?))
# return nil instead of empty PATH, to unset environment variables # return nil instead of empty PATH, to unset environment variables
@ -69,10 +90,11 @@ class PATH
private private
def parse(*paths) sig { params(paths: T::Array[Elements]).returns(T::Array[String]) }
def parse(paths)
paths.flatten paths.flatten
.compact .compact
.flat_map { |p| Pathname.new(p).to_path.split(File::PATH_SEPARATOR) } .flat_map { |p| Pathname(p).to_path.split(File::PATH_SEPARATOR) }
.uniq .uniq
end end
end end