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

ActiveRecordFixtures compiler should support passing no args #1895

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions lib/tapioca/dsl/compilers/active_record_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ module Compilers
# # test_case.rbi
# # typed: true
# class ActiveSupport::TestCase
# sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[Post]) }
# sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(Post) }
# sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol))
# .returns(T::Array[Post]) }
# def posts(fixture_name, *other_fixtures); end
# def posts(fixture_name = nil, *other_fixtures); end
# end
# ~~~
class ActiveRecordFixtures < Compiler
Expand Down Expand Up @@ -110,9 +111,15 @@ def method_names_from_eager_fixture_loader
def create_fixture_method(mod, name)
return_type = return_type_for_fixture(name)
mod << RBI::Method.new(name) do |node|
node.add_param("fixture_name")
node.add_opt_param("fixture_name", "nil")
node.add_rest_param("other_fixtures")

node.add_sig do |sig|
sig.add_param("fixture_name", "NilClass")
sig.add_param("other_fixtures", "NilClass")
sig.return_type = "T::Array[#{return_type}]"
end

node.add_sig do |sig|
sig.add_param("fixture_name", "T.any(String, Symbol)")
sig.add_param("other_fixtures", "NilClass")
Expand Down
3 changes: 2 additions & 1 deletion manual/compiler_activerecordfixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ The generated RBI by this compiler will produce the following
# test_case.rbi
# typed: true
class ActiveSupport::TestCase
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[Post]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(Post) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol))
.returns(T::Array[Post]) }
def posts(fixture_name, *other_fixtures); end
def posts(fixture_name = nil, *other_fixtures); end
end
~~~
15 changes: 10 additions & 5 deletions spec/tapioca/dsl/compilers/active_record_fixtures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ class Post < ActiveRecord::Base
# typed: strong

class ActiveSupport::TestCase
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[Post]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(Post) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)).returns(T::Array[Post]) }
def posts(fixture_name, *other_fixtures); end
def posts(fixture_name = nil, *other_fixtures); end
end
RBI

Expand Down Expand Up @@ -111,13 +112,15 @@ class User < ActiveRecord::Base
# typed: strong

class ActiveSupport::TestCase
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[Blog::Post]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(Blog::Post) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)).returns(T::Array[Blog::Post]) }
def blog_posts(fixture_name, *other_fixtures); end
def blog_posts(fixture_name = nil, *other_fixtures); end

sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[User]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(User) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)).returns(T::Array[User]) }
def users(fixture_name, *other_fixtures); end
def users(fixture_name = nil, *other_fixtures); end
end
RBI

Expand All @@ -144,9 +147,10 @@ class Post < ActiveRecord::Base
# typed: strong

class ActiveSupport::TestCase
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[Post]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(Post) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)).returns(T::Array[Post]) }
def posts_with_other_names(fixture_name, *other_fixtures); end
def posts_with_other_names(fixture_name = nil, *other_fixtures); end
end
RBI

Expand All @@ -166,9 +170,10 @@ def posts_with_other_names(fixture_name, *other_fixtures); end
# typed: strong

class ActiveSupport::TestCase
sig { params(fixture_name: NilClass, other_fixtures: NilClass).returns(T::Array[T.untyped]) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: NilClass).returns(T.untyped) }
sig { params(fixture_name: T.any(String, Symbol), other_fixtures: T.any(String, Symbol)).returns(T::Array[T.untyped]) }
def posts(fixture_name, *other_fixtures); end
def posts(fixture_name = nil, *other_fixtures); end
end
RBI

Expand Down
Loading