Class: RuboCop::Cop::RBS::Lint::UselessAccessModifier

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

Overview

Examples:

default

# bad
class Foo
  public # this is redundant (default access is public)

  def method: () -> void
end

# bad
class Foo
  # The following is redundant (methods defined on the class'
  # singleton class are not affected by the private modifier)
  private

  def self.method3: () -> void
end

# bad
class Foo
  private # this is redundant (no following methods are defined)
end

# good
class Foo
  private # this is not redundant (a method is defined)

  def method2
  end
end

Constant Summary collapse

MSG =
'Useless `%<current>s` access modifier.'

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_constant, #on_rbs_def, #on_rbs_global, #on_rbs_interface, #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

#autocorrect(corrector, range) ⇒ Object



87
88
89
90
# File 'lib/rubocop/cop/rbs/lint/useless_access_modifier.rb', line 87

def autocorrect(corrector, range)
  line = range_by_whole_lines(range, include_final_newline: true)
  corrector.remove(line)
end

#on_rbs_class(decl) ⇒ Object Also known as: on_rbs_module



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
80
81
82
83
84
# File 'lib/rubocop/cop/rbs/lint/useless_access_modifier.rb', line 42

def on_rbs_class(decl)
  current = nil
  unused = true
  decl.members.each do |member|
    next unless member.location

    case member
    when ::RBS::AST::Members::Public
      if current.nil? || current.is_a?(::RBS::AST::Members::Public)
        range = location_to_range(member.location)
        add_offense(range, message: format(MSG, { current: 'public' })) do |corrector|
          autocorrect(corrector, range)
        end
      end
      current = member
    when ::RBS::AST::Members::Private
      if current.is_a?(::RBS::AST::Members::Private)
        range = location_to_range(member.location)
        add_offense(range, message: format(MSG, { current: 'private' })) do |corrector|
          autocorrect(corrector, range)
        end
      end
      current = member
    when ::RBS::AST::Members::MethodDefinition
      if member.kind != :singleton
        unused = false
      end
    end
  end

  if unused && current
    range = location_to_range(current.location)
    vis = case current
          when ::RBS::AST::Members::Public
            'public'
          when ::RBS::AST::Members::Private
            'private'
          end
    add_offense(range, message: format(MSG, { current: vis })) do |corrector|
      autocorrect(corrector, range)
    end
  end
end