class Rubocop::Cop::Style::SpaceAroundBraces

Checks that block braces have surrounding space.

Constants

MSG_LEFT
MSG_RIGHT

Public Instance Methods

check(t1, t2, msg) click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 211
def check(t1, t2, msg)
  unless space_between?(t1, t2)
    brace_token = msg == MSG_LEFT ? t1 : t2
    add_offence(:convention, brace_token.pos, msg)
  end
end
get_positions_not_to_check(tokens, sexp) click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 190
def get_positions_not_to_check(tokens, sexp)
  positions_not_to_check = []

  on_node(:hash, sexp) do |hash|
    b_ix = index_of_first_token(hash, tokens)
    e_ix = index_of_last_token(hash, tokens)
    positions_not_to_check << tokens[b_ix].pos << tokens[e_ix].pos
  end

  # TODO: Check braces inside string/symbol/regexp/xstr interpolation.
  on_node([:dstr, :dsym, :regexp, :xstr], sexp) do |s|
    b_ix = index_of_first_token(s, tokens)
    e_ix = index_of_last_token(s, tokens)
    tokens[b_ix..e_ix].each do |t|
      positions_not_to_check << t.pos if t.type == :tRCURLY
    end
  end

  positions_not_to_check
end
inspect(source_buffer, source, tokens, sexp, comments) click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 175
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(2) do |t1, t2|
    next if ([t1.pos, t2.pos] - positions_not_to_check).size < 2

    type1, type2 = t1.type, t2.type
    # :tLBRACE in hash literals, :tLCURLY otherwise.
    next if [:tLCURLY, :tLBRACE].include?(type1) && type2 == :tRCURLY
    check(t1, t2, MSG_LEFT) if type1 == :tLCURLY || type2 == :tLCURLY
    check(t1, t2, MSG_RIGHT) if type2 == :tRCURLY
  end
end