Module: RuboCop::Cop::Sorbet::MutableConstantSorbetAwareBehaviour

Defined in:
lib/rubocop/cop/sorbet/mutable_constant_sorbet_aware_behaviour.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rubocop/cop/sorbet/mutable_constant_sorbet_aware_behaviour.rb', line 10

def prepended(base)
  # @!method t_let(node)
  base.def_node_matcher(:t_let, <<~PATTERN)
    (send (const nil? :T) :let $_constant _type)
  PATTERN

  # @!method sorbet_type_declaration?(node)
  base.def_node_matcher(:sorbet_type_declaration?, <<~PATTERN)
    {
      (send nil? {:type_member :type_template} ...)
      (block (send nil? {:type_member :type_template} ...) ...)
      (send (const nil? :T) :type_alias ...)
      (block (send (const nil? :T) :type_alias ...) ...)
    }
  PATTERN
end

Instance Method Details

#on_assignment(value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/sorbet/mutable_constant_sorbet_aware_behaviour.rb', line 28

def on_assignment(value)
  # Sorbet type declarations such as `type_member`, `type_template`,
  # and `T.type_alias` produce type objects that cannot be frozen
  # (calling `.freeze` on them breaks Sorbet), so they are exempt.
  return if sorbet_type_declaration?(value)

  t_let(value) do |constant|
    value = constant
  end

  super(value)
end