class Rubocop::Cop::Style::SpaceAroundOperators
Checks that operators have space around them, except for ** which should not have surrounding space.
Constants
- BINARY_OPERATORS
- MSG_DETECTED
- MSG_MISSING
Public Instance Methods
check_missing_space(token_before, token, token_after)
click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 156 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_pipes(sexp, positions_not_to_check)
click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 94 def do_not_check_block_arg_pipes(sexp, positions_not_to_check) # each { |a| } # ^ ^ on_node(:block, sexp) do |b| on_node(:args, b) do |a| positions_not_to_check << a.loc.begin << a.loc.end if a.loc.begin end end end
do_not_check_class_lshift_self(tokens, sexp, positions_not_to_check)
click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 114 def do_not_check_class_lshift_self(tokens, sexp, positions_not_to_check) # class <<self # ^ on_node(:sclass, sexp) do |sclass| ix = index_of_first_token(sclass, tokens) if tokens[ix, 2].map(&:type) == [:kCLASS, :tLSHFT] positions_not_to_check << tokens[ix + 1].pos end end end
do_not_check_def_things(tokens, sexp, positions_not_to_check)
click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 127 def do_not_check_def_things(tokens, sexp, positions_not_to_check) # def +(other) # ^ on_node(:def, sexp) do |def_node| # def each &block # ^ # def each *args # ^ on_node([:blockarg, :restarg], def_node) do |arg_node| positions_not_to_check << tokens[index_of_first_token(arg_node, tokens)].pos end positions_not_to_check << tokens[index_of_first_token(def_node, tokens) + 1].pos end end
do_not_check_param_default(tokens, sexp, positions_not_to_check)
click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 104 def do_not_check_param_default(tokens, sexp, positions_not_to_check) # func(a, b=nil) # ^ on_node(:optarg, sexp) do |optarg| _arg, equals, _value = tokens[index_of_first_token(optarg, tokens), 3] positions_not_to_check << equals.pos end end
do_not_check_singleton_operator_defs(tokens, sexp, positions_not_to_check)
click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 144 def do_not_check_singleton_operator_defs(tokens, sexp, positions_not_to_check) # def self.===(other) # ^ on_node(:defs, sexp) do |defs_node| _receiver, name, _args = *defs_node ix = index_of_first_token(defs_node, tokens) name_token = tokens[ix..-1].find { |t| t.text == name.to_s } positions_not_to_check << name_token.pos end end
get_positions_not_to_check(tokens, sexp)
click to toggle source
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.
# File lib/rubocop/cop/style/surrounding_space.rb, line 83 def get_positions_not_to_check(tokens, sexp) positions_not_to_check = [] do_not_check_block_arg_pipes(sexp, positions_not_to_check) do_not_check_param_default(tokens, sexp, positions_not_to_check) do_not_check_class_lshift_self(tokens, sexp, positions_not_to_check) do_not_check_def_things(tokens, sexp, positions_not_to_check) do_not_check_singleton_operator_defs(tokens, sexp, positions_not_to_check) positions_not_to_check end
has_space?(token_before, token, token_after)
click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 163 def has_space?(token_before, token, token_after) space_between?(token_before, token) && space_between?(token, token_after) end
inspect(source_buffer, source, tokens, sexp, comments)
click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 58 def inspect(source_buffer, source, tokens, sexp, comments) return unless sexp @source = source positions_not_to_check = get_positions_not_to_check(tokens, sexp) 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