Class: Rubocop::Cop::Style::SpaceAroundOperators

Inherits:
Cop
  • Object
show all
Includes:
SurroundingSpace
Defined in:
lib/rubocop/cop/style/space_around_operators.rb

Overview

Checks that operators have space around them, except for ** which should not have surrounding space.

Constant Summary collapse

MSG_MISSING =
"Surrounding space missing for operator '%s'."
MSG_DETECTED =
'Space around operator ** detected.'
BINARY_OPERATORS =

rubocop:disable SymbolName

[:tEQL,    :tAMPER2,  :tPIPE,  :tCARET, :tPLUS,  :tMINUS, :tSTAR2,
:tDIVIDE, :tPERCENT, :tEH,    :tCOLON, :tANDOP, :tOROP,  :tMATCH,
:tNMATCH, :tEQ,      :tNEQ,   :tGT,    :tRSHFT, :tGEQ,   :tLT,
:tLSHFT,  :tLEQ,     :tASSOC, :tEQQ,   :tCMP,   :tOP_ASGN]

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods included from SurroundingSpace

#index_of_first_token, #index_of_last_token, #space_between?, #token_table

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#autocorrect(range) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 155

def autocorrect(range)
  @corrections << lambda do |corrector|
    case range.source
    when /\*\*/
      corrector.replace(range, '**')
    else
      corrector.insert_before(range, ' ') unless range.source =~ /^\s/
      corrector.insert_after(range, ' ') unless range.source =~ /\s$/
    end
  end
end

#check_missing_space(token_before, token, token_after) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 127

def check_missing_space(token_before, token, token_after)
  unless space_on_both_sides?(token_before, token, token_after)
    text = token.text.to_s + (token.type == :tOP_ASGN ? '=' : '')
    convention(token_with_surrounding_space(token), token.pos,
               MSG_MISSING.format(text))
  end
end

#do_not_check_block_arg_pipesObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 56

def do_not_check_block_arg_pipes
  # each { |a| }
  #        ^ ^
  positions = []
  on_node(:block, @processed_source.ast) do |b|
    on_node(:args, b) do |a|
      positions << a.loc.begin << a.loc.end if a.loc.begin
    end
  end
  positions
end

#do_not_check_class_lshift_selfObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 81

def do_not_check_class_lshift_self
  # class <<self
  #       ^
  positions = []
  tokens = @processed_source.tokens
  on_node(:sclass, @processed_source.ast) do |sclass|
    ix = index_of_first_token(sclass)
    if tokens[ix, 2].map(&:type) == [:kCLASS, :tLSHFT]
      positions << tokens[ix + 1].pos
    end
  end
  positions
end

#do_not_check_def_thingsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 95

def do_not_check_def_things
  # def +(other)
  #     ^
  positions = []
  tokens = @processed_source.tokens
  on_node(:def, @processed_source.ast) do |def_node|
    # def each &block
    #          ^
    # def each *args
    #          ^
    on_node([:blockarg, :restarg], def_node) do |arg_node|
      positions << tokens[index_of_first_token(arg_node)].pos
    end
    positions << tokens[index_of_first_token(def_node) + 1].pos
  end
  positions
end

#do_not_check_param_defaultObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 68

def do_not_check_param_default
  # func(a, b=nil)
  #          ^
  positions = []
  tokens = @processed_source.tokens
  on_node(:optarg, @processed_source.ast) do |optarg|
    _arg, equals, _value = tokens[index_of_first_token(optarg),
                                  3]
    positions << equals.pos
  end
  positions
end

#do_not_check_singleton_operator_defsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 113

def do_not_check_singleton_operator_defs
  # def self.===(other)
  #          ^
  positions = []
  tokens = @processed_source.tokens
  on_node(:defs, @processed_source.ast) do |defs_node|
    _receiver, name, _args = *defs_node
    ix = index_of_first_token(defs_node)
    name_token = tokens[ix..-1].find { |t| t.text == name.to_s }
    positions << name_token.pos
  end
  positions
end

#investigate(processed_source) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 20

def investigate(processed_source)
  return unless processed_source.ast
  @processed_source = processed_source
  tokens = processed_source.tokens
  tokens.each_cons(3) do |token_before, token, token_after|
    next if token_before.type == :kDEF # TODO: remove?
    next if token_before.type == :tDOT # Called as method.
    next if positions_not_to_check.include?(token.pos)

    case token.type
    when :tPOW, :tDSTAR
      if space_on_any_side?(token_before, token, token_after)
        convention(token_with_surrounding_space(token), token.pos,
                   MSG_DETECTED)
      end
    when *BINARY_OPERATORS
      check_missing_space(token_before, token, token_after)
    end
  end
end

#positions_not_to_checkObject

Returns an array of positions marking the tokens that this cop should not check, either because the token is not an operator or because another cop does the check.



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

def positions_not_to_check
  @positions_not_to_check ||= begin
    positions = []
    positions.concat(do_not_check_block_arg_pipes)
    positions.concat(do_not_check_param_default)
    positions.concat(do_not_check_class_lshift_self)
    positions.concat(do_not_check_def_things)
    positions.concat(do_not_check_singleton_operator_defs)
    positions
  end
end

#space_on_any_side?(token_before, token, token_after) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 150

def space_on_any_side?(token_before, token, token_after)
  space_between?(token_before, token) || space_between?(token,
                                                        token_after)
end

#space_on_both_sides?(token_before, token, token_after) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
148
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 145

def space_on_both_sides?(token_before, token, token_after)
  space_between?(token_before, token) && space_between?(token,
                                                        token_after)
end

#token_with_surrounding_space(token) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/rubocop/cop/style/space_around_operators.rb', line 135

def token_with_surrounding_space(token)
  src = @processed_source.buffer.source
  begin_pos = token.pos.begin_pos
  begin_pos -= 1 while src[begin_pos - 1] =~ /[ \t]/
  end_pos = token.pos.end_pos
  end_pos += 1 while src[end_pos] =~ /[ \t]/
  Parser::Source::Range.new(@processed_source.buffer, begin_pos,
                            end_pos)
end