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:

# 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

documentation_url, #investigation_rbs, #location_to_range, #on_new_investigation, #on_other_file, #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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rubocop/cop/rbs/layout/space_around_operators.rb', line 63

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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/rbs/layout/space_around_operators.rb', line 44

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

#check_type_params(decl) ⇒ Object



56
57
58
59
60
61
# File 'lib/rubocop/cop/rbs/layout/space_around_operators.rb', line 56

def check_type_params(decl)
  decl.type_params.each do |type_param|
    check_type(type_param.default_type) if type_param.default_type
    check_type(type_param.upper_bound_type) if type_param.upper_bound_type
  end
end

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



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

def on_rbs_class(decl)
  check_type_params(decl)
end

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



32
33
34
# File 'lib/rubocop/cop/rbs/layout/space_around_operators.rb', line 32

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

#on_rbs_def(decl) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/rubocop/cop/rbs/layout/space_around_operators.rb', line 23

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

#on_rbs_type_alias(decl) ⇒ Object



39
40
41
42
# File 'lib/rubocop/cop/rbs/layout/space_around_operators.rb', line 39

def on_rbs_type_alias(decl)
  check_type_params(decl)
  check_type(decl.type)
end