Skip to content

Commit

Permalink
Collect both SmartProperties classes and modules
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
  • Loading branch information
Morriar committed Sep 16, 2021
1 parent d24fdaf commit 82dc4f9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/tapioca/compilers/dsl/smart_properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
all_classes.select do |c|
c < ::SmartProperties
end.reject do |c|
name_of(c).nil? || c == ::SmartProperties::Validations::Ancestor
all_modules.select do |c|
name_of(c) &&
c != ::SmartProperties::Validations::Ancestor &&
c < ::SmartProperties && ::SmartProperties::ClassMethods === c
end
end

Expand Down
42 changes: 38 additions & 4 deletions spec/tapioca/compilers/dsl/smart_properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Tapioca::Compilers::Dsl::SmartPropertiesSpec < DslSpec
assert_empty(gathered_constants)
end

it("gathers only SmartProperty classes") do
it("gathers only SmartProperty classes and modules") do
add_ruby_file("content.rb", <<~RUBY)
class Post
include ::SmartProperties
Expand All @@ -21,16 +21,27 @@ class User
class Comment
end
module Viewable
include ::SmartProperties
end
module Editable
end
RUBY

assert_equal(["Post", "User"], gathered_constants)
assert_equal(["Post", "User", "Viewable"], gathered_constants)
end

it("ignores SmartProperty classes without a name") do
it("ignores SmartProperty classes and modules without a name") do
add_ruby_file("content.rb", <<~RUBY)
post = Class.new do
include ::SmartProperties
end
viewable = Module.new do
include ::SmartProperties
end
RUBY

assert_empty(gathered_constants)
Expand All @@ -52,7 +63,7 @@ class Post
assert_equal(expected, rbi_for(:Post))
end

it("generates RBI file for simple smart property") do
it("generates RBI file for simple smart property class") do
add_ruby_file("post.rb", <<~RUBY)
class Post
include SmartProperties
Expand All @@ -75,6 +86,29 @@ def title=(title); end
assert_equal(expected, rbi_for(:Post))
end

it("generates RBI file for simple smart property module") do
add_ruby_file("viewable.rb", <<~RUBY)
module Viewable
include SmartProperties
property :title, accepts: String
end
RUBY

expected = <<~RBI
# typed: strong
module Viewable
sig { returns(T.nilable(::String)) }
def title; end
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
def title=(title); end
end
RBI

assert_equal(expected, rbi_for(:Viewable))
end

it("generates RBI file for required smart property") do
add_ruby_file("post.rb", <<~RUBY)
class Post
Expand Down

0 comments on commit 82dc4f9

Please sign in to comment.