soutaro / rbs-inline

Inline RBS type declaration
MIT License
165 stars 6 forks source link

Implement class/module declaration via blocks #69

Closed soutaro closed 1 week ago

soutaro commented 1 week ago

Some method calls with blocks implicitly defines a class/module or switch the method definition context. One of the most popular example would be ActiveSupport::Concern.

module Foo
  extend ActiveSupport::Concern

  class_methods do
    def foo = 123
  end
end

The #class_methods defines ClassMethods module that will be extend-ed into a class/module where Foo is include-ed.

To support the coding pattern, rbs-inline introduces @rbs class and @rbs module annotations.

module Foo
  extend ActiveSupport::Concern

  # @rbs module ClassMethods
  class_methods do
    # @rbs () -> Integer
    def foo = 123
  end
end

The @rbs module annotation attached to class_methods call defines a module called ClassMethods and put the method definitions inside the module.

# output

module Foo
  module ClassMethods
    def foo: () -> Integer
  end
end