Class: SorbetRails::HelperRbiFormatter

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/sorbet-rails/helper_rbi_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(helpers) ⇒ HelperRbiFormatter

Returns a new instance of HelperRbiFormatter.



8
9
10
11
# File 'lib/sorbet-rails/helper_rbi_formatter.rb', line 8

def initialize(helpers)
  @parlour = T.let(Parlour::RbiGenerator.new, Parlour::RbiGenerator)
  @helpers = T.let(helpers, T::Array[Module])
end

Instance Method Details

#generate_rbiObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sorbet-rails/helper_rbi_formatter.rb', line 16

def generate_rbi
  puts "-- Generate sigs for helpers --"

  @parlour.root.add_comment([
    'This is an autogenerated file for Rails helpers.',
    'Please rerun bundle exec rake rails_rbi:helpers to regenerate.'
  ])

  @helpers.each do |helper|
    @parlour.root.create_module(helper.to_s) do |mod|
      mod.create_include('Kernel')
      mod.create_include('ActionView::Helpers')
      ::SorbetRails.config.extra_helper_includes.each do |extra_helper|
        mod.create_include(extra_helper) unless extra_helper == helper.to_s
      end
    end
  end

  if ActionController::Helpers.method_defined?(:helpers)
    # Adds the `helpers` method that provides access to all methods within the
    # application's helpers.
    # https://api.rubyonrails.org/classes/ActionController/Helpers/ClassMethods.html#method-i-helpers
    @parlour.root.create_module('ActionController::Helpers') do |mod|
      mod.create_method(
        'helpers',
        return_type: "T.all(#{@helpers.join(', ')})"
      )
    end
  end

  return @parlour.rbi
end