Skip to content

Definitions for automatically generated classes would be missing #22

@hibariya

Description

@hibariya

I noticed another kind of missing-definition problem while I was trying to use my working branch (mentioned in #20) in a Rails app: when some classes are generated automatically like ActiveRecord does, we have no chance to generate the definition today.

(I'll try to figure out some solutions, but LMK if you have already been working on it, or have any advice 🙏 )

For example, let's say we have active record models called User and Bookmark (a user has many bookmarks), and a method that returns an association of bookmarks.

class User < ApplicationRecord
  has_many :bookmarks
end

class Bookmark < ApplicationRecord
  belongs_to :user
end

class SomeClass
  def initialize(user)
    @user = user
  end

  def something_returns_association
    @user.bookmarks
  end
end

and run the following script:

trace = RBS::Trace.new
trace.enable do
  user = User.create
  p SomeClass.new(user).something_returns_association
end

trace.save_comments
trace.save_files out_dir: 'sig'

Then we will get the following comments in the User class:

class SomeClass
  # @rbs (User) -> void
  def initialize(user)
    @user = user
  end

  # @rbs () -> Bookmark::ActiveRecord_Associations_CollectionProxy
  def something_returns_association
    @user.bookmarks
  end
end

and an rbs file as follows:

class SomeClass
  def initialize: (User) -> void

  def something_returns_association: () -> Bookmark::ActiveRecord_Associations_CollectionProxy
end

However, since we don't have the definition of Bookmark::ActiveRecord_Associations_CollectionProxy, we will get the following error when steep check runs.

sig/app/models/some_class.rbs:4:43: [error] Cannot find type `Bookmark::ActiveRecord_Associations_CollectionProxy`
│ Diagnostic ID: RBS::UnknownTypeName
│
└   def something_returns_association: () -> Bookmark::ActiveRecord_Associations_CollectionProxy
                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions