Class: RuboCop::Cop::RBS::Layout::SpaceAroundOperators

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

Overview

Examples:

default

# bad
Integer|String

# good
Integer | String

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_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_operator(type, operator) ⇒ Object



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
# File 'lib/rubocop/cop/rbs/layout/space_around_operators.rb', line 44

def check_operator(type, operator)
  type.types.each_cons(2) do |before, after|
    next unless before.location.end_line == after.location.start_line

    operator_index = type.location.source.index(
      operator,
      before.location.end_pos - type.location.start_pos
    ) or raise
    operator_index += type.location.start_pos
    operator_range = range_between(operator_index, operator_index + 1)

    before_char = processed_source.raw_source[operator_index - 1]
    if before_char != ' '
      add_offense(operator_range, message: "Use one space before `#{operator}`.") do |corrector|
        corrector.insert_before(operator_range, ' ')
      end
    end

    after_char = processed_source.raw_source[operator_index + 1]
    if after_char != ' '
      add_offense(operator_range, message: "Use one space after `#{operator}`.") do |corrector|
        corrector.insert_after(operator_range, ' ')
      end
    end
  end
end

#check_type(type) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/rbs/layout/space_around_operators.rb', line 32

def check_type(type)
  case type
  when ::RBS::Types::Union
    check_operator(type, '|')
  when ::RBS::Types::Intersection
    check_operator(type, '&')
  end
  type.each_type do |t|
    check_type(t)
  end
end

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



24
25
26
# File 'lib/rubocop/cop/rbs/layout/space_around_operators.rb', line 24

def on_rbs_constant(decl)
  check_type(decl.type)
end

#on_rbs_def(decl) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rubocop/cop/rbs/layout/space_around_operators.rb', line 16

def on_rbs_def(decl)
  decl.overloads.each do |overload|
    overload.method_type.each_type do |type|
      check_type(type)
    end
  end
end