Skip to content

Commit fc1445a

Browse files
authored
Merge pull request #678 from Shopify/uk-fix-smart-properties
Refactor how `SmartProperties` methods are generated
2 parents 42900e3 + 4cbb639 commit fc1445a

File tree

3 files changed

+259
-121
lines changed

3 files changed

+259
-121
lines changed

lib/tapioca/compilers/dsl/smart_properties.rb

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,15 @@ def decorate(root, constant)
7171
)
7272
return if properties.keys.empty?
7373

74-
instance_methods = constant.instance_methods(false).map(&:to_s).to_set
75-
7674
root.create_path(constant) do |k|
77-
properties.values.each do |property|
78-
generate_methods_for_property(k, property) do |method_name|
79-
!instance_methods.include?(method_name)
75+
smart_properties_methods_name = "SmartPropertiesGeneratedMethods"
76+
k.create_module(smart_properties_methods_name) do |mod|
77+
properties.values.each do |property|
78+
generate_methods_for_property(mod, property)
8079
end
8180
end
81+
82+
k.create_include(smart_properties_methods_name)
8283
end
8384
end
8485

@@ -95,26 +96,21 @@ def gather_constants
9596

9697
sig do
9798
params(
98-
klass: RBI::Scope,
99-
property: ::SmartProperties::Property,
100-
block: T.proc.params(arg: String).returns(T::Boolean)
99+
mod: RBI::Scope,
100+
property: ::SmartProperties::Property
101101
).void
102102
end
103-
def generate_methods_for_property(klass, property, &block)
103+
def generate_methods_for_property(mod, property)
104104
type = type_for(property)
105105

106106
if property.writable?
107107
name = property.name.to_s
108108
method_name = "#{name}="
109109

110-
klass.create_method(
111-
method_name,
112-
parameters: [create_param(name, type: type)],
113-
return_type: type
114-
) if block.call(method_name)
110+
mod.create_method(method_name, parameters: [create_param(name, type: type)], return_type: type)
115111
end
116112

117-
klass.create_method(property.reader.to_s, return_type: type) if block.call(property.reader.to_s)
113+
mod.create_method(property.reader.to_s, return_type: type)
118114
end
119115

120116
BOOLEANS = T.let([

spec/tapioca/cli/dsl_spec.rb

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,15 @@ class Post
101101
# Please instead update this file by running `bin/tapioca dsl Post`.
102102
103103
class Post
104-
sig { returns(T.nilable(::String)) }
105-
def title; end
104+
include SmartPropertiesGeneratedMethods
106105
107-
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
108-
def title=(title); end
106+
module SmartPropertiesGeneratedMethods
107+
sig { returns(T.nilable(::String)) }
108+
def title; end
109+
110+
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
111+
def title=(title); end
112+
end
109113
end
110114
RBI
111115

@@ -206,11 +210,15 @@ class Comment
206210
# Please instead update this file by running `bin/tapioca dsl Post`.
207211
208212
class Post
209-
sig { returns(T.nilable(::String)) }
210-
def title; end
213+
include SmartPropertiesGeneratedMethods
211214
212-
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
213-
def title=(title); end
215+
module SmartPropertiesGeneratedMethods
216+
sig { returns(T.nilable(::String)) }
217+
def title; end
218+
219+
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
220+
def title=(title); end
221+
end
214222
end
215223
RBI
216224

@@ -222,11 +230,15 @@ def title=(title); end
222230
# Please instead update this file by running `bin/tapioca dsl Namespace::Comment`.
223231
224232
class Namespace::Comment
225-
sig { returns(::String) }
226-
def body; end
233+
include SmartPropertiesGeneratedMethods
227234
228-
sig { params(body: ::String).returns(::String) }
229-
def body=(body); end
235+
module SmartPropertiesGeneratedMethods
236+
sig { returns(::String) }
237+
def body; end
238+
239+
sig { params(body: ::String).returns(::String) }
240+
def body=(body); end
241+
end
230242
end
231243
RBI
232244

@@ -277,11 +289,15 @@ class Role
277289
# Please instead update this file by running `bin/tapioca dsl Foo::Role`.
278290
279291
class Foo::Role
280-
sig { returns(T.nilable(::String)) }
281-
def title; end
292+
include SmartPropertiesGeneratedMethods
282293
283-
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
284-
def title=(title); end
294+
module SmartPropertiesGeneratedMethods
295+
sig { returns(T.nilable(::String)) }
296+
def title; end
297+
298+
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
299+
def title=(title); end
300+
end
285301
end
286302
RBI
287303

@@ -428,11 +444,15 @@ class Post
428444
# typed: true
429445
430446
class Post
431-
sig { returns(T.nilable(::String)) }
432-
def title; end
447+
include SmartPropertiesGeneratedMethods
433448
434-
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
435-
def title=(title); end
449+
module SmartPropertiesGeneratedMethods
450+
sig { returns(T.nilable(::String)) }
451+
def title; end
452+
453+
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
454+
def title=(title); end
455+
end
436456
end
437457
RBI
438458
end
@@ -663,15 +683,18 @@ def gather_constants
663683
class Post
664684
include GeneratedBar
665685
include GeneratedFoo
666-
667-
sig { returns(T.nilable(::String)) }
668-
def title; end
669-
670-
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
671-
def title=(title); end
686+
include SmartPropertiesGeneratedMethods
672687
673688
module GeneratedBar; end
674689
module GeneratedFoo; end
690+
691+
module SmartPropertiesGeneratedMethods
692+
sig { returns(T.nilable(::String)) }
693+
def title; end
694+
695+
sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
696+
def title=(title); end
697+
end
675698
end
676699
RBI
677700

0 commit comments

Comments
 (0)