Class: Rubocop::Cop::SpaceInsideHashLiteralBraces

Inherits:
Cop
  • Object
show all
Includes:
SurroundingSpace
Defined in:
lib/rubocop/cop/surrounding_space.rb

Constant Summary collapse

MSG =
'Space inside hash literal braces %s.'

Instance Attribute Summary

Attributes inherited from Cop

#debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods included from SurroundingSpace

#build_token_table, #index_of_first_token, #index_of_last_token, #space_between?

Methods inherited from Cop

#add_offence, cop_name, #has_report?, #ignore_node, inherited, #initialize, #name, #on_comment

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#check(t1, t2) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/rubocop/cop/surrounding_space.rb', line 256

def check(t1, t2)
  types = [t1, t2].map(&:type)
  braces = [:tLBRACE, :tRCURLY]
  return if types == braces || (braces - types).size == 2
  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.line, MSG % word) if is_offence
end

#inspect(source, tokens, sexp, comments) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rubocop/cop/surrounding_space.rb', line 244

def inspect(source, tokens, sexp, comments)
  @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