class Rubocop::Cop::Style::SpaceInsideHashLiteralBraces

Checks that braces used for hash literals have or don’t have surrounding space depending on configuration.

Constants

MSG

Public Instance Methods

check(t1, t2) click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 278
def check(t1, t2)
  types = [t1, t2].map(&:type)
  braces = [:tLBRACE, :tRCURLY]
  return if types == braces || (braces - types).size == 2
  # No offence if line break inside.
  return if t1.pos.line < t2.pos.line
  has_space = space_between?(t1, t2)
  is_offence, word = if self.class.config['EnforcedStyleIsWithSpaces']
                       [!has_space, 'missing']
                     else
                       [has_space, 'detected']
                     end
  add_offence(:convention, t1.pos, sprintf(MSG, word)) if is_offence
end
inspect(source_buffer, source, tokens, sexp, comments) click to toggle source
# File lib/rubocop/cop/style/surrounding_space.rb, line 265
def inspect(source_buffer, source, tokens, sexp, comments)
  return unless sexp
  @source = source
  on_node(:hash, sexp) do |hash|
    b_ix = index_of_first_token(hash, tokens)
    e_ix = index_of_last_token(hash, tokens)
    if tokens[b_ix].type == :tLBRACE # Hash literal with braces?
      check(tokens[b_ix], tokens[b_ix + 1])
      check(tokens[e_ix - 1], tokens[e_ix])
    end
  end
end