Module: RuboCop::Cop::SpaceInside

Includes:
SurroundingSpace
Included in:
RuboCop::Cop::Style::SpaceInsideBrackets, RuboCop::Cop::Style::SpaceInsideParens
Defined in:
lib/rubocop/cop/mixin/space_inside.rb

Overview

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

Defined Under Namespace

Classes: Brackets

Constant Summary collapse

MSG =
'Space inside %s detected.'.freeze

Instance Method Summary collapse

Methods included from SurroundingSpace

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

Instance Method Details

#autocorrect(range) ⇒ Object



30
31
32
# File 'lib/rubocop/cop/mixin/space_inside.rb', line 30

def autocorrect(range)
  ->(corrector) { corrector.remove(range) }
end

#investigate(processed_source) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rubocop/cop/mixin/space_inside.rb', line 12

def investigate(processed_source)
  @processed_source = processed_source
  brackets = Brackets.new(*specifics)
  processed_source.tokens.each_cons(2) do |t1, t2|
    next unless brackets.left_side?(t1) || brackets.right_side?(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)

    range = Parser::Source::Range.new(processed_source.buffer,
                                      t1.pos.end_pos,
                                      t2.pos.begin_pos)
    add_offense(range, range, format(MSG, brackets.kind))
  end
end