tapioca icon indicating copy to clipboard operation
tapioca copied to clipboard

Tapioca does not generate RBI methods for Struct subclasses

Open bdewater opened this issue 2 years ago • 2 comments

Pulling this out of the Sorbet Slack space and putting it here in case people are surprised by this and can find something about it.

I noticed this pattern in Faraday (ref https://github.com/lostisland/faraday/pull/1489 and https://github.com/lostisland/faraday/pull/1491):

class SSLOptions < Struct.new(:verify_hostname)
  def verify?; end
end

tapioca gem generates RBI methods for verify? but not for verify_hostname/verify_hostname=. The Ruby docs recommend the block form to define a struct, which avoids creating an unused anonymous class in the ancestor chain:

SSLOptions = Struct.new(:verify_hostname) do
  def verify?; end
end

which also happens to help Tapioca pick the verify_hostname/verify_hostname= methods.

It seems in https://github.com/Shopify/tapioca/pull/464 the intent was to fix this, but it seems to have been abandoned. IDK if this is still the plan or that people should just use the Style/StructInheritance Rubocop to avoid the issue from occurring?

bdewater avatar Feb 07 '23 19:02 bdewater

Looks like we return early here because the method's owner is not the constant (SSLOptions) but an anonymous class.

@paracycle can we use something else other than method.owner to get this information. Maybe special case struct definitions and do constant.instance_methods.include?(method.name)?

KaanOzkan avatar Oct 16 '23 17:10 KaanOzkan

Any special handling should also take care of the Ruby 3.2+ Data class. It has the same problem and like for Struct, there's also a Rubocop for it: https://github.com/rubocop/rubocop/pull/11728

bdewater avatar Oct 18 '23 13:10 bdewater