Class: RuboCop::Cop::RBS::Layout::OverloadIndentation
- Inherits:
-
RBS::CopBase
- Object
- Base
- RBS::CopBase
- RuboCop::Cop::RBS::Layout::OverloadIndentation
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/rbs/layout/overload_indentation.rb
Overview
Instance Attribute Summary
Attributes inherited from RBS::CopBase
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
Instance Method Details
#on_rbs_def(decl) ⇒ Object
25 26 27 28 29 30 31 32 33 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 |
# File 'lib/rubocop/cop/rbs/layout/overload_indentation.rb', line 25 def on_rbs_def(decl) base_pos = decl.location.start_pos base_col = decl.location.start_column overload_starts = decl.overloads.map { |overload| overload.method_type.location.start_pos } tokens = ::RBS::Parser.lex(decl.location.source).value.reject { |t| t.type == :tTRIVIA } first_colon_column = base_col + tokens.find { |t| t.type == :pCOLON }&.location&.start_column ([nil] + tokens).each_cons(3) do |before, , after| next unless before next unless next unless after next unless &.type == :pBAR loc = &.location next unless loc next unless overload_starts.include?(base_pos + after.location.start_pos) if before.location.end_line == .location.start_line range = range_between(base_pos + .location.start_pos, base_pos + .location.end_pos) add_offense(range, message: "Insert newline before `|`") do |corrector| space = range_between(base_pos + before.location.end_pos, base_pos + .location.start_pos) corrector.replace(space, "\n#{' ' * first_colon_column}") end end if .location.end_line != after.location.start_line range = range_between(base_pos + .location.start_pos, base_pos + .location.end_pos) add_offense(range, message: "Remove newline after `|`") do |corrector| space = range_between(base_pos + .location.end_pos, base_pos + after.location.start_pos) corrector.replace(space, ' ') end elsif .location.start_column != first_colon_column range = range_between(base_pos + .location.start_pos, base_pos + .location.end_pos) add_offense(range, message: 'Indent the `|` to the first `:`') do |corrector| space = range_between(base_pos + .location.start_pos - .location.start_column, base_pos + .location.start_pos) corrector.replace(space, ' ' * first_colon_column) end end end end |