Class: RuboCop::Cop::Sorbet::ForbidIncludeConstLiteral

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/sorbet/forbid_include_const_literal.rb

Overview

Correct ‘send` expressions in include statements by constant literals.

Sorbet, the static checker, is not (yet) able to support constructs on the following form:

“‘ruby class MyClass

include send_expr

end “‘

Multiple occurences of this can be found in Shopify’s code base like:

“‘ruby include Rails.application.routes.url_helpers “` or “`ruby include Polaris::Engine.helpers “`

Constant Summary collapse

MSG =
"`%<inclusion_method>s` must only be used with constant literals as arguments"
RESTRICT_ON_SEND =
[:include, :extend, :prepend].freeze

Instance Method Summary collapse

Instance Method Details

#dynamic_inclusion?(node) ⇒ Object



36
37
38
# File 'lib/rubocop/cop/sorbet/forbid_include_const_literal.rb', line 36

def_node_matcher :dynamic_inclusion?, <<~PATTERN
  (send nil? ${:include :extend :prepend} $#neither_const_nor_self?)
PATTERN

#on_send(node) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/sorbet/forbid_include_const_literal.rb', line 40

def on_send(node)
  dynamic_inclusion?(node) do |inclusion_method, included|
    return unless within_onymous_module?(node)

    add_offense(node, message: format(MSG, inclusion_method: inclusion_method)) do |corrector|
      corrector.replace(node, "T.unsafe(self).#{inclusion_method} #{included.source}")
    end
  end
end