Skip to content

Commit

Permalink
Make id type non-nilable
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-tan committed Apr 8, 2024
1 parent 7afc04b commit da45252
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/tapioca/dsl/compilers/active_record_relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ def create_common_methods
if constant.table_exists?
primary_key_type = constant.type_for_attribute(constant.primary_key)
type = Tapioca::Dsl::Helpers::ActiveModelTypeHelper.type_for(primary_key_type)
type = RBIHelper.as_non_nilable_type(type)
id_types << type if type != "T.untyped"
end

Expand Down
9 changes: 9 additions & 0 deletions lib/tapioca/helpers/rbi_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ def as_nilable_type(type)
end
end

sig { params(type: String).returns(String) }
def as_non_nilable_type(type)
if type.match(/\A(?:::)?T.nilable\((.+)\)\z/)
T.must(::Regexp.last_match(1))
else
type
end
end

sig { params(name: String).returns(T::Boolean) }
def valid_method_name?(name)
# try to parse a method definition with this name
Expand Down
4 changes: 2 additions & 2 deletions spec/tapioca/dsl/compilers/active_record_relations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def fifth; end
sig { returns(::Post) }
def fifth!; end
sig { params(args: T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything], T.nilable(::CustomId))).returns(::Post) }
sig { params(args: T::Array[T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything], T.nilable(::CustomId))]).returns(T::Enumerable[::Post]) }
sig { params(args: T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything], ::CustomId)).returns(::Post) }
sig { params(args: T::Array[T.any(String, Symbol, ::ActiveSupport::Multibyte::Chars, T::Boolean, BigDecimal, Numeric, ::ActiveRecord::Type::Binary::Data, ::ActiveRecord::Type::Time::Value, Date, Time, ::ActiveSupport::Duration, T::Class[T.anything], ::CustomId)]).returns(T::Enumerable[::Post]) }
def find(args); end
sig { params(args: T.untyped).returns(T.nilable(::Post)) }
Expand Down
9 changes: 9 additions & 0 deletions spec/tapioca/helpers/rbi_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ class Tapioca::RBIHelperSpec < Minitest::Spec
include Tapioca::RBIHelper

describe Tapioca::RBIHelper do
specify "as_non_nilable_type removes T.nilable() and ::T.nilable() if it's the outermost part of the string" do
T.bind(self, T.untyped)

assert_equal(as_non_nilable_type("T.nilable(String)"), "String")
assert_equal(as_non_nilable_type("::T.nilable(String)"), "String")
assert_equal(as_non_nilable_type("String"), "String")
assert_equal(as_non_nilable_type("T.any(T.nilable(String), Integer)"), "T.any(T.nilable(String), Integer)")
end

it "accepts valid method names" do
[
"f",
Expand Down

0 comments on commit da45252

Please sign in to comment.