module RubbyCop::Cop::SpaceInside

Common functionality for checking for spaces inside various kinds of brackets.

Constants

MSG

Public Instance Methods

autocorrect(range) click to toggle source
# File lib/rubbycop/cop/mixin/space_inside.rb, line 18
def autocorrect(range)
  ->(corrector) { corrector.remove(range) }
end
investigate(processed_source) click to toggle source
# File lib/rubbycop/cop/mixin/space_inside.rb, line 11
def investigate(processed_source)
  @processed_source = processed_source
  each_extraneous_space(processed_source.tokens) do |kind, range|
    add_offense(range, range, format(MSG, kind))
  end
end

Private Instance Methods

each_extraneous_space(tokens) { |kind, range_between_tokens(t1, t2)| ... } click to toggle source
# File lib/rubbycop/cop/mixin/space_inside.rb, line 24
def each_extraneous_space(tokens)
  brackets = Brackets.new(*specifics)
  tokens.each_cons(2) do |t1, t2|
    next unless matching_brackets?(brackets, t1, t2)

    # If the second token is a comment, that means that a line break
    # follows, and that the rules for space inside don't apply.
    next if t2.type == :tCOMMENT
    next unless t2.pos.line == t1.pos.line && space_between?(t1, t2)

    yield brackets.kind, range_between_tokens(t1, t2)
  end
end
matching_brackets?(brackets, t1, t2) click to toggle source
# File lib/rubbycop/cop/mixin/space_inside.rb, line 38
def matching_brackets?(brackets, t1, t2)
  brackets.left_side?(t1) || brackets.right_side?(t2)
end
range_between_tokens(t1, t2) click to toggle source
# File lib/rubbycop/cop/mixin/space_inside.rb, line 42
def range_between_tokens(t1, t2)
  range_between(t1.pos.end_pos, t2.pos.begin_pos)
end