Class: RuboCop::Cop::RBS::Style::TrueFalse

Inherits:
RBS::CopBase
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rbs/style/true_false.rb

Overview

Examples:

default

# bad
def foo: (true | false) -> (true | false)

# bad
def foo: (TrueClass | FalseClass) -> (TrueClass | FalseClass)

# good
def foo: (bool) -> bool

Instance Attribute Summary

Attributes inherited from RBS::CopBase

#processed_rbs_source

Instance Method Summary collapse

Methods inherited from RBS::CopBase

#investigation_rbs, #location_to_range, #on_new_investigation, #on_other_file, #on_rbs_attribute, #on_rbs_class, #on_rbs_constant, #on_rbs_global, #on_rbs_interface, #on_rbs_module, #on_rbs_new_investigation, #on_rbs_parsing_error, #on_rbs_private, #on_rbs_public, #on_rbs_type_alias, #on_rbs_var, #parse_rbs, #rbs_buffer, #tokenize, #walk

Methods included from RBS::OnTypeHelper

#on_not_type, #on_type

Instance Method Details

#find_replacement(type, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubocop/cop/rbs/style/true_false.rb', line 34

def find_replacement(type, &block)
  case type
  when ::RBS::Types::Union
    has_true = has_false = false
    type.types.each do |t|
      case t
      when ::RBS::Types::Literal
        has_true = true if t.literal == true
        has_false = true if t.literal == false
      when ::RBS::Types::ClassInstance
        case t.name.to_s
        when 'TrueClass', '::TrueClass'
          has_true = true
        when 'FalseClass', '::FalseClass'
          has_false = true
        end
      end
    end
    if has_true && has_false
      replaced = type.types.dup
      first_index = nil
      i = -1
      replaced.delete_if do |t|
        i += 1
        case t
        when ::RBS::Types::Literal
          (t.literal == true || t.literal == false).tap do |r|
            first_index ||= i if r
          end
        when ::RBS::Types::ClassInstance
          t.name.to_s.then do |s|
            s == 'TrueClass' || s == '::TrueClass' || s == 'FalseClass' || s == '::FalseClass'
          end.tap do |r|
            first_index ||= i if r
          end
        end
      end
      replaced.insert(first_index || 0, ::RBS::Types::Bases::Bool.new(location: nil))
      block.call([type, ::RBS::Types::Union.new(types: replaced, location: nil)])
    end
  else
    type.each_type do |type|
      find_replacement(type, &block)
    end
  end
end

#on_rbs_def(decl) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubocop/cop/rbs/style/true_false.rb', line 20

def on_rbs_def(decl)
  decl.overloads.each do |overload|
    overload.method_type.each_type do |type|
      find_replacement(type) do |t, replaced|
        range = location_to_range(t.location)
        add_offense(range, message: "Use `#{replaced}` instead of `#{t}`") do |corrector|
          corrector.replace(range, replaced.to_s)
        end
      end
    end
  end
end