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

Inherits:
Cop
  • Object
show all
Includes:
SurroundingSpace
Defined in:
lib/rubocop/cop/style/surrounding_space.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 =
[: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]

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

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_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

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

Instance Method Details

#check_missing_space(token_before, token, token_after) ⇒ Object



164
165
166
167
168
169
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 164

def check_missing_space(token_before, token, token_after)
  unless has_space?(token_before, token, token_after)
    text = token.text.to_s + (token.type == :tOP_ASGN ? '=' : '')
    add_offence(:convention, token.pos, MSG_MISSING % text)
  end
end

#do_not_check_block_arg_pipesObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 93

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



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 118

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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 132

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



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 105

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



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 150

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

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

Returns:

  • (Boolean)


171
172
173
174
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 171

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

#investigate(processed_source) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rubocop/cop/style/surrounding_space.rb', line 58

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
      if has_space?(token_before, token, token_after)
        add_offence(:convention, 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.



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

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