Class: RuboCop::Cop::RBS::Layout::ExtraSpacing

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

Overview

Examples:

default

# bad
def   foo:   ()   ->   void

# good
def foo: () -> void

Constant Summary collapse

MSG =
'Unnecessary spacing detected.'

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_def, #on_rbs_global, #on_rbs_interface, #on_rbs_module, #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

#aligned_locationsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubocop/cop/rbs/layout/extra_spacing.rb', line 38

def aligned_locations
  comments = processed_rbs_source.tokens.select(&:comment?)
  Set.new.tap do |aligned|
    comments.map(&:location).each_cons(2) do |loc1, loc2|
      next unless loc1
      next unless loc2

      if loc1.start_column == loc2.start_column
        aligned << loc1.start_line << loc2.start_line
      end
    end
  end
end

#on_rbs_new_investigationObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/rbs/layout/extra_spacing.rb', line 18

def on_rbs_new_investigation
  aligned_comments = aligned_locations()
  tokens = processed_rbs_source.tokens.reject { |t| t.type == :tTRIVIA }
  tokens.each_cons(2) do |a, b|
    next unless a&.location
    next unless b&.location

    if (b.type == :tCOMMENT || b.type == :tLINECOMMENT)
      next if aligned_comments.include?(b.location.start_line)
    end
    next if a.location.end_line != b.location.start_line
    next if a.location.end_pos + 1 >= b.location.start_pos

    space = range_between(a.location.end_pos, b.location.start_pos - 1)
    add_offense(space) do |corrector|
      corrector.remove(space)
    end
  end
end