Class: RuboCop::Cop::RBS::Style::RedundantParentheses

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

Overview

Examples:

default

# bad
def foo: () -> (bool)

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

# good
def foo: () -> bool

# good
def foo: ((true | false)) -> bool

Defined Under Namespace

Modules: BeforeTokenIfLparen Classes: ParenChecker

Instance Attribute Summary

Attributes inherited from RBS::CopBase

#processed_rbs_source

Instance Method Summary collapse

Methods included from BeforeTokenIfLparen

#before_token_if_lparen

Methods inherited from RBS::CopBase

#investigation_rbs, #location_to_range, #on_new_investigation, #on_other_file, #on_rbs_class, #on_rbs_interface, #on_rbs_module, #on_rbs_new_investigation, #on_rbs_parsing_error, #on_rbs_private, #on_rbs_public, #parse_rbs, #rbs_buffer, #tokenize, #walk

Methods included from RBS::OnTypeHelper

#on_not_type, #on_type

Instance Method Details

#check_type(tokens:, type:, base:, skip: Set.new) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/rubocop/cop/rbs/style/redundant_parentheses.rb', line 139

def check_type(tokens:, type:, base:, skip: Set.new)
  ParenChecker.new(
    processed_source:,
    base:,
    tokens:,
    type:,
    skip:,
    cop: self
  ).check
end

#on_rbs_constant(const) ⇒ Object Also known as: on_rbs_global, on_rbs_type_alias, on_rbs_attribute, on_rbs_var



150
151
152
153
154
155
# File 'lib/rubocop/cop/rbs/style/redundant_parentheses.rb', line 150

def on_rbs_constant(const)
  tokens = tokenize(const.location.source)
  type = const.type
  base = const.location.start_pos
  check_type(tokens:, type:, base:)
end

#on_rbs_def(decl) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rubocop/cop/rbs/style/redundant_parentheses.rb', line 120

def on_rbs_def(decl)
  base = decl.location.start_pos
  tokens = tokenize(decl.location.source)
  skip = Set.new
  decl.overloads.each do |overload|
    before_token_if_lparen(tokens, base, overload.method_type.type) do |b|
      skip << (b.location.start_pos + base)
    end
    if overload.method_type.block
      before_token_if_lparen(tokens, base, overload.method_type.block.type) do |b|
        skip << (b.location.start_pos + base)
      end
    end
    overload.method_type.each_type do |type|
      check_type(tokens:, type:, base:, skip:)
    end
  end
end