class Rubocop::Cop::Style::SpaceAroundBraces
Checks that block braces have surrounding space.
def check(t1, t2, msg)
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
def investigate(processed_source)
def investigate(processed_source) return unless processed_source.ast @processed_source = processed_source processed_source.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
def positions_not_to_check
def positions_not_to_check @positions_not_to_check ||= begin positions = [] ast = @processed_source.ast tokens = @processed_source.tokens on_node(:hash, ast) do |hash| b_ix = index_of_first_token(hash) e_ix = index_of_last_token(hash) positions << tokens[b_ix].pos << tokens[e_ix].pos end # TODO: Check braces inside string/symbol/regexp/xstr # interpolation. on_node([:dstr, :dsym, :regexp, :xstr], ast) do |s| b_ix = index_of_first_token(s) e_ix = index_of_last_token(s) tokens[b_ix..e_ix].each do |t| positions << t.pos if t.type == :tRCURLY end end positions end end